test_sph_harm.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. from __future__ import division, print_function, absolute_import
  2. import numpy as np
  3. from numpy.testing import assert_allclose
  4. import scipy.special as sc
  5. def test_first_harmonics():
  6. # Test against explicit representations of the first four
  7. # spherical harmonics which use `theta` as the azimuthal angle,
  8. # `phi` as the polar angle, and include the Condon-Shortley
  9. # phase.
  10. # Notation is Ymn
  11. def Y00(theta, phi):
  12. return 0.5*np.sqrt(1/np.pi)
  13. def Yn11(theta, phi):
  14. return 0.5*np.sqrt(3/(2*np.pi))*np.exp(-1j*theta)*np.sin(phi)
  15. def Y01(theta, phi):
  16. return 0.5*np.sqrt(3/np.pi)*np.cos(phi)
  17. def Y11(theta, phi):
  18. return -0.5*np.sqrt(3/(2*np.pi))*np.exp(1j*theta)*np.sin(phi)
  19. harms = [Y00, Yn11, Y01, Y11]
  20. m = [0, -1, 0, 1]
  21. n = [0, 1, 1, 1]
  22. theta = np.linspace(0, 2*np.pi)
  23. phi = np.linspace(0, np.pi)
  24. theta, phi = np.meshgrid(theta, phi)
  25. for harm, m, n in zip(harms, m, n):
  26. assert_allclose(sc.sph_harm(m, n, theta, phi),
  27. harm(theta, phi),
  28. rtol=1e-15, atol=1e-15,
  29. err_msg="Y^{}_{} incorrect".format(m, n))