Posted Wednesday, March 09, 2005 9:12 AM
|
|
|
|
Hi,
I am in the process of attempting to convert a number of old MatLab routines to utilize the .NET Matrix Library. I've run into two issues which seem to be more painful than they should be:
1) The MatLab '/' operator [matrix division]. I am able to [I believe] properly simulate the following matlab statement:
x = A/B
In VB.NET:
x = B.Transpose().Solve(A.Transpose()).Transpose()
Is there an easier way to perform this function? I arrived at this solution by performing the translation as described in the MatLab docs:
A / B = (B' \ A' )'
According to the MatLab docs, A\B == the solution to Ax = B for x, so I assumed that the .Solve() method of the matrix class would provide me similar behavior. Does this seem reasonable?
2) Assigning entire rows/columns of data. It would be really handy to be able to set an entire row/column of data similar to what can be done in MatLab, e.g.:
x(:,1) = y [where len(y) = rank(x)]
Right now, I just have a simple method that accepts a column/row and a vector to insert and iterates over the array inserting the appropriate rows. In the end, it may just be syntactic sugar to support such a method, but it would make things a little easier to deal with.
Thanks!
Ben
|
|
Posted Wednesday, March 09, 2005 10:34 AM
|
|
|
|
Hi
Regarding (1):
==============
If matrix A is invertible you can substitute x = B.Transpose().Solve(A.Transpose()).Transpose() by x = A.Times(B.Inverse()) Please try the following and compare the results with those found in this page : http://www.egr.up.edu/contrib/nguyentom/Matlab/
Sub Main()
Dim A As New Matrix(New Double(,) {{1, 2, 3}, {4, 5, 6}, {7, 8, 0}})
Dim B As New Matrix(New Double(,) {{2, 4, 6}, {0, 3, 7}, {9, 8, 1}})
Dim C As Matrix
C = A.Times(B.Inverse)
Console.WriteLine(C)
Console.Read()
End Sub
Regarding (2)
=============
You are absolutely right. In the next version (2.2) we' ve planned to incude two new methods, FillRow and FillColumn having various overloads taking values from vectors, arrays, constants, that will do the function you are describing.
Thank you for trying .NET Matrix Library and your suggestions,
Trifon Triantafillidis | Lead Developer |
|
|
|
Posted Wednesday, March 09, 2005 10:49 AM
|
|
|
|
I forgot to mention above that Transpose is not an expensive operation and "x = B.Transpose().Solve(A.Transpose()).Transpose()" may look a bit lengthy but is not that bad.
Trifon Triantafillidis | Lead Developer |
|
|
|
Posted Wednesday, March 09, 2005 2:53 PM
|
|
|
|
Thanks for the reply. I had thought about using the A * inv(B) shortcut, but the MatLab docs state that they aren't strictly equivalent:
quote:
Slash or matrix right division. B/A is roughly the same as B*inv(A). More precisely, B/A = (A'\B')'
I guess my question then becomes "how close is 'roughly'" I'll have to test both methods and see what the results look like.
Thanks!
Ben
|
|
Posted Wednesday, March 09, 2005 3:16 PM
|
|
|
|
When B is not square or is singular then you cannot use x = A.Times(B.Inverse).
x = B.Transpose().Solve(A.Transpose()).Transpose() will always return a result provided matrix B has the same number of columns as A.
More about the Solve method: http://www.bluebit.gr/NET/Library/Matrix-Solve.html
Trifon Triantafillidis | Lead Developer |
|
|
|
|
|