/** * 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]); }
/** * <p> * Find the polynomial roots and for each root compute the Fundamental matrix. * Given the two matrices it will compute an alpha such that the determinant is zero.<br> * * det(&alpha*F1 + (1-α)*F2 ) = 0 * </p> */ public void computeSolutions( FastQueue<DenseMatrix64F> solutions ) { if( !rootFinder.process(poly)) return; List<Complex64F> zeros = rootFinder.getRoots(); for( Complex64F c : zeros ) { if( !c.isReal() && Math.abs(c.imaginary) > 1e-10 ) continue; DenseMatrix64F F = solutions.grow(); double a = c.real; double b = 1-c.real; for( int i = 0; i < 9; i++ ) { F.data[i] = a*F1.data[i] + b*F2.data[i]; } // det(F) = 0 is already enforced, but for essential matrices it needs to enforce // that the first two singular values are zero and the last one is zero if( !computeFundamental && !projectOntoEssential(F) ) { solutions.removeTail(); } } }
/** * <p> * Find the polynomial roots and for each root compute the Fundamental matrix. * Given the two matrices it will compute an alpha such that the determinant is zero.<br> * * det(&alpha*F1 + (1-α)*F2 ) = 0 * </p> */ public void computeSolutions( FastQueue<DMatrixRMaj> solutions ) { if( !rootFinder.process(poly)) return; List<Complex_F64> zeros = rootFinder.getRoots(); for( Complex_F64 c : zeros ) { if( !c.isReal() && Math.abs(c.imaginary) > 1e-10 ) continue; DMatrixRMaj F = solutions.grow(); double a = c.real; double b = 1-c.real; for( int i = 0; i < 9; i++ ) { F.data[i] = a*F1.data[i] + b*F2.data[i]; } // det(F) = 0 is already enforced, but for essential matrices it needs to enforce // that the first two singular values are zero and the last one is zero if( !computeFundamental && !projectOntoEssential(F) ) { solutions.removeTail(); } } }
public static void main( String args[] ) { // Select which algorithm to use PolynomialRoots finder = PolynomialOps.createRootFinder(6, RootFinderType.EVD); // Create an arbitrary 3rd order polynomial // f(x) = 2 + 0.2*x + 5*x^2 + 3*x^3 Polynomial poly = Polynomial.wrap( 2 , 0.2 , 5 , 3 ); // Find the roots if( !finder.process(poly) ) throw new RuntimeException("Failed to find solution!"); // Print the solution List<Complex_F64> roots = finder.getRoots(); System.out.println("Total roots found: "+roots.size()); for( Complex_F64 c : roots ) { if( !c.isReal() ) { System.out.println("root is imaginary: "+c); continue; } double value = poly.evaluate(c.real); System.out.println("Polynomial value at "+c.real+" is "+value); } } }
return false; for( Complex_F64 c : findRoots.getRoots() ) { if( !c.isReal() ) continue;
return false; for( Complex64F c : findRoots.getRoots() ) { if( !c.isReal() ) continue;
for( Complex64F r : rootFinder.getRoots() ) { if( r.isReal() ) { root = r;
for( Complex_F64 r : rootFinder.getRoots() ) { if( r.isReal() ) { root = r;
return false; List<Complex64F> roots = rootFinder.getRoots();
return false; List<Complex_F64> roots = rootFinder.getRoots();
@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); } } }
return false; if( !selectBestSolution(rootFinder.getRoots(),f1,f2,a,b,c,d)) return false;