LegendreRuleFactory.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 org.apache.commons.math4.legacy.core.Pair;

  19. /**
  20.  * Factory that creates Gauss-type quadrature rule using Legendre polynomials.
  21.  * In this implementation, the lower and upper bounds of the natural interval
  22.  * of integration are -1 and 1, respectively.
  23.  * The Legendre polynomials are evaluated using the recurrence relation
  24.  * presented in <a href="http://en.wikipedia.org/wiki/Abramowitz_and_Stegun">
  25.  * Abramowitz and Stegun, 1964</a>.
  26.  *
  27.  * @since 3.1
  28.  */
  29. public class LegendreRuleFactory extends BaseRuleFactory<Double> {
  30.     /** {@inheritDoc} */
  31.     @Override
  32.     protected Pair<Double[], Double[]> computeRule(int numberOfPoints) {
  33.         if (numberOfPoints == 1) {
  34.             // Break recursion.
  35.             return new Pair<>(new Double[] { 0d },
  36.                                                 new Double[] { 2d });
  37.         }

  38.         // Get previous rule.
  39.         // If it has not been computed yet it will trigger a recursive call
  40.         // to this method.
  41.         final Double[] previousPoints = getRuleInternal(numberOfPoints - 1).getFirst();

  42.         // Compute next rule.
  43.         final Double[] points = new Double[numberOfPoints];
  44.         final Double[] weights = new Double[numberOfPoints];

  45.         // Find i-th root of P[n+1] by bracketing.
  46.         final int iMax = numberOfPoints / 2;
  47.         for (int i = 0; i < iMax; i++) {
  48.             // Lower-bound of the interval.
  49.             double a = (i == 0) ? -1 : previousPoints[i - 1].doubleValue();
  50.             // Upper-bound of the interval.
  51.             double b = (iMax == 1) ? 1 : previousPoints[i].doubleValue();
  52.             // P[j-1](a)
  53.             double pma = 1;
  54.             // P[j](a)
  55.             double pa = a;
  56.             // P[j-1](b)
  57.             double pmb = 1;
  58.             // P[j](b)
  59.             double pb = b;
  60.             for (int j = 1; j < numberOfPoints; j++) {
  61.                 final int two_j_p_1 = 2 * j + 1;
  62.                 final int j_p_1 = j + 1;
  63.                 // P[j+1](a)
  64.                 final double ppa = (two_j_p_1 * a * pa - j * pma) / j_p_1;
  65.                 // P[j+1](b)
  66.                 final double ppb = (two_j_p_1 * b * pb - j * pmb) / j_p_1;
  67.                 pma = pa;
  68.                 pa = ppa;
  69.                 pmb = pb;
  70.                 pb = ppb;
  71.             }
  72.             // Now pa = P[n+1](a), and pma = P[n](a) (same holds for b).
  73.             // Middle of the interval.
  74.             double c = 0.5 * (a + b);
  75.             // P[j-1](c)
  76.             double pmc = 1;
  77.             // P[j](c)
  78.             double pc = c;
  79.             boolean done = false;
  80.             while (!done) {
  81.                 done = b - a <= Math.ulp(c);
  82.                 pmc = 1;
  83.                 pc = c;
  84.                 for (int j = 1; j < numberOfPoints; j++) {
  85.                     // P[j+1](c)
  86.                     final double ppc = ((2 * j + 1) * c * pc - j * pmc) / (j + 1);
  87.                     pmc = pc;
  88.                     pc = ppc;
  89.                 }
  90.                 // Now pc = P[n+1](c) and pmc = P[n](c).
  91.                 if (!done) {
  92.                     if (pa * pc <= 0) {
  93.                         b = c;
  94.                         pmb = pmc;
  95.                         pb = pc;
  96.                     } else {
  97.                         a = c;
  98.                         pma = pmc;
  99.                         pa = pc;
  100.                     }
  101.                     c = 0.5 * (a + b);
  102.                 }
  103.             }
  104.             final double d = numberOfPoints * (pmc - c * pc);
  105.             final double w = 2 * (1 - c * c) / (d * d);

  106.             points[i] = c;
  107.             weights[i] = w;

  108.             final int idx = numberOfPoints - i - 1;
  109.             points[idx] = -c;
  110.             weights[idx] = w;
  111.         }
  112.         // If "numberOfPoints" is odd, 0 is a root.
  113.         // Note: as written, the test for oddness will work for negative
  114.         // integers too (although it is not necessary here), preventing
  115.         // a FindBugs warning.
  116.         if ((numberOfPoints & 1) != 0) {
  117.             double pmc = 1;
  118.             for (int j = 1; j < numberOfPoints; j += 2) {
  119.                 pmc = -j * pmc / (j + 1);
  120.             }
  121.             final double d = numberOfPoints * pmc;
  122.             final double w = 2 / (d * d);

  123.             points[iMax] = 0d;
  124.             weights[iMax] = w;
  125.         }

  126.         return new Pair<>(points, weights);
  127.     }
  128. }