IterativeLegendreFieldGaussIntegrator.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.analysis.integration.gauss.FieldGaussIntegrator;
  25. import org.hipparchus.analysis.integration.gauss.FieldGaussIntegratorFactory;
  26. import org.hipparchus.exception.LocalizedCoreFormats;
  27. import org.hipparchus.exception.MathIllegalArgumentException;
  28. import org.hipparchus.exception.MathIllegalStateException;
  29. import org.hipparchus.util.FastMath;

  30. /**
  31.  * This algorithm divides the integration interval into equally-sized
  32.  * sub-interval and on each of them performs a
  33.  * <a href="http://mathworld.wolfram.com/Legendre-GaussQuadrature.html">
  34.  * Legendre-Gauss</a> quadrature.
  35.  * Because of its <em>non-adaptive</em> nature, this algorithm can
  36.  * converge to a wrong value for the integral (for example, if the
  37.  * function is significantly different from zero toward the ends of the
  38.  * integration interval).
  39.  * In particular, a change of variables aimed at estimating integrals
  40.  * over infinite intervals as proposed
  41.  * <a href="http://en.wikipedia.org/w/index.php?title=Numerical_integration#Integrals_over_infinite_intervals">
  42.  *  here</a> should be avoided when using this class.
  43.  *
  44.  * @param <T> Type of the field elements.
  45.  * @since 2.0
  46.  */
  47. public class IterativeLegendreFieldGaussIntegrator<T extends CalculusFieldElement<T>>
  48.     extends BaseAbstractFieldUnivariateIntegrator<T> {

  49.     /** Factory that computes the points and weights. */
  50.     private final FieldGaussIntegratorFactory<T> factory;

  51.     /** Number of integration points (per interval). */
  52.     private final int numberOfPoints;

  53.     /**
  54.      * Builds an integrator with given accuracies and iterations counts.
  55.      *
  56.      * @param field field to which function argument and value belong
  57.      * @param n Number of integration points.
  58.      * @param relativeAccuracy Relative accuracy of the result.
  59.      * @param absoluteAccuracy Absolute accuracy of the result.
  60.      * @param minimalIterationCount Minimum number of iterations.
  61.      * @param maximalIterationCount Maximum number of iterations.
  62.      * @throws MathIllegalArgumentException if minimal number of iterations
  63.      * or number of points are not strictly positive.
  64.      * @throws MathIllegalArgumentException if maximal number of iterations
  65.      * is smaller than or equal to the minimal number of iterations.
  66.      */
  67.     public IterativeLegendreFieldGaussIntegrator(final Field<T> field, final int n,
  68.                                                  final double relativeAccuracy,
  69.                                                  final double absoluteAccuracy,
  70.                                                  final int minimalIterationCount,
  71.                                                  final int maximalIterationCount)
  72.         throws MathIllegalArgumentException {
  73.         super(field, relativeAccuracy, absoluteAccuracy, minimalIterationCount, maximalIterationCount);
  74.         if (n <= 0) {
  75.             throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_OF_POINTS, n);
  76.         }
  77.         factory = new FieldGaussIntegratorFactory<>(field);
  78.         numberOfPoints = n;
  79.     }

  80.     /**
  81.      * Builds an integrator with given accuracies.
  82.      *
  83.      * @param field field to which function argument and value belong
  84.      * @param n Number of integration points.
  85.      * @param relativeAccuracy Relative accuracy of the result.
  86.      * @param absoluteAccuracy Absolute accuracy of the result.
  87.      * @throws MathIllegalArgumentException if {@code n < 1}.
  88.      */
  89.     public IterativeLegendreFieldGaussIntegrator(final Field<T> field, final int n,
  90.                                                  final double relativeAccuracy,
  91.                                                  final double absoluteAccuracy)
  92.         throws MathIllegalArgumentException {
  93.         this(field, n, relativeAccuracy, absoluteAccuracy,
  94.              DEFAULT_MIN_ITERATIONS_COUNT, DEFAULT_MAX_ITERATIONS_COUNT);
  95.     }

  96.     /**
  97.      * Builds an integrator with given iteration counts.
  98.      *
  99.      * @param field field to which function argument and value belong
  100.      * @param n Number of integration points.
  101.      * @param minimalIterationCount Minimum number of iterations.
  102.      * @param maximalIterationCount Maximum number of iterations.
  103.      * @throws MathIllegalArgumentException if minimal number of iterations
  104.      * is not strictly positive.
  105.      * @throws MathIllegalArgumentException if maximal number of iterations
  106.      * is smaller than or equal to the minimal number of iterations.
  107.      * @throws MathIllegalArgumentException if {@code n < 1}.
  108.      */
  109.     public IterativeLegendreFieldGaussIntegrator(final Field<T> field, final int n,
  110.                                                  final int minimalIterationCount,
  111.                                                  final int maximalIterationCount)
  112.                                                                  throws MathIllegalArgumentException {
  113.         this(field, n, DEFAULT_RELATIVE_ACCURACY, DEFAULT_ABSOLUTE_ACCURACY,
  114.              minimalIterationCount, maximalIterationCount);
  115.     }

  116.     /** {@inheritDoc} */
  117.     @Override
  118.     protected T doIntegrate()
  119.         throws MathIllegalArgumentException, MathIllegalStateException {
  120.         // Compute first estimate with a single step.
  121.         T oldt = stage(1);

  122.         int n = 2;
  123.         while (true) {
  124.             // Improve integral with a larger number of steps.
  125.             final T t = stage(n);

  126.             // Estimate the error.
  127.             final double delta = FastMath.abs(t.subtract(oldt)).getReal();
  128.             final double limit =
  129.                 FastMath.max(getAbsoluteAccuracy(),
  130.                              FastMath.abs(oldt).add(FastMath.abs(t)).multiply(0.5 * getRelativeAccuracy()).getReal());

  131.             // check convergence
  132.             if (iterations.getCount() + 1 >= getMinimalIterationCount() &&
  133.                 delta <= limit) {
  134.                 return t;
  135.             }

  136.             // Prepare next iteration.
  137.             final double ratio = FastMath.min(4, FastMath.pow(delta / limit, 0.5 / numberOfPoints));
  138.             n = FastMath.max((int) (ratio * n), n + 1);
  139.             oldt = t;
  140.             iterations.increment();
  141.         }
  142.     }

  143.     /**
  144.      * Compute the n-th stage integral.
  145.      *
  146.      * @param n Number of steps.
  147.      * @return the value of n-th stage integral.
  148.      * @throws MathIllegalStateException if the maximum number of evaluations
  149.      * is exceeded.
  150.      */
  151.     private T stage(final int n)
  152.         throws MathIllegalStateException {

  153.         final T min = getMin();
  154.         final T max = getMax();
  155.         final T step = max.subtract(min).divide(n);

  156.         T sum = getField().getZero();
  157.         for (int i = 0; i < n; i++) {
  158.             // Integrate over each sub-interval [a, b].
  159.             final T a = min.add(step.multiply(i));
  160.             final T b = a.add(step);
  161.             final FieldGaussIntegrator<T> g = factory.legendre(numberOfPoints, a, b);
  162.             sum = sum.add(g.integrate(super::computeObjectiveValue));
  163.         }

  164.         return sum;
  165.     }

  166. }