def Zlm(l, m, th, ph): from sympy import simplify if m > 0: zz = C.NegativeOne()**m*(Ylm(l, m, th, ph) + Ylm_c(l, m, th, ph))/sqrt(2) elif m == 0: return Ylm(l, m, th, ph) else: zz = C.NegativeOne()**m*(Ylm(l, -m, th, ph) - Ylm_c(l, -m, th, ph))/(I*sqrt(2)) zz = zz.expand(complex=True) zz = simplify(zz) return zz