RungeKuttaIntegrator1D extends Integrator1D<Double, Double> { @Override public Double integrate(final Function1D<Double, Double> f, final Double lower, final Double upper) { ArgumentChecker.notNull(lower, "lower"); ArgumentChecker.notNull(upper, "upper"); if (Double.isNaN(lower) || Double.isInfinite(lower) || Double.isInfinite(upper) || Double.isNaN(upper)) { throw new OpenGammaRuntimeException("lower or upper was NaN or Inf"); } final double h = (upper - lower) / _minSteps; double f1, f2, f3, x; x = lower; f1 = f.evaluate(x); if (Double.isNaN(f1) || Double.isInfinite(f1)) { throw new OpenGammaRuntimeException("function evaluation returned NaN or Inf"); } double result = 0.0; for (int i = 0; i < _minSteps; i++) { f2 = f.evaluate(x + h / 2.0); if (Double.isNaN(f2) || Double.isInfinite(f2)) { throw new OpenGammaRuntimeException("function evaluation returned NaN or Inf"); } f3 = f.evaluate(x + h); if (Double.isNaN(f3) || Double.isInfinite(f3)) { throw new OpenGammaRuntimeException("function evaluation returned NaN or Inf"); } result += calculateRungeKuttaFourthOrder(f, x, h, f1, f2, f3); f1 = f3; x += h; } return result; } RungeKuttaIntegrator1D(final double absTol, final double relTol, final int minSteps);  RungeKuttaIntegrator1D(final double tol, final int minSteps);  RungeKuttaIntegrator1D(final double atol, final double rtol);  RungeKuttaIntegrator1D(final double tol);  RungeKuttaIntegrator1D(final int minSteps);  RungeKuttaIntegrator1D(); @Override Double integrate(final Function1D<Double, Double> f, final Double lower, final Double upper);  }
@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 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); }