Add your own alternative version
Stats
316.2K views 10.2K downloads 119 bookmarked
Posted
12 Jan 2004

Comments and Discussions



The author has described this library as a port from the JAMA library. That library is stated by NIST to be preliminary and is written in Java. After testing in the latest Java NetBeans IDE I have discovered that, unlike C#, Java does not recognize the double[,] (matrix) syntax. Consequently, the NIST/MatLab authors were stuck with the 'jagged array' construct, double[][]. The explanation below as to 'why double[][]' makes no sense to me at all, nor do I think the issue is any way significantly related to GC or performance. It's simply that the author of this current C# library would have to have rewritten the whole port using double[,], a daunting chore to say the least.
Here are a couple of relevant code snippets that might help. The first is my own, the second from
Converting jagged array to 2D array C#  Stack Overflow[^]
<pre>
double[][] D3jj = { new double[] { 1.0, 4.0, 7.0, 10.0, 12.0, 14.0, 8.2 }, new double[] { 2.0, 5.0, 8.0, 11.0, 14.0 }, new double[] { 3.0, 6.0, 9.0, 12.0 } };
PrintJaggedArray(D3jj);
/* Output:
1 4 7 10 12 14 8.2
2 5 8 11 14
3 6 9 12
*/
static void PrintJaggedArray(double[][] array)
{
int nrows = array.GetLength(0);
for (int i = 0; i < nrows; i++)
{
for (int j = 0; j < array[i].GetLength(0); j++) // array[i].GetLength(0) gives i th row length
{
Console.Write(array[i][j] + " ");
} Console.WriteLine(" ");
} Console.WriteLine(" ");
}
static T[,] To2D<t>(T[][] source)
{
try
{
int FirstDim = source.Length;
int SecondDim = source.GroupBy(row => row.Length).Single().Key; // throws InvalidOperationException if source is not rectangular
var result = new T[FirstDim, SecondDim];
for (int i = 0; i < FirstDim; ++i)
for (int j = 0; j < SecondDim; ++j)
result[i, j] = source[i][j];
return result;
}
catch (InvalidOperationException)
{
throw new InvalidOperationException("The given jagged array is not rectangular.");
}
}
Now this is not an insignificant decision on the part of NIST to go with Java jagged array syntax. Had they selected C# in the first place, the implementation of complex matrices becomes almost trivial.
Using System.Numerics;
Complex[,] Cx = new Complex[3, 3];
Cx[0, 0] = new Complex(1.0, 1.0); Cx[0, 1] = new Complex(2.0, 1.0); Cx[0, 2] = new Complex(1.0, 1.0);
Cx[1, 0] = new Complex(2.0, 2.0); Cx[1, 1] = new Complex(1.0, 2.0); Cx[1, 2] = new Complex(2.0, 3.0);
Cx[2, 0] = new Complex(3.0, 1.0); Cx[2, 1] = new Complex(4.0, 1.0); Cx[2, 2] = new Complex(3.0, 1.0);
PrintComplexMatrix(Cx);
static void PrintComplexMatrix(Complex[,] Cx)
{
for (int i = 0; i < Cx.GetLength(0); i++)
{
for (int j = 0; j < Cx.GetLength(1); j++)
{
Console.Write(Cx[i, j] + " ");
} Console.WriteLine(" ");
} Console.WriteLine(" ");
}
modified 15Aug16 18:25pm.





For example, I have an array A declared as:
double[,] A = new double[6,3]
How do I create a GeneralMatrix from A?





Hi All, I am newbie here ,i really beginner in C# . So my problem is how can I use EigenvalueDecomposition class for finding the eigenvalue of my matrix I hope u can also provide the code example Thanx u
Peace!!





hOw aBout sUpporting cOmplex nUmbers iN tHe nEw vErsion? ?





I've been looking for something like this for a while now, and this seems to fit my needs wonderfully.





Any one can Help How to use DotNetMatrix to find LU Decomposition. And to return L, U,P . Please Help..





Hello, I have tried to use your sample code, but VS 2010 gives me this error:
"Array initializers can only be used in a variable or field initializer. Try using a new expression instead."
VS 2010 simply does not like the way the array is initialized:
double[][] array = {{1.,2.,3},{4.,5.,6.},{7.,8.,10.}};
I have tried to change it to:
double[,] array = {{1,2,3},{4,5,6},{7,8,10}};
which is correct way for VS 2010, but then GeneralMatrix(array) does not like this form.
How shall one initialize an array so that it will work with this probably awesome tool?
Many thanks for any hints.
hugo





I also encounter the very same problem with VS2003.





double[][] avals = {new double[]{1.0, 4.0, 7.0, 10.0}, new double[]{2.0, 5.0, 8.0, 11.0}, new double[]{3.0, 6.0, 9.0, 12.0}};
look at the examples in the sources and here http://msdn.microsoft.com/enus/library/sf3awfty(v=vs.71).aspx
Please correct this in the example on this page
modified 6Nov12 20:34pm.






I created two functions to get the Max and Min Values in a given matrix. If this is something you want to add to the project let me know and I will provide the code. Its just a couple of loops and a compare statement... Not sure how to contribute on Code Project, yet...





Hi Paul,
Great job. I just want to know if you have some updated version of theses codes?
Is there a C++ version available?
Thanks a lot.





Hi,
I used this dll really effectively in a research project. I like to make my work accessible to other researchers free. But with the dependency of Dll, I dont know whether I can do it or not?
Is there an copy right issue if I distribute your DLL with my dll?
Thank you
Tharindu





Hello Tharindu,
As you can see in the License section, this is a public domain code,
so you can anything you like with it.
You do not need to have it as a DLL, you can directly use the source codes in your projects.
The source code is very old now, you can clean it and keep anything changes you make, it is
a public domain stuff, just maintain any license notifications in the source code itself.
Best regards,
Paul.
Jesus Christ is LOVE! Please tell somebody.





Thank you for the clarification.





Hi
I use your Libary. it's very very nice code
but i have still a problem
i want to delet da row or a col ?
i didn#t have found a soulution for this problem
cya
muhaa





Hi Paul, first, thank you for sharing your work.
Here's a sample linear system I want to solve:
49x  7y + z = 6
25x  5y + z = 4
9x  3y + z = 2
How can I solve this with GeneralMatrix? I would greatly appreciate it if you could give me some sample code.
Thanks,
Andy





Hi I think the solve is
GeneralMatrix A
like
49 7 1
25 5 1
9 3 1
and
GeneralMatrix B
like
6
4
2
GeneralMatrix N=A.Transpose()*A;
GeneralMatrix n=A.Transpose()*B;
GeneralMatrix x=N.Inverse()*n
x.GetElement(0,0) is your x
x.GetElement(1,0) is your y
x.GetElement(2,0) is your z
i use this for solving overdetermined equation but is still work for your problem
cya
muhaa





Hi,
I have a 3x3 matrix, V:
V =
100 10 1
625 25 1
900 30 1
In MatLab I do the QR and get:
>> [Q,R] = qr(V,0)
Q =
0.0909 0.8789 0.4683
0.5680 0.3405 0.7493
0.8180 0.3341 0.4683
R =
1.0e+003 *
1.1003 0.0396 0.0015
0 0.0073 0.0009
0 0 0.0002
However when using the DotNetMatrix I get:
Matrix Q =
0.0908856214131766 0.878886544558686 0.468292905790847
0.568035133832354 0.340488733969466 0.749268649265355
0.817970592718589 0.334104570207539 0.468292905790847
While the values are good, the right hand column is all a negative of the MatLab results.
Any suggestions?
Regards
Lachlan.





LachlanGro wrote: While the values are good, the right hand column is all a negative of the MatLab results.
True, I have verified it. I have also used the example matrix in the wikipedia http://en.wikipedia.org/wiki/QR_decomposition[^] and the signs do not correspond.
LachlanGro wrote: Any suggestions?
I am currently looking into it, I do not have any immediate idea of the source of the problem.
Best regards,
Paul.
Jesus Christ is LOVE! Please tell somebody.





Hi Paul,
I posted a similar question on the Jama discussion list and apparently there is no bug, its just that the solution is "not unique".
While I believe this is the case, I could not find an indication of what would be unique.
Either way I pass exactly the same data into other libraries (such as BlueBit, Matlab) and the results are fine.
Regards,
Lachlan.





LachlanGro wrote: I posted a similar question on the Jama discussion list and apparently there is no bug, its just that the solution is "not unique".
Thanks for the information.
LachlanGro wrote: While I believe this is the case, I could not find an indication of what would be unique.
Computations based on the results are valid, also the Q'Q = I, so that may be the case.
LachlanGro wrote: Either way I pass exactly the same data into other libraries (such as BlueBit, Matlab) and the results are fine.
Currently, I do not have any math package so I was only looking for books and online examples to compare. I will look into the algorithm used to see if we could get the same results as Matlab to prevent any doubt.
Best regards,
Paul.
Jesus Christ is LOVE! Please tell somebody.





The third column of Q is eigenvector, a unit direction vector, negative value is OK, just denotes the opposite direction.





Thanks for this library, it is very useful!
I have a question: I arrive at some point with a Matrix that is singular, and I want to perform an Inverse on it.
GeneralMatrix matrixInvertQ = matrixMultiplyN.Inverse();
...
public virtual Matrix Inverse()
{
return Solve(Identity(m, m));
}
The Inverse method calls Solve, which instantiates a LUDecomposition object and calls its Solve method. There it tests for singularity, and I get the "Matrix is singular" exception.
I have very limited knowledge in mathematics, so bear with me. At the point where the matrix is considered singular, shouldn't a pseudoinverse by performed?
In the comments for the Inverse method, it says "inverse(A) if A is square, pseudoinverse otherwise".
Is there a part missing from implementation?
Thanks!





Hello,
Singularity and pseudoinverse are different.
To understand the pseudoinverse, see the following
1. In PlanetMath[^]
2. Example[^]
Best regards,
Paul.
Jesus Christ is LOVE! Please tell somebody.







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.

