LegendrePolynomialFunction extends OrthogonalPolynomialFunctionGenerator { @Override public DoubleFunction1D[] getPolynomials(final int n) { Validate.isTrue(n >= 0); final 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(final int n); @Override Pair<DoubleFunction1D, DoubleFunction1D>[] getPolynomialsAndFirstDerivative(final int n);  }
@Test(expectedExceptions = IllegalArgumentException.class) public void testBadN() { LEGENDRE.getPolynomials(-3); }
@Test public void test() { DoubleFunction1D[] p = LEGENDRE.getPolynomials(0); assertEquals(p.length, 1); final double x = 1.23; assertEquals(p[0].evaluate(x), 1, EPS); p = LEGENDRE.getPolynomials(1); assertEquals(p.length, 2); assertEquals(p[1].evaluate(x), x, EPS); for (int i = 0; i <= 10; i++) { p = LEGENDRE.getPolynomials(i); for (int j = 0; j <= i; j++) { assertEquals(P[j].evaluate(x), p[j].evaluate(x), EPS); } } }