In [1]:
import scipy.special
import numpy as np

Special functions

In [2]:
def legendre(z, l, u):
    return (-1)**u * scipy.special.gamma(l + u + 1) / \
           scipy.special.gamma(l - u + 1) * ((1 - z) / (1 + z)) ** (u/2.) * \
           scipy.special.hyp2f1(-l, l + 1, u + 1, (1 - z) / 2.)
In [4]:
import matplotlib.pyplot as plt
zz = np.linspace(-1, 1, 1000)
for i in range(1, 5):
    plt.plot(zz, legendre(zz, i, 0), label=str(i))
plt.legend()
plt.show()
-c:3: RuntimeWarning: divide by zero encountered in divide
In [ ]: