PolynomialsUtils { public static PolynomialFunction createLegendrePolynomial(final int degree) { return buildPolynomial(degree, LEGENDRE_COEFFICIENTS, new RecurrenceCoefficientsGenerator() { public BigFraction[] generate(int k) { final int kP1 = k + 1; return new BigFraction[] { BigFraction.ZERO, new BigFraction(k + kP1, kP1), new BigFraction(k, kP1)}; } }); } private  PolynomialsUtils(); static PolynomialFunction createChebyshevPolynomial(final int degree); static PolynomialFunction createHermitePolynomial(final int degree); static PolynomialFunction createLaguerrePolynomial(final int degree); static PolynomialFunction createLegendrePolynomial(final int degree); static PolynomialFunction createJacobiPolynomial(final int degree, final int v, final int w); static double[] shift(final double[] coefficients,
                                 final double shift);  }
@Test public void testFirstLegendrePolynomials() { checkPolynomial(PolynomialsUtils.createLegendrePolynomial(3), 2l, "-3 x + 5 x^3"); checkPolynomial(PolynomialsUtils.createLegendrePolynomial(2), 2l, "-1 + 3 x^2"); checkPolynomial(PolynomialsUtils.createLegendrePolynomial(1), 1l, "x"); checkPolynomial(PolynomialsUtils.createLegendrePolynomial(0), 1l, "1"); checkPolynomial(PolynomialsUtils.createLegendrePolynomial(7), 16l, "-35 x + 315 x^3 - 693 x^5 + 429 x^7"); checkPolynomial(PolynomialsUtils.createLegendrePolynomial(6), 16l, "-5 + 105 x^2 - 315 x^4 + 231 x^6"); checkPolynomial(PolynomialsUtils.createLegendrePolynomial(5), 8l, "15 x - 70 x^3 + 63 x^5"); checkPolynomial(PolynomialsUtils.createLegendrePolynomial(4), 8l, "3 - 30 x^2 + 35 x^4"); }
@Test public void testLegendreDifferentials() { for (int k = 0; k < 12; ++k) { PolynomialFunction Pk0 = PolynomialsUtils.createLegendrePolynomial(k); PolynomialFunction Pk1 = Pk0.polynomialDerivative(); PolynomialFunction Pk2 = Pk1.polynomialDerivative(); PolynomialFunction g0 = new PolynomialFunction(new double[] { k * (k + 1) }); PolynomialFunction g1 = new PolynomialFunction(new double[] { 0, -2 }); PolynomialFunction g2 = new PolynomialFunction(new double[] { 1, 0, -1 }); PolynomialFunction Pk0g0 = Pk0.multiply(g0); PolynomialFunction Pk1g1 = Pk1.multiply(g1); PolynomialFunction Pk2g2 = Pk2.multiply(g2); checkNullPolynomial(Pk0g0.add(Pk1g1.add(Pk2g2))); } }
@Test public void testLegendreOrthogonality() { UnivariateFunction weight = new UnivariateFunction() { public double value(double x) { return 1; } }; for (int i = 0; i < 10; ++i) { PolynomialFunction pi = PolynomialsUtils.createLegendrePolynomial(i); for (int j = 0; j <= i; ++j) { PolynomialFunction pj = PolynomialsUtils.createLegendrePolynomial(j); checkOrthogonality(pi, pj, weight, -1, 1, 0.1, 1.0e-13); } } }
@Test public void testHighDegreeLegendre() { PolynomialsUtils.createLegendrePolynomial(40); double[] l40 = PolynomialsUtils.createLegendrePolynomial(40).getCoefficients(); double denominator = 274877906944d; double[] numerators = new double[] { +34461632205d, -28258538408100d, +3847870979902950d, -207785032914759300d, +5929294332103310025d, -103301483474866556880d, +1197358103913226000200d, -9763073770369381232400d, +58171647881784229843050d, -260061484647976556945400d, +888315281771246239250340d, -2345767627188139419665400d, +4819022625419112503443050d, -7710436200670580005508880d, +9566652323054238154983240d, -9104813935044723209570256d, +6516550296251767619752905d, -3391858621221953912598660d, +1211378079007840683070950d, -265365894974690562152100d, +26876802183334044115405d }; for (int i = 0; i < l40.length; ++i) { if (i % 2 == 0) { double ci = numerators[i / 2] / denominator; Assert.assertEquals(ci, l40[i], FastMath.abs(ci) * 1e-15); } else { Assert.assertEquals(0, l40[i], 0); } } }