/** * The inner integral function of the repeated 1-D integrations. * For a given $y$ it returns $\int_{x_1}^{x_2} f(x,y) dx$. * * @param f the bi-function * @param lower the lower bound (for the inner-first variable) * @param upper the upper bound (for the inner-first variable) * @return the inner integral function */ private Function<Double, Double> innerIntegral(BiFunction<Double, Double, Double> f, Double lower, Double upper) { return y -> _integrator1D.integrate(x -> f.apply(x, y), lower, upper); }
@Override public Double integrate(BiFunction<Double, Double, Double> f, Double[] lower, Double[] upper) { return _integrator1D.integrate(innerIntegral(f, lower[0], upper[0]), lower[1], upper[1]); }
private Double integration(Function<Double, Double> f, Double lower, Double upper) { double res = integrator.integrate(f, lower, upper); return integrationRec(f, lower, upper, res, MAX_IT); }
private double integrationRec(Function<Double, Double> f, double lower, double upper, double res, double counter) { double localTol = gain * tol; double half = 0.5 * (lower + upper); double newResDw = integrator.integrate(f, lower, half); double newResUp = integrator.integrate(f, half, upper); double newRes = newResUp + newResDw; if (Math.abs(res - newRes) < localTol || counter == 0 || (Math.abs(res) < 1.e-14 && Math.abs(newResUp) < 1.e-14 && Math.abs(newResDw) < 1.e-14)) { return newRes + (newRes - res) / gain; } return integrationRec(f, lower, half, newResDw, counter - 1) + integrationRec(f, half, upper, newResUp, counter - 1); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullUpperBoundValue() { INTEGRATOR.integrate(F, L, new Double[] {null }); } }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullLowerBound() { INTEGRATOR.integrate(F, null, U); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testEmptyLowerBound() { INTEGRATOR.integrate(F, new Double[0], U); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testEmptyUpperBound() { INTEGRATOR.integrate(F, L, new Double[0]); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullFunction() { INTEGRATOR.integrate(null, L, U); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullUpperBound() { INTEGRATOR.integrate(F, L, null); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullLowerBoundValue() { INTEGRATOR.integrate(F, new Double[] {null }, U); }
@Test public void testGaussLegendre() { double upper = 2; double lower = -6; final Integrator1D<Double, Double> integrator = new GaussLegendreQuadratureIntegrator1D(6); assertEquals(F1.apply(upper) - F1.apply(lower), integrator.integrate(DF1, lower, upper), EPS); lower = -0.56; upper = 1.4; assertEquals(F1.apply(upper) - F1.apply(lower), integrator.integrate(DF1, lower, upper), EPS); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullLowerBound() { getIntegrator().integrate(DF, null, UPPER); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullUpperBound() { getIntegrator().integrate(DF, LOWER, null); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullFunction() { getIntegrator().integrate(null, LOWER, UPPER); }
@Test public void testGaussJacobi() { final double upper = 12; final double lower = -1; final Integrator1D<Double, Double> integrator = new GaussJacobiQuadratureIntegrator1D(7); assertEquals(F1.apply(upper) - F1.apply(lower), integrator.integrate(DF1, lower, upper), EPS); }
@Test public void test() { final double eps = 1e-9; final int minSteps = 10; final Integrator1D<Double, Double> integrator = new RungeKuttaIntegrator1D(eps, eps, minSteps); double lower = 0; double upper = 2.0; assertEquals(4.0, integrator.integrate(CUBE, lower, upper), eps); lower = 0.0; upper = 1.5; assertEquals(0.625, integrator.integrate(TRIANGLE, lower, upper), eps); lower = -30; upper = 30; assertEquals(1.0, integrator.integrate(MIX_NORM, lower, upper), eps); }
@Test public void testGaussLaguerre() { final double upper = Double.POSITIVE_INFINITY; final double lower = 0; final Integrator1D<Double, Double> integrator = new GaussLaguerreQuadratureIntegrator1D(15); assertEquals(0.5, integrator.integrate(DF2, lower, upper), EPS); }
@Test public void testCutoff() { final double eps = 1e-9; final int minSteps = 10; final Integrator1D<Double, Double> integrator = new RungeKuttaIntegrator1D(eps, eps, minSteps); final double lower = -1.0; final double upper = 1.0; assertEquals(0.0, integrator.integrate(SIN_INV_X, lower, upper), eps); }