Click here to Skip to main content
16,017,207 members
Articles / Programming Languages / C#
Article

DotNetMatrix: Simple Matrix Library for .NET

Rate me:
Please Sign up or sign in to vote.
4.94/5 (62 votes)
12 Jan 2004Public Domain4 min read 485.1K   12.2K   123   114
A set of C# classes providing basic matrix operations

Introduction

The classes provided with this article give you a basic linear algebra package for .NET. It provides user-level C# classes for constructing and manipulating real, dense matrices. It is meant to provide sufficient functionality for routine problems, packaged in a way that is natural and understandable to non-experts. That said, it is simply a port of a public domain Java matrix library, called JAMA. Check the JAMA web site for more information.

Background

Currently, I work for a small GIS company here in Japan developing GIS components for application developers. Coordinate Transformation and therefore Affine Transformation is a very basic part of our development efforts. Recently, I was assigned a task of designing and implementing a new GIS system for the .NET framework with the ability to easily port to Java and other frameworks. I decided to make maximum use of matrix-based affine transformation, which is also a requirement of the OpenGIS Coordinate Transformation Specifications.

Then, I discovered that the Matrix class provided as part of the GDI+ in the .NET implements the affine transformations in a manner different from standard specifications. In short, while the standard 2D Coordinate System Affine Transformation Matrix is define as

The matrix defined by GDI+ is

The effect is that most affine transformations with the GDI+ Matrix class will not conform to standard or specifications. For instance, by standard (and mathematically) anti-clockwise (or counter-clockwise) rotations are considered positive but must be negative when using the GDI+ classes.
To solve this problem, I decided to implement a standard affine transformation matrix and found this small Java general matrix library - JAMA. The classes presented with this article are ported from the JAMA with .NET specific improvements like operator overloading etc.

In a different article, I will discuss and present my affine transformation matrix, which should give the same effect (same properties and methods as the GDI+ Matrix class). The rest of the article is extracted from the JAMA documentation, and I will refer to the library as DotNetMatrix (the namespace).

Capabilities

DotNetMatrix is comprised of six C# classes: GeneralMatrix, CholeskyDecomposition, LUDecomposition, QRDecomposition, SingularValueDecomposition and EigenvalueDecomposition.

The GeneralMatrix class provides the fundamental operations of numerical linear algebra. Various constructors create Matrices from two dimensional arrays of double precision floating point numbers. Various gets and sets (properties) provide access to submatrices and matrix elements. The basic arithmetic operations include matrix addition and multiplication, matrix norms and selected element-by-element array operations. A convenient matrix print method is also included.

Five fundamental matrix decompositions, which consist of pairs or triples of matrices, permutation vectors, and the like, produce results in five decomposition classes. These decompositions are accessed by the GeneralMatrix class to compute solutions of simultaneous linear equations, determinants, inverses and other matrix functions. The five decompositions are

  • Cholesky Decomposition of symmetric, positive definite matrices
  • LU Decomposition (Gaussian elimination) of rectangular matrices
  • QR Decomposition of rectangular matrices
  • Eigenvalue Decomposition of both symmetric and nonsymmetric square matrices
  • Singular Value Decomposition of rectangular matrices
The DotNetMatrix deals only with real matrices, there is not support for complex matrices.

The design of DotNetMatrix represents a compromise between the need for pure and elegant object-oriented design and the need to enable high performance implementations.

Summary of DotNetMatrix Capabilities
Object Manipulationconstructors
set elements
get elements
copy
clone
Elementary Operationsaddition
subtraction
multiplication
scalar multiplication
element-wise multiplication
element-wise division
unary minus
transpose
norm
DecompositionsCholesky
LU
QR
SVD
symmetric eigenvalue
nonsymmetric eigenvalue
Equation Solutionnonsingular systems
least squares
Derived Quantitiescondition number
determinant
rank
inverse
pseudoinverse

Using the Code

The following simple example solves a 3x3 linear system Ax=b and computes the norm of the residual.

C#
double[][] array = {{1.,2.,3},{4.,5.,6.},{7.,8.,10.}};
GeneralMatrix A = new GeneralMatrix(array);
GeneralMatrix b = GeneralMatrix.Random(3,1);
GeneralMatrix x = A.Solve(b);
GeneralMatrix Residual = A.Multiply(x).Subtract(b);
double rnorm = Residual.NormInf();

Points of Interest

There is an empty ISerializable interface implementation in all the classes. I do not know if serialization support is really needed, I could complete the implementations if needed.

License

This is basically a port of public domain codes, so the result is in the public domain.

History

  • Jan 10, 2004 - initial release

Conclusion

Matrices are valuable mathematical tools which can be used to solve very complex problems, and coordinate transformation is no exception. The classes presented here can help you implement your own affine transformation classes to get a bit far than the .NET framework offers you. Also, since the classes are completely C# unlike the GDI+ classes, which are wrappers for a COM implementation, there is no interop overhead.

In my next article, I will present an affine transformation matrix class based on this library. Suggestions for improvements are welcomed.

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication


Written By
Engineer
Japan Japan
Systems Engineer

Comments and Discussions

 
QuestionPseudo Inverse Pin
Ananda at IBM16-Dec-19 19:00
Ananda at IBM16-Dec-19 19:00 
AnswerRe: Pseudo Inverse Pin
Ananda at IBM18-Dec-19 1:25
Ananda at IBM18-Dec-19 1:25 
QuestionRegister for COM interoop Pin
Member 1406799430-Nov-18 12:06
Member 1406799430-Nov-18 12:06 
QuestionI think I know why double[][] instead of double[,] Pin
Michael B Pliam15-Aug-16 12:07
Michael B Pliam15-Aug-16 12:07 
QuestionHow do i create a matrix from a 2d array Pin
Member 1257701511-Jul-16 21:07
Member 1257701511-Jul-16 21:07 
Questionhow to use the code for finding eigenvalue Pin
Saiful Bahri17-Feb-14 0:21
Saiful Bahri17-Feb-14 0:21 
QuestionhOw aBout sUpporting cOmplex nUmbers? Pin
cdinten27-Jun-13 17:55
cdinten27-Jun-13 17:55 
GeneralMy vote of 5 Pin
Lucas Allen8-Aug-12 7:18
Lucas Allen8-Aug-12 7:18 
QuestionLU Decomposition Pin
vsrujans4-Apr-12 14:08
vsrujans4-Apr-12 14:08 
QuestionProblem with array initialization Pin
hugolove1-Apr-12 23:48
hugolove1-Apr-12 23:48 
QuestionRe: Problem with array initialization Pin
shingnanwu8-Apr-12 8:02
shingnanwu8-Apr-12 8:02 
AnswerRe: Problem with array initialization Pin
Geovanny Morillo Ordoñez6-Nov-12 14:23
Geovanny Morillo Ordoñez6-Nov-12 14:23 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey18-Feb-12 3:25
professionalManoj Kumar Choubey18-Feb-12 3:25 
QuestionAdd GetMax and GetMin functions Pin
ChadSixt29-Dec-11 5:40
ChadSixt29-Dec-11 5:40 
Questionany new version or C++ version available? Pin
little_tiger26-Jan-11 22:54
little_tiger26-Jan-11 22:54 
Generalabout License Pin
Tharindu Nishad Patikirikorala6-Dec-10 16:17
Tharindu Nishad Patikirikorala6-Dec-10 16:17 
GeneralRe: about License Pin
Paul Selormey6-Dec-10 16:48
Paul Selormey6-Dec-10 16:48 
GeneralRe: about License Pin
Tharindu Nishad Patikirikorala6-Dec-10 19:20
Tharindu Nishad Patikirikorala6-Dec-10 19:20 
QuestionDelet Rows or Colums ? Pin
muhaa22215-Jun-09 8:43
muhaa22215-Jun-09 8:43 
QuestionHow to solve a system of 3 linear equations in 3 vars, x,y,z Pin
andeezlemoyo2-Dec-08 12:35
andeezlemoyo2-Dec-08 12:35 
AnswerRe: How to solve a system of 3 linear equations in 3 vars, x,y,z Pin
muhaa22215-Jun-09 8:37
muhaa22215-Jun-09 8:37 
GeneralQR Decomposition - Possible Bug Pin
LachlanGro9-Nov-08 23:40
LachlanGro9-Nov-08 23:40 
Hi,

I have a 3x3 matrix, V:
V =
100 10 1
625 25 1
900 30 1

In MatLab I do the QR and get:
>> [Q,R] = qr(V,0)
Q =
-0.0909 0.8789 0.4683
-0.5680 0.3405 -0.7493
-0.8180 -0.3341 0.4683
R =
1.0e+003 *
-1.1003 -0.0396 -0.0015
0 0.0073 0.0009
0 0 0.0002

However when using the DotNetMatrix I get:
Matrix Q =
-0.0908856214131766 0.878886544558686 -0.468292905790847
-0.568035133832354 0.340488733969466 0.749268649265355
-0.817970592718589 -0.334104570207539 -0.468292905790847

While the values are good, the right hand column is all a negative of the MatLab results.

Any suggestions?

Regards
Lachlan.
GeneralRe: QR Decomposition - Possible Bug Pin
Paul Selormey10-Nov-08 15:13
Paul Selormey10-Nov-08 15:13 
GeneralRe: QR Decomposition - Possible Bug Pin
LachlanGro10-Nov-08 15:48
LachlanGro10-Nov-08 15:48 
GeneralRe: QR Decomposition - Possible Bug Pin
Paul Selormey10-Nov-08 17:08
Paul Selormey10-Nov-08 17:08 

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

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