Class LegendreShifted

java.lang.Object
com.polytechnik.utils.LegendreShifted
All Implemented Interfaces:
BasisFunctionsCalculatable, BasisFunctionsMultipliable, BasisPolynomials, ConfederateMatrixCalculatable, SimpleBasisPolynomials

public class LegendreShifted extends Object implements BasisPolynomials
Legendre polynomials shifted to [0:1] interval.
  • Field Details

  • Constructor Details

    • LegendreShifted

      public LegendreShifted()
  • Method Details

    • getNext

      public double[] getNext(int n, double[] Pnm1, double[] Pnm2, boolean flag_type2, BasisFunctionsCalculatable PBASIS)
      Calculates n-th Legendre polynomial shifted to [0,1]. Uses: \[ nP_n=2*(2n-1)xP_{n-1}-(2n-1)P_{n-1}-(n-1)P_{n-2} \]
      Specified by:
      getNext in interface BasisPolynomials
      Parameters:
      n - P index.
      Pnm1 - Input \( P_{n-1} \).
      Pnm2 - Input \( P_{n-2} \).
      flag_type2 - Whether to use type 2 polynomials.
      PBASIS - The basis in which to calculte.
      Returns:
      \( P_{n} \) polynomial, array dimension n+1.
    • getBasisFunctionsOnPolynomialArgument

      public double[][] getBasisFunctionsOnPolynomialArgument(int numQl, double[] pol, BasisFunctionsCalculatable PBASIS)
      Description copied from interface: SimpleBasisPolynomials
      This method calculates \( Q_l(pol(x))\), where pol(x) is a given polynomial and \( Q_l \) is basis finction of this (not PBASIS!) basis. This method may not be stable for a large dimensions of pol.length. Equivalent to numQl applications of SimpleBasisPolynomials.getNext(int, double[], double[], com.polytechnik.utils.BasisFunctionsCalculatable) to pol array. The method can be used to calculate e.g. \( T_n(P_k(x)) \) or \( T_n(P_k(L_m(x^j))) \). For example
      
      new Legendre().getBasisFunctionsOnPolynomialArgument(4,new double []{0,0,0,0,1},new Chebyshev())
      ={
      { 1.0 },
      { 0.0, 0.0, 0.0, 0.0, 1.0 },
      { 0.25, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.75 },
      { 0.0, 0.0, 0.0, 0.0, 0.375, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.625 }
      }
      
      means
      \( P_0(T_4(x))=T_0 \)
      \( P_1(T_4(x))=T_4(x) \)
      \( P_2(T_4(x))=0.25*T_0+0.75*T_8(x) \)
      \( P_3(T_4(x))=0.375*T_4(x)+0.625*T_{12}(x) \).
      Specified by:
      getBasisFunctionsOnPolynomialArgument in interface SimpleBasisPolynomials
      Parameters:
      numQl - The number of basis functions to calculate.
      pol - The polynomial to use in \( Q_l(pol(x))\), it is given in the PBASIS basis.
      PBASIS - the basis in which pol is given and the result is returned.
      Returns:
      An array of numQl dimension, elements: \( Q_0(pol(x)) \), \( Q_1(pol(x)) \), ... \( Q_{numQl-1}(pol(x)) \) in the basis PBASIS.
    • calculate

      public double calculate(int n, double x)
      Calculation of the value of n-th Legendre polynomial shifted to [0,1].
      Specified by:
      calculate in interface BasisFunctionsCalculatable
      Parameters:
      n - P index.
      x - Argument.
      Returns:
      The value of the Legendre shifted polynomial Pn at x.
    • getMomentsFromSample

      public double[] getMomentsFromSample(int nmoments, double[] x, double[] w, int nelsinsample)
      Description copied from interface: BasisFunctionsCalculatable
      Given observations x and weights w calculate the 0..nmoms-1 moments in the basis.
      Specified by:
      getMomentsFromSample in interface BasisFunctionsCalculatable
      Parameters:
      nmoments - The number of moments to calculate.
      x - observation samples.
      w - weights.
      nelsinsample - How many elements to sample.
      Returns:
      nelsinsample generalzied moments, \( k=0\dots nmoms-1 \): \( \sum_{j=0}^{nelsinsample-1} Q_k(x[j])*w[j] \).
    • getD0

      public double getD0(double[] Pcoefs, double x)
      Calculates \( \sum_k P_k(x) Pcoefs[k] \) using Clenshaw recurrence formula.
      Specified by:
      getD0 in interface BasisFunctionsCalculatable
      Parameters:
      Pcoefs - Coefficients.
      x - Pont to estimate the sum.
      Returns:
      The sum.
    • getD1

      public double getD1(double[] Pcoefs, double x)
      Calculates \( d/dx \sum_k P_k(x) Pcoefs[k] \) using Clenshaw recurrence formula.
      Specified by:
      getD1 in interface BasisFunctionsCalculatable
      Parameters:
      Pcoefs - Coefficients.
      x - Pont to estimate the sum.
      Returns:
      The derivative.
    • getD2

      public double getD2(double[] Pcoefs, double x)
      Calculates \( d^2/dx^2 \sum_k P_k(x) Pcoefs[k] \) using Clenshaw recurrence formula.
      Specified by:
      getD2 in interface BasisFunctionsCalculatable
      Parameters:
      Pcoefs - Coefficients.
      x - Pont to estimate the sum.
      Returns:
      The derivative.
    • getDifferentiated

      public double[] getDifferentiated(double[] Pcoefs)
      Description copied from interface: BasisFunctionsCalculatable
      Differentiate a function in basis,
      Specified by:
      getDifferentiated in interface BasisFunctionsCalculatable
      Parameters:
      Pcoefs - : \( F(x)=\sum_k Q_k(x) Qcoefs[k] \).
      Returns:
      \( dF/dx=\sum_k Q'_k(x) Qcoefs[k] \).
    • getIntegrated

      public double[] getIntegrated(double[] Pcoefs)
      Integrate shifted Legendre polynomial. Prudnikov, Brychkov, Marichev, volume 2, 2003, formulae 1.14.1.1 page 46.
      Specified by:
      getIntegrated in interface BasisFunctionsCalculatable
      Parameters:
      Pcoefs - : \( F(x)=\sum_k Q_k(x) Qcoefs[k] \).
      Returns:
      \( \int F(x)dx=\sum_k Qcoefs[k] \int Q_k(x) dx \). The integral is typically normalized as I(0)=0.
    • setQlQmExpansionCoefs

      public void setQlQmExpansionCoefs(int l, int m, double[] s)
      Expand \( P_l(z)P_m(z)=\sum_{k=0}^{l+m} s_k P_k(z) \), formulae 8.915.5, A(9036), page 1040, Gradshtein & Ryzhik 1963. The formulae of Neumann-Adams, see W. N. Bailey, Generalized hypergeometric series, p.101, Cambridge, 1962.
      Specified by:
      setQlQmExpansionCoefs in interface BasisFunctionsMultipliable
      Parameters:
      l - l.
      m - m.
      s - coefs.
    • getaxbP

      public double[] getaxbP(double[] p, double ma, double mb)
      Multiply p by ma*x+mb. Calculations in Shifted Legendre basis. Use Legendre recurrence, see formulae 8.914.1, UVII99, page 1040, Gradshtein & Ryzhik 1963. Do not mistake with a and b recurrence coefficiemts of getAB().
      Specified by:
      getaxbP in interface BasisFunctionsCalculatable
      Parameters:
      p - Input polynomial.
      Returns:
      p*(ma*x+mb), an array of the length 1+p.length.
    • sdiv1

      public SDivRes sdiv1(double[] p, double x0)
      Divide p by (x-x0), returns quotient and remainder. Calculations in Shifted Legendre basis. Use Legendre recurrence, see formulae 8.914.1, UVII99, page 1040, Gradshtein & Ryzhik 1963.
      Specified by:
      sdiv1 in interface BasisFunctionsCalculatable
      Parameters:
      p - Input polynomial.
      x0 - To divide on (x-x0)
      Returns:
      Quotent and remainder.
    • setNewtonBinomialLikeCoefs

      public void setNewtonBinomialLikeCoefs(int n, double[] b, double[] q, double a, double x0)
      Calculates P_n(a(x+x0)) using 3 term recurrence Calculations in Shifted Legendre basis. The b_k are defined as: \( P_n(a(x+x0))=\sum_{k=0}^{k=n}b_k^{n} P_k(x) \) The method should be applied consequently to get \( b_k^{n} \) for n=0,1,2,3,... Uses Legendre recurrence, see formulae 8.914.1, UVII99, page 1040, Gradshtein & Ryzhik 1963.
      Specified by:
      setNewtonBinomialLikeCoefs in interface BasisPolynomials
      Parameters:
      n - The max dimension of b.
      b - On input: \( b_k^{n-1} \), on output \( b_k^{n} \)
      q - On input: \( b_k^{n-2} \), on output: \( b_k^{n-1} \).
    • getXQkMomentsFromQkMoments

      public double[] getXQkMomentsFromQkMoments(double[] moments)
      Calculate <F|xPk> moments for k=0..n-1 from <F|Pk>, k=0..n.
      Specified by:
      getXQkMomentsFromQkMoments in interface BasisFunctionsCalculatable
      Parameters:
      moments - <F|Pk>.
      Returns:
      <F|xPk>.
    • getConfederateMatrix

      public double[] getConfederateMatrix(double[] coefs)
      Calculates confederate matrix to solve polynomial equations in Legendre basis directly. We use the three term recurrence: \[ P_k=(x*2*(2*k-1)/k-(2*k-1)/k)*P_{k-1}-((k-1)/k)*P_{k-2} \] Use ABASISLEGENDRESHIFTED to calculate roots.
      Specified by:
      getConfederateMatrix in interface ConfederateMatrixCalculatable
    • get2LegendreAveraged

      public static double get2LegendreAveraged(double[] p1, double[] p2)
    • getXPnprimeplusPnCoefs

      private static double[] getXPnprimeplusPnCoefs(int Pn_number)
      Generating expansion coefficients xP'n+Pn=(n+1)Pn+sum_{k=0}^{k=n-1}(2k+1)P_k
    • getXDiffFplusF

      public static double[] getXDiffFplusF(double[] Pcoefs)
      Calculates xF'+F.
      Parameters:
      Pcoefs - ,input,
      Returns:
      xF'+F
    • getXPnprimeplusPnMoments

      private static double[] getXPnprimeplusPnMoments(double[] Pcoefs)
      Calculate <F|Pn+xP'n> moments. Same as getXPnprimeplusPnMomentsFromPnMoments(double[]) but uses extra (2k+1) normalizing factor.
      Parameters:
      Pcoefs - Input, Pcoefs[k]=(2*k+1)<F|Pk>, k=0..n.
      Returns:
      (2*k+1)<F|Pk+xP'k>, k=0..n.
    • getXPnprimeplusPnMomentsFromPnMoments

      public static double[] getXPnprimeplusPnMomentsFromPnMoments(double[] moments)
      Calculate <F|Pn+xP'n> moments.
      Parameters:
      moments - Input, moments[k]=<F|Pk>, k=0..n.
      Returns:
      <F|Pk+xP'k>, k=0..n.
    • get1mXPnMomentsFromPnMoments

      public static double[] get1mXPnMomentsFromPnMoments(double[] moments)
      Calculate <F|(1-x)Pk> moments for k=0..n-1 from <F|Pk>, k=0..n
      Parameters:
      moments - Input, moments[k]=<F|Pk>, k=0..n;
      Returns:
      <F|(1-x)Pk>, k=0..n-1;
    • getXDiffF

      public static double[] getXDiffF(double[] Pcoefs)
      Calculate xPcoefs'(x) in Legendre shifted basis.
      Parameters:
      Pcoefs - .
      Returns:
      xPcoefs'.
    • setQlQmExpansionCoefs_test

      public static void setQlQmExpansionCoefs_test(int l, int m, double[] s)
      The same as setQlQmExpansionCoefs, but uses 3 term recursion instead of hypergeometric function expansion. It is rather slow at large l,m. Used for unit test.
    • sumQlQmExpansionCoefs

      private static void sumQlQmExpansionCoefs(int l, int m, double[] s, double c)
    • getxP

      public static double[] getxP(double[] p)
      Multiply p by x. Calculations in Shifted Legendre basis. Use Legendre recurrence, see formulae 8.914.1, UVII99, page 1040, Gradshtein & Ryzhik 1963.
      Parameters:
      p - Input polynomial.
      Returns:
      x*p
    • get1mxP

      public static double[] get1mxP(double[] p)
      Multiply p by (1-x). Calculations in Shifted Legendre basis. Use Legendre recurrence, see formulae 8.914.1, UVII99, page 1040, Gradshtein & Ryzhik 1963.
      Parameters:
      p - Input polynomial.
      Returns:
      (1-x)*p
    • getRoots

      static double[] getRoots(int n, double x0, boolean flag_radau)
      A different way to calculate roots. Use Jacobi matrix.
      Parameters:
      n - The Laguerre polynomial order.
      x0 - Radau edge, ignored if flag_radau=false;
      flag_radau - Whether to use Radau polynomial.
      Returns:
      The roots.
    • getRoots

      public static double[] getRoots(int n)
    • getRadauRoots

      public static double[] getRadauRoots(int n)
    • getWeights

      public static double[] getWeights(double[] xx)
      Uses Hildebrand, F. B. "Introduction to Numerical Analysis." New York: McGraw-Hill, pp. 323-325, 1956.
    • getNodesPolynomial

      public static double[] getNodesPolynomial(int n)
    • getRadauWeights

      public static double[] getRadauWeights(double[] xr)
      Radau exact weights from Walter Gautschi, "Gauss-Radau formulae for Jacobi and Laguerre weight functions".
    • getRadauNodesPolynomial

      public static double[] getRadauNodesPolynomial(int n)
    • getChristoffelKernel

      public static double[] getChristoffelKernel(int n)
      Christoffel function.
    • main

      public static void main(String[] args)
      A unit test.