FieldLaguerreRuleFactory.java

  1. /*
  2.  * Licensed to the Hipparchus project 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 Hipparchus project 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. package org.hipparchus.analysis.integration.gauss;

  18. import org.hipparchus.CalculusFieldElement;
  19. import org.hipparchus.Field;
  20. import org.hipparchus.exception.MathIllegalArgumentException;
  21. import org.hipparchus.util.MathArrays;
  22. import org.hipparchus.util.Pair;

  23. /**
  24.  * Factory that creates Gauss-type quadrature rule using Laguerre polynomials.
  25.  *
  26.  * @see <a href="http://en.wikipedia.org/wiki/Gauss%E2%80%93Laguerre_quadrature">Gauss-Laguerre quadrature (Wikipedia)</a>
  27.  * @param <T> Type of the number used to represent the points and weights of
  28.  * the quadrature rules.
  29.  * @since 2.0
  30.  */
  31. public class FieldLaguerreRuleFactory<T extends CalculusFieldElement<T>> extends FieldAbstractRuleFactory<T> {

  32.     /** Simple constructor
  33.      * @param field field to which rule coefficients belong
  34.      */
  35.     public FieldLaguerreRuleFactory(final Field<T> field) {
  36.         super(field);
  37.     }

  38.     /** {@inheritDoc} */
  39.     @Override
  40.     public Pair<T[], T[]> computeRule(int numberOfPoints)
  41.         throws MathIllegalArgumentException {

  42.         final Field<T> field = getField();

  43.         // find nodes as roots of Laguerre polynomial
  44.         final Laguerre<T> p      =  new Laguerre<>(numberOfPoints);
  45.         final T[]      points = findRoots(numberOfPoints, p::ratio);

  46.         // compute weights
  47.         final T[] weights = MathArrays.buildArray(field, numberOfPoints);
  48.         final int      n1         = numberOfPoints + 1;
  49.         final long     n1Squared  = n1 * (long) n1;
  50.         final Laguerre<T> laguerreN1 = new Laguerre<>(n1);
  51.         for (int i = 0; i < numberOfPoints; i++) {
  52.             final T y = laguerreN1.value(points[i]);
  53.             weights[i] = points[i].divide(y.square().multiply(n1Squared));
  54.         }

  55.         return new Pair<>(points, weights);

  56.     }

  57.     /** Laguerre polynomial.
  58.      * @param <T> Type of the field elements.
  59.      */
  60.     private static class Laguerre<T extends CalculusFieldElement<T>> {

  61.         /** Degree. */
  62.         private int degree;

  63.         /** Simple constructor.
  64.          * @param degree polynomial degree
  65.          */
  66.         Laguerre(int degree) {
  67.             this.degree = degree;
  68.         }

  69.         /** Evaluate polynomial.
  70.          * @param x point at which polynomial must be evaluated
  71.          * @return value of the polynomial
  72.          */
  73.         public T value(final T x) {
  74.             return lNlNm1(x)[0];
  75.         }

  76.         /** Compute ratio L(x)/L'(x).
  77.          * @param x point at which ratio must be computed
  78.          * @return ratio L(x)/L'(x)
  79.          */
  80.         public T ratio(T x) {
  81.             T[] l = lNlNm1(x);
  82.             return x.multiply(l[0]).divide(l[0].subtract(l[1]).multiply(degree));
  83.         }

  84.         /** Compute Lₙ(x) and Lₙ₋₁(x).
  85.          * @param x point at which polynomials are evaluated
  86.          * @return array containing Lₙ(x) at index 0 and Lₙ₋₁(x) at index 1
  87.          */
  88.         private T[] lNlNm1(final T x) {
  89.             T[] l = MathArrays.buildArray(x.getField(), 2);
  90.             l[0] = x.subtract(1).negate();
  91.             l[1] = x.getField().getOne();
  92.             for (int n = 1; n < degree; n++) {
  93.                 // apply recurrence relation (n+1) Lₙ₊₁(x) = (2n + 1 - x) Lₙ(x) - n Lₙ₋₁(x)
  94.                 final T lp = l[0].multiply(x.negate().add(2 * n + 1)).subtract(l[1].multiply(n)).divide(n + 1);
  95.                 l[1] = l[0];
  96.                 l[0] = lp;
  97.             }
  98.             return l;
  99.         }

  100.     }

  101. }