SymmetricGaussIntegrator.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.math4.legacy.analysis.integration.gauss;

  18. import org.apache.commons.math4.legacy.analysis.UnivariateFunction;
  19. import org.apache.commons.math4.legacy.core.Pair;

  20. /**
  21.  * This class's implements {@link #integrate(UnivariateFunction) integrate}
  22.  * method assuming that the integral is symmetric about 0.
  23.  * This allows to reduce numerical errors.
  24.  *
  25.  * @since 3.3
  26.  */
  27. public class SymmetricGaussIntegrator extends GaussIntegrator {
  28.     /**
  29.      * Creates an integrator from the given {@code points} and {@code weights}.
  30.      * The integration interval is defined by the first and last value of
  31.      * {@code points} which must be sorted in increasing order.
  32.      *
  33.      * @param points Integration points.
  34.      * @param weights Weights of the corresponding integration nodes.
  35.      * @throws org.apache.commons.math4.legacy.exception.NonMonotonicSequenceException if the {@code points} are not
  36.      * sorted in increasing order.
  37.      * @throws org.apache.commons.math4.legacy.exception.DimensionMismatchException if points and weights don't have the same length
  38.      */
  39.     public SymmetricGaussIntegrator(double[] points,
  40.                                     double[] weights) {
  41.         super(points, weights);
  42.     }

  43.     /**
  44.      * Creates an integrator from the given pair of points (first element of
  45.      * the pair) and weights (second element of the pair.
  46.      *
  47.      * @param pointsAndWeights Integration points and corresponding weights.
  48.      * @throws org.apache.commons.math4.legacy.exception.NonMonotonicSequenceException if the {@code points} are not
  49.      * sorted in increasing order.
  50.      *
  51.      * @see #SymmetricGaussIntegrator(double[], double[])
  52.      */
  53.     public SymmetricGaussIntegrator(Pair<double[], double[]> pointsAndWeights) {
  54.         this(pointsAndWeights.getFirst(), pointsAndWeights.getSecond());
  55.     }

  56.     /**
  57.      * {@inheritDoc}
  58.      */
  59.     @Override
  60.     public double integrate(UnivariateFunction f) {
  61.         final int ruleLength = getNumberOfPoints();

  62.         if (ruleLength == 1) {
  63.             return getWeight(0) * f.value(0d);
  64.         }

  65.         final int iMax = ruleLength / 2;
  66.         double s = 0;
  67.         double c = 0;
  68.         for (int i = 0; i < iMax; i++) {
  69.             final double p = getPoint(i);
  70.             final double w = getWeight(i);

  71.             final double f1 = f.value(p);
  72.             final double f2 = f.value(-p);

  73.             final double y = w * (f1 + f2) - c;
  74.             final double t = s + y;

  75.             c = (t - s) - y;
  76.             s = t;
  77.         }

  78.         if ((ruleLength & 1) != 0) {
  79.             final double w = getWeight(iMax);

  80.             final double y = w * f.value(0d) - c;
  81.             final double t = s + y;

  82.             s = t;
  83.         }

  84.         return s;
  85.     }
  86. }