-2

I need to solve an underdetermined linear equations system in c#. For example

Underdetermined linear equations system:
x + 3 = y + z
x + w = 2

Result:
x = r1
y = -r2 + r1 + 3
z = r2
w = 2 - r1
and now I initialize r1 and r2 with 3 and 4 to get one of my retults.

I try to use Math.Net in c# like this

using MathNet.Numerics.LinearAlgebra;
namespace SolveLinearEquations
{
    class Program
    {
        static void Main(string[] args)
        {
            var A = Matrix<double>.Build.DenseOfArray(new double[,] {
                { 1, -1, -1, 0 },
                { 1, 0, 0, 1 }
            });
            var B = Vector<double>.Build.Dense(new double[] { -3, 2 });
            var X = A.Solve(B);
        }
    }
}

but I take an exception like this

System.ArgumentException: 'Matrix dimensions must agree: 2x4.'

Can't Math.Net solve an underdetermined linear equations system or ...? What's the best solution for this?

2 Answers2

0

I solved it like this

using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearAlgebra.Double.Solvers;

namespace SolveLinearEquations
{
    class Program
    {
        static void Main(string[] args)
        {
            var A = Matrix<double>.Build.DenseOfArray(new double[,] {
                { 1, -1, -1, 0 },
                { 1, 0, 0, 1 },
                { 0, 0, 0, 0 },
                { 0, 0, 0, 0 }
            });
            var B = Vector<double>.Build.Dense(new double[] { -3, 2, 0, 0 });
            var x = A.SolveIterative(B, new MlkBiCgStab());
        }
    }
}

but now I have a problem. This solution gives me an unique value for each variable and sometimes this values are negative but I need positive values for each variable. what should I do?

  • 1
    please update the question to include the actual, remaining problem. – Cee McSharpface Apr 11 '20 at 12:27
  • Actually, no. Do not do what @Cee suggests. That turns the question into a [chamelon question](https://meta.stackexchange.com/questions/43478/exit-strategies-for-chameleon-questions). If the original question is solved, an answer should be posted and accepted. If a new question still remains, then a new question should be posted. – Peter Duniho Apr 11 '20 at 16:10
0

Here are three ways to try.

1) Change your undetermined systen by adding another equation. For example, if you are getting r1<0, then add the equation r1 = a for some a >= 0;

2) Recast your problem as a linear programming problem. This would give you the ability to add constraints, and also you can build the objective function to get a result that you like. So far as I know, this is not directly supported by MathNet, but there is an attempt at a solution here: http://type-nat.ch/post/lp-simplex-draft/

3) Use a general purpose minimizing function. Define the function to be minimized as the sum of error terms based on the system's equations. For example, change y=mx+b to (y-(mx+b))^2. For constraints, add a conditional error term, e.g. c = if (r1<0, r1^2, else 0)

phv3773
  • 487
  • 4
  • 10