Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
utils_polynomials.py
1import numpy as np
2
3# from numba import njit
4from scipy.special import legendre
5
6
7def legendre_coeffs(kpol):
8 """
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.
11
12 Parameters
13 ----------
14 kpol : scalar, type -> float
15 degree of polynomials for approximation
16
17 Returns
18 ----------
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
21 """
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]
25 return mat_coeffs_leg
26
27
28# @njit
29def phi_pol(pol_coeffs, x):
30 """
31 Evaluate polynomial sum_{i=0}^{k} a_i x^i by Horner's method
32
33 Parameters
34 ----------
35 pol_coeffs : 1D array (dim = k+1), type -> float
36 coefficients of polynomial of order k sort from low to high orders
37
38 x : scalar, type -> float
39 value to evaluate the polynomial
40
41 Returns
42 ----------
43 result : scalar, type -> float
44 evaluation of polynomial of order k at x
45
46 """
47
48 result = 0.0
49 for c in pol_coeffs[::-1]:
50 result = result * x + c
51 return result
52
53
54# @njit
55def polynomial_derivative_coeffs(k, pol_coeffs):
56 """
57 Compute coefficients for the derivative of polynomial with coeff pol_coeffs.
58
59 Parameters
60 ----------
61 k : scalar, type -> integer
62 order of polynomials
63 pol_coeffs : 1D array (dim = k+1), type -> float
64 coefficients of polynomial of order k
65
66 Returns
67 ----------
68 dcoeffs: 1D array (dim = k), type -> float
69 coefficients of the derivative of polynomial of order k
70
71 """
72
73 if k == 0:
74 return np.zeros(1)
75 dcoeffs = np.zeros(k)
76 for i in range(1, k + 1):
77 dcoeffs[i - 1] = i * pol_coeffs[i]
78 return dcoeffs
79
80
81# @njit
82def dphik(ak, hj, xij):
83 """
84 Derivative of P_k(xij) with respect to x, where xij = 2/hj*(x-xj)
85
86 Parameters
87 ----------
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
91 width of the bin
92 xij : scalar, type -> float
93 variable mapping the mass bin j in [-1,1], needed for Legendre polynomials
94
95 Returns
96 ----------
97 Evaluation at xij of the derivative of P_k(xij) with respect to x
98
99 """
100 k = len(ak) - 1
101 d_coeffs = polynomial_derivative_coeffs(k, ak)
102 return phi_pol(d_coeffs, xij) * (2.0 / hj)
103
104
105def coeff_norm_leg(k):
106 """
107 Compute the normalisation coefficient of a Legendre polynomial with same order k
108
109 Parameters
110 ----------
111 k : scalar, type -> integer
112 degree of Legendre polynomials
113 .
114
115 Returns
116 -------
117 type -> float
118 normalisation coefficient
119 """
120 return 2.0 / (2.0 * k + 1.0)