求三次多项式的根

find roots of cubic polynomial

我正在尝试使用 math.numerics 求三次多项式 ax^3+bx^2+cx+d=0 的根。这个包很棒,但我很难开始使用它。请有人解释如何找到根源,并简单解释如何 运行 来自 Github 的示例包?

我添加了对包的引用

using MathNet.Numerics;

这是我尝试过的:

var roots = FindRoots.Cubic(d, c, b, a);
double root1=roots.item1;
double root2=roots.item2;
double root3=roots.item3;

但我得到一个错误 "The type 'Complex' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Numerics'"。添加using System.Numerics报错,并没有解决问题。

有什么建议吗?

如果您使用 Visual Studio,您需要在解决方案资源管理器中右键单击项目的引用文件夹,单击添加引用,然后单击 select System.Numerics 来自程序集 > 框架列表:

因为 MathNet.Numerics.FindRoots.Cubic returns roots as complex numbers, you must use the System.Numerics.Complex 键入而不是 double 来存储你的根:

using System.Numerics;
using MathNet.Numerics;

class Program
{
    static void Main()
    {
        double d = 0, c = -1, b = 0, a = 1; // x^3 - x
        var roots = FindRoots.Cubic(d, c, b, a);
        Complex root1 = roots.Item1;
        Complex root2 = roots.Item2;
        Complex root3 = roots.Item3;
    }
}

如果只想处理实数,调用MathNet.Numerics.RootFinding.Cubic.RealRoots instead (which will return any complex-valued roots as Double.NaN):

using MathNet.Numerics.RootFinding;
...
var roots = Cubic.RealRoots(d, c, b); // "a" is assumed to be 1
double root1 = roots.Item1;
double root2 = roots.Item2;
double roo13 = roots.Item3;