LaguerrePolynomialRealRootFinder implements Polynomial1DRootFinder<Double> { @Override public Double[] getRoots(final RealPolynomialFunction1D function) { ArgumentChecker.notNull(function, "function"); try { final Complex[] roots = ROOT_FINDER.solveAll(function.getCoefficients(), 0); final List<Double> realRoots = new ArrayList<>(); for (final Complex c : roots) { if (CompareUtils.closeEquals(c.getImaginary(), 0, EPS)) { realRoots.add(c.getReal()); } } if (realRoots.isEmpty()) { throw new MathException("Could not find any real roots"); } return realRoots.toArray(EMPTY_ARRAY); } catch (final FunctionEvaluationException e) { throw new MathException(e); } catch (final org.apache.commons.math.ConvergenceException e) { throw new MathException(e); } }  @Override Double[] getRoots(final RealPolynomialFunction1D function);  }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullFunction() { ROOT_FINDER.getRoots(null); }
@Test(expectedExceptions = MathException.class) public void testNoRealRoots() { ROOT_FINDER.getRoots(NO_REAL_ROOTS); }
@Test public void test() { Double[] result = ROOT_FINDER.getRoots(TWO_REAL_ROOTS); Arrays.sort(result); assertEquals(result.length, 2); assertEquals(result[0], -4, EPS); assertEquals(result[1], -3, EPS); result = ROOT_FINDER.getRoots(ONE_REAL_ROOT); assertEquals(result.length, 2); assertEquals(result[0], 3, EPS); assertEquals(result[1], 3, EPS); result = ROOT_FINDER.getRoots(CLOSE_ROOTS); Arrays.sort(result); assertEquals(result.length, 2); assertEquals(result[1] - result[0], 1e-6, 1e-8); }