Coverage for /usr/lib/python3/dist-packages/sympy/physics/matrices.py: 20%

44 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2025-06-14 15:55 +0200

1"""Known matrices related to physics""" 

2 

3from sympy.core.numbers import I 

4from sympy.matrices.dense import MutableDenseMatrix as Matrix 

5from sympy.utilities.decorator import deprecated 

6 

7 

8def msigma(i): 

9 r"""Returns a Pauli matrix `\sigma_i` with `i=1,2,3`. 

10 

11 References 

12 ========== 

13 

14 .. [1] https://en.wikipedia.org/wiki/Pauli_matrices 

15 

16 Examples 

17 ======== 

18 

19 >>> from sympy.physics.matrices import msigma 

20 >>> msigma(1) 

21 Matrix([ 

22 [0, 1], 

23 [1, 0]]) 

24 """ 

25 if i == 1: 

26 mat = ( 

27 (0, 1), 

28 (1, 0) 

29 ) 

30 elif i == 2: 

31 mat = ( 

32 (0, -I), 

33 (I, 0) 

34 ) 

35 elif i == 3: 

36 mat = ( 

37 (1, 0), 

38 (0, -1) 

39 ) 

40 else: 

41 raise IndexError("Invalid Pauli index") 

42 return Matrix(mat) 

43 

44 

45def pat_matrix(m, dx, dy, dz): 

46 """Returns the Parallel Axis Theorem matrix to translate the inertia 

47 matrix a distance of `(dx, dy, dz)` for a body of mass m. 

48 

49 Examples 

50 ======== 

51 

52 To translate a body having a mass of 2 units a distance of 1 unit along 

53 the `x`-axis we get: 

54 

55 >>> from sympy.physics.matrices import pat_matrix 

56 >>> pat_matrix(2, 1, 0, 0) 

57 Matrix([ 

58 [0, 0, 0], 

59 [0, 2, 0], 

60 [0, 0, 2]]) 

61 

62 """ 

63 dxdy = -dx*dy 

64 dydz = -dy*dz 

65 dzdx = -dz*dx 

66 dxdx = dx**2 

67 dydy = dy**2 

68 dzdz = dz**2 

69 mat = ((dydy + dzdz, dxdy, dzdx), 

70 (dxdy, dxdx + dzdz, dydz), 

71 (dzdx, dydz, dydy + dxdx)) 

72 return m*Matrix(mat) 

73 

74 

75def mgamma(mu, lower=False): 

76 r"""Returns a Dirac gamma matrix `\gamma^\mu` in the standard 

77 (Dirac) representation. 

78 

79 Explanation 

80 =========== 

81 

82 If you want `\gamma_\mu`, use ``gamma(mu, True)``. 

83 

84 We use a convention: 

85 

86 `\gamma^5 = i \cdot \gamma^0 \cdot \gamma^1 \cdot \gamma^2 \cdot \gamma^3` 

87 

88 `\gamma_5 = i \cdot \gamma_0 \cdot \gamma_1 \cdot \gamma_2 \cdot \gamma_3 = - \gamma^5` 

89 

90 References 

91 ========== 

92 

93 .. [1] https://en.wikipedia.org/wiki/Gamma_matrices 

94 

95 Examples 

96 ======== 

97 

98 >>> from sympy.physics.matrices import mgamma 

99 >>> mgamma(1) 

100 Matrix([ 

101 [ 0, 0, 0, 1], 

102 [ 0, 0, 1, 0], 

103 [ 0, -1, 0, 0], 

104 [-1, 0, 0, 0]]) 

105 """ 

106 if mu not in (0, 1, 2, 3, 5): 

107 raise IndexError("Invalid Dirac index") 

108 if mu == 0: 

109 mat = ( 

110 (1, 0, 0, 0), 

111 (0, 1, 0, 0), 

112 (0, 0, -1, 0), 

113 (0, 0, 0, -1) 

114 ) 

115 elif mu == 1: 

116 mat = ( 

117 (0, 0, 0, 1), 

118 (0, 0, 1, 0), 

119 (0, -1, 0, 0), 

120 (-1, 0, 0, 0) 

121 ) 

122 elif mu == 2: 

123 mat = ( 

124 (0, 0, 0, -I), 

125 (0, 0, I, 0), 

126 (0, I, 0, 0), 

127 (-I, 0, 0, 0) 

128 ) 

129 elif mu == 3: 

130 mat = ( 

131 (0, 0, 1, 0), 

132 (0, 0, 0, -1), 

133 (-1, 0, 0, 0), 

134 (0, 1, 0, 0) 

135 ) 

136 elif mu == 5: 

137 mat = ( 

138 (0, 0, 1, 0), 

139 (0, 0, 0, 1), 

140 (1, 0, 0, 0), 

141 (0, 1, 0, 0) 

142 ) 

143 m = Matrix(mat) 

144 if lower: 

145 if mu in (1, 2, 3, 5): 

146 m = -m 

147 return m 

148 

149#Minkowski tensor using the convention (+,-,-,-) used in the Quantum Field 

150#Theory 

151minkowski_tensor = Matrix( ( 

152 (1, 0, 0, 0), 

153 (0, -1, 0, 0), 

154 (0, 0, -1, 0), 

155 (0, 0, 0, -1) 

156)) 

157 

158 

159@deprecated( 

160 """ 

161 The sympy.physics.matrices.mdft method is deprecated. Use 

162 sympy.DFT(n).as_explicit() instead. 

163 """, 

164 deprecated_since_version="1.9", 

165 active_deprecations_target="deprecated-physics-mdft", 

166) 

167def mdft(n): 

168 r""" 

169 .. deprecated:: 1.9 

170 

171 Use DFT from sympy.matrices.expressions.fourier instead. 

172 

173 To get identical behavior to ``mdft(n)``, use ``DFT(n).as_explicit()``. 

174 """ 

175 from sympy.matrices.expressions.fourier import DFT 

176 return DFT(n).as_mutable()