12,064,546 members (54,252 online)
CSML - C# Matrix Library - is a compact and lightweight package for numerical linear algebra. Many matrix operations known from Matlab, Scilab and Co. are implemented.
Version 0.91 - Last update: November 29, 2007.
Make sure to return to this article once in a while for updates. A project of this size a is big thing for one man to handle. Scilab (free Matlab clone), for instance, has been created by an academic consortium, and Matlab's creator, Mathworks, is a well-fed enterprise.
Bug fixes will always come as updates, and if you have a look at the history below, you will see there have been quit a number of, ehm, improvements in a short period of time.
The core of the library, the
Matrix class, includes over 90 methods for matrix operations such as multiplication, summation, exponentiation, and solving linear equation systems; matrix manipulations such as concatenation, insertion, transpose, inverse, flipping, symmetrizing, insertion, and extraction; for matrix computations such as determinant, trace, permanent, norm (Frobenius, Euclidian, maximum norm, taxi norm, p-norm), condition number; for matrix decompositions such as LU factorization, Gram-Schmidtian orthogonalization, and Cholesky factorization.
Now the entire library has been updated to work with complex arithmetic. A real matrix M is to be considered a special complex matrix, where M.Re() == M, e.i., the imaginary part of each entry equals 0.
The project is nearly entirely PDF-documented; most methods are also illustrated with examples. Difficult algorithms like the computation of the inverse are explained on a mathematical level as well. If vitally necessary, complexity classes of certain algorithms are noted.
At this time, there is no implementation of
(*) - Working on it. Any contribution is welcome. A complex numbers library has been issued by me here on CodeProject, look for CompLib. A library for polynomials has been released as well, PolyLib.
Two general ways for using the code:
using CSML;" on top of your source file
Let us see an example. Say, we want to compute the determinant and the inverse of the 2 by 2 matrix [1, 1; 1, 2]:
Matrix M = new Matrix("1,1;1,2"); // init Complex det = M.Determinant(); // det = 1 Matrix Minv = M.Inverse(); // Minv = [2, -1; -1, 1] string buf = M.ToString(); // for outputting M in a multiline textbox
For details of implementation and usage, refer to the documentation.
This project is issued without license and warranty, it should be considered a gift to the developer's community in general and to this page in particular - most of my programming knowledge is based on free code, on examples and tutorials submitted without the greed for money. I am now in the happy position not having to turn anything into bucks: this is the result.
None of the algorithms presented here is optimized for speed. The idea behind this project is to demonstrate many common and well-known algorithms for matrices in a straightforward and understandable way.
Possible optimization ideas are:
Eigenvalues() method in its current state uses basic QR iteration based upon Gram-Schmidtian orthogonalization. This implies two things:
In fact, I had thoroughly satisfying results only for triangular matrices and symmetric positive definite matrices.
These problems mirror the difficulties buried under the Eigenvalue problem. Since the Eigenvalues of a matrix A are defined as the roots of the characteristic polynomial:
p(L) = det(A-L*id)
computation is mathematically equivalent to the computation of a determinant and the n roots of p. (Well, this would work for all matrices with any distribution of Eigenvalues, but it is numerically the worst idea, since Weierstrass iteration (compare the
Roots() method in PolyLib) is badly conditioned for polynomials with roots not being well-separated.)
Therefore, I am working on a canonical double-shift QR iteration based upon Givens rotations. That is the way Matlab's function
That I am forced to talk at length about Eigenvalues, although there are so many other difficult computations implemented, reveals to me that this problem is one of the deepest and most bothersome in basic numerical linear algebra.
Conjugate gradient method (
SolveCG() is implemented but buggy); I'm going to issue an example project showing the usage and basic functionality of CSML one of these days.
QRIterationHessenberg()are still buggy.
InverseLeverrier()fixed; this black box method should now be standard for matrix inversion (
Inverse()is much slower for general matrices, but fast for special matrices being orthogonal, unitary, or diagonal).
ToString(string format)method in Complex.cs beautified; multiplication of double and complex values slightly changed (no bugs there, but inconsistencies).
Arg()fixed (thanks Petr Stanislav!); this affected
try-catchblocks obsolete and increasing speed enormously.
SolveSafefixed, renamed to
Solveand made static. This method is now nearly equivalent to the Matlab backslash operator. Fixation of
SolveSafe(using LU decomposition with column pivoting) suddenly made the old
Solvemethod superfluous, which has been removed.
Sizemethod removed and replaced with read-only properties
IsVectormethod renamed with
VectorLength(tests if a matrix is a vector, e.i. n by 1 or 1 by n).
New in this version:
IsPermutation()removed (now: permutation matrices are precisely the involuntary 0-1 matrices; refer to doc).
Insert()to insert a submatrix at a certain position.
Extract()to copy a submatrix from a given matrix.
BlockMatrix()to build a matrix from four given matrices.
ChessboardMatrix()to construct matrices with interchanging 0-1 entries.
Random()extended to generate not just double, but also integer matrices.
Im()to extract the real/imaginary parts of a matrix.
SolveSafe()marked as buggy.
HouseholderVector()implemented but yet (correctly) marked as buggy.
InverseLeverrier(), using the Leverrier method (RTFM).
Matrix operator *(Matrix A, double x)".
Cholesky(); Cholesky decomposition is possible only for SPD matrices.