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
19 import org.hipparchus.CalculusFieldElement;
20 import org.hipparchus.Field;
21 import org.hipparchus.exception.MathIllegalArgumentException;
22 import org.hipparchus.util.MathArrays;
23 import org.hipparchus.util.Pair;
24
25 /**
26 * Factory that creates Gauss-type quadrature rule using Laguerre polynomials.
27 *
28 * @see <a href="http://en.wikipedia.org/wiki/Gauss%E2%80%93Laguerre_quadrature">Gauss-Laguerre quadrature (Wikipedia)</a>
29 * @param <T> Type of the number used to represent the points and weights of
30 * the quadrature rules.
31 * @since 2.0
32 */
33 public class FieldLaguerreRuleFactory<T extends CalculusFieldElement<T>> extends FieldAbstractRuleFactory<T> {
34
35 /** Simple constructor
36 * @param field field to which rule coefficients belong
37 */
38 public FieldLaguerreRuleFactory(final Field<T> field) {
39 super(field);
40 }
41
42 /** {@inheritDoc} */
43 @Override
44 public Pair<T[], T[]> computeRule(int numberOfPoints)
45 throws MathIllegalArgumentException {
46
47 final Field<T> field = getField();
48
49 // find nodes as roots of Laguerre polynomial
50 final Laguerre<T> p = new Laguerre<>(numberOfPoints);
51 final T[] points = findRoots(numberOfPoints, p::ratio);
52
53 // compute weights
54 final T[] weights = MathArrays.buildArray(field, numberOfPoints);
55 final int n1 = numberOfPoints + 1;
56 final long n1Squared = n1 * (long) n1;
57 final Laguerre<T> laguerreN1 = new Laguerre<>(n1);
58 for (int i = 0; i < numberOfPoints; i++) {
59 final T y = laguerreN1.value(points[i]);
60 weights[i] = points[i].divide(y.square().multiply(n1Squared));
61 }
62
63 return new Pair<>(points, weights);
64
65 }
66
67 /** Laguerre polynomial.
68 * @param <T> Type of the field elements.
69 */
70 private static class Laguerre<T extends CalculusFieldElement<T>> {
71
72 /** Degree. */
73 private int degree;
74
75 /** Simple constructor.
76 * @param degree polynomial degree
77 */
78 Laguerre(int degree) {
79 this.degree = degree;
80 }
81
82 /** Evaluate polynomial.
83 * @param x point at which polynomial must be evaluated
84 * @return value of the polynomial
85 */
86 public T value(final T x) {
87 return lNlNm1(x)[0];
88 }
89
90 /** Compute ratio L(x)/L'(x).
91 * @param x point at which ratio must be computed
92 * @return ratio L(x)/L'(x)
93 */
94 public T ratio(T x) {
95 T[] l = lNlNm1(x);
96 return x.multiply(l[0]).divide(l[0].subtract(l[1]).multiply(degree));
97 }
98
99 /** Compute Lₙ(x) and Lₙ₋₁(x).
100 * @param x point at which polynomials are evaluated
101 * @return array containing Lₙ(x) at index 0 and Lₙ₋₁(x) at index 1
102 */
103 private T[] lNlNm1(final T x) {
104 T[] l = MathArrays.buildArray(x.getField(), 2);
105 l[0] = x.subtract(1).negate();
106 l[1] = x.getField().getOne();
107 for (int n = 1; n < degree; n++) {
108 // apply recurrence relation (n+1) Lₙ₊₁(x) = (2n + 1 - x) Lₙ(x) - n Lₙ₋₁(x)
109 final T lp = l[0].multiply(x.negate().add(2 * n + 1)).subtract(l[1].multiply(n)).divide(n + 1);
110 l[1] = l[0];
111 l[0] = lp;
112 }
113 return l;
114 }
115
116 }
117
118 }