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.  *      http://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.apache.commons.math3.analysis.integration.gauss;

  18. import org.apache.commons.math3.analysis.UnivariateFunction;
  19. import org.apache.commons.math3.exception.DimensionMismatchException;
  20. import org.apache.commons.math3.exception.NonMonotonicSequenceException;
  21. import org.apache.commons.math3.util.MathArrays;
  22. import org.apache.commons.math3.util.Pair;

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

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

  53.         MathArrays.checkOrder(points, MathArrays.OrderDirection.INCREASING, true, true);

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

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

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

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

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

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