5

I've recently begun using MathNet to implement our linear algebra, however I'm having some trouble translation MATLAB functions to MathNet.

In MATLAB I often use the simple solve using the backslash operator:

C = A \ B

What is the equivalent of this in MathNet?

I get the same results in a small matrix using C = Inv(A) * B, but I don't know if the result is as precise.

Tomas Petricek
  • 240,744
  • 19
  • 378
  • 553
Bildsoe
  • 1,310
  • 6
  • 31
  • 44

4 Answers4

5

var C = A.QR().Solve(B); (using QR decomposition)

For square matrices also: var C = A.LU().Solve(B); (using LU decomposition)

Christoph Rüegg
  • 4,626
  • 1
  • 20
  • 34
2

I don't think MathNet has any "equivalent" of Matlab's backslash operator. See this site for some info on how Matlab's backslash works: Matlab manual on mldivide(). I guess you could look at some of the solve methods, like QRSolve, but I don't think they will be as easy to use...

What do you mean by "precise"? Are you asking if MathNet's inv() does exact inversion of a matrix, or are you simply asking if you could calculate C as Inv(A)*(B)?

If you are asking the later, yes, for square matrices Matlab's backslash is roughly the same as Inv(A)*(B).

Niclas
  • 1,220
  • 6
  • 12
  • Im asking the latter yes. Ok, then I'll just use this. When you write that is roughly the same, wherein lies the difference? – Bildsoe Feb 09 '12 at 10:23
  • The difference is that Matlab does Gaussian elimination when using the backslash operator, thereby reducing computational complexity and increasing numerical stability. Explicit computation of the inverse could be ill-conditioned under some conditions and therefore when working in Matlab you are better of using backslash. – Niclas Feb 09 '12 at 10:35
  • Ok, thank you. I think I need to look into some linear algebra :) – Bildsoe Feb 09 '12 at 10:39
1

With the tests I've made using Matlab and Math.Net Numerics:

Matrix A Vector B

Matlab: A \ B Math.Net Numerics: A.QR().Solve(B)

Both give the same results (in my case). I think it will work with B being a Matrix also.

FHeNuS
  • 101
  • 5
0

If you use ILNumerics.Net Library, You can try ILMath.linsolve(A, B);

ghun
  • 1