我正在尝试使用 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;
但我收到错误 “类型‘Complex’在未引用的程序集中定义。您必须添加对程序集‘System.Numerics’的引用”。添加 using System.Numerics 会出错,并且无法解决问题。
有什么建议吗?
请您参考如下方法:
如果您使用的是 Visual Studio,则需要在解决方案资源管理器中右键单击项目的引用文件夹,单击添加引用,然后从程序集 > 框架列表中选择 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;
