Pour calculer les polynômes de Legendre sous python on peut utiliser le module legendre de scipy, exemple:
Calculer les polynômes de Legendre avec scipy
>>> from scipy.special import legendre>>> n = 3>>> Pn = legendre(n)>>> Pn(0)0.0>>> Pn(0.2)-0.27999999999999997>>> Pn(0.5)-0.43749999999999994>>>
Pour calculer sur un intervalle (ex: de -1 a 1 avec un pas de 0.1) on peut passer par numpy:
>>> from scipy.special import legendre>>> import numpy as np>>> n = 3>>> Pn = legendre(n)>>> x = np.arange(-1,1,0.1)>>> xarray([ -1.00000000e+00, -9.00000000e-01, -8.00000000e-01,-7.00000000e-01, -6.00000000e-01, -5.00000000e-01,-4.00000000e-01, -3.00000000e-01, -2.00000000e-01,-1.00000000e-01, -2.22044605e-16, 1.00000000e-01,2.00000000e-01, 3.00000000e-01, 4.00000000e-01,5.00000000e-01, 6.00000000e-01, 7.00000000e-01,8.00000000e-01, 9.00000000e-01])>>> y = Pn(x)>>> yarray([ -1.00000000e+00, -4.72500000e-01, -8.00000000e-02,1.92500000e-01, 3.60000000e-01, 4.37500000e-01,4.40000000e-01, 3.82500000e-01, 2.80000000e-01,1.47500000e-01, 3.33066907e-16, -1.47500000e-01,-2.80000000e-01, -3.82500000e-01, -4.40000000e-01,-4.37500000e-01, -3.60000000e-01, -1.92500000e-01,8.00000000e-02, 4.72500000e-01])
Tracer les polynômes de Legendre avec matplolib
On peut aussi tracer les polynômes de Legendre avec matplolib:

from scipy.special import legendreimport matplotlib.pyplot as pltimport numpy as npmin = -1.0max = 1.0step = 0.05for n in range(6):Pn = legendre(n)x = np.arange(min,max+step,step)y = Pn(x)plt.plot(x, y)plt.xlim(-1.0,1.0)plt.ylim(-1.0,1.01)plt.savefig('legendre_polynomes.png')plt.show()
Références
| Liens | Site |
|---|---|
| Use Python SciPy to compute the Rodrigues formula P_n(x) (Legendre polynomials) | stackoverflow |
| Polynôme de Legendre | wikipedia |
| Special functions (scipy.special) | scipy |
| scipy.special.legendre | scipy |
| Legendre Module (numpy.polynomial.legendre) | scipy |
