Class Matrix
A matrix is represented by a nested list of double point numbers.
So, you would write simply [[1,0],[0,1]] to create a 2x2 identity matrix.
public class Matrix : List<List<double>>, IList<List<double>>, ICollection<List<double>>, IReadOnlyList<List<double>>, IReadOnlyCollection<List<double>>, IEnumerable<List<double>>, IList, ICollection, IEnumerable
Initializes an empty matrix.
public Matrix()
Constructs a matrix by given number of rows and columns.
All the parameters are set to zero.
public static Matrix Construct(int rows, int columns)
Type | Name | Description |
---|---|---|
System.Int32 | rows | A positive integer, for the number of rows. |
System.Int32 | columns | A positive integer, for the number of columns. |
Type | Description |
---|---|
Matrix | A matrix reflecting the number of rows and columns provided. |
This routine uses Doolittle's method with partial pivoting to decompose the n x n matrix A,
into a unit lower triangular matrix L and an upper triangular matrix U and P is a permutation array
such that PA = LU. With this method you can always have a LU decomposition, rather than LU factorization.
The LU decomposition with pivoting always exists, even if the matrix is singular, so the constructor will never fail.
The primary use of the LU decomposition is in the solution of square systems of simultaneous linear equations.
The LUP decomposition provides a more robust method of solving linear systems than LU decomposition without pivoting, and it is approximately the same cost.
This will fail if non singular.
public static Matrix Decompose(Matrix m, out int[] permutation)
Type | Name | Description |
---|---|---|
Matrix | m | Matrix has to be decomposed. |
System.Int32[] | permutation | The row pivot information is in one-dimensional array. |
Type | Description |
---|---|
Matrix | The matrix representing the lower matrix and upper matrix together. |
Creates a copy.
public static Matrix Duplicate(Matrix matrix)
Type | Name | Description |
---|---|---|
Matrix | matrix | Matrix has to be duplicated. |
Type | Description |
---|---|
Matrix | Copied matrix. |
Fills the diagonal determined by the row and column position of a matrix.
public Matrix FillDiagonal(int row, int column, double valueToFill)
Type | Name | Description |
---|---|---|
System.Int32 | row | The value row where the diagonal begin. |
System.Int32 | column | The column value where the diagonal begin. |
System.Double | valueToFill | The value to fill on the diagonal. |
Type | Description |
---|---|
Matrix | A matrix with the identified diagonal filled. |
Creates an identity matrix of a given size.
public static Matrix Identity(int size, double diagonalValue = 1)
Type | Name | Description |
---|---|---|
System.Int32 | size | The size of the matrix. |
System.Double | diagonalValue | The value which will be filled the diagonal. |
Type | Description |
---|---|
Matrix | Identity matrix of the given size. |
Computes the inverse of a Matrix.
public static Matrix Inverse(Matrix matrix)
Type | Name | Description |
---|---|---|
Matrix | matrix | The matrix to invert. |
Type | Description |
---|---|
Matrix | The inverted matrix. |
Gets a value indicating whether this matrix is valid.
Matrix is valid when has at least one column and one row and rows have at least 2 elements.
public bool IsValid()
Type | Description |
---|---|
System.Boolean | True if it is a valid matrix. |
This routine uses Doolittle's method to solve the linear equation Ax = B.
This routine is called after the matrix A has been decomposed.
The solution proceeds by solving the linear equation Ly = B for y and,
subsequently solving the linear equation Ux = y for x.
public static Vector Solve(Matrix LuMatrix, int[] permutation, Vector b)
Type | Name | Description |
---|---|---|
Matrix | LuMatrix | Decomposed matrix in lower and upper triangle. |
System.Int32[] | permutation | The permutation row, or pivot row interchanged with row i. |
Vector | b | Column vector. |
Type | Description |
---|---|
Vector | The solution of the equation Ax = B is a vector. |
Constructs the string representation the matrix.
public override string ToString()
Type | Description |
---|---|
System.String | Text string. |
Transposes a matrix.
This is like swapping rows with columns.
public Matrix Transpose()
Type | Description |
---|---|
Matrix | The matrix transposed. |
Adds two matrices.
public static Matrix operator +(Matrix a, Matrix b)
Type | Name | Description |
---|---|---|
Matrix | a | First matrix. |
Matrix | b | Second matrix. |
Type | Description |
---|---|
Matrix | The result sum matrix. |
Divides a matrix by a constant.
public static Matrix operator /(Matrix m, double a)
Type | Name | Description |
---|---|---|
Matrix | m | Matrix has to be multiply. |
System.Double | a | Value to operate the division. |
Type | Description |
---|---|
Matrix | Matrix divided by a constant. |
Multiplies two matrices assuming they are of compatible dimensions.
public static Matrix operator *(Matrix a, Matrix b)
Type | Name | Description |
---|---|---|
Matrix | a | First matrix. |
Matrix | b | Second matrix. |
Type | Description |
---|---|
Matrix | The product matrix. |
Multiplies a matrix by a constant.
public static Matrix operator *(Matrix m, double a)
Type | Name | Description |
---|---|---|
Matrix | m | Matrix has to be multiply. |
System.Double | a | Value to operate the multiplication. |
Type | Description |
---|---|
Matrix | Matrix multiply by a constant. |
Subtracts two matrices.
public static Matrix operator -(Matrix a, Matrix b)
Type | Name | Description |
---|---|---|
Matrix | a | First matrix. |
Matrix | b | Second matrix. |
Type | Description |
---|---|
Matrix | The subtraction matrix. |