Search code examples
c#linear-algebramath.net

Solve non square linear system with Math.net


I'm trying to solve a non square linear system with Math.net. But I get an error Matrix dimensions must agree: 3x7.

Here is some example code:

using MathNet.Numerics.LinearAlgebra;
var mBuilder = Matrix<double>.Build;
var vBuilder = Vector<double>.Build;
var A = mBuilder.DenseOfArray(new double[,]
{
    { 3, 2, 1, 5, -1, 0, 0 },
    { 2, 1, 1, 2, 0, -1, 0 },
    { 5, 1, 3, 4, 0, 0, -1 }
});
var b = vBuilder.DenseOfArray(new double[] { -3, -5, -2 });
Vector<double> x;
x = A.Solve(b);

Cleary the system has a solution (e.g. X = {0, 0, 0, 0, 3, 5, 2}). How can I solve such a system with Math.Net?


Solution

  • You can not use the Matrix.Solve function with a non-square matrix because there is no inverse and no unique solutions for a rectangular matrix. Google "inverse of rectangular matrix" for explanations galore. You can use pseudoinverse however, as shown below.

            var mBuilder = Matrix<double>.Build;
            var A = mBuilder.DenseOfArray(new double[,]
          {
              { 3, 2, 1, 5, -1, 0, 0 },
              { 2, 1, 1, 2, 0, -1, 0 },
              { 5, 1, 3, 4, 0, 0, -1 }
          });
    
            Matrix<double> b = Matrix<double>.Build.Dense(3, 1);
            b[0, 0] = -3.0;
            b[1, 0] = -5.0;
            b[2, 0] = -2.0;
    
            var p = A.PseudoInverse();
            var x = p * b;
    
            // verify
            var o = A * x;