GaussIntegrator.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.gauss;

  22. import org.hipparchus.analysis.UnivariateFunction;
  23. import org.hipparchus.exception.MathIllegalArgumentException;
  24. import org.hipparchus.util.MathArrays;
  25. import org.hipparchus.util.Pair;

  26. /**
  27.  * Class that implements the Gaussian rule for
  28.  * {@link #integrate(UnivariateFunction) integrating} a weighted
  29.  * function.
  30.  *
  31.  */
  32. public class GaussIntegrator {
  33.     /** Nodes. */
  34.     private final double[] points;
  35.     /** Nodes weights. */
  36.     private final double[] weights;

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

  51.         MathArrays.checkEqualLength(points, weights);
  52.         MathArrays.checkOrder(points, MathArrays.OrderDirection.INCREASING, true, true);

  53.         this.points = points.clone();
  54.         this.weights = weights.clone();
  55.     }

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

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

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

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

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