1

How can I perform a Right Matrix Division in C#?. In MATLAB the code would be

AA(I) = ((X(I,:)-mu)/si)*(X(I,:)-mu)';
%where,
%I is index
%AA is a matrix of 1330x1 double
%X is a matrix of 1330x158 double
%mu is a matrix of 1x134 double
%si is a matrix of 134x134 double

For now I am using jagged arrays to perform all my computations. Is there a library I should use that can do efficient matrix computations?

Update with using MathNet LinearAlgebra

 /* MathNet Matrix tests */
            Matrix<double> AA = Matrix<double>.Build.Dense(1330, 1, 1.0);
            Matrix<double> XX = Matrix<double>.Build.Dense(1330, 158, 1.0);
            Matrix<double> si = Matrix<double>.Build.Dense(134, 134, 1.0);
            Matrix<double> mu = Matrix<double>.Build.Dense(1, 134, 1.0);

            AA.Row(0) = ((XX.Row(0) - mu.Row(0)) * si.Inverse()) * (XX.Row(0) - mu.Row(0)).Transpose();

update:

AA.SetRow(0, ((XX.Row(0) - mu.Row(0)) * si.Inverse()) * ((XX.Row(0) - mu.Row(0)).ToRowMatrix().Transpose()));

As suggested by @ander-biguri, the above is the implementation using MathNet Numerics library.

Question: I get an error stating that Transpose cannot be applied to vectors. How can I achieve AA(I) = ((X(I,:)-mu)/si)*(X(I,:)-mu)' statement, what am I doing wrong here?

opto abhi
  • 317
  • 1
  • 3
  • 13
  • Matrix division.`A/B` is `A*B^-1`. Sure, use a library, any, that will do it for you – Ander Biguri Nov 16 '16 at 15:17
  • @AnderBiguri: Please see the updated question, I tried with MathNet. I am doing something wrong when it says I cannot apply a transpose to a vector, but I need to do the subtraction and multiplication between vectors and matrix. – opto abhi Nov 16 '16 at 15:46
  • The dimensions do not seem to match. Do you intent to subtract a vector of length 134 from a vector of length 158? Math.NET Numerics currently does not allow this, mainly to prevent errors. – Christoph Rüegg Nov 18 '16 at 20:29
  • @ChristophRüegg: Yes I do intent to perform that, since you mention that dimensions do not match, why is it that Math.NET not throw an error or exception. I never got any error (pleases see the updated code in c#) – opto abhi Nov 18 '16 at 20:33
  • Your updated code *does* throw an ArgumentException, as expected (just verified) – Christoph Rüegg Nov 18 '16 at 20:52
  • I see. So what would you advise to achieve this right matrix division in .NET. none of the libraries have a direct function I can use, I will have to write one myself. – opto abhi Nov 18 '16 at 20:55
  • 1
    What that "matrix division" really does is solve a linear equation system. In Math.NET Numerics you'd typically write this along the lines of `si.Solve(XX.Row(0) - mu.Row(0))`. – Christoph Rüegg Nov 18 '16 at 21:10
  • 1
    And you'd typically also use vectors instead of single row/column matrices, saving you from the need to transpose in the first place. – Christoph Rüegg Nov 18 '16 at 21:17

0 Answers0