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    *      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.math4.legacy.analysis.integration.gauss;
18  
19  import java.util.Arrays;
20  
21  import org.apache.commons.math4.legacy.analysis.polynomials.PolynomialFunction;
22  import org.apache.commons.math4.legacy.analysis.polynomials.PolynomialsUtils;
23  import org.apache.commons.math4.legacy.linear.EigenDecomposition;
24  import org.apache.commons.math4.legacy.linear.MatrixUtils;
25  import org.apache.commons.math4.legacy.linear.RealMatrix;
26  import org.apache.commons.math4.legacy.core.Pair;
27  
28  /**
29   * Factory that creates Gauss-type quadrature rule using Laguerre polynomials.
30   *
31   * @see <a href="http://en.wikipedia.org/wiki/Gauss%E2%80%93Laguerre_quadrature">Gauss-Laguerre quadrature (Wikipedia)</a>
32   * @since 4.0
33   */
34  public class LaguerreRuleFactory extends BaseRuleFactory<Double> {
35      /** {@inheritDoc} */
36      @Override
37      protected Pair<Double[], Double[]> computeRule(int numberOfPoints) {
38          final RealMatrix companionMatrix = companionMatrix(numberOfPoints);
39          final EigenDecomposition eigen = new EigenDecomposition(companionMatrix);
40          final double[] roots = eigen.getRealEigenvalues();
41          Arrays.sort(roots);
42  
43          final Double[] points = new Double[numberOfPoints];
44          final Double[] weights = new Double[numberOfPoints];
45  
46          final int n1 = numberOfPoints + 1;
47          final long n1Squared = n1 * (long) n1;
48          final PolynomialFunction laguerreN1 = PolynomialsUtils.createLaguerrePolynomial(n1);
49          for (int i = 0; i < numberOfPoints; i++) {
50              final double xi = roots[i];
51              points[i] = xi;
52  
53              final double val = laguerreN1.value(xi);
54              weights[i] = xi / n1Squared / (val * val);
55          }
56  
57          return new Pair<>(points, weights);
58      }
59  
60      /**
61       * @param degree Matrix dimension.
62       * @return a square matrix.
63       */
64      private RealMatrix companionMatrix(final int degree) {
65          final RealMatrix c = MatrixUtils.createRealMatrix(degree, degree);
66  
67          for (int i = 0; i < degree; i++) {
68              c.setEntry(i, i, 2.0 * i + 1);
69              if (i + 1 < degree) {
70                  // subdiagonal
71                  c.setEntry(i+1, i, -(i + 1));
72              }
73              if (i - 1 >= 0) {
74                  // superdiagonal
75                  c.setEntry(i-1, i, -i);
76              }
77          }
78  
79          return c;
80      }
81  }