SimpsonIntegrator.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.math4.legacy.analysis.integration;

  18. import org.apache.commons.math4.legacy.exception.NumberIsTooLargeException;
  19. import org.apache.commons.math4.core.jdkmath.JdkMath;

  20. /**
  21.  * Implements <a href="http://mathworld.wolfram.com/SimpsonsRule.html">
  22.  * Simpson's Rule</a> for integration of real univariate functions.
  23.  *
  24.  * See <b>Introduction to Numerical Analysis</b>, ISBN 038795452X, chapter 3.
  25.  *
  26.  * <p>
  27.  * This implementation employs the basic trapezoid rule to calculate Simpson's
  28.  * rule.
  29.  *
  30.  * <p>
  31.  * <em>Caveat:</em> At each iteration, the algorithm refines the estimation by
  32.  * evaluating the function twice as many times as in the previous iteration;
  33.  * When specifying a {@link #integrate(int,UnivariateFunction,double,double)
  34.  * maximum number of function evaluations}, the caller must ensure that it
  35.  * is compatible with the {@link #SimpsonIntegrator(int,int) requested minimal
  36.  * number of iterations}.
  37.  *
  38.  * @since 1.2
  39.  */
  40. public class SimpsonIntegrator extends BaseAbstractUnivariateIntegrator {
  41.     /** Maximal number of iterations for Simpson. */
  42.     private static final int SIMPSON_MAX_ITERATIONS_COUNT = 30;

  43.     /**
  44.      * Build a Simpson integrator with given accuracies and iterations counts.
  45.      * @param relativeAccuracy relative accuracy of the result
  46.      * @param absoluteAccuracy absolute accuracy of the result
  47.      * @param minimalIterationCount Minimum number of iterations.
  48.      * @param maximalIterationCount Maximum number of iterations.
  49.      * It must be less than or equal to 30.
  50.      * @throws org.apache.commons.math4.legacy.exception.NotStrictlyPositiveException
  51.      * if {@code minimalIterationCount <= 0}.
  52.      * @throws org.apache.commons.math4.legacy.exception.NumberIsTooSmallException
  53.      * if {@code maximalIterationCount < minimalIterationCount}.
  54.      * is lesser than or equal to the minimal number of iterations
  55.      * @throws NumberIsTooLargeException if {@code maximalIterationCount > 30}.
  56.      */
  57.     public SimpsonIntegrator(final double relativeAccuracy,
  58.                              final double absoluteAccuracy,
  59.                              final int minimalIterationCount,
  60.                              final int maximalIterationCount) {
  61.         super(relativeAccuracy, absoluteAccuracy, minimalIterationCount, maximalIterationCount);
  62.         if (maximalIterationCount > SIMPSON_MAX_ITERATIONS_COUNT) {
  63.             throw new NumberIsTooLargeException(maximalIterationCount,
  64.                                                 SIMPSON_MAX_ITERATIONS_COUNT, false);
  65.         }
  66.     }

  67.     /**
  68.      * Build a Simpson integrator with given iteration counts.
  69.      * @param minimalIterationCount Minimum number of iterations.
  70.      * @param maximalIterationCount Maximum number of iterations.
  71.      * It must be less than or equal to 30.
  72.      * @throws org.apache.commons.math4.legacy.exception.NotStrictlyPositiveException
  73.      * if {@code minimalIterationCount <= 0}.
  74.      * @throws org.apache.commons.math4.legacy.exception.NumberIsTooSmallException
  75.      * if {@code maximalIterationCount < minimalIterationCount}.
  76.      * is lesser than or equal to the minimal number of iterations
  77.      * @throws NumberIsTooLargeException if {@code maximalIterationCount > 30}.
  78.      */
  79.     public SimpsonIntegrator(final int minimalIterationCount,
  80.                              final int maximalIterationCount) {
  81.         super(minimalIterationCount, maximalIterationCount);
  82.         if (maximalIterationCount > SIMPSON_MAX_ITERATIONS_COUNT) {
  83.             throw new NumberIsTooLargeException(maximalIterationCount,
  84.                                                 SIMPSON_MAX_ITERATIONS_COUNT, false);
  85.         }
  86.     }

  87.     /**
  88.      * Construct an integrator with default settings.
  89.      */
  90.     public SimpsonIntegrator() {
  91.         super(DEFAULT_MIN_ITERATIONS_COUNT, SIMPSON_MAX_ITERATIONS_COUNT);
  92.     }

  93.     /** {@inheritDoc} */
  94.     @Override
  95.     protected double doIntegrate() {
  96.         // Simpson's rule requires at least two trapezoid stages.
  97.         // So we set the first sum using two trapezoid stages.
  98.         final TrapezoidIntegrator qtrap = new TrapezoidIntegrator();

  99.         final double s0 = qtrap.stage(this, 0);
  100.         double oldt = qtrap.stage(this, 1);
  101.         double olds = (4 * oldt - s0) / 3.0;
  102.         while (true) {
  103.             // The first iteration is the first refinement of the sum.
  104.             iterations.increment();
  105.             final int i = getIterations();
  106.             final double t = qtrap.stage(this, i + 1); // 1-stage ahead of the iteration
  107.             final double s = (4 * t - oldt) / 3.0;
  108.             if (i >= getMinimalIterationCount()) {
  109.                 final double delta = JdkMath.abs(s - olds);
  110.                 final double rLimit = getRelativeAccuracy() * (JdkMath.abs(olds) + JdkMath.abs(s)) * 0.5;
  111.                 if (delta <= rLimit ||
  112.                     delta <= getAbsoluteAccuracy()) {
  113.                     return s;
  114.                 }
  115.             }
  116.             olds = s;
  117.             oldt = t;
  118.         }
  119.     }
  120. }