Introduction
Matrix Library .NET v2.0 is a totally free advanced matrix library. This library contains class Matrix which provides many static methods for making various matrix operations on objects derived from the class or on arrays defined as double of any dimension. The cMathLib
v2.0 class is an enhancement to a previous submission to planet-source-code. The previous code was written in VB.NET, due to the popularity it received I decided to submit this newer version. This version is written in C# .NET, and contains a lot of enhancements to the previous code plus newer methods for matrix manipulations.
Some methods included in this library are: add, subtract, multiply, Transpose, Determinant, Inverse, LU decomposition, Eigen Values and Vectors, Pseudoinverse, Singular Value Decomposition, SolveLinear Equations, Rank, Dot Product, Cross Product, etc.
Also the '+','-','*' operators are overloaded to work with the objects derived from the matrix class. The code used in this class is highly optimised for speed. Errors are handled as exceptions. The whole class is documented and I also provided a help file describing the class. If you compile the class as a ‘DLL’ it can be easily used in other .NET languages.
I used this library in my master's thesis project (manipulation of an industrial robot) extensively and I was very satisfied with its performance and robustness.
A lot of hard work was put into the developing of this class, the reason for sharing such a work for free, is my belief in sharing good work and free source code.
(Included in this submission:
- A test project using class
cMatrix
,
cMatrix
as a DLL project so if compiled can be used with other .NET languages and
- A ‘CHM’ help describing the class)
Using the Code
The class name is cMatrixLib
, all you need to do in order to use it in your project is add the file cMatrixLib.cs to your solution and refer to it.
In order to do some matrix manipulation on let's say two matrices 'A' and 'B' whose dimensions are 4 x 4, you will need to instantiate a variable of type Matrix
in this way:
Matrix A = new Matrix(4,4);
Matrix B = new Matrix(4, 4);
To fill values in the newly created matrices, we treat the variables as regular .NET arrays, i.e.:
A[0, 0] = 1 ; A[0, 1] = 2 ; A[0, 2] = 3 ; A[0, 3] = 4;
A[1, 0] = 5 ; A[1, 1] = 6 ; A[1, 2] = 7 ; A[1, 3] = 8;
A[2, 0] = 9 ; A[2, 1] = 10; A[2, 2] = 1 ; A[2, 3] = 12;
A[3, 0] = 13 ; A[3, 1] = -14; A[3, 2] = 15 ; A[3, 3] = 16;
Random rnd = new Random();
for (int i = 0; i<B.NoRows;i++)
for (int j = 0; j < B.NoCols;j++)
B[i, j] = 2 * rnd.NextDouble();
Now we have matrix 'A' and 'B' defined. In order to do some matrix operation on these matrices, all we need to do is use the static
methods in the class cMatrixLibrary
. For example, to get the inverse of Matrix A and assign it to a new matrix, let's say 'C':
Matrix C = Matrix.Inverse(A);
The '+','-','*' operators are overloaded to work with the objects derived from the matrix
class. So if we need to for example add matrix A and B and assign their result to a new matrix D:
Matrix D = A + B;
You can also manipulate vectors using the same class, to do that you need to set the dimensions of the Matrix as 3 x 1. for example:
Matrix V1 = new Matrix(3,1);
V1[0, 0] = 1 ; V1[1, 0] = 2 ; V1[2, 0] = 3;
For more information on the library, please refer to the help file included in the rar file. Also there is a sample project in the download to serve as a guide on how to use the library.
Operations Supported
The cMatrixLibrary
supports the following:
- Add
- Cross Product of two vectors
- Determinant
- Dot Product of two vectors
- Eigen Values and Vectors of a matrix
- Identity
- Inverse
- LU decomposition of a matrix
- Multiply
- Pseudoinverse
- Print Matrix (Returns a matrix as a string, so it can be viewed in a multi-text textbox or in a richtextBox )
- Rank of a Matrix
- Solve Linear (Solves a set of n linear equations A.X = B, and returns X)
- Subtract
- SVD (Evaluates the Singular Value Decomposition of a matrix, returns the matrices S, U and V. Such that a given Matrix = U x S x V')
- Transpose and
- Vector Magnitude