+ * |xact - xexp| ≤  n  + * `Math.ulp(`xexp`)`,
where ε is the maximum absolute error. + * + * @param integrator Quadrature rule under test. + * @param maxDegree Maximum degree of monomials to be tested. + * @param eps ε. + * @param numUlps Value of the maximum relative error (in ulps). + */ + public GaussianQuadratureAbstractTest(GaussIntegrator integrator, + int maxDegree, + double eps, + double numUlps) { + this.integrator = integrator; + this.maxDegree = maxDegree; + this.eps = eps; + this.numUlps = numUlps; + } + + /** + * Returns the expected value of the integral of the specified monomial. + * The integration is carried out on the natural interval of the quadrature + * rule under test. + * + * @param n Degree of the monomial. + * @return the expected value of the integral of xn. + */ + public abstract double getExpectedValue(final int n); + + /** + * Checks that the value of the integral of each monomial + * `x0, ... , xp` + * returned by the quadrature rule under test conforms with the expected + * value. + * Here {@code p} denotes the degree of the highest polynomial for which + * exactness is to be expected. + */ + @Test + public void testAllMonomials() { + for (int n = 0; n <= maxDegree; n++) { + final double expected = getExpectedValue(n); + + final Power monomial = new Power(n); + final double actual = integrator.integrate(monomial); + + // System.out.println(n + "/" + maxDegree + " " + integrator.getNumberOfPoints() + // + " " + expected + " " + actual + " " + Math.ulp(expected)); + if (expected == 0) { + Assert.assertEquals("while integrating monomial x**" + n + + " with a " + + integrator.getNumberOfPoints() + "-point quadrature rule", + expected, actual, eps); + } else { + double err = Math.abs(actual - expected) / Math.ulp(expected); + Assert.assertEquals("while integrating monomial x**" + n + " with a " + + + integrator.getNumberOfPoints() + "-point quadrature rule, " + + " error was " + err + " ulps", + expected, actual, Math.ulp(expected) * numUlps); + } + } + } +} Propchange: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/integration/gauss/GaussianQuadratureAbstractTest.java ------------------------------------------------------------------------------ svn:eol-style = native Added: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/integration/gauss/LegendreHighPrecisionParametricTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/integration/gauss/LegendreHighPrecisionParametricTest.java?rev=1360706&view=auto ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/integration/gauss/LegendreHighPrecisionParametricTest.java (added) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/integration/gauss/LegendreHighPrecisionParametricTest.java Thu Jul 12 14:43:18 2012 @@ -0,0 +1,69 @@ +package org.apache.commons.math3.analysis.integration.gauss; + +import java.util.ArrayList; +import java.util.Collection; + +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +/** + * Test of the {@link LegendreHighPrecisionRuleFactory}. + * This parameterized test extends the standard test for Gaussian quadrature + * rule, where each monomial is tested in turn. + * Parametrization allows to test automatically 0, 1, ... , {@link #MAX_NUM_POINTS} + * quadrature rules. + * + * @version \$Id\$ + */ +@RunWith(value=Parameterized.class) +public class LegendreHighPrecisionParametricTest extends GaussianQuadratureAbstractTest { + private static GaussIntegratorFactory factory = new GaussIntegratorFactory(); + + /** + * The highest order quadrature rule to be tested. + */ + public static final int MAX_NUM_POINTS = 30; + + /** + * Creates a new instance of this test, with the specified number of nodes + * for the Gauss-Legendre quadrature rule. + * + * @param numberOfPoints Order of integration rule. + * @param maxDegree Maximum degree of monomials to be tested. + * @param eps Value of ε. + * @param numUlps Value of the maximum relative error (in ulps). + */ + public LegendreHighPrecisionParametricTest(int numberOfPoints, + int maxDegree, + double eps, + double numUlps) { + super(factory.legendreHighPrecision(numberOfPoints), + maxDegree, eps, numUlps); + } + + /** + * Returns the collection of parameters to be passed to the constructor of + * this class. + * Gauss-Legendre quadrature rules of order 1, ..., {@link #MAX_NUM_POINTS} + * will be constructed. + * + * @return the collection of parameters for this parameterized test. + */ + @Parameters + public static Collection getParameters() { + final ArrayList parameters = new ArrayList(); + for (int k = 1; k <= MAX_NUM_POINTS; k++) { + parameters.add(new Object[] { k, 2 * k - 1, Math.ulp(1d), 13d }); + } + return parameters; + } + + @Override + public double getExpectedValue(final int n) { + if (n % 2 == 1) { + return 0; + } + return 2d / (n + 1); + } +} Propchange: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/integration/gauss/LegendreHighPrecisionParametricTest.java ------------------------------------------------------------------------------ svn:eol-style = native Added: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/integration/gauss/LegendreHighPrecisionTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/integration/gauss/LegendreHighPrecisionTest.java?rev=1360706&view=auto ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/integration/gauss/LegendreHighPrecisionTest.java (added) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/integration/gauss/LegendreHighPrecisionTest.java Thu Jul 12 14:43:18 2012 @@ -0,0 +1,43 @@ +package org.apache.commons.math3.analysis.integration.gauss; + +import org.apache.commons.math3.analysis.function.Cos; +import org.apache.commons.math3.analysis.function.Inverse; +import org.apache.commons.math3.analysis.function.Log; +import org.apache.commons.math3.analysis.UnivariateFunction; +import org.junit.Test; +import org.junit.Assert; + +/** + * Test of the {@link LegendreHighPrecisionRuleFactory}. + * + * @version \$Id\$ + */ +public class LegendreHighPrecisionTest { + private static GaussIntegratorFactory factory = new GaussIntegratorFactory(); + + @Test + public void testCos() { + final UnivariateFunction cos = new Cos(); + + final GaussIntegrator integrator = factory.legendreHighPrecision(7, 0, Math.PI / 2); + final double s = integrator.integrate(cos); + // System.out.println("s=" + s + " e=" + 1); + Assert.assertEquals(1, s, Math.ulp(1d)); + } + + + @Test + public void testInverse() { + final UnivariateFunction inv = new Inverse(); + final UnivariateFunction log = new Log(); + + final double lo = 12.34; + final double hi = 456.78; + + final GaussIntegrator integrator = factory.legendreHighPrecision(60, lo, hi); + final double s = integrator.integrate(inv); + final double expected = log.value(hi) - log.value(lo); + // System.out.println("s=" + s + " e=" + expected); + Assert.assertEquals(expected, s, 1e-15); + } +} Propchange: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/integration/gauss/LegendreHighPrecisionTest.java ------------------------------------------------------------------------------ svn:eol-style = native Added: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/integration/gauss/LegendreParametricTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/integration/gauss/LegendreParametricTest.java?rev=1360706&view=auto ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/integration/gauss/LegendreParametricTest.java (added) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/integration/gauss/LegendreParametricTest.java Thu Jul 12 14:43:18 2012 @@ -0,0 +1,69 @@ +package org.apache.commons.math3.analysis.integration.gauss; + +import java.util.ArrayList; +import java.util.Collection; + +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +/** + * Test of the {@link LegendreRuleFactory}. + * This parameterized test extends the standard test for Gaussian quadrature + * rule, where each monomial is tested in turn. + * Parametrization allows to test automatically 0, 1, ... , {@link #MAX_NUM_POINTS} + * quadrature rules. + * + * @version \$Id\$ + */ +@RunWith(value=Parameterized.class) +public class LegendreParametricTest extends GaussianQuadratureAbstractTest { + private static GaussIntegratorFactory factory = new GaussIntegratorFactory(); + + /** + * The highest order quadrature rule to be tested. + */ + public static final int MAX_NUM_POINTS = 30; + + /** + * Creates a new instance of this test, with the specified number of nodes + * for the Gauss-Legendre quadrature rule. + * + * @param numberOfPoints Order of integration rule. + * @param maxDegree Maximum degree of monomials to be tested. + * @param eps Value of ε. + * @param numUlps Value of the maximum relative error (in ulps). + */ + public LegendreParametricTest(int numberOfPoints, + int maxDegree, + double eps, + double numUlps) { + super(factory.legendre(numberOfPoints), + maxDegree, eps, numUlps); + } + + /** + * Returns the collection of parameters to be passed to the constructor of + * this class. + * Gauss-Legendre quadrature rules of order 1, ..., {@link #MAX_NUM_POINTS} + * will be constructed. + * + * @return the collection of parameters for this parameterized test. + */ + @Parameters + public static Collection getParameters() { + final ArrayList parameters = new ArrayList(); + for (int k = 1; k <= MAX_NUM_POINTS; k++) { + parameters.add(new Object[] { k, 2 * k - 1, Math.ulp(1d), 91d }); + } + return parameters; + } + + @Override + public double getExpectedValue(final int n) { + if (n % 2 == 1) { + return 0; + } + return 2d / (n + 1); + } +} Propchange: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/integration/gauss/LegendreParametricTest.java ------------------------------------------------------------------------------ svn:eol-style = native Added: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/integration/gauss/LegendreTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/integration/gauss/LegendreTest.java?rev=1360706&view=auto ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/integration/gauss/LegendreTest.java (added) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/integration/gauss/LegendreTest.java Thu Jul 12 14:43:18 2012 @@ -0,0 +1,43 @@ +package org.apache.commons.math3.analysis.integration.gauss; + +import org.apache.commons.math3.analysis.function.Cos; +import org.apache.commons.math3.analysis.function.Inverse; +import org.apache.commons.math3.analysis.function.Log; +import org.apache.commons.math3.analysis.UnivariateFunction; +import org.junit.Test; +import org.junit.Assert; + +/** + * Test of the {@link LegendreRuleFactory}. + * + * @version \$Id\$ + */ +public class LegendreTest { + private static GaussIntegratorFactory factory = new GaussIntegratorFactory(); + + @Test + public void testCos() { + final UnivariateFunction cos = new Cos(); + + final GaussIntegrator integrator = factory.legendre(7, 0, Math.PI / 2); + final double s = integrator.integrate(cos); + // System.out.println("s=" + s + " e=" + 1); + Assert.assertEquals(1, s, Math.ulp(1d)); + } + + + @Test + public void testInverse() { + final UnivariateFunction inv = new Inverse(); + final UnivariateFunction log = new Log(); + + final double lo = 12.34; + final double hi = 456.78; + + final GaussIntegrator integrator = factory.legendre(60, lo, hi); + final double s = integrator.integrate(inv); + final double expected = log.value(hi) - log.value(lo); + // System.out.println("s=" + s + " e=" + expected); + Assert.assertEquals(expected, s, 1e-14); + } +} Propchange: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/integration/gauss/LegendreTest.java ------------------------------------------------------------------------------ svn:eol-style = native