### Question:
LegendrePolynomialFunction extends OrthogonalPolynomialFunctionGenerator { @Override public DoubleFunction1D[] getPolynomials(int n) { ArgChecker.isTrue(n >= 0); DoubleFunction1D[] polynomials = new DoubleFunction1D[n + 1]; for (int i = 0; i <= n; i++) { if (i == 0) { polynomials[i] = getOne(); } else if (i == 1) { polynomials[i] = getX(); } else { polynomials[i] = (polynomials[i - 1] .multiply(getX()) .multiply(2 * i - 1) .subtract(polynomials[i - 2].multiply(i - 1))).multiply(1. / i); } } return polynomials; }  @Override DoubleFunction1D[] getPolynomials(int n); @Override Pair<DoubleFunction1D, DoubleFunction1D>[] getPolynomialsAndFirstDerivative(int n);  }### Answer:
@Test public void testBadN() { assertThatIllegalArgumentException() .isThrownBy(() -> LEGENDRE.getPolynomials(-3)); }
@Test public void test() { DoubleFunction1D[] p = LEGENDRE.getPolynomials(0); assertThat(p.length).isEqualTo(1); final double x = 1.23; assertThat(p[0].applyAsDouble(x)).isCloseTo(1, offset(EPS)); p = LEGENDRE.getPolynomials(1); assertThat(p.length).isEqualTo(2); assertThat(p[1].applyAsDouble(x)).isCloseTo(x, offset(EPS)); for (int i = 0; i <= 10; i++) { p = LEGENDRE.getPolynomials(i); for (int j = 0; j <= i; j++) { assertThat(P[j].applyAsDouble(x)).isCloseTo(p[j].applyAsDouble(x), offset(EPS)); } } }