Chi-Tech
legendrepoly.cc
Go to the documentation of this file.
1#include "legendrepoly.h"
2#include <cmath>
3
4#include <algorithm>
5//###################################################################
6/**Provides the function evaluation of the Legendre polynomial
7 * P_N at value x.
8
9 \param N int Order of the Legendre polynomial.
10 \param x double The evaluation point.*/
11double chi_math::Legendre(int N, double x)
12{
13 double Pnm1 = 1;
14 double Pn = x;
15 double Pnp1 = 0;
16
17 if (N==0) {return 1;}
18
19 if (N==1) {return x;}
20
21 for (int n=2;n<=N; n++)
22 {
23 int ns = n-1;
24 Pnp1 = ((2.0*ns+1)/(ns+1.0))*x*Pn - (ns/(ns+1.0))*Pnm1;
25 Pnm1 = Pn;
26 Pn = Pnp1;
27 }
28
29 return Pnp1;
30}
31
32//###################################################################
33/**Provides the function evaluation of the derivative of Pn at value x
34
35 \param N int Order of the Legendre polynomial.
36 \param x double The evaluation point.*/
37double chi_math::dLegendredx(int N, double x)
38{
39 if (N==0) {return 0;}
40
41 if (N==1) {return 1;}
42
43 double retval = (N*x/(x*x-1))*Legendre(N,x);
44 retval-= (N/(x*x-1))*Legendre(N-1,x);
45
46 return retval;
47}
48
49//###################################################################
50/**Provides the function evaluation of the second derivative of Pn at value x
51
52 \param N int Order of the Legendre polynomial.
53 \param x double The evaluation point.*/
54double chi_math::d2Legendredx2(int N, double x)
55{
56 double epsilon = 1.0e-8;
57 if (N==0) {return 0.0;}
58
59 if (N==1) {return 0.0;}
60
61 double xpos = std::min(x+epsilon, 1.0-1.0e-10);
62 double xneg = std::max(x-epsilon,-1.0+1.0e-10);
63 double dx = xpos - xneg;
64
65 double dPdx_pos = dLegendredx(N,xpos);
66 double dPdx_neg = dLegendredx(N,xneg);
67
68 return (dPdx_pos - dPdx_neg)/dx;
69}
double d2Legendredx2(int N, double x)
Definition: legendrepoly.cc:54
double dLegendredx(int N, double x)
Definition: legendrepoly.cc:37
double Legendre(int N, double x)
Definition: legendrepoly.cc:11