解决在使用mathdotnet线性方程组的系统? [英] Solving system of linear equations using mathdotnet?

查看:483
本文介绍了解决在使用mathdotnet线性方程组的系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要解决像,

(4-X)* 2 =(Y-1)* 10 + 2 结果
X = Y * 2 + 1

该方程以字符串形式提供。
是否有表达mathdotnet一个方程的方法吗?我只能想办法写表达式。

The equations are available in string form. Is there a way to express a equation in mathdotnet? I can only find ways to write expressions.

推荐答案

Math.NET Numerics的可以数值求解任何线性系统,但我想这不是你要找的内容。

Math.NET Numerics can solve any linear system numerically, but I suppose that's not what you're looking for.

数学.NET对符号可以处理符号表达式,虽然这个项目处于早期阶段,还没有理解方程的概念。但是,我们仍然可以用它来解决一些简单的系统,如这些,有一些工作 - 做什么我们会做手工,以及

Math.NET Symbolics can deal with symbolic expressions, although this project is in an early stage and does not yet understand the concept of equations. However, we can still use it to solve simple systems like these, with a bit of work - by doing what we would do by hand as well.

首先,让我们定义一个小功能,解决了令一个线性方程高达1:

First, let's define a small function to solve a single linear equation of order up to 1:

using Expr = MathNet.Symbolics.Expression;

Expr SolveSimpleRoot(Expr variable, Expr expr)
{
    // try to bring expression into polynomial form
    Expr simple = Algebraic.Expand(Rational.Numerator(Rational.Simplify(variable,expr)));

    // extract coefficients, solve known forms of order up to 1
    Expr[] coeff = Polynomial.Coefficients(variable,simple);
    switch(coeff.Length)
    {
        case 1: return Expr.Zero.Equals(coeff[0]) ? variable : Expr.Undefined;
        case 2: return Rational.Simplify(variable,Algebraic.Expand(-coeff[0]/coeff[1]));
        default: return Expr.Undefined;
    }
}



然后,我们可以用这个如下解决系统

Then we can use this to solve the system as follows:

// declare variables
var x = Expr.Symbol("x");
var y = Expr.Symbol("y");

// Parse left and right side of both equations
Expr aleft = Infix.ParseOrThrow("(4-x)*2");
Expr aright = Infix.ParseOrThrow("(y-1)*10+2");
Expr bleft = Infix.ParseOrThrow("x");
Expr bright = Infix.ParseOrThrow("y*2+1");

// Solve both equations to x
Expr ax = SolveSimpleRoot(x,aleft-aright); // "8 - 5*y"
Expr bx = SolveSimpleRoot(x,bleft-bright); // "1 + 2*y"

// Equate both terms of x, solve to y
Expr cy = SolveSimpleRoot(y,ax-bx); // "1"

// Substitute term of y into one of the terms of x
Expr cx = Algebraic.Expand(Structure.Substitute(y,cy,ax)); // "3"

// Print expression in Infix notation
Console.WriteLine(Infix.Print(cx)); // x=3
Console.WriteLine(Infix.Print(cy)); // y=1

这篇关于解决在使用mathdotnet线性方程组的系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆