Click here to Skip to main content
Licence CPOL
First Posted 10 Jan 2010
Views 36,740
Downloads 3,972
Bookmarked 81 times

Advanced Matrix Library in C#. NET

By Anas S. A. | 12 Jan 2010
The Matrix Library provides many static methods for making various matrix operations on objects derived from the class in C# .NET

1

2
1 vote, 2.4%
3
5 votes, 12.2%
4
35 votes, 85.4%
5
4.82/5 - 41 votes
1 removed
μ 4.79, σa 0.89 [?]

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:

  1. A test project using class cMatrix,
  2. cMatrix as a  DLL project so if compiled can be used with other .NET languages and
  3. 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:

// define a matrix A with dimensions (4x4)
Matrix A = new Matrix(4,4);
        
// define a matrix B with dimensions (4x4)
Matrix B = new Matrix(4, 4);

To fill values in the newly created matrices, we treat the variables as regular .NET arrays, i.e.:

//Assign some values to matrix A and B
//notice the use of indexers
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;
//For matrix B we fill it random numbers
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:

// Add Matrix A and B
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:

// define a vector V1
Matrix V1 = new Matrix(3,1);
        
//Assign some values to vectors V1 and V2
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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Anas S. A.

Program Manager
Medical Software
Turkey Turkey

Member
I have been working on C# .Net since 2000.Currently continuing Phd in Mechatronics Engineering. I am mainly interested in Image Processing, 3D, Robotics and interfacing computers to external hardware.
 
I believe knowledge should be shared so that we can progress and make a better world.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionQuestion - usage in a project PinmvpDave Kerr1:04 9 Feb '12  
QuestionVB.NET version??? PinmemberMember 860795111:12 31 Jan '12  
Question5 stars Pinmemberevren21:35 25 Jan '12  
GeneralMy vote of 5 Pinmembercodehackp2p6:15 19 Dec '11  
BugUnnecessary cloning of double[,] ? PinmemberMember 83737785:56 2 Dec '11  
QuestionOLS regression Pinmemberdjonger23:07 26 Nov '11  
QuestionSVD Pinmemberdingoxiii666:55 25 Nov '11  
GeneralMy vote of 5 PinmemberProfessionalEmad11:21 12 Oct '11  
Questiontype conversion error PinmemberMember 82870365:01 4 Oct '11  
AnswerRe: type conversion error Pinmembernantaniel10:08 22 Nov '11  
QuestionThanks and a question, Pinmemberbyfour0:17 30 Sep '11  
AnswerRe: Thanks and a question, Pinmembervladkoj0:32 30 Sep '11  
Generalthx a lot Pinmembermacgeisser7:57 27 May '11  
Generalmatrix dimension PinmemberMember 77021496:15 18 May '11  
GeneralRe: matrix dimension PinmemberAnas S. A.6:47 18 May '11  
GeneralThanks a lot Pinmembers922401196:35 24 Mar '11  
GeneralThanks a million for your project! Pinmemberzhoufcumt14:21 8 Mar '11  
GeneralMy vote of 5 Pinmemberbwise8376:32 25 Jan '11  
GeneralGreat work! PinmemberAndriy Ivanov14:48 31 Aug '10  
Generalsvd calculation bug PinmemberPravin_borkar20:22 24 Aug '10  
GeneralRe: svd calculation bug Pinmemberdreampower5:20 25 Aug '10  
QuestionRe: svd calculation bug Pinmemberniedziowiedz14:13 17 Mar '11  
AnswerRe: svd calculation bug PinmemberMember 77852413:14 19 Sep '11  
GeneralMy vote of 5 PinmemberHidetoshi Nishimura23:17 22 Aug '10  
GeneralThank's Man - You rock! Pinmemberk4m1lo15:23 12 Aug '10  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120209.1 | Last Updated 13 Jan 2010
Article Copyright 2010 by Anas S. A.
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid