IntegrandGenerator.java

  1. package org.drip.numerical.quadrature;

  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>IntegrandGenerator</i> contains the Settings that enable the Generation of Integrand Quadrature and
  76.  * Weights for the Specified Orthogonal Polynomial Scheme. 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/quadrature/README.md">R<sup>1</sup> Gaussian Integration Quadrature Schemes</a></li>
  107.  *  </ul>
  108.  *
  109.  * @author Lakshmi Krishnamurthy
  110.  */

  111. public class IntegrandGenerator
  112. {
  113.     private double _lowerBound = java.lang.Double.NaN;
  114.     private double _upperBound = java.lang.Double.NaN;
  115.     private org.drip.function.definition.R1ToR1 _weightFunction = null;
  116.     private org.drip.numerical.quadrature.OrthogonalPolynomialSuite _orthogonalPolynomialSuite = null;

  117.     /**
  118.      * Construct the Gauss-Legendre Integrand Quadrature Generator
  119.      *
  120.      * @param orthogonalPolynomialSuite Orthogonal Polynomial Suite
  121.      *
  122.      * @return The Gauss-Legendre Integrand Quadrature Generator
  123.      */

  124.     public static final IntegrandGenerator GaussLegendre (
  125.         final org.drip.numerical.quadrature.OrthogonalPolynomialSuite orthogonalPolynomialSuite)
  126.     {
  127.         try
  128.         {
  129.             return new IntegrandGenerator (
  130.                 orthogonalPolynomialSuite,
  131.                 org.drip.numerical.quadrature.WeightFunctionBuilder.Legendre(),
  132.                 -1.,
  133.                 1.
  134.             );
  135.         }
  136.         catch (java.lang.Exception e)
  137.         {
  138.             e.printStackTrace();
  139.         }

  140.         return null;
  141.     }

  142.     /**
  143.      * Construct the Gauss-Jacobi Integrand Quadrature Generator
  144.      *
  145.      * @param orthogonalPolynomialSuite Orthogonal Polynomial Suite
  146.      * @param alpha Jacobi Alpha
  147.      * @param beta Jacobi Beta
  148.      *
  149.      * @return The Gauss-Jacobi Integrand Quadrature Generator
  150.      */

  151.     public static final IntegrandGenerator GaussJacobi (
  152.         final org.drip.numerical.quadrature.OrthogonalPolynomialSuite orthogonalPolynomialSuite,
  153.         final double alpha,
  154.         final double beta)
  155.     {
  156.         try
  157.         {
  158.             return new IntegrandGenerator (
  159.                 orthogonalPolynomialSuite,
  160.                 org.drip.numerical.quadrature.WeightFunctionBuilder.Jacobi (
  161.                     alpha,
  162.                     beta
  163.                 ),
  164.                 -1.,
  165.                 1.
  166.             );
  167.         }
  168.         catch (java.lang.Exception e)
  169.         {
  170.             e.printStackTrace();
  171.         }

  172.         return null;
  173.     }

  174.     /**
  175.      * Construct the Gauss-Chebyshev (Second-Kind) Integrand Quadrature Generator
  176.      *
  177.      * @param orthogonalPolynomialSuite Orthogonal Polynomial Suite
  178.      *
  179.      * @return The Gauss-Chebyshev (Second-Kind) Integrand Quadrature Generator
  180.      */

  181.     public static final IntegrandGenerator GaussChebyshevSecondKind (
  182.         final org.drip.numerical.quadrature.OrthogonalPolynomialSuite orthogonalPolynomialSuite)
  183.     {
  184.         try
  185.         {
  186.             return new IntegrandGenerator (
  187.                 orthogonalPolynomialSuite,
  188.                 org.drip.numerical.quadrature.WeightFunctionBuilder.ChebyshevSecondKind(),
  189.                 -1.,
  190.                 1.
  191.             );
  192.         }
  193.         catch (java.lang.Exception e)
  194.         {
  195.             e.printStackTrace();
  196.         }

  197.         return null;
  198.     }

  199.     /**
  200.      * Construct the Gauss-Chebyshev (First-Kind) Integrand Quadrature Generator
  201.      *
  202.      * @param orthogonalPolynomialSuite Orthogonal Polynomial Suite
  203.      *
  204.      * @return The Gauss-Chebyshev (First-Kind) Integrand Quadrature Generator
  205.      */

  206.     public static final IntegrandGenerator GaussChebyshevFirstKind (
  207.         final org.drip.numerical.quadrature.OrthogonalPolynomialSuite orthogonalPolynomialSuite)
  208.     {
  209.         try
  210.         {
  211.             return new IntegrandGenerator (
  212.                 orthogonalPolynomialSuite,
  213.                 org.drip.numerical.quadrature.WeightFunctionBuilder.ChebyshevFirstKind(),
  214.                 -1.,
  215.                 1.
  216.             );
  217.         }
  218.         catch (java.lang.Exception e)
  219.         {
  220.             e.printStackTrace();
  221.         }

  222.         return null;
  223.     }

  224.     /**
  225.      * Construct the Gauss-Laguerre Integrand Quadrature Generator
  226.      *
  227.      * @param orthogonalPolynomialSuite Orthogonal Polynomial Suite
  228.      *
  229.      * @return The Gauss-Laguerre Integrand Quadrature Generator
  230.      */

  231.     public static final IntegrandGenerator GaussLaguerre (
  232.         final org.drip.numerical.quadrature.OrthogonalPolynomialSuite orthogonalPolynomialSuite)
  233.     {
  234.         try
  235.         {
  236.             return new IntegrandGenerator (
  237.                 orthogonalPolynomialSuite,
  238.                 org.drip.numerical.quadrature.WeightFunctionBuilder.Laguerre(),
  239.                 -1.,
  240.                 1.
  241.             );
  242.         }
  243.         catch (java.lang.Exception e)
  244.         {
  245.             e.printStackTrace();
  246.         }

  247.         return null;
  248.     }

  249.     /**
  250.      * Construct the Generalized Gauss-Laguerre Integrand Quadrature Generator
  251.      *
  252.      * @param orthogonalPolynomialSuite Orthogonal Polynomial Suite
  253.      * @param alpha Generalized Laguerre Alpha
  254.      *
  255.      * @return The Generalized Gauss-Laguerre Integrand Quadrature Generator
  256.      */

  257.     public static final IntegrandGenerator GeneralizedGaussLaguerre (
  258.         final org.drip.numerical.quadrature.OrthogonalPolynomialSuite orthogonalPolynomialSuite,
  259.         final double alpha)
  260.     {
  261.         try
  262.         {
  263.             return new IntegrandGenerator (
  264.                 orthogonalPolynomialSuite,
  265.                 org.drip.numerical.quadrature.WeightFunctionBuilder.GeneralizedLaguerre (alpha),
  266.                 -1.,
  267.                 1.
  268.             );
  269.         }
  270.         catch (java.lang.Exception e)
  271.         {
  272.             e.printStackTrace();
  273.         }

  274.         return null;
  275.     }

  276.     /**
  277.      * Construct the Gauss-Hermite Integrand Quadrature Generator
  278.      *
  279.      * @param orthogonalPolynomialSuite Orthogonal Polynomial Suite
  280.      *
  281.      * @return The Gauss-Hermite Integrand Quadrature Generator
  282.      */

  283.     public static final IntegrandGenerator GaussHermite (
  284.         final org.drip.numerical.quadrature.OrthogonalPolynomialSuite orthogonalPolynomialSuite)
  285.     {
  286.         try
  287.         {
  288.             return new IntegrandGenerator (
  289.                 orthogonalPolynomialSuite,
  290.                 org.drip.numerical.quadrature.WeightFunctionBuilder.Hermite(),
  291.                 -1.,
  292.                 1.
  293.             );
  294.         }
  295.         catch (java.lang.Exception e)
  296.         {
  297.             e.printStackTrace();
  298.         }

  299.         return null;
  300.     }

  301.     /**
  302.      * IntegrandGenerator Constructor
  303.      *
  304.      * @param orthogonalPolynomialSuite Orthogonal Polynomial Suite
  305.      * @param weightFunction Weight Function
  306.      * @param lowerBound Lower Bound
  307.      * @param upperBound Upper Bound
  308.      *
  309.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  310.      */

  311.     public IntegrandGenerator (
  312.         final org.drip.numerical.quadrature.OrthogonalPolynomialSuite orthogonalPolynomialSuite,
  313.         final org.drip.function.definition.R1ToR1 weightFunction,
  314.         final double lowerBound,
  315.         final double upperBound)
  316.         throws java.lang.Exception
  317.     {
  318.         if (null == (_orthogonalPolynomialSuite = orthogonalPolynomialSuite) ||
  319.             null == (_weightFunction = weightFunction) ||
  320.             !org.drip.numerical.common.NumberUtil.IsValid (_lowerBound = lowerBound) ||
  321.             !org.drip.numerical.common.NumberUtil.IsValid (_upperBound = upperBound) ||
  322.                 _lowerBound >= _upperBound)
  323.         {
  324.             throw new java.lang.Exception ("IntegrandGenerator Constructor => Invalid Inputs");
  325.         }
  326.     }

  327.     /**
  328.      * Retrieve the Orthogonal Polynomial Suite
  329.      *
  330.      * @return The Orthogonal Polynomial Suite
  331.      */

  332.     public org.drip.numerical.quadrature.OrthogonalPolynomialSuite orthogonalPolynomialSuite()
  333.     {
  334.         return _orthogonalPolynomialSuite;
  335.     }

  336.     /**
  337.      * Retrieve the Weight Function
  338.      *
  339.      * @return The Weight Function
  340.      */

  341.     public org.drip.function.definition.R1ToR1 weightFunction()
  342.     {
  343.         return _weightFunction;
  344.     }

  345.     /**
  346.      * Retrieve the Lower Integration Bound
  347.      *
  348.      * @return The Lower Integration Bound
  349.      */

  350.     public double lowerBound()
  351.     {
  352.         return _lowerBound;
  353.     }

  354.     /**
  355.      * Retrieve the Upper Integration Bound
  356.      *
  357.      * @return The Upper Integration Bound
  358.      */

  359.     public double upperBound()
  360.     {
  361.         return _upperBound;
  362.     }

  363.     /**
  364.      * Generate the Integral of the Weight Function Over the Bounds
  365.      *
  366.      * @return The Integral of the Weight Function Over the Bounds
  367.      *
  368.      * @throws java.lang.Exception Thrown if it cannot be computed
  369.      */

  370.     public double weightFunctionIntegral()
  371.         throws java.lang.Exception
  372.     {
  373.         return _weightFunction.integrate (
  374.             _lowerBound,
  375.             _upperBound
  376.         );
  377.     }

  378.     /**
  379.      * Generate the Weight at the specified Node for the specified Orthogonal Polynomial
  380.      *
  381.      * @param x X Node
  382.      * @param degree Orthogonal Polynomial Degree
  383.      *
  384.      * @return The Weight at the specified Node for the specified Orthogonal Polynomial
  385.      *
  386.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  387.      */

  388.     public double nodeWeight (
  389.         final double x,
  390.         final int degree)
  391.         throws java.lang.Exception
  392.     {
  393.         if (!org.drip.numerical.common.NumberUtil.IsValid (x))
  394.         {
  395.             throw new java.lang.Exception ("IntegrandGenerator::nodeWeight => Invalid Inputs");
  396.         }

  397.         if (0 > degree)
  398.         {
  399.             return 0.;
  400.         }

  401.         final org.drip.numerical.quadrature.OrthogonalPolynomial orthogonalPolynomialN =
  402.             _orthogonalPolynomialSuite.orthogonalPolynomial (degree);

  403.         final org.drip.numerical.quadrature.OrthogonalPolynomial orthogonalPolynomialNMinusOne =
  404.             _orthogonalPolynomialSuite.orthogonalPolynomial (degree - 1);

  405.         if (null == orthogonalPolynomialN || null == orthogonalPolynomialNMinusOne)
  406.         {
  407.             throw new java.lang.Exception ("IntegrandGenerator::nodeWeight => Invalid Inputs");
  408.         }

  409.         double weightIntegrand = new org.drip.function.definition.R1ToR1 (null)
  410.         {
  411.             @Override public double evaluate (
  412.                 final double z)
  413.                 throws java.lang.Exception
  414.             {
  415.                 double pNMinusOne = orthogonalPolynomialNMinusOne.evaluate (z);

  416.                 return _weightFunction.evaluate (z) * pNMinusOne * pNMinusOne;
  417.             }
  418.         }.integrate (
  419.             _lowerBound,
  420.             _upperBound
  421.         );

  422.         return orthogonalPolynomialN.degreeCoefficient() * weightIntegrand / (
  423.             orthogonalPolynomialNMinusOne.degreeCoefficient() *
  424.             orthogonalPolynomialNMinusOne.evaluate (x) *
  425.             orthogonalPolynomialN.derivative (
  426.                 x,
  427.                 1
  428.             )
  429.         );
  430.     }

  431.     /**
  432.      * Compute the Loaded Inner Product between the Polynomial identified by their Degrees
  433.      *
  434.      * @param degree1 Polynomial Degree #1
  435.      * @param degree2 Polynomial Degree #2
  436.      *
  437.      * @return The Loaded Inner Product
  438.      *
  439.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  440.      */

  441.     public double loadedInnerProduct (
  442.         final int degree1,
  443.         final int degree2)
  444.         throws java.lang.Exception
  445.     {
  446.         if (0 > degree1 || 0 > degree2)
  447.         {
  448.             return 0.;
  449.         }

  450.         final org.drip.numerical.quadrature.OrthogonalPolynomial orthogonalPolynomial1 =
  451.             _orthogonalPolynomialSuite.orthogonalPolynomial (degree1);

  452.         final org.drip.numerical.quadrature.OrthogonalPolynomial orthogonalPolynomial2 =
  453.             _orthogonalPolynomialSuite.orthogonalPolynomial (degree2);

  454.         if (null == orthogonalPolynomial1 || null == orthogonalPolynomial2)
  455.         {
  456.             throw new java.lang.Exception ("IntegrandGenerator::loadedInnerProduct => Invalid Inputs");
  457.         }

  458.         return new org.drip.function.definition.R1ToR1 (null)
  459.         {
  460.             @Override public double evaluate (
  461.                 final double z)
  462.                 throws java.lang.Exception
  463.             {
  464.                 return z * _weightFunction.evaluate (z) * orthogonalPolynomial1.evaluate (z) *
  465.                     orthogonalPolynomial2.evaluate (z);
  466.             }
  467.         }.integrate (
  468.             _lowerBound,
  469.             _upperBound
  470.         );
  471.     }

  472.     /**
  473.      * Compute the Unloaded Inner Product between the Polynomial identified by their Degrees
  474.      *
  475.      * @param degree1 Polynomial Degree #1
  476.      * @param degree2 Polynomial Degree #2
  477.      *
  478.      * @return The Unloaded Inner Product
  479.      *
  480.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  481.      */

  482.     public double unloadedInnerProduct (
  483.         final int degree1,
  484.         final int degree2)
  485.         throws java.lang.Exception
  486.     {
  487.         if (0 > degree1 || 0 > degree2)
  488.         {
  489.             return 0.;
  490.         }

  491.         final org.drip.numerical.quadrature.OrthogonalPolynomial orthogonalPolynomial1 =
  492.             _orthogonalPolynomialSuite.orthogonalPolynomial (degree1);

  493.         final org.drip.numerical.quadrature.OrthogonalPolynomial orthogonalPolynomial2 =
  494.             _orthogonalPolynomialSuite.orthogonalPolynomial (degree2);

  495.         if (null == orthogonalPolynomial1 || null == orthogonalPolynomial2)
  496.         {
  497.             throw new java.lang.Exception ("IntegrandGenerator::unloadedInnerProduct => Invalid Inputs");
  498.         }

  499.         return new org.drip.function.definition.R1ToR1 (null)
  500.         {
  501.             @Override public double evaluate (
  502.                 final double z)
  503.                 throws java.lang.Exception
  504.             {
  505.                 return _weightFunction.evaluate (z) * orthogonalPolynomial1.evaluate (z) *
  506.                     orthogonalPolynomial2.evaluate (z);
  507.             }
  508.         }.integrate (
  509.             _lowerBound,
  510.             _upperBound
  511.         );
  512.     }

  513.     /**
  514.      * Generate the Golub-Welsch Matrix A Entry
  515.      *
  516.      * @param degree The Orthogonal Polynomial Degree
  517.      *
  518.      * @return The Golub-Welsch Matrix A Entry
  519.      *
  520.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  521.      */

  522.     public double golubWelschA (
  523.         final int degree)
  524.         throws java.lang.Exception
  525.     {
  526.         return loadedInnerProduct (
  527.             degree,
  528.             degree
  529.         ) / unloadedInnerProduct (
  530.             degree,
  531.             degree
  532.         );
  533.     }

  534.     /**
  535.      * Generate the Golub-Welsch Matrix B Entry
  536.      *
  537.      * @param degree The Orthogonal Polynomial Degree
  538.      *
  539.      * @return The Golub-Welsch Matrix B Entry
  540.      *
  541.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  542.      */

  543.     public double golubWelschB (
  544.         final int degree)
  545.         throws java.lang.Exception
  546.     {
  547.         return unloadedInnerProduct (
  548.             degree,
  549.             degree
  550.         ) / unloadedInnerProduct (
  551.             degree - 1,
  552.             degree - 1
  553.         );
  554.     }

  555.     /**
  556.      * Generate the Cross Polynomial Recurrence Matrix to be used in the Golub-Welsch Algorithm
  557.      *
  558.      * @return The Cross Polynomial Recurrence Matrix to be used in the Golub-Welsch Algorithm
  559.      */

  560.     public org.drip.numerical.quadrature.GolubWelsch generateRecurrenceMatrix()
  561.     {
  562.         int size = _orthogonalPolynomialSuite.size();

  563.         double[][] golubWelschMatrix = new double[size][size];

  564.         for (int row = 0; row < size; ++row)
  565.         {
  566.             for (int column = 0; column < size; ++column)
  567.             {
  568.                 golubWelschMatrix[row][column] = column == row + 1 ? 1. : 0.;
  569.             }
  570.         }

  571.         try
  572.         {
  573.             for (int row = 0; row < size; ++row)
  574.             {
  575.                 golubWelschMatrix[row][row] = loadedInnerProduct (
  576.                     row,
  577.                     row
  578.                 ) / unloadedInnerProduct (
  579.                     row,
  580.                     row
  581.                 );

  582.                 if (0 < row)
  583.                 {
  584.                     golubWelschMatrix[row][row - 1] = unloadedInnerProduct (
  585.                         row,
  586.                         row
  587.                     ) / unloadedInnerProduct (
  588.                         row - 1,
  589.                         row - 1
  590.                     );
  591.                 }
  592.             }

  593.             return new org.drip.numerical.quadrature.GolubWelsch (golubWelschMatrix);
  594.         }
  595.         catch (java.lang.Exception e)
  596.         {
  597.             e.printStackTrace();
  598.         }

  599.         return null;
  600.     }

  601.     /**
  602.      * Generate the Quadrature Nodes and Scaled Weights Using the Gil, Segura, and Temme (2007) Scheme
  603.      *
  604.      * @return The Quadrature Nodes and Scaled Weights
  605.      */

  606.     public org.drip.numerical.common.Array2D gilSeguraTemme2007()
  607.     {
  608.         org.drip.numerical.quadrature.GolubWelsch golubWelsch = generateRecurrenceMatrix();

  609.         if (null == golubWelsch)
  610.         {
  611.             return null;
  612.         }

  613.         org.drip.numerical.common.Array2D nodesAndUnscaledWeights = golubWelsch.nodesAndUnscaledWeights();

  614.         if (null == nodesAndUnscaledWeights)
  615.         {
  616.             return null;
  617.         }

  618.         double[] unscaledWeightArray = nodesAndUnscaledWeights.y();

  619.         double[] nodeArray = nodesAndUnscaledWeights.x();

  620.         int size = nodeArray.length;
  621.         double[] scaledWeightArray = new double[size];

  622.         try
  623.         {
  624.             double weightFunctionIntegral = weightFunctionIntegral();

  625.             for (int nodeIndex = 0; nodeIndex < size; ++nodeIndex)
  626.             {
  627.                 scaledWeightArray[nodeIndex] = unscaledWeightArray[nodeIndex] * weightFunctionIntegral;
  628.             }
  629.         }
  630.         catch (java.lang.Exception e)
  631.         {
  632.             e.printStackTrace();

  633.             return null;
  634.         }

  635.         return org.drip.numerical.common.Array2D.FromArray (
  636.             nodeArray,
  637.             scaledWeightArray
  638.         );
  639.     }
  640. }