test_physics_matrices.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. from sympy.physics.matrices import msigma, mgamma, minkowski_tensor, pat_matrix, mdft
  2. from sympy.core.numbers import (I, Rational)
  3. from sympy.core.singleton import S
  4. from sympy.functions.elementary.miscellaneous import sqrt
  5. from sympy.matrices.dense import (Matrix, eye, zeros)
  6. from sympy.testing.pytest import warns_deprecated_sympy
  7. def test_parallel_axis_theorem():
  8. # This tests the parallel axis theorem matrix by comparing to test
  9. # matrices.
  10. # First case, 1 in all directions.
  11. mat1 = Matrix(((2, -1, -1), (-1, 2, -1), (-1, -1, 2)))
  12. assert pat_matrix(1, 1, 1, 1) == mat1
  13. assert pat_matrix(2, 1, 1, 1) == 2*mat1
  14. # Second case, 1 in x, 0 in all others
  15. mat2 = Matrix(((0, 0, 0), (0, 1, 0), (0, 0, 1)))
  16. assert pat_matrix(1, 1, 0, 0) == mat2
  17. assert pat_matrix(2, 1, 0, 0) == 2*mat2
  18. # Third case, 1 in y, 0 in all others
  19. mat3 = Matrix(((1, 0, 0), (0, 0, 0), (0, 0, 1)))
  20. assert pat_matrix(1, 0, 1, 0) == mat3
  21. assert pat_matrix(2, 0, 1, 0) == 2*mat3
  22. # Fourth case, 1 in z, 0 in all others
  23. mat4 = Matrix(((1, 0, 0), (0, 1, 0), (0, 0, 0)))
  24. assert pat_matrix(1, 0, 0, 1) == mat4
  25. assert pat_matrix(2, 0, 0, 1) == 2*mat4
  26. def test_Pauli():
  27. #this and the following test are testing both Pauli and Dirac matrices
  28. #and also that the general Matrix class works correctly in a real world
  29. #situation
  30. sigma1 = msigma(1)
  31. sigma2 = msigma(2)
  32. sigma3 = msigma(3)
  33. assert sigma1 == sigma1
  34. assert sigma1 != sigma2
  35. # sigma*I -> I*sigma (see #354)
  36. assert sigma1*sigma2 == sigma3*I
  37. assert sigma3*sigma1 == sigma2*I
  38. assert sigma2*sigma3 == sigma1*I
  39. assert sigma1*sigma1 == eye(2)
  40. assert sigma2*sigma2 == eye(2)
  41. assert sigma3*sigma3 == eye(2)
  42. assert sigma1*2*sigma1 == 2*eye(2)
  43. assert sigma1*sigma3*sigma1 == -sigma3
  44. def test_Dirac():
  45. gamma0 = mgamma(0)
  46. gamma1 = mgamma(1)
  47. gamma2 = mgamma(2)
  48. gamma3 = mgamma(3)
  49. gamma5 = mgamma(5)
  50. # gamma*I -> I*gamma (see #354)
  51. assert gamma5 == gamma0 * gamma1 * gamma2 * gamma3 * I
  52. assert gamma1 * gamma2 + gamma2 * gamma1 == zeros(4)
  53. assert gamma0 * gamma0 == eye(4) * minkowski_tensor[0, 0]
  54. assert gamma2 * gamma2 != eye(4) * minkowski_tensor[0, 0]
  55. assert gamma2 * gamma2 == eye(4) * minkowski_tensor[2, 2]
  56. assert mgamma(5, True) == \
  57. mgamma(0, True)*mgamma(1, True)*mgamma(2, True)*mgamma(3, True)*I
  58. def test_mdft():
  59. with warns_deprecated_sympy():
  60. assert mdft(1) == Matrix([[1]])
  61. with warns_deprecated_sympy():
  62. assert mdft(2) == 1/sqrt(2)*Matrix([[1,1],[1,-1]])
  63. with warns_deprecated_sympy():
  64. assert mdft(4) == Matrix([[S.Half, S.Half, S.Half, S.Half],
  65. [S.Half, -I/2, Rational(-1,2), I/2],
  66. [S.Half, Rational(-1,2), S.Half, Rational(-1,2)],
  67. [S.Half, I/2, Rational(-1,2), -I/2]])