QuadraticRealRootFinder implements Polynomial1DRootFinder<Double> { @Override public Double[] getRoots(final RealPolynomialFunction1D function) { Validate.notNull(function, "function"); final double[] coefficients = function.getCoefficients(); Validate.isTrue(coefficients.length == 3, "Function is not a quadratic"); final double c = coefficients[0]; final double b = coefficients[1]; final double a = coefficients[2]; final double discriminant = b * b - 4 * a * c; if (discriminant < 0) { throw new MathException("No real roots for quadratic"); } final double q = -0.5 * (b + Math.signum(b) * discriminant); return new Double[] {q / a, c / q}; }  @Override Double[] getRoots(final RealPolynomialFunction1D function);  }
@Test public void test() { try { FINDER.getRoots(null); Assert.fail(); } catch (final IllegalArgumentException e) { } try { FINDER.getRoots(new RealPolynomialFunction1D(new double[] {1., 2., 3., 4.})); Assert.fail(); } catch (final IllegalArgumentException e) { } try { FINDER.getRoots(new RealPolynomialFunction1D(new double[] {12., 1., 12.})); Assert.fail(); } catch (final MathException e) { } final Double[] roots = FINDER.getRoots(F); assertEquals(roots[0], -4.0, EPS); assertEquals(roots[1], -3.0, EPS); }