1

I'm trying to find the roots of a cubic polynomial ax^3+bx^2+cx+d=0 using math.numerics. The package is great, but I'm struggling to get started with it. Please can someone explain how to find the roots, and a simple explanation on how to run the sample package from Github?

I've added a reference to the package

using MathNet.Numerics;

and this is what I've tried:

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

but I get an error "The type 'Complex' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Numerics'". Adding using System.Numerics gives an error and doesn't solve the problem.

Any suggestions please?

Zeus
  • 1,496
  • 2
  • 24
  • 53
  • you need to input the code in the top most part Using System.Numerics – Albert Laure Jul 15 '16 at 03:22
  • that's where I put it, along with all the other assemblies, directly below ``using MathNet.Numerics``. Intellisense says the assembly is not required. – Zeus Jul 15 '16 at 03:35

1 Answers1

5

If you're using Visual Studio, you need to right-click the References folder for your project in Solution Explorer, click Add Reference, and then select System.Numerics from the Assemblies > Framework list:

Screenshot of Reference Manager dialog box

Because MathNet.Numerics.FindRoots.Cubic returns roots as complex numbers, you must use the System.Numerics.Complex type instead of double to store your roots:

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;
    }
}

If you only want to deal with real numbers, call 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;
Michael Liu
  • 52,147
  • 13
  • 117
  • 150
  • Yes, he tried that, but "it gave an error". *rolleyes* – Blorgbeard Jul 15 '16 at 03:21
  • @Blorgbeard: The OP stated that he added `using System.Numerics;` to his code file, instead of adding a reference to the project. – Michael Liu Jul 15 '16 at 03:22
  • "Adding using System.Numerics gives an error and doesn't solve the problem." – Blorgbeard Jul 15 '16 at 03:22
  • Oh, beg your pardon, just read that again, you're right - lol. – Blorgbeard Jul 15 '16 at 03:22
  • I've added the reference as Michael suggested and the compile error has gone. I was just adding the ``using ...`` line to the top of the class - sorry I should have been clearer. Thanks for the suggestion. I'll also try using complex instead of double. – Zeus Jul 15 '16 at 04:21
  • thanks,it works! Can you explain how can I use the sample package from GitHub? I can't compile and run it. – Zeus Jul 15 '16 at 04:26
  • @Zeus: What happens when you try to compile and run it? – Michael Liu Jul 15 '16 at 13:37
  • @Michael - if I open ``Examples.sln`` and click ``Start`` in debug or release mode I get an error saying ``"A project with an Output Type of Class Library cannot be started directly"``. I'm doing something wrong, but this is new to me so I'm not sure what I should be doing. – Zeus Jul 18 '16 at 06:28
  • @Michael - do you know where I can download a manual on the package's various functions? – Zeus Jul 18 '16 at 06:29
  • See http://stackoverflow.com/questions/23996140/math-net-numerics-how-to-run-examples and http://numerics.mathdotnet.com/api/ – Michael Liu Jul 19 '16 at 02:57
  • Needs reference to MathNet.Numerics – Sith2021 Aug 31 '23 at 22:24