HornerPolynomialCalculator { @Complexity("O(n)") public double calculate(double x, double... coefficients) { if (coefficients.length == 0) { return 0; } double result = coefficients[coefficients.length - 1]; for (int i = coefficients.length - 2; i >= 0; i --) { result = coefficients[i] + x * result; } return result; }  @Complexity("O(n)") double calculate(double x, double... coefficients);  }
@Test public void testCalculatingPolynomial() throws Exception { final HornerPolynomialCalculator calculator = new HornerPolynomialCalculator(); double x = 1.0; assertThat(calculator.calculate(x, -10, 0, 1, -2, 1), is(Math.pow(x, 4) - 2 * Math.pow(x, 3) + Math.pow(x, 2) - 10)); x = -1.0; assertThat(calculator.calculate(x, -10, 0, 1, -2, 1), is(Math.pow(x, 4) - 2 * Math.pow(x, 3) + Math.pow(x, 2) - 10)); }
@Test public void testEmptyCoefficientList() throws Exception { assertThat(new HornerPolynomialCalculator().calculate(0), is(0d)); assertThat(new HornerPolynomialCalculator().calculate(1), is(0d)); assertThat(new HornerPolynomialCalculator().calculate(2), is(0d)); assertThat(new HornerPolynomialCalculator().calculate(3), is(0d)); }
@Test public void testCalculatingZeroValue() throws Exception { final HornerPolynomialCalculator calculator = new HornerPolynomialCalculator(); double x = 2.0; assertThat(calculator.calculate(x, 0, -2, 1), is(0 - 2 * x + x * x)); }