RombergIntegrator.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 the <a href="http://mathworld.wolfram.com/RombergIntegration.html">
  28.  * Romberg Algorithm</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.  * Romberg integration employs k successive refinements of the trapezoid
  33.  * rule to remove error terms less than order O(N^(-2k)). Simpson's rule
  34.  * is a special case of k = 2.</p>
  35.  *
  36.  */
  37. public class RombergIntegrator extends BaseAbstractUnivariateIntegrator {

  38.     /** Maximal number of iterations for Romberg. */
  39.     public static final int ROMBERG_MAX_ITERATIONS_COUNT = 32;

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

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

  86.     /**
  87.      * Construct a Romberg integrator with default settings
  88.      * (max iteration count set to {@link #ROMBERG_MAX_ITERATIONS_COUNT})
  89.      */
  90.     public RombergIntegrator() {
  91.         super(DEFAULT_MIN_ITERATIONS_COUNT, ROMBERG_MAX_ITERATIONS_COUNT);
  92.     }

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

  97.         final int m = iterations.getMaximalCount() + 1;
  98.         double[] previousRow = new double[m];
  99.         double[] currentRow = new double[m];

  100.         TrapezoidIntegrator qtrap = new TrapezoidIntegrator();
  101.         currentRow[0] = qtrap.stage(this, 0);
  102.         iterations.increment();
  103.         double olds = currentRow[0];
  104.         while (true) {

  105.             final int i = iterations.getCount();

  106.             // switch rows
  107.             final double[] tmpRow = previousRow;
  108.             previousRow = currentRow;
  109.             currentRow = tmpRow;

  110.             currentRow[0] = qtrap.stage(this, i);
  111.             iterations.increment();
  112.             for (int j = 1; j <= i; j++) {
  113.                 // Richardson extrapolation coefficient
  114.                 final double r = (1L << (2 * j)) - 1;
  115.                 final double tIJm1 = currentRow[j - 1];
  116.                 currentRow[j] = tIJm1 + (tIJm1 - previousRow[j - 1]) / r;
  117.             }
  118.             final double s = currentRow[i];
  119.             if (i >= getMinimalIterationCount()) {
  120.                 final double delta  = FastMath.abs(s - olds);
  121.                 final double rLimit = getRelativeAccuracy() * (FastMath.abs(olds) + FastMath.abs(s)) * 0.5;
  122.                 if ((delta <= rLimit) || (delta <= getAbsoluteAccuracy())) {
  123.                     return s;
  124.                 }
  125.             }
  126.             olds = s;
  127.         }

  128.     }

  129. }