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.  *      https://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. /*
  18.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.analysis.integration;

  22. import org.hipparchus.exception.LocalizedCoreFormats;
  23. import org.hipparchus.exception.MathIllegalArgumentException;
  24. import org.hipparchus.exception.MathIllegalStateException;
  25. import org.hipparchus.util.FastMath;

  26. /**
  27.  * Implements <a href="http://mathworld.wolfram.com/SimpsonsRule.html">
  28.  * Simpson's Rule</a> for integration of real univariate functions. For
  29.  * reference, see <b>Introduction to Numerical Analysis</b>, ISBN 038795452X,
  30.  * chapter 3.
  31.  * <p>
  32.  * This implementation employs the basic trapezoid rule to calculate Simpson's
  33.  * rule.</p>
  34.  *
  35.  */
  36. public class SimpsonIntegrator extends BaseAbstractUnivariateIntegrator {

  37.     /** Maximal number of iterations for Simpson. */
  38.     public static final int SIMPSON_MAX_ITERATIONS_COUNT = 64;

  39.     /**
  40.      * Build a Simpson integrator with given accuracies and iterations counts.
  41.      * @param relativeAccuracy relative accuracy of the result
  42.      * @param absoluteAccuracy absolute accuracy of the result
  43.      * @param minimalIterationCount minimum number of iterations
  44.      * @param maximalIterationCount maximum number of iterations
  45.      * (must be less than or equal to {@link #SIMPSON_MAX_ITERATIONS_COUNT})
  46.      * @exception MathIllegalArgumentException if minimal number of iterations
  47.      * is not strictly positive
  48.      * @exception MathIllegalArgumentException if maximal number of iterations
  49.      * is lesser than or equal to the minimal number of iterations
  50.      * @exception MathIllegalArgumentException if maximal number of iterations
  51.      * is greater than {@link #SIMPSON_MAX_ITERATIONS_COUNT}
  52.      */
  53.     public SimpsonIntegrator(final double relativeAccuracy,
  54.                              final double absoluteAccuracy,
  55.                              final int minimalIterationCount,
  56.                              final int maximalIterationCount)
  57.         throws MathIllegalArgumentException {
  58.         super(relativeAccuracy, absoluteAccuracy, minimalIterationCount, maximalIterationCount);
  59.         if (maximalIterationCount > SIMPSON_MAX_ITERATIONS_COUNT) {
  60.             throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_LARGE_BOUND_EXCLUDED,
  61.                                                    maximalIterationCount, SIMPSON_MAX_ITERATIONS_COUNT);
  62.         }
  63.     }

  64.     /**
  65.      * Build a Simpson integrator with given iteration counts.
  66.      * @param minimalIterationCount minimum number of iterations
  67.      * @param maximalIterationCount maximum number of iterations
  68.      * (must be less than or equal to {@link #SIMPSON_MAX_ITERATIONS_COUNT})
  69.      * @exception MathIllegalArgumentException if minimal number of iterations
  70.      * is not strictly positive
  71.      * @exception MathIllegalArgumentException if maximal number of iterations
  72.      * is lesser than or equal to the minimal number of iterations
  73.      * @exception MathIllegalArgumentException if maximal number of iterations
  74.      * is greater than {@link #SIMPSON_MAX_ITERATIONS_COUNT}
  75.      */
  76.     public SimpsonIntegrator(final int minimalIterationCount,
  77.                              final int maximalIterationCount)
  78.         throws MathIllegalArgumentException {
  79.         super(minimalIterationCount, maximalIterationCount);
  80.         if (maximalIterationCount > SIMPSON_MAX_ITERATIONS_COUNT) {
  81.             throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_LARGE_BOUND_EXCLUDED,
  82.                                                    maximalIterationCount, SIMPSON_MAX_ITERATIONS_COUNT);
  83.         }
  84.     }

  85.     /**
  86.      * Construct an integrator with default settings.
  87.      * (max iteration count set to {@link #SIMPSON_MAX_ITERATIONS_COUNT})
  88.      */
  89.     public SimpsonIntegrator() {
  90.         super(DEFAULT_MIN_ITERATIONS_COUNT, SIMPSON_MAX_ITERATIONS_COUNT);
  91.     }

  92.     /** {@inheritDoc} */
  93.     @Override
  94.     protected double doIntegrate()
  95.         throws MathIllegalStateException {

  96.         TrapezoidIntegrator qtrap = new TrapezoidIntegrator();
  97.         if (getMinimalIterationCount() == 1) {
  98.             return (4 * qtrap.stage(this, 1) - qtrap.stage(this, 0)) / 3.0;
  99.         }

  100.         // Simpson's rule requires at least two trapezoid stages.
  101.         double olds = 0;
  102.         double oldt = qtrap.stage(this, 0);
  103.         while (true) {
  104.             final double t = qtrap.stage(this, iterations.getCount());
  105.             iterations.increment();
  106.             final double s = (4 * t - oldt) / 3.0;
  107.             if (iterations.getCount() >= getMinimalIterationCount()) {
  108.                 final double delta = FastMath.abs(s - olds);
  109.                 final double rLimit =
  110.                     getRelativeAccuracy() * (FastMath.abs(olds) + FastMath.abs(s)) * 0.5;
  111.                 if ((delta <= rLimit) || (delta <= getAbsoluteAccuracy())) {
  112.                     return s;
  113.                 }
  114.             }
  115.             olds = s;
  116.             oldt = t;
  117.         }

  118.     }

  119. }