SymmetricFieldGaussIntegrator.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.CalculusFieldElement;
  23. import org.hipparchus.analysis.CalculusFieldUnivariateFunction;
  24. import org.hipparchus.exception.MathIllegalArgumentException;
  25. import org.hipparchus.util.Pair;

  26. /**
  27.  * This class's implements {@link #integrate(CalculusFieldUnivariateFunction) integrate}
  28.  * method assuming that the integral is symmetric about 0.
  29.  * This allows to reduce numerical errors.
  30.  *
  31.  * @param <T> Type of the field elements.
  32.  * @since 2.0
  33.  */
  34. public class SymmetricFieldGaussIntegrator<T extends CalculusFieldElement<T>> extends FieldGaussIntegrator<T> {
  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 MathIllegalArgumentException if the {@code points} are not
  43.      * sorted in increasing order.
  44.      * @throws MathIllegalArgumentException if points and weights don't have the same length
  45.      */
  46.     public SymmetricFieldGaussIntegrator(T[] points, T[] weights)
  47.         throws MathIllegalArgumentException {
  48.         super(points, weights);
  49.     }

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

  64.     /**
  65.      * {@inheritDoc}
  66.      */
  67.     @Override
  68.     public T integrate(CalculusFieldUnivariateFunction<T> f) {
  69.         final int ruleLength = getNumberOfPoints();

  70.         final T zero = getPoint(0).getField().getZero();
  71.         if (ruleLength == 1) {
  72.             return getWeight(0).multiply(f.value(zero));
  73.         }

  74.         final int iMax = ruleLength / 2;
  75.         T s = zero;
  76.         T c = zero;
  77.         for (int i = 0; i < iMax; i++) {
  78.             final T p = getPoint(i);
  79.             final T w = getWeight(i);

  80.             final T f1 = f.value(p);
  81.             final T f2 = f.value(p.negate());

  82.             final T y = w.multiply(f1.add(f2)).subtract(c);
  83.             final T t = s.add(y);

  84.             c = t.subtract(s).subtract(y);
  85.             s = t;
  86.         }

  87.         if (ruleLength % 2 != 0) {
  88.             final T w = getWeight(iMax);

  89.             final T y = w.multiply(f.value(zero)).subtract(c);
  90.             final T t = s.add(y);

  91.             s = t;
  92.         }

  93.         return s;
  94.     }
  95. }