LegendreHighPrecisionRuleFactory.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.math.BigDecimal;
  19. import java.math.MathContext;

  20. import org.apache.commons.math4.legacy.core.Pair;

  21. /**
  22.  * Factory that creates Gauss-type quadrature rule using Legendre polynomials.
  23.  * In this implementation, the lower and upper bounds of the natural interval
  24.  * of integration are -1 and 1, respectively.
  25.  * The Legendre polynomials are evaluated using the recurrence relation
  26.  * presented in <a href="http://en.wikipedia.org/wiki/Abramowitz_and_Stegun">
  27.  * Abramowitz and Stegun, 1964</a>.
  28.  *
  29.  * @since 3.1
  30.  */
  31. public class LegendreHighPrecisionRuleFactory extends BaseRuleFactory<BigDecimal> {
  32.     /** Settings for enhanced precision computations. */
  33.     private final MathContext mContext;
  34.     /** The number {@code 2}. */
  35.     private final BigDecimal two;
  36.     /** The number {@code -1}. */
  37.     private final BigDecimal minusOne;
  38.     /** The number {@code 0.5}. */
  39.     private final BigDecimal oneHalf;

  40.     /**
  41.      * Default precision is {@link MathContext#DECIMAL128 DECIMAL128}.
  42.      */
  43.     public LegendreHighPrecisionRuleFactory() {
  44.         this(MathContext.DECIMAL128);
  45.     }

  46.     /**
  47.      * @param mContext Precision setting for computing the quadrature rules.
  48.      */
  49.     public LegendreHighPrecisionRuleFactory(MathContext mContext) {
  50.         this.mContext = mContext;
  51.         two = new BigDecimal("2", mContext);
  52.         minusOne = new BigDecimal("-1", mContext);
  53.         oneHalf = new BigDecimal("0.5", mContext);
  54.     }

  55.     /** {@inheritDoc} */
  56.     @Override
  57.     protected Pair<BigDecimal[], BigDecimal[]> computeRule(int numberOfPoints) {
  58.         if (numberOfPoints == 1) {
  59.             // Break recursion.
  60.             return new Pair<>(new BigDecimal[] { BigDecimal.ZERO },
  61.                                                         new BigDecimal[] { two });
  62.         }

  63.         // Get previous rule.
  64.         // If it has not been computed yet it will trigger a recursive call
  65.         // to this method.
  66.         final BigDecimal[] previousPoints = getRuleInternal(numberOfPoints - 1).getFirst();

  67.         // Compute next rule.
  68.         final BigDecimal[] points = new BigDecimal[numberOfPoints];
  69.         final BigDecimal[] weights = new BigDecimal[numberOfPoints];

  70.         // Find i-th root of P[n+1] by bracketing.
  71.         final int iMax = numberOfPoints / 2;
  72.         for (int i = 0; i < iMax; i++) {
  73.             // Lower-bound of the interval.
  74.             BigDecimal a = (i == 0) ? minusOne : previousPoints[i - 1];
  75.             // Upper-bound of the interval.
  76.             BigDecimal b = (iMax == 1) ? BigDecimal.ONE : previousPoints[i];
  77.             // P[j-1](a)
  78.             BigDecimal pma = BigDecimal.ONE;
  79.             // P[j](a)
  80.             BigDecimal pa = a;
  81.             // P[j-1](b)
  82.             BigDecimal pmb = BigDecimal.ONE;
  83.             // P[j](b)
  84.             BigDecimal pb = b;
  85.             for (int j = 1; j < numberOfPoints; j++) {
  86.                 final BigDecimal b_two_j_p_1 = new BigDecimal(2 * j + 1, mContext);
  87.                 final BigDecimal b_j = new BigDecimal(j, mContext);
  88.                 final BigDecimal b_j_p_1 = new BigDecimal(j + 1, mContext);

  89.                 // Compute P[j+1](a)
  90.                 // ppa = ((2 * j + 1) * a * pa - j * pma) / (j + 1);

  91.                 BigDecimal tmp1 = a.multiply(b_two_j_p_1, mContext);
  92.                 tmp1 = pa.multiply(tmp1, mContext);
  93.                 BigDecimal tmp2 = pma.multiply(b_j, mContext);
  94.                 // P[j+1](a)
  95.                 BigDecimal ppa = tmp1.subtract(tmp2, mContext);
  96.                 ppa = ppa.divide(b_j_p_1, mContext);

  97.                 // Compute P[j+1](b)
  98.                 // ppb = ((2 * j + 1) * b * pb - j * pmb) / (j + 1);

  99.                 tmp1 = b.multiply(b_two_j_p_1, mContext);
  100.                 tmp1 = pb.multiply(tmp1, mContext);
  101.                 tmp2 = pmb.multiply(b_j, mContext);
  102.                 // P[j+1](b)
  103.                 BigDecimal ppb = tmp1.subtract(tmp2, mContext);
  104.                 ppb = ppb.divide(b_j_p_1, mContext);

  105.                 pma = pa;
  106.                 pa = ppa;
  107.                 pmb = pb;
  108.                 pb = ppb;
  109.             }
  110.             // Now pa = P[n+1](a), and pma = P[n](a). Same holds for b.
  111.             // Middle of the interval.
  112.             BigDecimal c = a.add(b, mContext).multiply(oneHalf, mContext);
  113.             // P[j-1](c)
  114.             BigDecimal pmc = BigDecimal.ONE;
  115.             // P[j](c)
  116.             BigDecimal pc = c;
  117.             boolean done = false;
  118.             while (!done) {
  119.                 BigDecimal tmp1 = b.subtract(a, mContext);
  120.                 BigDecimal tmp2 = c.ulp().multiply(BigDecimal.TEN, mContext);
  121.                 done = tmp1.compareTo(tmp2) <= 0;
  122.                 pmc = BigDecimal.ONE;
  123.                 pc = c;
  124.                 for (int j = 1; j < numberOfPoints; j++) {
  125.                     final BigDecimal b_two_j_p_1 = new BigDecimal(2 * j + 1, mContext);
  126.                     final BigDecimal b_j = new BigDecimal(j, mContext);
  127.                     final BigDecimal b_j_p_1 = new BigDecimal(j + 1, mContext);

  128.                     // Compute P[j+1](c)
  129.                     tmp1 = c.multiply(b_two_j_p_1, mContext);
  130.                     tmp1 = pc.multiply(tmp1, mContext);
  131.                     tmp2 = pmc.multiply(b_j, mContext);
  132.                     // P[j+1](c)
  133.                     BigDecimal ppc = tmp1.subtract(tmp2, mContext);
  134.                     ppc = ppc.divide(b_j_p_1, mContext);

  135.                     pmc = pc;
  136.                     pc = ppc;
  137.                 }
  138.                 // Now pc = P[n+1](c) and pmc = P[n](c).
  139.                 if (!done) {
  140.                     if (pa.signum() * pc.signum() <= 0) {
  141.                         b = c;
  142.                         pmb = pmc;
  143.                         pb = pc;
  144.                     } else {
  145.                         a = c;
  146.                         pma = pmc;
  147.                         pa = pc;
  148.                     }
  149.                     c = a.add(b, mContext).multiply(oneHalf, mContext);
  150.                 }
  151.             }
  152.             final BigDecimal nP = new BigDecimal(numberOfPoints, mContext);
  153.             BigDecimal tmp1 = pmc.subtract(c.multiply(pc, mContext), mContext);
  154.             tmp1 = tmp1.multiply(nP);
  155.             tmp1 = tmp1.pow(2, mContext);
  156.             BigDecimal tmp2 = c.pow(2, mContext);
  157.             tmp2 = BigDecimal.ONE.subtract(tmp2, mContext);
  158.             tmp2 = tmp2.multiply(two, mContext);
  159.             tmp2 = tmp2.divide(tmp1, mContext);

  160.             points[i] = c;
  161.             weights[i] = tmp2;

  162.             final int idx = numberOfPoints - i - 1;
  163.             points[idx] = c.negate(mContext);
  164.             weights[idx] = tmp2;
  165.         }
  166.         // If "numberOfPoints" is odd, 0 is a root.
  167.         // Note: as written, the test for oddness will work for negative
  168.         // integers too (although it is not necessary here), preventing
  169.         // a FindBugs warning.
  170.         if ((numberOfPoints & 1) != 0) {
  171.             BigDecimal pmc = BigDecimal.ONE;
  172.             for (int j = 1; j < numberOfPoints; j += 2) {
  173.                 final BigDecimal b_j = new BigDecimal(j, mContext);
  174.                 final BigDecimal b_j_p_1 = new BigDecimal(j + 1, mContext);

  175.                 // pmc = -j * pmc / (j + 1);
  176.                 pmc = pmc.multiply(b_j, mContext);
  177.                 pmc = pmc.divide(b_j_p_1, mContext);
  178.                 pmc = pmc.negate(mContext);
  179.             }

  180.             // 2 / pow(numberOfPoints * pmc, 2);
  181.             final BigDecimal nP = new BigDecimal(numberOfPoints, mContext);
  182.             BigDecimal tmp1 = pmc.multiply(nP, mContext);
  183.             tmp1 = tmp1.pow(2, mContext);
  184.             BigDecimal tmp2 = two.divide(tmp1, mContext);

  185.             points[iMax] = BigDecimal.ZERO;
  186.             weights[iMax] = tmp2;
  187.         }

  188.         return new Pair<>(points, weights);
  189.     }
  190. }