/** * Creates an instance. * * @param n The number of sample points to be used in the integration, not negative or zero * @param generator The generator of weights and abscissas */ public GaussianQuadratureIntegrator1D(int n, QuadratureWeightAndAbscissaFunction generator) { ArgChecker.isTrue(n > 0, "number of intervals must be > 0"); ArgChecker.notNull(generator, "generating function"); this.size = n; this.generator = generator; this.quadrature = generator.generate(size); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullFunction() { getFunction().generate(-1); }
@Test public void test() { final int n = 12; final GaussianQuadratureData f1 = GAUSS_LEGENDRE.generate(n); final GaussianQuadratureData f2 = GAUSS_JACOBI_GL_EQUIV.generate(n); final GaussianQuadratureData f3 = GAUSS_JACOBI_CHEBYSHEV_EQUIV.generate(n); final double[] w1 = f1.getWeights(); final double[] w2 = f2.getWeights(); final double[] x1 = f1.getAbscissas(); final double[] x2 = f2.getAbscissas(); assertTrue(w1.length == w2.length); assertTrue(x1.length == x2.length); for (int i = 0; i < n; i++) { assertEquals(w1[i], w2[i], EPS); assertEquals(x1[i], -x2[i], EPS); } final double[] w3 = f3.getWeights(); final double[] x3 = f3.getAbscissas(); final double chebyshevWeight = Math.PI / n; final Function<Integer, Double> chebyshevAbscissa = new Function<Integer, Double>() { @Override public Double apply(final Integer x) { return -Math.cos(Math.PI * (x + 0.5) / n); } }; for (int i = 0; i < n; i++) { assertEquals(chebyshevWeight, w3[i], EPS); assertEquals(chebyshevAbscissa.apply(i), -x3[i], EPS); } }