Cognitoware.Robotics.dll
Class Matrix
System.Object
Cognitoware.Mathematics.Data.Matrix
Summary
A linear algebra matrix.
Constructor Summary
Creates a 1x1 matrix containing the passed-in value.
Creates an NxM matrix containing the values in the array where rows=N and cols=M.
Matrix(Int32, Int32, Double[])
Creates an NxM matrix containing the values in the array where rows=N and cols=M.
Matrix(Int32, Int32, Boolean, Double[])
Creates an NxM matrix containing the values in the array where rows=N and cols=M.
Method Summary
Adds two matrices.
Calculates the determinant of the matrix.
Divides a matrix by a scalar.
Equals(Object)
Inherited from System.Object
Finalize()
Inherited from System.Object
Returns the value at a row and column position.
Returns the values in the specified column in a new Vector.
GetHashCode()
Inherited from System.Object
Returns the values in the specified row in a new Vector.
GetType()
Inherited from System.Object
Casts a double into a 1x1 matrix.
Creates an inverse of the Matrix.
Performs LU decomposition by Gaussian Elimination with partial pivoting.
MemberwiseClone()
Inherited from System.Object
Takes the product of two matrices.
Multiplies a matrix by a scalar.
Creates the square root of the Matrix.
Subtracts one matrix from a second matrix.
Creates a string representation of the matrix.
Creates the transpose of the Matrix.
Details
Matrix is considered to be immutable.
Internally, the matrix uses an array of doubles to store data.
This array can be defined to be row based, in which the first N elements represent the values in the first row,
or column based, in which the first M elements represent the values in the first column.
All operations create a new matrix instead of modifying the existing one.
The protection around this immutability is weak for performance reasons.
The user is responsible for not changing the underlying array.
Constructor Details
public Matrix(Double x)
Creates a 1x1 matrix containing the passed-in value.
Many classes use Matrix, but sometimes you are working with a single value.
This constructor makes it easier to construct the 1x1 covariance matrix to
represent the error of a single value.
Parameters:
x
- The value of the first row and first column in the 1x1 matrix.
public Matrix(Int32 rows, Int32 cols)
Creates an NxM matrix containing the values in the array where rows=N and cols=M.
All elements of the matrix are initialized to zero.
Parameters:
rows
- The number of rows in the matrix.
cols
- The number of columns int the matrix.
public Matrix(Int32 rows, Int32 cols, Double[] values)
Creates an NxM matrix containing the values in the array where rows=N and cols=M.
The array is expected to be of length rows*cols.
The first n elements represents the items in the first row.
Parameters:
rows
- The number of rows in the matrix.
cols
- The number of columns int the matrix.
values
-
The values in the matrix.
This is a params argument so the values of the array can be passed as individual arguments.
family Matrix(Int32 rows, Int32 cols, Boolean rowBased, Double[] values)
Creates an NxM matrix containing the values in the array where rows=N and cols=M.
The array is expected to be of length rows*cols.
The matrix can be created to be row-based or column-based.
Parameters:
rows
- The number of rows in the matrix.
cols
- The number of columns int the matrix.
rowBased
-
If rowBased is true, the first n elements represents the items in the first row.
If rowBased is false, the first n elements represents the items in the first column.
values
-
The values in the matrix.
The first n elements represents the items in the first row.
This is a params argument so the values of the array can be passed as individual arguments.
Method Details
public static Matrix Addition(Matrix m0, Matrix m1)
Creates the a matrix that is the sum of two matrices.
The operand matrices must be of the same size.
Parameters:
m0
- The left operand.
m1
- The right operand.
Returns:
A matrix containing the sum of the two arguments.
public static Matrix CreateIdentity(Int32 n)
Create a square identity matrix of the specified order.
Each element along the diagonal is one and all other elements are zero.
Parameters:
n
- The number of rows and columns in the new matrix.
public virtual Double Determinant()
Calculates the determinant of the matrix.
LU decomposition by Gaussian elimination with partial pivoting is used to calculate matrices larger than 3x3.
Throws an invalid operation exception if the matrix is not square.
Returns:
The determinate of the Matrix
public static Matrix Division(Matrix m, Double scalar)
Creates a matrix that contains each element of the original matrix divided by the scalar.
Parameters:
m
- The matrix to scale.
scalar
- The double value used to inversely scale the matrix.
Returns:
A matrix inversely scaled by the inverse of the double parameter.
public Double Get(Int32 row, Int32 col)
Returns the value at a row and column position.
Row and position are not bounds checked.
It is possible to get bad data with bad parameters.
It is also possible for an exception to be thrown.
Parameters:
row
- The row index of the desired element.
col
- The column index of the desired element.
Returns:
The value of the matrix at the specified row and column.
public Vector GetColumn(Int32 col)
Returns the values in the specified column in a new Vector.
This function is more efficient in column-base matrices than in row-based matrices.
Parameters:
col
- The index of the column to be retrieved.
Returns:
A new Vector containing the values in the specified column.
public Vector GetRow(Int32 row)
Returns the values in the specified row in a new Vector.
This function is more efficient in row-base matrices than in column-based matrices.
Parameters:
row
- The index of the row to be retrieved.
Returns:
A new Vector containing the values in the specified column.
public static Matrix Implicit(Double d)
Casts a double into a 1x1 matrix.
Parameters:
d
- The value for the single matrix element.
Returns:
A 1x1 matrix containing the parameter value.
public virtual Matrix Inverse()
Uses LU elimination to calculate an inverse of the matrix.
No pivoting is performed during the calculation.
The original matrix is not changed.
Returns:
A new matrix representing the inverse of the original matrix.
public Matrix LUDecompositionByGE( swapCount, pivot)
Uses Gaussian elimination to decompose the matrix into a unique lower diagonal matrix and upper diagonal matrix.
Partial pivoting is used to increase the stability of the operation.
The results of the row swaps are returned in the pivot array.
The number of row swaps is returned in the out parameter swapCount
Both the L and U matrices are stored in the single matrix mean.
The L matrix is stored in the lower left triangle of the matrix.
The U matrix is stored in the upper right triangle of the matrix.
The rows must be accessed using the pivot array to correctly access the triangles.
This function is used to calculate the determinant of matrices larger than 3x3.
Parameters:
swapCount
-
An out parameter providing the number of row swaps performed during pivoting.
pivot
-
An out parameter providing the postions into which rows were swapped.
Returns:
A Matrix containing the lower triangular matrix L where (pivot[row] >= column),
and the upper triangular matrix where (pivot[row] < column).
public static Matrix Multiply(Matrix m0, Matrix m1)
Creates the matrix that is the product of two matrices.
The inner product of each row of m0 is taken with each column of m1.
The number of columns in m0 must equal the number of rows of m1.
The resulting matrix has the same number of rows of m0 and the same number of columns of m1.
Parameters:
m0
- The left operand.
m1
- The right operand.
Returns:
A matrix containing the cross product of the two arguments.
public static Matrix Multiply(Double scalar, Matrix m)
Creates a matrix that contains each element of the original matrix divided by the scalar.
Parameters:
scalar
- The double value used to scale the matrix.
m
- The matrix to scale.
Returns:
A matrix scaled by the double parameter.
public virtual Matrix Sqrt()
Uses Cholesky decomposition to calcuate the square root of this Matrix.
No pivoting is performed during the calculation.
The original matrix is not changed.
Returns:
A new matrix representing the square root of the original matrix.
public static Matrix Subtraction(Matrix m0, Matrix m1)
Creates the a matrix that is the difference of two matrices.
The operand matrices must be of the same size.
Parameters:
m0
- The left operand.
m1
- The right operand.
Returns:
A matrix containing the difference of the two arguments.
public override String ToString()
Creates a string representation of the matrix.
Returns:
A string representation of the matrix.
public Matrix Transpose()
EFficiently creates a new matrix that is the transpose of this matrix.
This is done by copying the internal array reference of this matrix into a new matrix and
flips the row/column based property.
Returns:
A new matrix that is a transpose of the original matrix.