Polynomial Root FindingΒΆ

The root of a polynomial is a complex number such that when feed into the polynomial will generate an output of 0.

https://github.com/lessthanoptimal/ddogleg/tree/v0.23.3/examples/src/org/ddogleg/example/ExamplePolynomialRoot.java

 1public static void main( String[] args ) {
 2    // Select which algorithm to use
 3    PolynomialRoots finder = PolynomialOps.createRootFinder(6, RootFinderType.EVD);
 4
 5    // Create an arbitrary 3rd order polynomial
 6    // f(x) = 2 + 0.2*x + 5*x^2 + 3*x^3
 7    Polynomial poly = Polynomial.wrap( 2 , 0.2 , 5 , 3 );
 8
 9    // Find the roots
10    if( !finder.process(poly) )
11        throw new RuntimeException("Failed to find solution!");
12
13    // Print the solution
14    List<Complex_F64> roots = finder.getRoots();
15
16    System.out.println("Total roots found: "+roots.size());
17
18    for( Complex_F64 c : roots ) {
19        if( !c.isReal() ) {
20            System.out.println("root is imaginary: "+c);
21            continue;
22        }
23
24        double value = poly.evaluate(c.real);
25        System.out.println("Polynomial value at "+c.real+" is "+value);
26    }
27}