Click here to Skip to main content
11,930,830 members (54,299 online)
Click here to Skip to main content
Add your own
alternative version


119 bookmarked

DotNetMatrix: Simple Matrix Library for .NET

, 12 Jan 2004 Public Domain
Rate this:
Please Sign up or sign in to vote.
A set of C# classes providing basic matrix operations


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.


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).


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 Manipulation constructors
set elements
get elements
Elementary Operations addition
scalar multiplication
element-wise multiplication
element-wise division
unary minus
Decompositions Cholesky
symmetric eigenvalue
nonsymmetric eigenvalue
Equation Solution nonsingular systems
least squares
Derived Quantities condition number

Using the Code

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

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.


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


  • Jan 10, 2004 - initial release


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.


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


About the Author

Paul Selormey
Japan Japan
Systems Engineer

You may also be interested in...

Comments and Discussions

Questionhow to use the code for finding eigenvalue Pin
Saiful Bahri17-Feb-14 1:21
memberSaiful Bahri17-Feb-14 1:21 
QuestionhOw aBout sUpporting cOmplex nUmbers? Pin
cdinten27-Jun-13 18:55
membercdinten27-Jun-13 18:55 
GeneralMy vote of 5 Pin
Lucas Allen8-Aug-12 8:18
memberLucas Allen8-Aug-12 8:18 
QuestionLU Decomposition Pin
vsrujans4-Apr-12 15:08
membervsrujans4-Apr-12 15:08 
QuestionProblem with array initialization Pin
hugolove2-Apr-12 0:48
memberhugolove2-Apr-12 0:48 
QuestionRe: Problem with array initialization Pin
shingnanwu8-Apr-12 9:02
membershingnanwu8-Apr-12 9:02 
AnswerRe: Problem with array initialization Pin
Geovanny Morillo Ordoñez6-Nov-12 15:23
memberGeovanny Morillo Ordoñez6-Nov-12 15:23 
double[][] avals = {new double[]{1.0, 4.0, 7.0, 10.0}, new double[]{2.0, 5.0, 8.0, 11.0}, new double[]{3.0, 6.0, 9.0, 12.0}};

look at the examples in the sources and here

Please correct this in the example on this page

modified 6-Nov-12 20:34pm.

GeneralMy vote of 5 Pin
manoj kumar choubey18-Feb-12 4:25
membermanoj kumar choubey18-Feb-12 4:25 
QuestionAdd GetMax and GetMin functions Pin
Member 810538729-Dec-11 6:40
memberMember 810538729-Dec-11 6:40 
Questionany new version or C++ version available? Pin
little_tiger26-Jan-11 23:54
memberlittle_tiger26-Jan-11 23:54 
Generalabout License Pin
Tharindu Nishad Patikirikorala6-Dec-10 17:17
memberTharindu Nishad Patikirikorala6-Dec-10 17:17 
GeneralRe: about License Pin
Paul Selormey6-Dec-10 17:48
memberPaul Selormey6-Dec-10 17:48 
GeneralRe: about License Pin
Tharindu Nishad Patikirikorala6-Dec-10 20:20
memberTharindu Nishad Patikirikorala6-Dec-10 20:20 
QuestionDelet Rows or Colums ? Pin
muhaa22215-Jun-09 9:43
membermuhaa22215-Jun-09 9:43 
QuestionHow to solve a system of 3 linear equations in 3 vars, x,y,z Pin
andeezlemoyo2-Dec-08 13:35
memberandeezlemoyo2-Dec-08 13:35 
AnswerRe: How to solve a system of 3 linear equations in 3 vars, x,y,z Pin
muhaa22215-Jun-09 9:37
membermuhaa22215-Jun-09 9:37 
GeneralQR Decomposition - Possible Bug Pin
LachlanGro10-Nov-08 0:40
memberLachlanGro10-Nov-08 0:40 
GeneralRe: QR Decomposition - Possible Bug Pin
Paul Selormey10-Nov-08 16:13
memberPaul Selormey10-Nov-08 16:13 
GeneralRe: QR Decomposition - Possible Bug Pin
LachlanGro10-Nov-08 16:48
memberLachlanGro10-Nov-08 16:48 
GeneralRe: QR Decomposition - Possible Bug Pin
Paul Selormey10-Nov-08 18:08
memberPaul Selormey10-Nov-08 18:08 
GeneralRe: QR Decomposition - Possible Bug Pin
LorenHello28-Oct-15 2:50
memberLorenHello28-Oct-15 2:50 
GeneralPseudoinverse Pin
FueledByIgnorance7-Jul-08 22:32
memberFueledByIgnorance7-Jul-08 22:32 
GeneralRe: Pseudoinverse Pin
Paul Selormey10-Jul-08 7:34
memberPaul Selormey10-Jul-08 7:34 
GeneralBug in Singular Value Decomposition Pin
ajwo19-Nov-07 9:48
memberajwo19-Nov-07 9:48 
GeneralRe: Bug in Singular Value Decomposition Pin
Paul Selormey19-Nov-07 11:02
memberPaul Selormey19-Nov-07 11:02 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151126.1 | Last Updated 13 Jan 2004
Article Copyright 2004 by Paul Selormey
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid