test_spherical_harmonics.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. from sympy.core.function import diff
  2. from sympy.core.numbers import (I, pi)
  3. from sympy.core.symbol import Symbol
  4. from sympy.functions.elementary.complexes import conjugate
  5. from sympy.functions.elementary.exponential import exp
  6. from sympy.functions.elementary.miscellaneous import sqrt
  7. from sympy.functions.elementary.trigonometric import (cos, cot, sin)
  8. from sympy.functions.special.spherical_harmonics import Ynm, Znm, Ynm_c
  9. def test_Ynm():
  10. # https://en.wikipedia.org/wiki/Spherical_harmonics
  11. th, ph = Symbol("theta", real=True), Symbol("phi", real=True)
  12. from sympy.abc import n,m
  13. assert Ynm(0, 0, th, ph).expand(func=True) == 1/(2*sqrt(pi))
  14. assert Ynm(1, -1, th, ph) == -exp(-2*I*ph)*Ynm(1, 1, th, ph)
  15. assert Ynm(1, -1, th, ph).expand(func=True) == sqrt(6)*sin(th)*exp(-I*ph)/(4*sqrt(pi))
  16. assert Ynm(1, 0, th, ph).expand(func=True) == sqrt(3)*cos(th)/(2*sqrt(pi))
  17. assert Ynm(1, 1, th, ph).expand(func=True) == -sqrt(6)*sin(th)*exp(I*ph)/(4*sqrt(pi))
  18. assert Ynm(2, 0, th, ph).expand(func=True) == 3*sqrt(5)*cos(th)**2/(4*sqrt(pi)) - sqrt(5)/(4*sqrt(pi))
  19. assert Ynm(2, 1, th, ph).expand(func=True) == -sqrt(30)*sin(th)*exp(I*ph)*cos(th)/(4*sqrt(pi))
  20. assert Ynm(2, -2, th, ph).expand(func=True) == (-sqrt(30)*exp(-2*I*ph)*cos(th)**2/(8*sqrt(pi))
  21. + sqrt(30)*exp(-2*I*ph)/(8*sqrt(pi)))
  22. assert Ynm(2, 2, th, ph).expand(func=True) == (-sqrt(30)*exp(2*I*ph)*cos(th)**2/(8*sqrt(pi))
  23. + sqrt(30)*exp(2*I*ph)/(8*sqrt(pi)))
  24. assert diff(Ynm(n, m, th, ph), th) == (m*cot(th)*Ynm(n, m, th, ph)
  25. + sqrt((-m + n)*(m + n + 1))*exp(-I*ph)*Ynm(n, m + 1, th, ph))
  26. assert diff(Ynm(n, m, th, ph), ph) == I*m*Ynm(n, m, th, ph)
  27. assert conjugate(Ynm(n, m, th, ph)) == (-1)**(2*m)*exp(-2*I*m*ph)*Ynm(n, m, th, ph)
  28. assert Ynm(n, m, -th, ph) == Ynm(n, m, th, ph)
  29. assert Ynm(n, m, th, -ph) == exp(-2*I*m*ph)*Ynm(n, m, th, ph)
  30. assert Ynm(n, -m, th, ph) == (-1)**m*exp(-2*I*m*ph)*Ynm(n, m, th, ph)
  31. def test_Ynm_c():
  32. th, ph = Symbol("theta", real=True), Symbol("phi", real=True)
  33. from sympy.abc import n,m
  34. assert Ynm_c(n, m, th, ph) == (-1)**(2*m)*exp(-2*I*m*ph)*Ynm(n, m, th, ph)
  35. def test_Znm():
  36. # https://en.wikipedia.org/wiki/Solid_harmonics#List_of_lowest_functions
  37. th, ph = Symbol("theta", real=True), Symbol("phi", real=True)
  38. assert Znm(0, 0, th, ph) == Ynm(0, 0, th, ph)
  39. assert Znm(1, -1, th, ph) == (-sqrt(2)*I*(Ynm(1, 1, th, ph)
  40. - exp(-2*I*ph)*Ynm(1, 1, th, ph))/2)
  41. assert Znm(1, 0, th, ph) == Ynm(1, 0, th, ph)
  42. assert Znm(1, 1, th, ph) == (sqrt(2)*(Ynm(1, 1, th, ph)
  43. + exp(-2*I*ph)*Ynm(1, 1, th, ph))/2)
  44. assert Znm(0, 0, th, ph).expand(func=True) == 1/(2*sqrt(pi))
  45. assert Znm(1, -1, th, ph).expand(func=True) == (sqrt(3)*I*sin(th)*exp(I*ph)/(4*sqrt(pi))
  46. - sqrt(3)*I*sin(th)*exp(-I*ph)/(4*sqrt(pi)))
  47. assert Znm(1, 0, th, ph).expand(func=True) == sqrt(3)*cos(th)/(2*sqrt(pi))
  48. assert Znm(1, 1, th, ph).expand(func=True) == (-sqrt(3)*sin(th)*exp(I*ph)/(4*sqrt(pi))
  49. - sqrt(3)*sin(th)*exp(-I*ph)/(4*sqrt(pi)))
  50. assert Znm(2, -1, th, ph).expand(func=True) == (sqrt(15)*I*sin(th)*exp(I*ph)*cos(th)/(4*sqrt(pi))
  51. - sqrt(15)*I*sin(th)*exp(-I*ph)*cos(th)/(4*sqrt(pi)))
  52. assert Znm(2, 0, th, ph).expand(func=True) == 3*sqrt(5)*cos(th)**2/(4*sqrt(pi)) - sqrt(5)/(4*sqrt(pi))
  53. assert Znm(2, 1, th, ph).expand(func=True) == (-sqrt(15)*sin(th)*exp(I*ph)*cos(th)/(4*sqrt(pi))
  54. - sqrt(15)*sin(th)*exp(-I*ph)*cos(th)/(4*sqrt(pi)))