FieldGaussIntegrator.java

  1. /*
  2.  * Licensed to the Hipparchus project 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 Hipparchus project 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. package org.hipparchus.analysis.integration.gauss;

  18. import org.hipparchus.CalculusFieldElement;
  19. import org.hipparchus.analysis.CalculusFieldUnivariateFunction;
  20. import org.hipparchus.exception.MathIllegalArgumentException;
  21. import org.hipparchus.util.MathArrays;
  22. import org.hipparchus.util.Pair;

  23. /**
  24.  * Class that implements the Gaussian rule for
  25.  * {@link #integrate(CalculusFieldUnivariateFunction) integrating} a weighted
  26.  * function.
  27.  *
  28.  * @param <T> Type of the field elements.
  29.  * @since 2.0
  30.  */
  31. public class FieldGaussIntegrator<T extends CalculusFieldElement<T>> {
  32.     /** Nodes. */
  33.     private final T[] points;
  34.     /** Nodes weights. */
  35.     private final T[] weights;

  36.     /**
  37.      * Creates an integrator from the given {@code points} and {@code weights}.
  38.      * The integration interval is defined by the first and last value of
  39.      * {@code points} which must be sorted in increasing order.
  40.      *
  41.      * @param points Integration points.
  42.      * @param weights Weights of the corresponding integration nodes.
  43.      * @throws MathIllegalArgumentException if the {@code points} are not
  44.      * sorted in increasing order.
  45.      * @throws MathIllegalArgumentException if points and weights don't have the same length
  46.      */
  47.     public FieldGaussIntegrator(T[] points, T[] weights)
  48.         throws MathIllegalArgumentException {

  49.         MathArrays.checkEqualLength(points, weights);
  50.         MathArrays.checkOrder(points, MathArrays.OrderDirection.INCREASING, true, true);

  51.         this.points = points.clone();
  52.         this.weights = weights.clone();
  53.     }

  54.     /**
  55.      * Creates an integrator from the given pair of points (first element of
  56.      * the pair) and weights (second element of the pair.
  57.      *
  58.      * @param pointsAndWeights Integration points and corresponding weights.
  59.      * @throws MathIllegalArgumentException if the {@code points} are not
  60.      * sorted in increasing order.
  61.      *
  62.      * @see #FieldGaussIntegrator(CalculusFieldElement[], CalculusFieldElement[])
  63.      */
  64.     public FieldGaussIntegrator(Pair<T[], T[]> pointsAndWeights)
  65.         throws MathIllegalArgumentException {
  66.         this(pointsAndWeights.getFirst(), pointsAndWeights.getSecond());
  67.     }

  68.     /**
  69.      * Returns an estimate of the integral of {@code f(x) * w(x)},
  70.      * where {@code w} is a weight function that depends on the actual
  71.      * flavor of the Gauss integration scheme.
  72.      * The algorithm uses the points and associated weights, as passed
  73.      * to the {@link #FieldGaussIntegrator(CalculusFieldElement[], CalculusFieldElement[]) constructor}.
  74.      *
  75.      * @param f Function to integrate.
  76.      * @return the integral of the weighted function.
  77.      */
  78.     public T integrate(CalculusFieldUnivariateFunction<T> f) {
  79.         T s = points[0].getField().getZero();
  80.         T c = s;
  81.         for (int i = 0; i < points.length; i++) {
  82.             final T x = points[i];
  83.             final T w = weights[i];
  84.             final T y = w.multiply(f.value(x)).subtract(c);
  85.             final T t = s.add(y);
  86.             c = t.subtract(s).subtract(y);
  87.             s = t;
  88.         }
  89.         return s;
  90.     }

  91.     /** Get order of the integration rule.
  92.      * @return the order of the integration rule (the number of integration
  93.      * points).
  94.      */
  95.     public int getNumberOfPoints() {
  96.         return points.length;
  97.     }

  98.     /**
  99.      * Gets the integration point at the given index.
  100.      * The index must be in the valid range but no check is performed.
  101.      * @param index index of the integration point
  102.      * @return the integration point.
  103.      */
  104.     public T getPoint(int index) {
  105.         return points[index];
  106.     }

  107.     /**
  108.      * Gets the weight of the integration point at the given index.
  109.      * The index must be in the valid range but no check is performed.
  110.      * @param index index of the integration point
  111.      * @return the weight.
  112.      */
  113.     public T getWeight(int index) {
  114.         return weights[index];
  115.     }
  116. }