LaguerreRuleFactory.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.math4.legacy.analysis.integration.gauss;

  18. import java.util.Arrays;

  19. import org.apache.commons.math4.legacy.analysis.polynomials.PolynomialFunction;
  20. import org.apache.commons.math4.legacy.analysis.polynomials.PolynomialsUtils;
  21. import org.apache.commons.math4.legacy.linear.EigenDecomposition;
  22. import org.apache.commons.math4.legacy.linear.MatrixUtils;
  23. import org.apache.commons.math4.legacy.linear.RealMatrix;
  24. import org.apache.commons.math4.legacy.core.Pair;

  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.  * @since 4.0
  30.  */
  31. public class LaguerreRuleFactory extends BaseRuleFactory<Double> {
  32.     /** {@inheritDoc} */
  33.     @Override
  34.     protected Pair<Double[], Double[]> computeRule(int numberOfPoints) {
  35.         final RealMatrix companionMatrix = companionMatrix(numberOfPoints);
  36.         final EigenDecomposition eigen = new EigenDecomposition(companionMatrix);
  37.         final double[] roots = eigen.getRealEigenvalues();
  38.         Arrays.sort(roots);

  39.         final Double[] points = new Double[numberOfPoints];
  40.         final Double[] weights = new Double[numberOfPoints];

  41.         final int n1 = numberOfPoints + 1;
  42.         final long n1Squared = n1 * (long) n1;
  43.         final PolynomialFunction laguerreN1 = PolynomialsUtils.createLaguerrePolynomial(n1);
  44.         for (int i = 0; i < numberOfPoints; i++) {
  45.             final double xi = roots[i];
  46.             points[i] = xi;

  47.             final double val = laguerreN1.value(xi);
  48.             weights[i] = xi / n1Squared / (val * val);
  49.         }

  50.         return new Pair<>(points, weights);
  51.     }

  52.     /**
  53.      * @param degree Matrix dimension.
  54.      * @return a square matrix.
  55.      */
  56.     private RealMatrix companionMatrix(final int degree) {
  57.         final RealMatrix c = MatrixUtils.createRealMatrix(degree, degree);

  58.         for (int i = 0; i < degree; i++) {
  59.             c.setEntry(i, i, 2.0 * i + 1);
  60.             if (i + 1 < degree) {
  61.                 // subdiagonal
  62.                 c.setEntry(i+1, i, -(i + 1));
  63.             }
  64.             if (i - 1 >= 0) {
  65.                 // superdiagonal
  66.                 c.setEntry(i-1, i, -i);
  67.             }
  68.         }

  69.         return c;
  70.     }
  71. }