4from scipy.special
import legendre
7def legendre_coeffs(kpol):
9 Returns a matrix (kpol+1,kpol+1) containing on each line the coefficients for each Legendre polynomial,
10 from degree 0 to kpol inclusive. On each line coefficients are ordered from low to high orders.
14 kpol : scalar, type -> float
15 degree of polynomials for approximation
19 mat_coeffs_leg : 2D array (dmin = (kpol+1,kpol+1)), type -> float
20 array containing the coefficients for Legendre polynomials up to degree kpol
22 mat_coeffs_leg = np.zeros((kpol + 1, kpol + 1))
23 for k
in range(kpol + 1):
24 mat_coeffs_leg[k, : k + 1] = legendre(k).c[::-1]
29def phi_pol(pol_coeffs, x):
31 Evaluate polynomial sum_{i=0}^{k} a_i x^i by Horner's method
35 pol_coeffs : 1D array (dim = k+1), type -> float
36 coefficients of polynomial of order k sort from low to high orders
38 x : scalar, type -> float
39 value to evaluate the polynomial
43 result : scalar, type -> float
44 evaluation of polynomial of order k at x
49 for c
in pol_coeffs[::-1]:
50 result = result * x + c
55def polynomial_derivative_coeffs(k, pol_coeffs):
57 Compute coefficients for the derivative of polynomial with coeff pol_coeffs.
61 k : scalar, type -> integer
63 pol_coeffs : 1D array (dim = k+1), type -> float
64 coefficients of polynomial of order k
68 dcoeffs: 1D array (dim = k), type -> float
69 coefficients of the derivative of polynomial of order k
76 for i
in range(1, k + 1):
77 dcoeffs[i - 1] = i * pol_coeffs[i]
82def dphik(ak, hj, xij):
84 Derivative of P_k(xij) with respect to x, where xij = 2/hj*(x-xj)
88 ak : 1D array (dim = k+1), type -> float
89 coefficients of polynomial of order k, sorted from low to high order
90 hj : scalar, type -> float
92 xij : scalar, type -> float
93 variable mapping the mass bin j in [-1,1], needed for Legendre polynomials
97 Evaluation at xij of the derivative of P_k(xij) with respect to x
101 d_coeffs = polynomial_derivative_coeffs(k, ak)
102 return phi_pol(d_coeffs, xij) * (2.0 / hj)
105def coeff_norm_leg(k):
107 Compute the normalisation coefficient of a Legendre polynomial with same order k
111 k : scalar, type -> integer
112 degree of Legendre polynomials
118 normalisation coefficient
120 return 2.0 / (2.0 * k + 1.0)