/** * Creates different polynomial root finders. * * @param maxCoefficients The maximum number of coefficients that will be processed. This is the order + 1 * @param which 0 = Sturm and 1 = companion matrix. * @return PolynomialRoots */ public static PolynomialRoots createRootFinder( int maxCoefficients , RootFinderType which ) { switch( which ) { case STURM: FindRealRootsSturm sturm = new FindRealRootsSturm(maxCoefficients,-1,1e-10,200,200); return new WrapRealRootsSturm(sturm); case EVD: return new RootFinderCompanion(); default: throw new IllegalArgumentException("Unknown algorithm: "+which); } }
/** * Creates a generic polynomial root finding class which will return all real or all real and complex roots * depending on the algorithm selected. * * @param type Which algorithm is to be returned. * @param maxDegree Maximum degree of the polynomial being considered. * @return Root finding algorithm. */ public static PolynomialRoots createRootFinder( RootFinderType type , int maxDegree ) { switch ( type ) { case EVD: return new RootFinderCompanion(); case STURM: FindRealRootsSturm sturm = new FindRealRootsSturm(maxDegree,-1,1e-10,30,20); return new WrapRealRootsSturm(sturm); } throw new IllegalArgumentException("Unknown type"); }
/** * Creates different polynomial root finders. * * @param maxCoefficients The maximum number of coefficients that will be processed. This is the order + 1 * @param which 0 = Sturm and 1 = companion matrix. * @return PolynomialRoots */ public static PolynomialRoots createRootFinder( int maxCoefficients , RootFinderType which ) { switch( which ) { case STURM: FindRealRootsSturm sturm = new FindRealRootsSturm(maxCoefficients,-1,1e-10,200,200); return new WrapRealRootsSturm(sturm); case EVD: return new RootFinderCompanion(); default: throw new IllegalArgumentException("Unknown algorithm: "+which); } }
/** * Creates a generic polynomial root finding class which will return all real or all real and complex roots * depending on the algorithm selected. * * @param type Which algorithm is to be returned. * @param maxDegree Maximum degree of the polynomial being considered. * @return Root finding algorithm. */ public static PolynomialRoots createRootFinder( RootFinderType type , int maxDegree ) { switch ( type ) { case EVD: return new RootFinderCompanion(); case STURM: FindRealRootsSturm sturm = new FindRealRootsSturm(maxDegree,-1,1e-10,30,20); return new WrapRealRootsSturm(sturm); } throw new IllegalArgumentException("Unknown type"); }
/** * Finds real and imaginary roots in a polynomial using the companion matrix and * Eigenvalue decomposition. The coefficients order is specified from smallest to largest. * Example, 5 + 6*x + 7*x^2 + 8*x^3 = [5,6,7,8] * * @param coefficients Polynomial coefficients from smallest to largest. * @return The found roots. */ @SuppressWarnings("ToArrayCallWithZeroLengthArrayArgument") public static Complex_F64[] polynomialRootsEVD(double... coefficients) { PolynomialRoots alg = new RootFinderCompanion(); if( !alg.process( Polynomial.wrap(coefficients)) ) throw new IllegalArgumentException("Algorithm failed, was the input bad?"); List<Complex_F64> coefs = alg.getRoots(); return coefs.toArray(new Complex_F64[0]); }
/** * Finds real and imaginary roots in a polynomial using the companion matrix and * Eigenvalue decomposition. The coefficients order is specified from smallest to largest. * Example, 5 + 6*x + 7*x^2 + 8*x^3 = [5,6,7,8] * * @param coefficients Polynomial coefficients from smallest to largest. * @return The found roots. */ @SuppressWarnings("ToArrayCallWithZeroLengthArrayArgument") public static Complex_F64[] polynomialRootsEVD(double... coefficients) { PolynomialRoots alg = new RootFinderCompanion(); if( !alg.process( Polynomial.wrap(coefficients)) ) throw new IllegalArgumentException("Algorithm failed, was the input bad?"); List<Complex_F64> coefs = alg.getRoots(); return coefs.toArray(new Complex_F64[0]); }
@Test public void basicTest() { for( int numCoef = 2; numCoef < 6; numCoef++ ) { Polynomial poly = new Polynomial(numCoef); for( int i = 0; i < numCoef; i++ ) { poly.c[i] = 10*(rand.nextDouble()-0.5); } PolynomialRoots alg = new RootFinderCompanion(); assertTrue(alg.process(poly)); List<Complex_F64> roots = alg.getRoots(); int numReal = 0; for( Complex_F64 c : roots ) { if( c.isReal() ) { assertEquals(0,poly.evaluate(c.real),1e-8); numReal++; } } int expectedRoots = PolynomialOps.countRealRoots(poly); assertTrue(numReal == expectedRoots); } } }