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.  *      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.Pair;

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

  48.     /**
  49.      * Creates an integrator from the given pair of points (first element of
  50.      * the pair) and weights (second element of the pair.
  51.      *
  52.      * @param pointsAndWeights Integration points and corresponding weights.
  53.      * @throws MathIllegalArgumentException if the {@code points} are not
  54.      * sorted in increasing order.
  55.      *
  56.      * @see #SymmetricGaussIntegrator(double[], double[])
  57.      */
  58.     public SymmetricGaussIntegrator(Pair<double[], double[]> pointsAndWeights)
  59.         throws MathIllegalArgumentException {
  60.         this(pointsAndWeights.getFirst(), pointsAndWeights.getSecond());
  61.     }

  62.     /**
  63.      * {@inheritDoc}
  64.      */
  65.     @Override
  66.     public double integrate(UnivariateFunction f) {
  67.         final int ruleLength = getNumberOfPoints();

  68.         if (ruleLength == 1) {
  69.             return getWeight(0) * f.value(0d);
  70.         }

  71.         final int iMax = ruleLength / 2;
  72.         double s = 0;
  73.         double c = 0;
  74.         for (int i = 0; i < iMax; i++) {
  75.             final double p = getPoint(i);
  76.             final double w = getWeight(i);

  77.             final double f1 = f.value(p);
  78.             final double f2 = f.value(-p);

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

  81.             c = (t - s) - y;
  82.             s = t;
  83.         }

  84.         if (ruleLength % 2 != 0) {
  85.             final double w = getWeight(iMax);

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

  88.             s = t;
  89.         }

  90.         return s;
  91.     }
  92. }