@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 Function1D<Integer, Double> chebyshevAbscissa = new Function1D<Integer, Double>() { @Override public Double evaluate(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.evaluate(i), -x3[i], EPS); } }
/** {@inheritDoc} */ @Override public Double integrate( final Function1D<Double, Double> function, final Double lower, final Double upper) { Validate.notNull(function); Validate.notNull(lower); Validate.notNull(upper); final Function1D<Double, Double> integral = getIntegralFunction(function, lower, upper); final GaussianQuadratureData quadrature = _generator.generate(_n); final double[] abscissas = quadrature.getAbscissas(); final int n = abscissas.length; final double[] weights = quadrature.getWeights(); double sum = 0; for (int i = 0; i < n; i++) { sum += integral.evaluate(abscissas[i]) * weights[i]; } return sum; }