FieldSimpsonIntegrator.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.CalculusFieldElement;
  23. import org.hipparchus.Field;
  24. import org.hipparchus.exception.LocalizedCoreFormats;
  25. import org.hipparchus.exception.MathIllegalArgumentException;
  26. import org.hipparchus.exception.MathIllegalStateException;
  27. import org.hipparchus.util.FastMath;

  28. /**
  29.  * Implements <a href="http://mathworld.wolfram.com/SimpsonsRule.html">
  30.  * Simpson's Rule</a> for integration of real univariate functions. For
  31.  * reference, see <b>Introduction to Numerical Analysis</b>, ISBN 038795452X,
  32.  * chapter 3.
  33.  * <p>
  34.  * This implementation employs the basic trapezoid rule to calculate Simpson's
  35.  * rule.</p>
  36.  * @param <T> Type of the field elements.
  37.  * @since 2.0
  38.  */
  39. public class FieldSimpsonIntegrator<T extends CalculusFieldElement<T>> extends BaseAbstractFieldUnivariateIntegrator<T> {

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

  42.     /**
  43.      * Build a Simpson integrator with given accuracies and iterations counts.
  44.      * @param field field to which function argument and value belong
  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.      * (must be less than or equal to {@link #SIMPSON_MAX_ITERATIONS_COUNT})
  50.      * @exception MathIllegalArgumentException if minimal number of iterations
  51.      * is not strictly positive
  52.      * @exception MathIllegalArgumentException if maximal number of iterations
  53.      * is lesser than or equal to the minimal number of iterations
  54.      * @exception MathIllegalArgumentException if maximal number of iterations
  55.      * is greater than {@link #SIMPSON_MAX_ITERATIONS_COUNT}
  56.      */
  57.     public FieldSimpsonIntegrator(final Field<T> field,
  58.                                   final double relativeAccuracy,
  59.                                   final double absoluteAccuracy,
  60.                                   final int minimalIterationCount,
  61.                                   final int maximalIterationCount)
  62.         throws MathIllegalArgumentException {
  63.         super(field, relativeAccuracy, absoluteAccuracy, minimalIterationCount, maximalIterationCount);
  64.         if (maximalIterationCount > SIMPSON_MAX_ITERATIONS_COUNT) {
  65.             throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_LARGE_BOUND_EXCLUDED,
  66.                                                    maximalIterationCount, SIMPSON_MAX_ITERATIONS_COUNT);
  67.         }
  68.     }

  69.     /**
  70.      * Build a Simpson integrator with given iteration counts.
  71.      * @param field field to which function argument and value belong
  72.      * @param minimalIterationCount minimum number of iterations
  73.      * @param maximalIterationCount maximum number of iterations
  74.      * (must be less than or equal to {@link #SIMPSON_MAX_ITERATIONS_COUNT})
  75.      * @exception MathIllegalArgumentException if minimal number of iterations
  76.      * is not strictly positive
  77.      * @exception MathIllegalArgumentException if maximal number of iterations
  78.      * is lesser than or equal to the minimal number of iterations
  79.      * @exception MathIllegalArgumentException if maximal number of iterations
  80.      * is greater than {@link #SIMPSON_MAX_ITERATIONS_COUNT}
  81.      */
  82.     public FieldSimpsonIntegrator(final Field<T> field,
  83.                                   final int minimalIterationCount,
  84.                                   final int maximalIterationCount)
  85.         throws MathIllegalArgumentException {
  86.         super(field, minimalIterationCount, maximalIterationCount);
  87.         if (maximalIterationCount > SIMPSON_MAX_ITERATIONS_COUNT) {
  88.             throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_LARGE_BOUND_EXCLUDED,
  89.                                                    maximalIterationCount, SIMPSON_MAX_ITERATIONS_COUNT);
  90.         }
  91.     }

  92.     /**
  93.      * Construct an integrator with default settings.
  94.      * @param field field to which function argument and value belong
  95.      * (max iteration count set to {@link #SIMPSON_MAX_ITERATIONS_COUNT})
  96.      */
  97.     public FieldSimpsonIntegrator(final Field<T> field) {
  98.         super(field, DEFAULT_MIN_ITERATIONS_COUNT, SIMPSON_MAX_ITERATIONS_COUNT);
  99.     }

  100.     /** {@inheritDoc} */
  101.     @Override
  102.     protected T doIntegrate()
  103.         throws MathIllegalStateException {

  104.         FieldTrapezoidIntegrator<T> qtrap = new FieldTrapezoidIntegrator<>(getField());
  105.         if (getMinimalIterationCount() == 1) {
  106.             return qtrap.stage(this, 1).multiply(4).subtract(qtrap.stage(this, 0)).divide(3.0);
  107.         }

  108.         // Simpson's rule requires at least two trapezoid stages.
  109.         T olds = getField().getZero();
  110.         T oldt = qtrap.stage(this, 0);
  111.         while (true) {
  112.             final T t = qtrap.stage(this, iterations.getCount());
  113.             iterations.increment();
  114.             final T s = t.multiply(4).subtract(oldt).divide(3.0);
  115.             if (iterations.getCount() >= getMinimalIterationCount()) {
  116.                 final double delta = FastMath.abs(s.subtract(olds)).getReal();
  117.                 final double rLimit = FastMath.abs(olds).add(FastMath.abs(s)).multiply(0.5 * getRelativeAccuracy()).getReal();
  118.                 if ((delta <= rLimit) || (delta <= getAbsoluteAccuracy())) {
  119.                     return s;
  120.                 }
  121.             }
  122.             olds = s;
  123.             oldt = t;
  124.         }

  125.     }

  126. }