/** * If a function $g(x)$ can be written as $W(x)f(x)$, where the weight function $W(x)$ corresponds * to one of the Gaussian quadrature forms, then we may approximate the integral of $g(x)$ over * a specific range as $\int^b_a g(x) dx =\int^b_a W(x)f(x) dx \approx \sum_{i=0}^{N-1} w_i f(x_i)$, * were the abscissas $x_i$ and the weights $w_i$ have been precomputed. This is accurate * if $f(x)$ can be approximated by a polynomial. * * @param polyFunction The function $f(x)$ rather than the full function $g(x) = W(x)f(x)$ * This should be well approximated by a polynomial. * @return The integral */ public double integrateFromPolyFunc(Function<Double, Double> polyFunction) { ArgChecker.notNull(polyFunction, "polyFunction"); double[] abscissas = quadrature.getAbscissas(); int n = abscissas.length; double[] weights = quadrature.getWeights(); double sum = 0; for (int i = 0; i < n; i++) { sum += polyFunction.apply(abscissas[i]) * weights[i]; } return sum; }
protected void assertResults(final GaussianQuadratureData f, final double[] x, final double[] w) { final double[] x1 = f.getAbscissas(); final double[] w1 = f.getWeights(); for (int i = 0; i < x.length; i++) { assertEquals(x1[i], x[i], EPS); assertEquals(w1[i], w[i], EPS); } } }
@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); } }
@Test public void test() { GaussianQuadratureData other = new GaussianQuadratureData(X, W); assertEquals(F, other); assertEquals(F.hashCode(), other.hashCode()); other = new GaussianQuadratureData(W, W); assertFalse(F.equals(other)); other = new GaussianQuadratureData(X, X); assertFalse(F.equals(other)); assertArrayEquals(F.getAbscissas(), X, 0); assertArrayEquals(F.getWeights(), W, 0); } }