View Javadoc
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  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.analysis.integration.gauss;
23  
24  import org.hipparchus.exception.MathIllegalArgumentException;
25  import org.hipparchus.util.Pair;
26  
27  /**
28   * Factory that creates Gauss-type quadrature rule using Legendre polynomials.
29   * In this implementation, the lower and upper bounds of the natural interval
30   * of integration are -1 and 1, respectively.
31   * The Legendre polynomials are evaluated using the recurrence relation
32   * presented in <a href="http://en.wikipedia.org/wiki/Abramowitz_and_Stegun">
33   * Abramowitz and Stegun, 1964</a>.
34   *
35   */
36  public class LegendreRuleFactory extends AbstractRuleFactory {
37  
38      /** Empty constructor.
39       * <p>
40       * This constructor is not strictly necessary, but it prevents spurious
41       * javadoc warnings with JDK 18 and later.
42       * </p>
43       * @since 3.0
44       */
45      public LegendreRuleFactory() { // NOPMD - unnecessary constructor added intentionally to make javadoc happy
46          // nothing to do
47      }
48  
49      /** {@inheritDoc} */
50      @Override
51      protected Pair<double[], double[]> computeRule(int numberOfPoints)
52          throws MathIllegalArgumentException {
53  
54          if (numberOfPoints == 1) {
55              // Break recursion.
56             return new Pair<>(new double[] { 0 } , new double[] { 2 });
57          }
58  
59          // find nodes as roots of Legendre polynomial
60          final Legendre p      =  new Legendre(numberOfPoints);
61          final double[] points = findRoots(numberOfPoints, p::ratio);
62          enforceSymmetry(points);
63  
64          // compute weights
65          final double[] weights = new double[numberOfPoints];
66          for (int i = 0; i <= numberOfPoints / 2; i++) {
67              final double c = points[i];
68              final double[] pKpKm1 = p.pNpNm1(c);
69              final double d = numberOfPoints * (pKpKm1[1] - c * pKpKm1[0]);
70              weights[i] = 2 * (1 - c * c) / (d * d);
71  
72              // symmetrical point
73              final int idx = numberOfPoints - i - 1;
74              weights[idx]  = weights[i];
75  
76          }
77  
78          return new Pair<>(points, weights);
79  
80      }
81  
82      /** Legendre polynomial. */
83      private static class Legendre {
84  
85          /** Degree. */
86          private int degree;
87  
88          /** Simple constructor.
89           * @param degree polynomial degree
90           */
91          Legendre(int degree) {
92              this.degree = degree;
93          }
94  
95          /** Compute ratio P(x)/P'(x).
96           * @param x point at which ratio must be computed
97           * @return ratio P(x)/P'(x)
98           */
99          public double ratio(double x) {
100             double pm = 1;
101             double p  = x;
102             double d  = 1;
103             for (int n = 1; n < degree; n++) {
104                 // apply recurrence relations (n+1) Pₙ₊₁(x)  = (2n+1) x Pₙ(x) - n Pₙ₋₁(x)
105                 // and                              P'ₙ₊₁(x) = (n+1) Pₙ(x) + x P'ₙ(x)
106                 final double pp = (p * (x * (2 * n + 1)) - pm * n) / (n + 1);
107                 d  = p * (n + 1) + d * x;
108                 pm = p;
109                 p  = pp;
110             }
111             return p / d;
112         }
113 
114         /** Compute Pₙ(x) and Pₙ₋₁(x).
115          * @param x point at which polynomials are evaluated
116          * @return array containing Pₙ(x) at index 0 and Pₙ₋₁(x) at index 1
117          */
118         private double[] pNpNm1(final double x) {
119             double[] p = { x, 1 };
120             for (int n = 1; n < degree; n++) {
121                 // apply recurrence relation (n+1) Pₙ₊₁(x) = (2n+1) x Pₙ(x) - n Pₙ₋₁(x)
122                 final double pp = (p[0] * (x * (2 * n + 1)) - p[1] * n) / (n + 1);
123                 p[1] = p[0];
124                 p[0] = pp;
125             }
126             return p;
127         }
128 
129     }
130 
131 }