Add your own alternative version
Stats
233.2K views 18.4K downloads 70 bookmarked
Posted
1 Jun 2007

Comments and Discussions


The calculation of determinant only works for a 2x2 matrix.





There is a bug which may cause this, please see my post "Solution to Bug affecting Inverse/Solve/Determinant easy code fix" below.





Hi,
I want to use CSML by loading it, then loading another project, and compiling both of them together. I am new to C#, so this is a big task.
I have your software loaded, and I have "using CSML;" in the AssemblyInfo.cs of my code, but it won't let me use: public Matrix m();
The software crashes. How do I compile CSML WITH my own software (not using the dll) and get it to recognize the calls in my code?
Secondly, how do I define a matrix as being comprised of complex numbers, and then load, one by one, data into the matrix?





Hey  thanks for this great library.
I think there is an issue with the [] (subtraction) operator... I'm noticing that if I have matrices A and B, and I define:
Matrix C = A  B
I get the expected result for C, but the matrix A has unexpectedly had its values changed to those of C. This sounds like some kind of pointer issue in the definition of the operator? I don't see this behavior for any other operators.





Hi,
Great job. I need to find out how to execute laft matrix division like A\B where let A by 7x7 matrix and B 7x1 matrix (rows x cols). I tried to inverse matrix A and then multiply by B but it's not enough as I need precision of 15 decimal places.





The library you present works great.
However one sometimes wonders why people keep writing such libraries since there are excellent frameworks like LAPACK (that are ported to C# as well).





Is the cofactor function available in the class?





Hi,
first of all, using the Library and must appreciate your contribution to humanity deeply
Second, i think the horizontalConcat method adds an extra row with unexpected values at the and of new Matrix.
Forgive me if i'm wrong
Thanks again for the great resource.





Great class. I did a quick and dirty solution to obtain a complex number from string because it is missing there:
http://pastebin.de/33687[^]
I have no idea how to optimize this. But it works.





private static Matrix m, n = new Matrix();
static void Main(string[] args)
{
Program p = new Program();
m = new Matrix("1, 2; 3, 4");
p.test();
Console.WriteLine("m =\n {0}\n n =\n {1}", m.ToString(), n.ToString());
Console.Read();
}
private void test()
{
Matrix x = new Matrix("1,1;1,1");
n = m  x;
}
Here is output: which is wrong, because it modify the matrix m. In reality, matrix m should not be modified.
m =
0; 1; \
2; 3; \
n =
0; 1; \
2; 3; \
Any idea on fixing this bug?





This will fix it. Remember, the + method will have the same problem. so do something similar to this will fix it.
hope this help
public static Matrix operator (Matrix A, Matrix B)
{
Matrix c = A.Clone();
if (c.RowCount != B.RowCount  c.ColumnCount != B.ColumnCount)
throw new ArgumentException("Matrices must be of the same dimension.");
for (int i = 1; i <= c.RowCount; i++)
{
for (int j = 1; j <= c.ColumnCount; j++)
{
c[i, j] = B[i, j];
}
}
return c;
}





We now use this where I work, best library we could find.






There is a solution to an Inverse bug by eshark11 a few posts down, see if it helps.





I can't see any function for matrix multiplication in the CSML?





There is! It's through operation overloads, so it's just to write C = A*B





Just exactly what I need...





dont' work correct. Убейся бляць об стену, только сложение и вычитание правильно работает!!!





dont' work correct. Убейся бляць об стену, только сложение и вычитание правильно работает!!!





Great library, Hanzzoid. Hope you return to this project some day.
In the meantime there is a bug which causes a "Warning: Matrix close to singular." exception, which we found during matrix.Inverse() and would also affect Solve and Determinant.
The solution is easy, and requires moving two lines down a little. All users of this library should do this.
1. Open the Matrix.cs file
2. Find this exception at line 1935 in LUSafe()
if (this[j, j] == 0)
throw new DivideByZeroException("Warning: Matrix close to singular.");
3. Move those two lines down by 14 lines so they are inside the loop at 1949
for (int i = j + 1; i <= n; i++)
{
this[i, j] = this[i, j] / this[j, j];
}
4. The result should look like this
for (int i = j + 1; i <= n; i++)
{
if (this[j, j] == 0)
throw new DivideByZeroException("Warning: Matrix close to singular.");
this[i, j] = this[i, j] / this[j, j];
}
This is for the code version 0.9 (Last Update: July 4, 2007)
Hope this helps someone,
Regards,
David Pierson





Hi Hanzzoid,
I'm kinda new to C#, but i'm trying to do simple Matrix calculations (inverse, transpose, vandermonde) which I see your program is very capable of. Due to the software and hardware I must work with, I've had to downgrade to using visual studio 2003. When trying to add csml.dll as a reference it is not recognised as proper COM or DLL file. And when I tried recompiling the source code, it says that the files are visual studio but a newer version and that I am unable to access them. I'm wondering is there a work around for this, or is there any other software that you could recommend that would accomplish this task?
Cheers,
Raymas





Raymas,
Instead of using the DLL, just add the two files Matrix.cs and Complex.cs to your project as source code files and compile them in.
You will need to do that anyway to fix the bug affecting Inverse, the solution to which I posted a minute ago.
Cheers,
David





Thanks David! It's working awesome now!





Great library has helped me plenty while working with MatLab code to C#, highly recommended.





I have computed a Matrix from which I now want to retrieve its values to write it into an doublearray. I read all the other topics but I found no way to do that.
Example
double data[i] = testMatrix[1,5];
testMatrix is of type Complex
Can someone help?
modified on Sunday, May 23, 2010 7:10 AM





I believe it is
double data[i] = testMatrix[1,5].Re;
When you access a matrix cell, you get a Complex Number, from which you may take the Real part. Hope it helps.





Hi this is the same doubt of the previous user.
Imagine that we have matrix A and B, and we perform an operation such as:
Matrix C = A.Inverse() * B;
How can I access to the double value of C[1,1]?
If I try:
double value = C[1, 1];
I get this error:
error CS0029: Cannot implicitly convert type 'WindowsFormsApplication1.Complex' to 'double'
Help me please!
Regards





You can write C[1,1].Re  this is the real value of the entity and will return a double instead of a Complex. There might be other solutions aswell..





i want to retrieve an index value say matrix[1][2] as an int value
how can i achieve that?
what i am trying is:
public static int matrix2int(Matrix m, int i, int j)
{
return (int)m.Row(i).Values[j1];
}
please help me i am waiting.





Hi there,
I had the same confusion as you are.
Since the matrix in arraylist, you can access individual element by m[i,j] directly. Go and try, it's in double.





@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.





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.





Any bug fixes for inverse??
Its not working for me too





I have also problems with inverse





Yes this is a bug and there is an easy fix.
See my post "Solution to Bug affecting Inverse/Solve/Determinant easy code fix".





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





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.





I noticed this as well and traced the bug back to the Rowfunction.
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





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.





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





When I write
Matrix d = b  a;
b changes. How come?





Fixed in 0.91, thanks for the hint.
(in Matrix operator() and Matrix operator+() in Matrix.cs,
I accidentally changed the first matrix parameter)





How do we get the latest version?
I still found that the operator is not behaving as it should in the current version. For example, C= A * B will cause operation in A to become A.
I think most of the operation assume passing parameter by value, while they actually are being passed by reference.
It's a great work, really. A lot of people really like this library.





problem solveing matrix, Great!






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





Ouch! Fixed in 0.91, thank you.





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





According to maths rules, only nonsingular square matrices can be inverted





As far as I know, nonsquare matrices cannot be inverted.
Although, if you want to solve
Ax = b
where A is nonsquare, 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.






General News Suggestion Question Bug Answer Joke Praise Rant Admin Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

