Click here to Skip to main content
11,641,064 members (61,490 online)
Click here to Skip to main content

DotNetMatrix: Simple Matrix Library for .NET

, 12 Jan 2004 Public Domain 282.4K 9K 120
Rate this:
Please Sign up or sign in to vote.
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 Manipulation constructors
set elements
get elements
copy
clone
Elementary Operations addition
subtraction
multiplication
scalar multiplication
element-wise multiplication
element-wise division
unary minus
transpose
norm
Decompositions Cholesky
LU
QR
SVD
symmetric eigenvalue
nonsymmetric eigenvalue
Equation Solution nonsingular systems
least squares
Derived Quantities condition 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.

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

Share

About the Author

Paul Selormey
Engineer
Japan Japan
Electrical/Mobile Communication Engineer. Currently developing GIS, Visualization and Industrial Automation software in Japan.

You may also be interested in...

Comments and Discussions

 
GeneralRe: Bug in Singular Value Decomposition Pin
mbreidt9-Apr-09 4:16
membermbreidt9-Apr-09 4:16 
GeneralProblem with eigenvalues Pin
Cukaricn30-Oct-07 6:07
memberCukaricn30-Oct-07 6:07 
GeneralKarhunen-Loève Pin
sergiomad25-Jun-07 14:32
membersergiomad25-Jun-07 14:32 
GeneralRe: Karhunen-Loève Pin
Paul Selormey25-Jun-07 17:37
memberPaul Selormey25-Jun-07 17:37 
QuestionIs it a bug in the constructor of GeneralMatrix? Pin
danielmark23-May-07 10:09
memberdanielmark23-May-07 10:09 
AnswerRe: Is it a bug in the constructor of GeneralMatrix? Pin
Paul Selormey25-Jun-07 18:16
memberPaul Selormey25-Jun-07 18:16 
QuestionAny sample code for a newbie Pin
tgpritchard28-Apr-07 14:00
membertgpritchard28-Apr-07 14:00 
AnswerRe: Any sample code for a newbie Pin
lukner20-Jun-07 12:46
memberlukner20-Jun-07 12:46 
Tom,

You have many options for least squares, although I'm unsure how to use this particular library for the job (it didn't do the QR decomposition as I expected ... I never got the expected Q matrix for some reason).

Anyway, here is a least squares solution using QR decomposition (sometimes called QR Factorization). Feel free to look up least squares on Wiki as well. Also, I give links to a much easier way of doing least squares at the end of this message.

http://www.alkires.com/teaching/ee103/Rec8_LLSAndQRFactorization.htm[^]

You can get QR decomposition and linear equation solvers which actually do work (they seem to be based on LAPACK) from here:

http://www.alglib.net/matrixops/general/qr.php routine, which seems to have come from LAPACK CGEQR

and

http://www.alglib.net/linequations/general/lu.php

Both of the links above have code samples and test code that seems to work.

**** However, you may want something much simpler, such as ... ****

http://www.codeguru.com/forum/showpost.php?p=1139483&postcount=2[^]

or

http://www.pdc.kth.se/training/Tutor/MPI/Pt2pt.Lab/least-squares.c[^] (some adaptation required since this is C code, but it should be easy to adapt).

All the best,
--Ralf
GeneralRe: Any sample code for a newbie Pin
Paul Selormey25-Jun-07 18:27
memberPaul Selormey25-Jun-07 18:27 
GeneralRe: Any sample code for a newbie Pin
lukner26-Jun-07 10:06
memberlukner26-Jun-07 10:06 
GeneralDynamic Size Pin
Alexis Blaze28-Feb-07 7:03
memberAlexis Blaze28-Feb-07 7:03 
GeneralRe: Dynamic Size Pin
Paul Selormey25-Jun-07 19:32
memberPaul Selormey25-Jun-07 19:32 
GeneralRe: Dynamic Size Pin
lukner26-Jun-07 2:21
memberlukner26-Jun-07 2:21 
GeneralRe: Dynamic Size Pin
Paul Selormey26-Jun-07 4:37
memberPaul Selormey26-Jun-07 4:37 
GeneralRe: Dynamic Size Pin
lukner26-Jun-07 10:05
memberlukner26-Jun-07 10:05 
GeneralRe: Dynamic Size Pin
Paul Selormey26-Jun-07 5:06
memberPaul Selormey26-Jun-07 5:06 
GeneralRe: Dynamic Size Pin
lukner26-Jun-07 10:21
memberlukner26-Jun-07 10:21 
GeneralRe: Dynamic Size Pin
Paul Selormey26-Jun-07 13:13
memberPaul Selormey26-Jun-07 13:13 
GeneralRe: Dynamic Size Pin
lukner26-Jun-07 16:09
memberlukner26-Jun-07 16:09 
Generalisspd flag Pin
mrbehn30-Nov-06 7:35
membermrbehn30-Nov-06 7:35 
GeneralRe: isspd flag Pin
Paul Selormey30-Nov-06 7:53
memberPaul Selormey30-Nov-06 7:53 
GeneralPenrose Moore- Pseudo Inverse Pin
Geist12314-Oct-06 14:31
memberGeist12314-Oct-06 14:31 
Generalleft division Pin
jbarreiro10127-Apr-05 3:57
memberjbarreiro10127-Apr-05 3:57 
Generalpseudoinverse & SVD problem Pin
KHeresy6-Dec-04 20:21
memberKHeresy6-Dec-04 20:21 
GeneralRe: pseudoinverse & SVD problem Pin
GodBoy7-Jun-05 3:04
sussGodBoy7-Jun-05 3:04 

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
Web04 | 2.8.150731.1 | Last Updated 13 Jan 2004
Article Copyright 2004 by Paul Selormey
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid