Click here to Skip to main content
14,331,729 members

DotNetMatrix: Simple Matrix Library for .NET

Rate this:
4.88 (60 votes)
Please Sign up or sign in to vote.
4.88 (60 votes)
12 Jan 2004Public Domain
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

Image 1

The matrix defined by GDI+ is

Image 2

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 Manipulationconstructors
set elements
get elements
Elementary Operationsaddition
scalar multiplication
element-wise multiplication
element-wise division
unary minus
symmetric eigenvalue
nonsymmetric eigenvalue
Equation Solutionnonsingular systems
least squares
Derived Quantitiescondition 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

Comments and Discussions

GeneralRe: Affine Transformation Pin
Paul Selormey27-Jan-04 0:10
memberPaul Selormey27-Jan-04 0:10 
GeneralRe: Affine Transformation Pin
pxp10-Mar-04 14:26
memberpxp10-Mar-04 14:26 
GeneralRe: Affine Transformation Pin
Paul Selormey14-Mar-04 15:00
memberPaul Selormey14-Mar-04 15:00 
GeneralRe: Affine Transformation Pin
pxp20-Mar-04 1:48
memberpxp20-Mar-04 1:48 
GeneralRe: Affine Transformation Pin
pxp7-Dec-04 8:06
memberpxp7-Dec-04 8:06 
GeneralRe: Affine Transformation Pin
StenSat28-Feb-06 8:29
memberStenSat28-Feb-06 8:29 
GeneralRe: Affine Transformation Pin
pxp1-Mar-06 5:43
memberpxp1-Mar-06 5:43 
GeneralFor small matrices, a struct is better than a class Pin
Frank Hileman23-Jan-04 8:48
memberFrank Hileman23-Jan-04 8:48 
We needed a replacement for the GDI+ Matrix class in our graphics system, We found that for a small matrix, 3x3 affine, which is internally 2x3 floats for vector graphics, a struct provides much better performance than a class, even if you pass the matrices around as method arguments often. Furthermore, you get value semantics for your operator overloading without having to hit the heap or bother the garbage collector. Our idea was validated recently when we noticed Avalon does the same thing. Not quite as fast as C++ but close!

If you are interested, get a beta (see sig), and look at the FMatrix class. We use it internally instead of System.Drawing.Matrix. On rendering, we must use the System.Drawing class, so we do, but we avoid it as much as possible, as it uses interop and is very slow.

Frank Hileman

check out
An animated vector graphics system integrated in
GeneralRe: For small matrices, a struct is better than a class Pin
Paul Selormey23-Jan-04 11:48
memberPaul Selormey23-Jan-04 11:48 
GeneralSimplex method Pin
yvdh20-Jan-04 0:57
memberyvdh20-Jan-04 0:57 
GeneralRe: Simplex method Pin
Paul Selormey20-Jan-04 2:38
memberPaul Selormey20-Jan-04 2:38 
GeneralTeXnicCenter Updated?! Pin
TeleStar13-Jan-04 19:04
memberTeleStar13-Jan-04 19:04 
GeneralRe: TeXnicCenter Updated?! Pin
Paul Selormey13-Jan-04 21:22
memberPaul Selormey13-Jan-04 21:22 
GeneralAbout performance. Pin
Jonathan de Halleux13-Jan-04 5:00
memberJonathan de Halleux13-Jan-04 5:00 
GeneralRe: About performance. (continue) Pin
Jonathan de Halleux13-Jan-04 7:47
memberJonathan de Halleux13-Jan-04 7:47 
GeneralRe: About performance. (continue) Pin
Paul Selormey13-Jan-04 13:01
memberPaul Selormey13-Jan-04 13:01 
GeneralRe: About performance. (continue) Pin
Jonathan de Halleux13-Jan-04 23:11
memberJonathan de Halleux13-Jan-04 23:11 
GeneralRe: About performance. (continue) Pin
Keith Farmer14-Jan-04 9:55
memberKeith Farmer14-Jan-04 9:55 
GeneraluBlas, uLapack and the .Net wrapper -> NuBlas Pin
Jonathan de Halleux17-Jan-04 3:15
memberJonathan de Halleux17-Jan-04 3:15 
GeneralRe: uBlas, uLapack and the .Net wrapper -> NuBlas Pin
Paul Selormey17-Jan-04 13:45
memberPaul Selormey17-Jan-04 13:45 
GeneralRe: Performance Pin
Frode Carlsen20-Jan-04 21:11
sussFrode Carlsen20-Jan-04 21:11 
GeneralRe: Performance Pin
Paul Selormey20-Jan-04 22:17
memberPaul Selormey20-Jan-04 22:17 
GeneralRe: uBlas, uLapack and the .Net wrapper -> NuBlas Pin
Jonathan de Halleux11-Feb-04 3:47
memberJonathan de Halleux11-Feb-04 3:47 
GeneralRe: uBlas, uLapack and the .Net wrapper -> NuBlas Pin
Paul Selormey13-Feb-04 12:40
memberPaul Selormey13-Feb-04 12:40 
GeneralRe: uBlas, uLapack and the .Net wrapper -> NuBlas Pin
Jonathan de Halleux15-Feb-04 21:56
memberJonathan de Halleux15-Feb-04 21:56 

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.

Posted 12 Jan 2004


123 bookmarked