matrices.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. """Known matrices related to physics"""
  2. from sympy.core.numbers import I
  3. from sympy.matrices.dense import MutableDenseMatrix as Matrix
  4. from sympy.utilities.decorator import deprecated
  5. def msigma(i):
  6. r"""Returns a Pauli matrix `\sigma_i` with ``i=1,2,3``.
  7. References
  8. ==========
  9. .. [1] https://en.wikipedia.org/wiki/Pauli_matrices
  10. Examples
  11. ========
  12. >>> from sympy.physics.matrices import msigma
  13. >>> msigma(1)
  14. Matrix([
  15. [0, 1],
  16. [1, 0]])
  17. """
  18. if i == 1:
  19. mat = (
  20. (0, 1),
  21. (1, 0)
  22. )
  23. elif i == 2:
  24. mat = (
  25. (0, -I),
  26. (I, 0)
  27. )
  28. elif i == 3:
  29. mat = (
  30. (1, 0),
  31. (0, -1)
  32. )
  33. else:
  34. raise IndexError("Invalid Pauli index")
  35. return Matrix(mat)
  36. def pat_matrix(m, dx, dy, dz):
  37. """Returns the Parallel Axis Theorem matrix to translate the inertia
  38. matrix a distance of `(dx, dy, dz)` for a body of mass m.
  39. Examples
  40. ========
  41. To translate a body having a mass of 2 units a distance of 1 unit along
  42. the `x`-axis we get:
  43. >>> from sympy.physics.matrices import pat_matrix
  44. >>> pat_matrix(2, 1, 0, 0)
  45. Matrix([
  46. [0, 0, 0],
  47. [0, 2, 0],
  48. [0, 0, 2]])
  49. """
  50. dxdy = -dx*dy
  51. dydz = -dy*dz
  52. dzdx = -dz*dx
  53. dxdx = dx**2
  54. dydy = dy**2
  55. dzdz = dz**2
  56. mat = ((dydy + dzdz, dxdy, dzdx),
  57. (dxdy, dxdx + dzdz, dydz),
  58. (dzdx, dydz, dydy + dxdx))
  59. return m*Matrix(mat)
  60. def mgamma(mu, lower=False):
  61. r"""Returns a Dirac gamma matrix `\gamma^\mu` in the standard
  62. (Dirac) representation.
  63. Explanation
  64. ===========
  65. If you want `\gamma_\mu`, use ``gamma(mu, True)``.
  66. We use a convention:
  67. `\gamma^5 = i \cdot \gamma^0 \cdot \gamma^1 \cdot \gamma^2 \cdot \gamma^3`
  68. `\gamma_5 = i \cdot \gamma_0 \cdot \gamma_1 \cdot \gamma_2 \cdot \gamma_3 = - \gamma^5`
  69. References
  70. ==========
  71. .. [1] https://en.wikipedia.org/wiki/Gamma_matrices
  72. Examples
  73. ========
  74. >>> from sympy.physics.matrices import mgamma
  75. >>> mgamma(1)
  76. Matrix([
  77. [ 0, 0, 0, 1],
  78. [ 0, 0, 1, 0],
  79. [ 0, -1, 0, 0],
  80. [-1, 0, 0, 0]])
  81. """
  82. if mu not in (0, 1, 2, 3, 5):
  83. raise IndexError("Invalid Dirac index")
  84. if mu == 0:
  85. mat = (
  86. (1, 0, 0, 0),
  87. (0, 1, 0, 0),
  88. (0, 0, -1, 0),
  89. (0, 0, 0, -1)
  90. )
  91. elif mu == 1:
  92. mat = (
  93. (0, 0, 0, 1),
  94. (0, 0, 1, 0),
  95. (0, -1, 0, 0),
  96. (-1, 0, 0, 0)
  97. )
  98. elif mu == 2:
  99. mat = (
  100. (0, 0, 0, -I),
  101. (0, 0, I, 0),
  102. (0, I, 0, 0),
  103. (-I, 0, 0, 0)
  104. )
  105. elif mu == 3:
  106. mat = (
  107. (0, 0, 1, 0),
  108. (0, 0, 0, -1),
  109. (-1, 0, 0, 0),
  110. (0, 1, 0, 0)
  111. )
  112. elif mu == 5:
  113. mat = (
  114. (0, 0, 1, 0),
  115. (0, 0, 0, 1),
  116. (1, 0, 0, 0),
  117. (0, 1, 0, 0)
  118. )
  119. m = Matrix(mat)
  120. if lower:
  121. if mu in (1, 2, 3, 5):
  122. m = -m
  123. return m
  124. #Minkowski tensor using the convention (+,-,-,-) used in the Quantum Field
  125. #Theory
  126. minkowski_tensor = Matrix( (
  127. (1, 0, 0, 0),
  128. (0, -1, 0, 0),
  129. (0, 0, -1, 0),
  130. (0, 0, 0, -1)
  131. ))
  132. @deprecated(
  133. """
  134. The sympy.physics.matrices.mdft method is deprecated. Use
  135. sympy.DFT(n).as_explicit() instead.
  136. """,
  137. deprecated_since_version="1.9",
  138. active_deprecations_target="deprecated-physics-mdft",
  139. )
  140. def mdft(n):
  141. r"""
  142. .. deprecated:: 1.9
  143. Use DFT from sympy.matrices.expressions.fourier instead.
  144. To get identical behavior to ``mdft(n)``, use ``DFT(n).as_explicit()``.
  145. """
  146. from sympy.matrices.expressions.fourier import DFT
  147. return DFT(n).as_mutable()