This project has moved. For the latest updates, please go here.

How to use Least Square method, if any

Mar 28, 2011 at 2:33 PM

I know clearly on how to slove Least Square problem in Math.NET Iridium while I am puzzled in Math.NET Numerics. I wonder if there is a method like .Solve in Math.NET Iridium, or I shall create the algorithm by myself. If there is no such method, will the project add it in the future?

Mar 29, 2011 at 3:28 AM

You can use the factorization solvers such QR - matrix.QR.Solve(...);

Apr 10, 2011 at 12:58 PM

Thanks to your solution~But, I am still puzzled by the efficiency between QR and SVD.

Before bringing Math.Net Numerics into use, I tested my project with DotNumerics -- Another C# Numerics library. In my experiment, sloving the same formula with Least Square method, QR is far more faster than others. But on Math.Net Numerics, SVG is faster than QR obviously. On the other hand, The total time Math.Net Numerics takes is more than DotNumerics (but CPU occupation is highter). I don't know why.

Here is the formula and my code under both library.

Fomular:

G *D=A, where G is an 13*6 matrix (known), D is an 6*1 matrix (unknown), A is an 13*1 matrix (known).

Code under DotNumerics:

D = leastSquares.QRorLQSolve(G, A);

Code under Math.Net Numerics:

G.QR().Solve(A, D);//QR

G.Svd(true).Solve(A, D);//SVD

Apr 10, 2011 at 1:09 PM

>But on Math.Net Numerics, SVG is faster than QR obviously.

I think the problem is that our QR code is threaded by default (SVD isn't threaded). Since you are using really small matrices, the threading overhead is slowing it down. Try using:

Control.DisableParallelization = true;

Is SVD still faster?

I think we might want to update the library so that we only use parallel code when matrices/vectors are of a certain size.

Apr 10, 2011 at 1:30 PM

Thanks so much.

After Adding MathNet.Numerics.Control.DisableParallelization = true; to the beginning, the efficiency makes an astounding advance. Now the time cost is as short as DotNumerics under the same calculated amount.

Under such condition, the CPU occupation is 25% (4-core-CPU). It seems that your idea is necessary~

Jun 16, 2011 at 10:39 AM

I am also trying to solve a Least Square problem. My equation is G *D=A, where G,D,A are MathNet.Numerics.LinearAlgebra.Matrix.  I cannot find neither QR() solver nor Svd(). I want to apply one of them in G matrix. What am i doing wrong?

Jun 16, 2011 at 10:55 AM

You need it include the type specific namespace to get the extension methods, such as (if you are using doubles):

using MathNet.Numerics.LinearAlgebra.Double;

Jun 16, 2011 at 11:44 AM

The problem has been solved. I had an older version of math.net dll. I downloaded the latest version and everything is OK. Thanks.