GaussIntegratorFactory.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 java.math.BigDecimal;

  19. import org.apache.commons.math3.exception.DimensionMismatchException;
  20. import org.apache.commons.math3.exception.NotStrictlyPositiveException;
  21. import org.apache.commons.math3.util.Pair;

  22. /**
  23.  * Class that provides different ways to compute the nodes and weights to be
  24.  * used by the {@link GaussIntegrator Gaussian integration rule}.
  25.  *
  26.  * @since 3.1
  27.  */
  28. public class GaussIntegratorFactory {
  29.     /** Generator of Gauss-Legendre integrators. */
  30.     private final BaseRuleFactory<Double> legendre = new LegendreRuleFactory();
  31.     /** Generator of Gauss-Legendre integrators. */
  32.     private final BaseRuleFactory<BigDecimal> legendreHighPrecision = new LegendreHighPrecisionRuleFactory();
  33.     /** Generator of Gauss-Hermite integrators. */
  34.     private final BaseRuleFactory<Double> hermite = new HermiteRuleFactory();

  35.     /**
  36.      * Creates a Gauss-Legendre integrator of the given order.
  37.      * The call to the
  38.      * {@link GaussIntegrator#integrate(org.apache.commons.math3.analysis.UnivariateFunction)
  39.      * integrate} method will perform an integration on the natural interval
  40.      * {@code [-1 , 1]}.
  41.      *
  42.      * @param numberOfPoints Order of the integration rule.
  43.      * @return a Gauss-Legendre integrator.
  44.      */
  45.     public GaussIntegrator legendre(int numberOfPoints) {
  46.         return new GaussIntegrator(getRule(legendre, numberOfPoints));
  47.     }

  48.     /**
  49.      * Creates a Gauss-Legendre integrator of the given order.
  50.      * The call to the
  51.      * {@link GaussIntegrator#integrate(org.apache.commons.math3.analysis.UnivariateFunction)
  52.      * integrate} method will perform an integration on the given interval.
  53.      *
  54.      * @param numberOfPoints Order of the integration rule.
  55.      * @param lowerBound Lower bound of the integration interval.
  56.      * @param upperBound Upper bound of the integration interval.
  57.      * @return a Gauss-Legendre integrator.
  58.      * @throws NotStrictlyPositiveException if number of points is not positive
  59.      */
  60.     public GaussIntegrator legendre(int numberOfPoints,
  61.                                     double lowerBound,
  62.                                     double upperBound)
  63.         throws NotStrictlyPositiveException {
  64.         return new GaussIntegrator(transform(getRule(legendre, numberOfPoints),
  65.                                              lowerBound, upperBound));
  66.     }

  67.     /**
  68.      * Creates a Gauss-Legendre integrator of the given order.
  69.      * The call to the
  70.      * {@link GaussIntegrator#integrate(org.apache.commons.math3.analysis.UnivariateFunction)
  71.      * integrate} method will perform an integration on the natural interval
  72.      * {@code [-1 , 1]}.
  73.      *
  74.      * @param numberOfPoints Order of the integration rule.
  75.      * @return a Gauss-Legendre integrator.
  76.      * @throws NotStrictlyPositiveException if number of points is not positive
  77.      */
  78.     public GaussIntegrator legendreHighPrecision(int numberOfPoints)
  79.         throws NotStrictlyPositiveException {
  80.         return new GaussIntegrator(getRule(legendreHighPrecision, numberOfPoints));
  81.     }

  82.     /**
  83.      * Creates an integrator of the given order, and whose call to the
  84.      * {@link GaussIntegrator#integrate(org.apache.commons.math3.analysis.UnivariateFunction)
  85.      * integrate} method will perform an integration on the given interval.
  86.      *
  87.      * @param numberOfPoints Order of the integration rule.
  88.      * @param lowerBound Lower bound of the integration interval.
  89.      * @param upperBound Upper bound of the integration interval.
  90.      * @return a Gauss-Legendre integrator.
  91.      * @throws NotStrictlyPositiveException if number of points is not positive
  92.      */
  93.     public GaussIntegrator legendreHighPrecision(int numberOfPoints,
  94.                                                  double lowerBound,
  95.                                                  double upperBound)
  96.         throws NotStrictlyPositiveException {
  97.         return new GaussIntegrator(transform(getRule(legendreHighPrecision, numberOfPoints),
  98.                                              lowerBound, upperBound));
  99.     }

  100.     /**
  101.      * Creates a Gauss-Hermite integrator of the given order.
  102.      * The call to the
  103.      * {@link SymmetricGaussIntegrator#integrate(org.apache.commons.math3.analysis.UnivariateFunction)
  104.      * integrate} method will perform a weighted integration on the interval
  105.      * {@code [-&inf;, +&inf;]}: the computed value is the improper integral of
  106.      * <code>
  107.      *  e<sup>-x<sup>2</sup></sup> f(x)
  108.      * </code>
  109.      * where {@code f(x)} is the function passed to the
  110.      * {@link SymmetricGaussIntegrator#integrate(org.apache.commons.math3.analysis.UnivariateFunction)
  111.      * integrate} method.
  112.      *
  113.      * @param numberOfPoints Order of the integration rule.
  114.      * @return a Gauss-Hermite integrator.
  115.      */
  116.     public SymmetricGaussIntegrator hermite(int numberOfPoints) {
  117.         return new SymmetricGaussIntegrator(getRule(hermite, numberOfPoints));
  118.     }

  119.     /**
  120.      * @param factory Integration rule factory.
  121.      * @param numberOfPoints Order of the integration rule.
  122.      * @return the integration nodes and weights.
  123.      * @throws NotStrictlyPositiveException if number of points is not positive
  124.      * @throws DimensionMismatchException if the elements of the rule pair do not
  125.      * have the same length.
  126.      */
  127.     private static Pair<double[], double[]> getRule(BaseRuleFactory<? extends Number> factory,
  128.                                                     int numberOfPoints)
  129.         throws NotStrictlyPositiveException, DimensionMismatchException {
  130.         return factory.getRule(numberOfPoints);
  131.     }

  132.     /**
  133.      * Performs a change of variable so that the integration can be performed
  134.      * on an arbitrary interval {@code [a, b]}.
  135.      * It is assumed that the natural interval is {@code [-1, 1]}.
  136.      *
  137.      * @param rule Original points and weights.
  138.      * @param a Lower bound of the integration interval.
  139.      * @param b Lower bound of the integration interval.
  140.      * @return the points and weights adapted to the new interval.
  141.      */
  142.     private static Pair<double[], double[]> transform(Pair<double[], double[]> rule,
  143.                                                       double a,
  144.                                                       double b) {
  145.         final double[] points = rule.getFirst();
  146.         final double[] weights = rule.getSecond();

  147.         // Scaling
  148.         final double scale = (b - a) / 2;
  149.         final double shift = a + scale;

  150.         for (int i = 0; i < points.length; i++) {
  151.             points[i] = points[i] * scale + shift;
  152.             weights[i] *= scale;
  153.         }

  154.         return new Pair<double[], double[]>(points, weights);
  155.     }
  156. }