1

我正在尝试ax^3+bx^2+cx+d=0使用 math.numerics 找到三次多项式的根。这个包很棒,但我很难开始使用它。请有人解释如何找到根源,以及如何从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'"。使用 System.Numerics 添加会出错,但不能解决问题。

请问有什么建议吗?

4

1 回答 1

4

如果您使用的是 Visual Studio,则需要在解决方案资源管理器中右键单击项目的 References 文件夹,单击添加引用,然后从 Assemblies > Framework 列表中选择System.Numerics :

参考管理器对话框的屏幕截图

因为MathNet.Numerics.FindRoots.Cubic将根作为复数返回,所以您必须使用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(它将返回任何复值根为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;
于 2016-07-15T03:20:59.817 回答