Download doc.zip - 210.8 KB (Documentation)
Version Info
Version 0.9 - Last Update: July 4, 2007
Introduction
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.
Remark
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.
Bugfixes 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.
What it can do
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 arithmetics. A real matrix M is to be considered a special complex matrix, where M.Re() == M, e.i. 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 algrithms are noted.
What it cannot do
At this time, there is no implementation of
- polynomials, interpolation, integration (*);
- eigenvectors (*);
(*) - 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 is has been released as well, PolyLib.
How to use CSML
Two general possibilities for using the code:
- Include CSML.dll as reference to your project and type "using CSML;" on top of your source file
- Include classes Matrix.cs and Complex.cs to your project and adjust namespaces
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");
Complex det = M.Determinant(); Matrix Minv = M.Inverse(); For details of implementation and usage, refer to the documentation.
Points of Interest
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.
A Word About Eigenvalues
The Eigenvalues() method in its current state uses basic QR iteration based upon Gram-Schmidtian orthogonalization. This implies two things:
- for now, computation of eigenvalues is possible only for real matrices
- if a matrix has multiple eigenvalues or complex eigenvalues, the method works, but junk may be returned
In fact, I had thoroughly satifying 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 canonical double-shift QR iteration based upon Givens rotations. That is the way Matlab's function eig is working.
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.
History
Coming soon
Update July 4, 2007
- added Hessenberg-Householder reduction, Householder reflection and Givens rotation; HessenbergHouseholder() works nicely; QRGivens() and QRIterationHessenberg() are still buggy
- slightly modified constructors for vector initialization
- bug in 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)
Update July 3, 2007 #2
- major bug in Arg() fixed (thanks Petr Stanislav!); this affected Log(), Pow() and Sqrt()
Update July 3, 2007
(1) minor bug within the matrix access routine fixed, making two try-catch blocks obsolete and increasing speed enormously; (2) defined matrix exponentiation with negative exponent k as exponentiation of the inverse with (-k); (3) major bug within the Insert() method fixed; (4) old matrix class (without complex number support) deleted
Update July 2, 2007
- A rainy Monday in Bavaria... Instead of learning for my exams, I updated and finished the documentation and added the hyperbolic functions Sinh, Cosh, Tanh, Coth, Sech, Csch to the Complex class.
- SolveSafe fixed, renamed to Solve and 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 Solve method superfluous, which has been removed.
- method Size removed and replaced with read-only properties RowCount and ColumnCount
- method IsVector renamed with VectorLength (tests, if a matrix is a vector, e.i. n by 1 or 1 by n)
Update June 29, 2007
Update June 10, 2007
- major multiplication bug removed; in "Matrix operator *(Matrix A, double x)"
- test for definiteness added (works for symmetric matrices only); needs beta-testing!
- test for symmetric positive definiteness (SPD) added and used within Cholesky(); Cholesky decomposition is possible only for SPD matrices
- undocumented basic graph algorithms: BFS (broad-first search), DFS (depth-first search) and Floyd (shortest paths between all vertices of a graph)
| You must Sign In to use this message board. |
|
|
 |
|
 |
@hanzzoid: Have there been any new developments recently? I noticed you mentioned a v0.91; was this ever released? The version available from this page has a few NotImplementedExceptions that I was wondering if you'd fixed.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
There is a bug in matrix inverse. I was trying to invert a 12 by 12 matrix and the determinant of the matrix was non zero(very big number) and the the code is giving a error message that matrix is singular. I checked the singular values of the matrix and they were all positive and the smallest one was 4. So i think inverse code should be fixed.
|
| Sign In·View Thread·PermaLink | 2.00/5 |
|
|
|
 |
|
|
 |
|
|
 |
|
 |
Hi, guys,
after I download and rebuild CSML on my computer, the following information is listed:
1. unreachable code detected in File Matrix.cs Line 2184 of project CSML 2. unreachable code detected in File Matrix.cs Line 2269 of project CSML 1. unreachable code detected in File Matrix.cs Line 2301 of project CSML
Do I ignore these 3 warnings? fankly, I want to exclude these 3 warnings. Could you tell me how to sovle it? the CSML DLL is still generated. Thanks in advance!
Dingjun
|
| Sign In·View Thread·PermaLink | 2.00/5 |
|
|
|
 |
|
 |
Hi!
Using HorizontalConcat to concatenate two matrices each containing one row doesn't work. It places the first row as a column vector in the first column and adds the second row as a row vector in a new row after the last element of the previous column vector.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
I noticed this as well and traced the bug back to the Row-function.
Change the following line: Matrix buf = new Matrix(columnCount, 1); to: Matrix buf = new Matrix(1, columnCount);
and both Row and HorizontalConcat will start working.
/Håkan MacLean
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hi,
I used the normal Inverse()-Methode and got a lot of "n.def ..." entries at console which should be 0. The debugger shows me that all values of that entries (Re,Im etc.) are NaN (constant for not a number). The InverseLeverrier()-Method works fine and places 0 instead of that "n.def ...". In Mathematica I also get 0.
What could be the reason for that ?
Thanks and greatings.
|
| Sign In·View Thread·PermaLink | 1.00/5 |
|
|
|
 |
|
 |
Hello,
I'm new to this so please excuse this probably simple question...
I have a 6x6 float array 'fa' that has been filled with data by another function. I would like to get the inverse of this written back to 'fa'. Is this possible and if so I would appreciate some advice as I can't seem to find the answer in the documentation.
Thanks in advance
|
| Sign In·View Thread·PermaLink | 1.50/5 |
|
|
|
 |
|
|
 |
|
 |
Fixed in 0.91, thanks for the hint.
(in Matrix operator-() and Matrix operator+() in Matrix.cs, I accidentally changed the first matrix parameter)
|
| Sign In·View Thread·PermaLink | 2.00/5 |
|
|
|
 |
 | Great  zhengdong jin | 5:18 23 Nov '07 |
|
|
 |
 | Re: Great  Rajnikant Sharma BYU | 15:56 28 Dec '08 |
|
|
 |
|
 |
Hi hanzzoid,
Great library! Very useful! Thank you!
I just noticed that your HorizontalConcat() does not work at all. If you change one line in it (make C be the transpose of first row of A), then this function becomes a working Vertical Concatination algorithm. Also, your VerticalConcat() works as Horizontal Concatenation.
Bottomline: the names appear flipped and one of the functions has a bug.
I might be wrong, but I thought I'd let you know, so that you can double check.
-Alexei
|
| Sign In·View Thread·PermaLink | 2.00/5 |
|
|
|
 |
|
|
 |
|
 |
Hi. Excellent work. Though I need to calculate the inverse of a m x n matrix while the class can only calculate it when the matrix is n x n. In matlab the operation is A\B, which is equibalent to inv(A)*B when A is n x n, but when not is a bit more complicated.  Can anybody help me with this calculation?
Thanks!;P
MeMyself
|
| Sign In·View Thread·PermaLink | 1.00/5 |
|
|
|
 |
|
|
 |
|
 |
As far as I know, non-square matrices cannot be inverted.
Although, if you want to solve
Ax = b
where A is non-square, you can equivalently solve
(A'A)x = A'b
where A' is the transpose of A --> (A'A) is square; iff a unique solution exists, (A'A) is invertible.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hello, I tried to use this class in my project, but I had to throw it away! I wanted to compute inverse matrix of Hermite spline form, but the library crashed saying something like "Matrix almost singular!" in LU. 
I computed it by hand -- much faster than using a computer. However, I still have to find some other library to use with my project...
|
| Sign In·View Thread·PermaLink | 2.00/5 |
|
|
|
 |
|
|
 |
|
 |
I just updated to the latest version of CSML. When writing Matrix Test = new Matrix(4, 4); Test[1,1]=1; I get the error message that the conversion from int to complex is not possible. How can I use the assignment Test[1,1]=1; as in the older versions of CSML? Or is this not possible any more?
|
| Sign In·View Thread·PermaLink | 2.00/5 |
|
|
|
 |
|
|
 |
|
 |
It would be great if meanful XML comments could be done for each method and property, so that it compiles without any warnings under Visual Studio 2005.
Thanks, Daniel
|
| Sign In·View Thread·PermaLink | 2.00/5 |
|
|
|
 |
|
 |
I quite believe I commented like hell. Do blame me for my wordiness, or for silly bugs, but the three warnings (or so) "unreachable code detected" come from unrelased methods, which do begin with a "System.NotImplementedExecteption".
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
That was not a blame, but a suggestion. When I compile, I have about 60 warnings due to the fact that there are no comments on the methods or properties. Maybe it's due to the fact that I have ghostdoc installed or something else. I am sure you will do well on those comments  Thanks for your work.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|