Home:ALL Converter>solve an underdetermined linear equations system in c#

solve an underdetermined linear equations system in c#

Ask Time:2020-04-11T15:42:57         Author:user12834642

Json Formatter

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?

Author:user12834642,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/61153551/solve-an-underdetermined-linear-equations-system-in-c-sharp
user12834642 :

I solved it like this\n\nusing MathNet.Numerics.LinearAlgebra;\nusing MathNet.Numerics.LinearAlgebra.Double.Solvers;\n\nnamespace SolveLinearEquations\n{\n class Program\n {\n static void Main(string[] args)\n {\n var A = Matrix<double>.Build.DenseOfArray(new double[,] {\n { 1, -1, -1, 0 },\n { 1, 0, 0, 1 },\n { 0, 0, 0, 0 },\n { 0, 0, 0, 0 }\n });\n var B = Vector<double>.Build.Dense(new double[] { -3, 2, 0, 0 });\n var x = A.SolveIterative(B, new MlkBiCgStab());\n }\n }\n}\n\n\nbut 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? ",
2020-04-11T12:23:31
yy