Click here to Skip to main content
Click here to Skip to main content

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
Electrical/Mobile Communication Engineer. Currently developing GIS, Visualization and Industrial Automation software in Japan.

Comments and Discussions

QuestionRe: Bug in Singular Value Decomposition Pinmembercelisdelorena27-Jun-08 5:42 
GeneralRe: Bug in Singular Value Decomposition when solving under-determined equation systems [modified] Pinmemberraz0rblade29-Aug-08 4:52 
GeneralRe: Bug in Singular Value Decomposition Pinmembermbreidt9-Apr-09 5:16 
GeneralProblem with eigenvalues PinmemberCukaricn30-Oct-07 7:07 
GeneralKarhunen-Loève Pinmembersergiomad25-Jun-07 15:32 
GeneralRe: Karhunen-Loève PinmemberPaul Selormey25-Jun-07 18:37 
QuestionIs it a bug in the constructor of GeneralMatrix? Pinmemberdanielmark23-May-07 11:09 
AnswerRe: Is it a bug in the constructor of GeneralMatrix? PinmemberPaul Selormey25-Jun-07 19:16 
QuestionAny sample code for a newbie Pinmembertgpritchard28-Apr-07 15:00 
AnswerRe: Any sample code for a newbie Pinmemberlukner20-Jun-07 13:46 
GeneralRe: Any sample code for a newbie PinmemberPaul Selormey25-Jun-07 19:27 
GeneralRe: Any sample code for a newbie Pinmemberlukner26-Jun-07 11:06 
GeneralDynamic Size PinmemberAlexis Blaze28-Feb-07 8:03 
GeneralRe: Dynamic Size PinmemberPaul Selormey25-Jun-07 20:32 
GeneralRe: Dynamic Size Pinmemberlukner26-Jun-07 3:21 
GeneralRe: Dynamic Size PinmemberPaul Selormey26-Jun-07 5:37 
GeneralRe: Dynamic Size Pinmemberlukner26-Jun-07 11:05 
GeneralRe: Dynamic Size PinmemberPaul Selormey26-Jun-07 6:06 
GeneralRe: Dynamic Size Pinmemberlukner26-Jun-07 11:21 
GeneralRe: Dynamic Size PinmemberPaul Selormey26-Jun-07 14:13 
GeneralRe: Dynamic Size Pinmemberlukner26-Jun-07 17:09 
Generalisspd flag Pinmembermrbehn30-Nov-06 8:35 
GeneralRe: isspd flag PinmemberPaul Selormey30-Nov-06 8:53 
GeneralPenrose Moore- Pseudo Inverse PinmemberGeist12314-Oct-06 15:31 
Generalleft division Pinmemberjbarreiro10127-Apr-05 4:57 
Generalpseudoinverse & SVD problem PinmemberKHeresy6-Dec-04 21:21 
GeneralRe: pseudoinverse & SVD problem PinsussGodBoy7-Jun-05 4:04 
JokeRe: pseudoinverse & SVD problem PinmemberKwon Tae In7-Mar-06 7:56 
GeneralOutOfMemory Exception Pinmemberernst_maurer10-Aug-04 7:09 
GeneralRe: OutOfMemory Exception PinmemberPaul Selormey10-Aug-04 11:53 
GeneralRe: OutOfMemory Exception Pinmembersenrique15-Oct-04 8:47 
GeneralRe: OutOfMemory Exception PinmemberPaul Selormey15-Oct-04 15:49 
QuestionMatrix Print? Pinmemberseabhcan30-Jun-04 6:40 
AnswerRe: Matrix Print? PinmemberPaul Selormey30-Jun-04 7:59 
AnswerRe: Matrix Print? PinmemberJiao Shuo10-Sep-12 4:05 
Generalleast square Pinmemberphil9169-Jun-04 5:04 
GeneralRe: least square PinmemberPaul Selormey30-Jun-04 8:06 
GeneralRe: least square PinmemberPaul Selormey1-Jul-04 2:25 
Generaldouble[][] or double[ , ] and Misc PinsussJoannes Vermorel13-Feb-04 11:26 
GeneralRe: double[][] or double[ , ] and Misc PinmemberPaul Selormey13-Feb-04 13:37 
GeneralRe: double[][] or double[ , ] and Misc PinsussJoannes Vermorel13-Feb-04 14:36 
GeneralRe: double[][] or double[ , ] and Misc PinmemberPaul Selormey13-Feb-04 19:01 
GeneralRe: double[][] or double[ , ] and Misc PineditorHeath Stewart23-Apr-04 3:18 
AnswerRe: double[][] or double[ , ] and Misc PinmemberAlex Cherkasov8-Apr-07 4:02 
GeneralAffine Transformation Pinmemberpxp26-Jan-04 22:48 
GeneralRe: Affine Transformation PinmemberPaul Selormey27-Jan-04 1:10 
GeneralRe: Affine Transformation Pinmemberpxp10-Mar-04 15:26 
GeneralRe: Affine Transformation PinmemberPaul Selormey14-Mar-04 16:00 
GeneralRe: Affine Transformation Pinmemberpxp20-Mar-04 2:48 
GeneralRe: Affine Transformation Pinmemberpxp7-Dec-04 9:06 

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.

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