GaussLegendreQuadratureGenerator.java

  1. package org.drip.numerical.integration;

  2. /*
  3.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  4.  */

  5. /*!
  6.  * Copyright (C) 2020 Lakshmi Krishnamurthy
  7.  * Copyright (C) 2019 Lakshmi Krishnamurthy
  8.  *
  9.  *  This file is part of DROP, an open-source library targeting analytics/risk, transaction cost analytics,
  10.  *      asset liability management analytics, capital, exposure, and margin analytics, valuation adjustment
  11.  *      analytics, and portfolio construction analytics within and across fixed income, credit, commodity,
  12.  *      equity, FX, and structured products. It also includes auxiliary libraries for algorithm support,
  13.  *      numerical analysis, numerical optimization, spline builder, model validation, statistical learning,
  14.  *      and computational support.
  15.  *  
  16.  *      https://lakshmidrip.github.io/DROP/
  17.  *  
  18.  *  DROP is composed of three modules:
  19.  *  
  20.  *  - DROP Product Core - https://lakshmidrip.github.io/DROP-Product-Core/
  21.  *  - DROP Portfolio Core - https://lakshmidrip.github.io/DROP-Portfolio-Core/
  22.  *  - DROP Computational Core - https://lakshmidrip.github.io/DROP-Computational-Core/
  23.  *
  24.  *  DROP Product Core implements libraries for the following:
  25.  *  - Fixed Income Analytics
  26.  *  - Loan Analytics
  27.  *  - Transaction Cost Analytics
  28.  *
  29.  *  DROP Portfolio Core implements libraries for the following:
  30.  *  - Asset Allocation Analytics
  31.  *  - Asset Liability Management Analytics
  32.  *  - Capital Estimation Analytics
  33.  *  - Exposure Analytics
  34.  *  - Margin Analytics
  35.  *  - XVA Analytics
  36.  *
  37.  *  DROP Computational Core implements libraries for the following:
  38.  *  - Algorithm Support
  39.  *  - Computation Support
  40.  *  - Function Analysis
  41.  *  - Model Validation
  42.  *  - Numerical Analysis
  43.  *  - Numerical Optimizer
  44.  *  - Spline Builder
  45.  *  - Statistical Learning
  46.  *
  47.  *  Documentation for DROP is Spread Over:
  48.  *
  49.  *  - Main                     => https://lakshmidrip.github.io/DROP/
  50.  *  - Wiki                     => https://github.com/lakshmiDRIP/DROP/wiki
  51.  *  - GitHub                   => https://github.com/lakshmiDRIP/DROP
  52.  *  - Repo Layout Taxonomy     => https://github.com/lakshmiDRIP/DROP/blob/master/Taxonomy.md
  53.  *  - Javadoc                  => https://lakshmidrip.github.io/DROP/Javadoc/index.html
  54.  *  - Technical Specifications => https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal
  55.  *  - Release Versions         => https://lakshmidrip.github.io/DROP/version.html
  56.  *  - Community Credits        => https://lakshmidrip.github.io/DROP/credits.html
  57.  *  - Issues Catalog           => https://github.com/lakshmiDRIP/DROP/issues
  58.  *  - JUnit                    => https://lakshmidrip.github.io/DROP/junit/index.html
  59.  *  - Jacoco                   => https://lakshmidrip.github.io/DROP/jacoco/index.html
  60.  *
  61.  *  Licensed under the Apache License, Version 2.0 (the "License");
  62.  *      you may not use this file except in compliance with the License.
  63.  *  
  64.  *  You may obtain a copy of the License at
  65.  *      http://www.apache.org/licenses/LICENSE-2.0
  66.  *  
  67.  *  Unless required by applicable law or agreed to in writing, software
  68.  *      distributed under the License is distributed on an "AS IS" BASIS,
  69.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  70.  *  
  71.  *  See the License for the specific language governing permissions and
  72.  *      limitations under the License.
  73.  */

  74. /**
  75.  * <i>GaussLegendreQuadratureGenerator</i> generates the Array of Orthogonal Legendre Polynomial Gaussian
  76.  * Quadrature Based Abscissa and their corresponding Weights. The References are:
  77.  *
  78.  * <br><br>
  79.  *  <ul>
  80.  *      <li>
  81.  *          Abramowitz, M., and I. A. Stegun (2007): <i>Handbook of Mathematics Functions</i> <b>Dover Book
  82.  *              on Mathematics</b>
  83.  *      </li>
  84.  *      <li>
  85.  *          Gil, A., J. Segura, and N. M. Temme (2007): <i>Numerical Methods for Special Functions</i>
  86.  *              <b>Society for Industrial and Applied Mathematics</b> Philadelphia
  87.  *      </li>
  88.  *      <li>
  89.  *          Press, W. H., S. A. Teukolsky, W. T. Vetterling, and B. P. Flannery (2007): <i>Numerical Recipes:
  90.  *              The Art of Scientific Computing 3rd Edition</i> <b>Cambridge University Press</b> New York
  91.  *      </li>
  92.  *      <li>
  93.  *          Stoer, J., and R. Bulirsch (2002): <i>Introduction to Numerical Analysis 3rd Edition</i>
  94.  *              <b>Springer</b>
  95.  *      </li>
  96.  *      <li>
  97.  *          Wikipedia (2019): Gaussian Quadrature https://en.wikipedia.org/wiki/Gaussian_quadrature
  98.  *      </li>
  99.  *  </ul>
  100.  *
  101.  *  <br><br>
  102.  *  <ul>
  103.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  104.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalAnalysisLibrary.md">Numerical Analysis Library</a></li>
  105.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/numerical/README.md">Numerical Quadrature, Differentiation, Eigenization, Linear Algebra, and Utilities</a></li>
  106.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/numerical/integration/README.md">R<sup>1</sup> R<sup>d</sup> Numerical Integration Schemes</a></li>
  107.  *  </ul>
  108.  *
  109.  * @author Lakshmi Krishnamurthy
  110.  */

  111. public class GaussLegendreQuadratureGenerator
  112. {

  113.     /**
  114.      * Generate the One Point Gauss Legendre Quadrature over [-1, +1]
  115.      *
  116.      * @param abscissaTransformer The Abscissa Transformer
  117.      *
  118.      * @return The One Point Gauss Legendre Quadrature over [-1, +1]
  119.      */

  120.     public static final org.drip.numerical.integration.QuadratureEstimator OnePoint (
  121.         final org.drip.numerical.integration.AbscissaTransform abscissaTransformer)
  122.     {
  123.         try
  124.         {
  125.             return new org.drip.numerical.integration.QuadratureEstimator (
  126.                 abscissaTransformer,
  127.                 org.drip.numerical.common.Array2D.FromArray (
  128.                     new double[]
  129.                     {
  130.                         0.000000000000000,
  131.                     },
  132.                     new double[]
  133.                     {
  134.                         2.000000000000000,
  135.                     }
  136.                 )
  137.             );
  138.         }
  139.         catch (java.lang.Exception e)
  140.         {
  141.             e.printStackTrace();
  142.         }

  143.         return null;
  144.     }

  145.     /**
  146.      * Generate the Two Point Gauss Legendre Quadrature over [-1, +1]
  147.      *
  148.      * @param abscissaTransformer The Abscissa Transformer
  149.      *
  150.      * @return The Two Point Gauss Legendre Quadrature over [-1, +1]
  151.      */

  152.     public static final org.drip.numerical.integration.QuadratureEstimator TwoPoint (
  153.         final org.drip.numerical.integration.AbscissaTransform abscissaTransformer)
  154.     {
  155.         double sqrt_1Over3_ = java.lang.Math.sqrt (1. / 3.);

  156.         try
  157.         {
  158.             return new org.drip.numerical.integration.QuadratureEstimator (
  159.                 abscissaTransformer,
  160.                 org.drip.numerical.common.Array2D.FromArray (
  161.                     new double[]
  162.                     {
  163.                         -sqrt_1Over3_,
  164.                          sqrt_1Over3_,
  165.                     },
  166.                     new double[]
  167.                     {
  168.                         1.000000000000000,
  169.                         1.000000000000000,
  170.                     }
  171.                 )
  172.             );
  173.         }
  174.         catch (java.lang.Exception e)
  175.         {
  176.             e.printStackTrace();
  177.         }

  178.         return null;
  179.     }

  180.     /**
  181.      * Generate the Three Point Gauss Legendre Quadrature over [-1, +1]
  182.      *
  183.      * @param abscissaTransformer The Abscissa Transformer
  184.      *
  185.      * @return The Three Point Gauss Legendre Quadrature over [-1, +1]
  186.      */

  187.     public static final org.drip.numerical.integration.QuadratureEstimator ThreePoint (
  188.         final org.drip.numerical.integration.AbscissaTransform abscissaTransformer)
  189.     {
  190.         double sqrt_3Over5_ = java.lang.Math.sqrt (3. / 5.);

  191.         try
  192.         {
  193.             return new org.drip.numerical.integration.QuadratureEstimator (
  194.                 abscissaTransformer,
  195.                 org.drip.numerical.common.Array2D.FromArray (
  196.                     new double[]
  197.                     {
  198.                         -sqrt_3Over5_,
  199.                          0.000000000000000,
  200.                          sqrt_3Over5_,
  201.                     },
  202.                     new double[]
  203.                     {
  204.                         5. / 9.,
  205.                         8. / 9.,
  206.                         5. / 9.,
  207.                     }
  208.                 )
  209.             );
  210.         }
  211.         catch (java.lang.Exception e)
  212.         {
  213.             e.printStackTrace();
  214.         }

  215.         return null;
  216.     }

  217.     /**
  218.      * Generate the Four Point Gauss Legendre Quadrature over [-1, +1]
  219.      *
  220.      * @param abscissaTransformer The Abscissa Transformer
  221.      *
  222.      * @return The Four Point Gauss Legendre Quadrature over [-1, +1]
  223.      */

  224.     public static final org.drip.numerical.integration.QuadratureEstimator FourPoint (
  225.         final org.drip.numerical.integration.AbscissaTransform abscissaTransformer)
  226.     {
  227.         double sqrt_30_Over36 = java.lang.Math.sqrt (30.) / 36.;

  228.         double nearWeight = 0.5 + sqrt_30_Over36;
  229.         double farWeight = 0.5 - sqrt_30_Over36;
  230.         double threeOver7 = 3. / 7.;

  231.         double twoOver7Sqrt_6Over5_ = 2. / 7. * java.lang.Math.sqrt (6. / 5.);

  232.         double farNode = java.lang.Math.sqrt (threeOver7 + twoOver7Sqrt_6Over5_);

  233.         double nearNode = java.lang.Math.sqrt (threeOver7 - twoOver7Sqrt_6Over5_);

  234.         try
  235.         {
  236.             return new org.drip.numerical.integration.QuadratureEstimator (
  237.                 abscissaTransformer,
  238.                 org.drip.numerical.common.Array2D.FromArray (
  239.                     new double[]
  240.                     {
  241.                         -farNode,
  242.                         -nearNode,
  243.                          nearNode,
  244.                          farNode,
  245.                     },
  246.                     new double[]
  247.                     {
  248.                         farWeight,
  249.                         nearWeight,
  250.                         nearWeight,
  251.                         farWeight,
  252.                     }
  253.                 )
  254.             );
  255.         }
  256.         catch (java.lang.Exception e)
  257.         {
  258.             e.printStackTrace();
  259.         }

  260.         return null;
  261.     }

  262.     /**
  263.      * Generate the Five Point Gauss Legendre Quadrature over [-1, +1]
  264.      *
  265.      * @param abscissaTransformer The Abscissa Transformer
  266.      *
  267.      * @return The Five Point Gauss Legendre Quadrature over [-1, +1]
  268.      */

  269.     public static final org.drip.numerical.integration.QuadratureEstimator FivePoint (
  270.         final org.drip.numerical.integration.AbscissaTransform abscissaTransformer)
  271.     {
  272.         double thirteenSqrt_70_ = 13. * java.lang.Math.sqrt (70.);

  273.         double nearWeight = (322. + thirteenSqrt_70_) / 900.;
  274.         double farWeight = (322. - thirteenSqrt_70_) / 900.;

  275.         double twoSqrt_10Over7_ = 2. * java.lang.Math.sqrt (10. / 7.);

  276.         double farNode = java.lang.Math.sqrt (5. + twoSqrt_10Over7_) / 3.;

  277.         double nearNode = java.lang.Math.sqrt (5. - twoSqrt_10Over7_) / 3.;

  278.         try
  279.         {
  280.             return new org.drip.numerical.integration.QuadratureEstimator (
  281.                 abscissaTransformer,
  282.                 org.drip.numerical.common.Array2D.FromArray (
  283.                     new double[]
  284.                     {
  285.                         -farNode,
  286.                         -nearNode,
  287.                          0.000000000000000,
  288.                          nearNode,
  289.                          farNode,
  290.                     },
  291.                     new double[]
  292.                     {
  293.                         farWeight,
  294.                         nearWeight,
  295.                         128. / 225.,
  296.                         nearWeight,
  297.                         farWeight,
  298.                     }
  299.                 )
  300.             );
  301.         }
  302.         catch (java.lang.Exception e)
  303.         {
  304.             e.printStackTrace();
  305.         }

  306.         return null;
  307.     }

  308.     /**
  309.      * Generate the One Point Gauss Legendre Quadrature over [a, b] onto [-1, +1]
  310.      *
  311.      * @param left Left Integrand Quadrature Limit
  312.      * @param right Right Integrand Quadrature Limit
  313.      *
  314.      * @return The One Point Gauss Legendre Quadrature over [a, b] onto [-1, +1]
  315.      */

  316.     public static final org.drip.numerical.integration.QuadratureEstimator OnePoint (
  317.         final double left,
  318.         final double right)
  319.     {
  320.         return OnePoint (
  321.             org.drip.numerical.integration.AbscissaTransform.DisplaceAndScaleMinusOne_PlusOne (
  322.                 left,
  323.                 right
  324.             )
  325.         );
  326.     }

  327.     /**
  328.      * Generate the Two Point Gauss Legendre Quadrature over [a, b] onto [-1, +1]
  329.      *
  330.      * @param left Left Integrand Quadrature Limit
  331.      * @param right Right Integrand Quadrature Limit
  332.      *
  333.      * @return The Two Point Gauss Legendre Quadrature over [a, b] onto [-1, +1]
  334.      */

  335.     public static final org.drip.numerical.integration.QuadratureEstimator TwoPoint (
  336.         final double left,
  337.         final double right)
  338.     {
  339.         return TwoPoint (
  340.             org.drip.numerical.integration.AbscissaTransform.DisplaceAndScaleMinusOne_PlusOne (
  341.                 left,
  342.                 right
  343.             )
  344.         );
  345.     }

  346.     /**
  347.      * Generate the Three Point Gauss Legendre Quadrature over [a, b] onto [-1, +1]
  348.      *
  349.      * @param left Left Integrand Quadrature Limit
  350.      * @param right Right Integrand Quadrature Limit
  351.      *
  352.      * @return The Three Point Gauss Legendre Quadrature over [a, b] onto [-1, +1]
  353.      */

  354.     public static final org.drip.numerical.integration.QuadratureEstimator ThreePoint (
  355.         final double left,
  356.         final double right)
  357.     {
  358.         return ThreePoint (
  359.             org.drip.numerical.integration.AbscissaTransform.DisplaceAndScaleMinusOne_PlusOne (
  360.                 left,
  361.                 right
  362.             )
  363.         );
  364.     }

  365.     /**
  366.      * Generate the Four Point Gauss Legendre Quadrature over [a, b] onto [-1, +1]
  367.      *
  368.      * @param left Left Integrand Quadrature Limit
  369.      * @param right Right Integrand Quadrature Limit
  370.      *
  371.      * @return The Four Point Gauss Legendre Quadrature over [a, b] onto [-1, +1]
  372.      */

  373.     public static final org.drip.numerical.integration.QuadratureEstimator FourPoint (
  374.         final double left,
  375.         final double right)
  376.     {
  377.         return FourPoint (
  378.             org.drip.numerical.integration.AbscissaTransform.DisplaceAndScaleMinusOne_PlusOne (
  379.                 left,
  380.                 right
  381.             )
  382.         );
  383.     }

  384.     /**
  385.      * Generate the Five Point Gauss Legendre Quadrature over [a, b] onto [-1, +1]
  386.      *
  387.      * @param left Left Integrand Quadrature Limit
  388.      * @param right Right Integrand Quadrature Limit
  389.      *
  390.      * @return The Five Point Gauss Legendre Quadrature over [a, b] onto [-1, +1]
  391.      */

  392.     public static final org.drip.numerical.integration.QuadratureEstimator FivePoint (
  393.         final double left,
  394.         final double right)
  395.     {
  396.         return FivePoint (
  397.             org.drip.numerical.integration.AbscissaTransform.DisplaceAndScaleMinusOne_PlusOne (
  398.                 left,
  399.                 right
  400.             )
  401.         );
  402.     }
  403. }