图片解析应用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

401 lines
14 KiB

  1. from sympy.matrices.dense import eye, Matrix
  2. from sympy.tensor.tensor import tensor_indices, TensorHead, tensor_heads, \
  3. TensExpr, canon_bp
  4. from sympy.physics.hep.gamma_matrices import GammaMatrix as G, LorentzIndex, \
  5. kahane_simplify, gamma_trace, _simplify_single_line, simplify_gamma_expression
  6. def _is_tensor_eq(arg1, arg2):
  7. arg1 = canon_bp(arg1)
  8. arg2 = canon_bp(arg2)
  9. if isinstance(arg1, TensExpr):
  10. return arg1.equals(arg2)
  11. elif isinstance(arg2, TensExpr):
  12. return arg2.equals(arg1)
  13. return arg1 == arg2
  14. def execute_gamma_simplify_tests_for_function(tfunc, D):
  15. """
  16. Perform tests to check if sfunc is able to simplify gamma matrix expressions.
  17. Parameters
  18. ==========
  19. `sfunc` a function to simplify a `TIDS`, shall return the simplified `TIDS`.
  20. `D` the number of dimension (in most cases `D=4`).
  21. """
  22. mu, nu, rho, sigma = tensor_indices("mu, nu, rho, sigma", LorentzIndex)
  23. a1, a2, a3, a4, a5, a6 = tensor_indices("a1:7", LorentzIndex)
  24. mu11, mu12, mu21, mu31, mu32, mu41, mu51, mu52 = tensor_indices("mu11, mu12, mu21, mu31, mu32, mu41, mu51, mu52", LorentzIndex)
  25. mu61, mu71, mu72 = tensor_indices("mu61, mu71, mu72", LorentzIndex)
  26. m0, m1, m2, m3, m4, m5, m6 = tensor_indices("m0:7", LorentzIndex)
  27. def g(xx, yy):
  28. return (G(xx)*G(yy) + G(yy)*G(xx))/2
  29. # Some examples taken from Kahane's paper, 4 dim only:
  30. if D == 4:
  31. t = (G(a1)*G(mu11)*G(a2)*G(mu21)*G(-a1)*G(mu31)*G(-a2))
  32. assert _is_tensor_eq(tfunc(t), -4*G(mu11)*G(mu31)*G(mu21) - 4*G(mu31)*G(mu11)*G(mu21))
  33. t = (G(a1)*G(mu11)*G(mu12)*\
  34. G(a2)*G(mu21)*\
  35. G(a3)*G(mu31)*G(mu32)*\
  36. G(a4)*G(mu41)*\
  37. G(-a2)*G(mu51)*G(mu52)*\
  38. G(-a1)*G(mu61)*\
  39. G(-a3)*G(mu71)*G(mu72)*\
  40. G(-a4))
  41. assert _is_tensor_eq(tfunc(t), \
  42. 16*G(mu31)*G(mu32)*G(mu72)*G(mu71)*G(mu11)*G(mu52)*G(mu51)*G(mu12)*G(mu61)*G(mu21)*G(mu41) + 16*G(mu31)*G(mu32)*G(mu72)*G(mu71)*G(mu12)*G(mu51)*G(mu52)*G(mu11)*G(mu61)*G(mu21)*G(mu41) + 16*G(mu71)*G(mu72)*G(mu32)*G(mu31)*G(mu11)*G(mu52)*G(mu51)*G(mu12)*G(mu61)*G(mu21)*G(mu41) + 16*G(mu71)*G(mu72)*G(mu32)*G(mu31)*G(mu12)*G(mu51)*G(mu52)*G(mu11)*G(mu61)*G(mu21)*G(mu41))
  43. # Fully Lorentz-contracted expressions, these return scalars:
  44. def add_delta(ne):
  45. return ne * eye(4) # DiracSpinorIndex.delta(DiracSpinorIndex.auto_left, -DiracSpinorIndex.auto_right)
  46. t = (G(mu)*G(-mu))
  47. ts = add_delta(D)
  48. assert _is_tensor_eq(tfunc(t), ts)
  49. t = (G(mu)*G(nu)*G(-mu)*G(-nu))
  50. ts = add_delta(2*D - D**2) # -8
  51. assert _is_tensor_eq(tfunc(t), ts)
  52. t = (G(mu)*G(nu)*G(-nu)*G(-mu))
  53. ts = add_delta(D**2) # 16
  54. assert _is_tensor_eq(tfunc(t), ts)
  55. t = (G(mu)*G(nu)*G(-rho)*G(-nu)*G(-mu)*G(rho))
  56. ts = add_delta(4*D - 4*D**2 + D**3) # 16
  57. assert _is_tensor_eq(tfunc(t), ts)
  58. t = (G(mu)*G(nu)*G(rho)*G(-rho)*G(-nu)*G(-mu))
  59. ts = add_delta(D**3) # 64
  60. assert _is_tensor_eq(tfunc(t), ts)
  61. t = (G(a1)*G(a2)*G(a3)*G(a4)*G(-a3)*G(-a1)*G(-a2)*G(-a4))
  62. ts = add_delta(-8*D + 16*D**2 - 8*D**3 + D**4) # -32
  63. assert _is_tensor_eq(tfunc(t), ts)
  64. t = (G(-mu)*G(-nu)*G(-rho)*G(-sigma)*G(nu)*G(mu)*G(sigma)*G(rho))
  65. ts = add_delta(-16*D + 24*D**2 - 8*D**3 + D**4) # 64
  66. assert _is_tensor_eq(tfunc(t), ts)
  67. t = (G(-mu)*G(nu)*G(-rho)*G(sigma)*G(rho)*G(-nu)*G(mu)*G(-sigma))
  68. ts = add_delta(8*D - 12*D**2 + 6*D**3 - D**4) # -32
  69. assert _is_tensor_eq(tfunc(t), ts)
  70. t = (G(a1)*G(a2)*G(a3)*G(a4)*G(a5)*G(-a3)*G(-a2)*G(-a1)*G(-a5)*G(-a4))
  71. ts = add_delta(64*D - 112*D**2 + 60*D**3 - 12*D**4 + D**5) # 256
  72. assert _is_tensor_eq(tfunc(t), ts)
  73. t = (G(a1)*G(a2)*G(a3)*G(a4)*G(a5)*G(-a3)*G(-a1)*G(-a2)*G(-a4)*G(-a5))
  74. ts = add_delta(64*D - 120*D**2 + 72*D**3 - 16*D**4 + D**5) # -128
  75. assert _is_tensor_eq(tfunc(t), ts)
  76. t = (G(a1)*G(a2)*G(a3)*G(a4)*G(a5)*G(a6)*G(-a3)*G(-a2)*G(-a1)*G(-a6)*G(-a5)*G(-a4))
  77. ts = add_delta(416*D - 816*D**2 + 528*D**3 - 144*D**4 + 18*D**5 - D**6) # -128
  78. assert _is_tensor_eq(tfunc(t), ts)
  79. t = (G(a1)*G(a2)*G(a3)*G(a4)*G(a5)*G(a6)*G(-a2)*G(-a3)*G(-a1)*G(-a6)*G(-a4)*G(-a5))
  80. ts = add_delta(416*D - 848*D**2 + 584*D**3 - 172*D**4 + 22*D**5 - D**6) # -128
  81. assert _is_tensor_eq(tfunc(t), ts)
  82. # Expressions with free indices:
  83. t = (G(mu)*G(nu)*G(rho)*G(sigma)*G(-mu))
  84. assert _is_tensor_eq(tfunc(t), (-2*G(sigma)*G(rho)*G(nu) + (4-D)*G(nu)*G(rho)*G(sigma)))
  85. t = (G(mu)*G(nu)*G(-mu))
  86. assert _is_tensor_eq(tfunc(t), (2-D)*G(nu))
  87. t = (G(mu)*G(nu)*G(rho)*G(-mu))
  88. assert _is_tensor_eq(tfunc(t), 2*G(nu)*G(rho) + 2*G(rho)*G(nu) - (4-D)*G(nu)*G(rho))
  89. t = 2*G(m2)*G(m0)*G(m1)*G(-m0)*G(-m1)
  90. st = tfunc(t)
  91. assert _is_tensor_eq(st, (D*(-2*D + 4))*G(m2))
  92. t = G(m2)*G(m0)*G(m1)*G(-m0)*G(-m2)
  93. st = tfunc(t)
  94. assert _is_tensor_eq(st, ((-D + 2)**2)*G(m1))
  95. t = G(m0)*G(m1)*G(m2)*G(m3)*G(-m1)
  96. st = tfunc(t)
  97. assert _is_tensor_eq(st, (D - 4)*G(m0)*G(m2)*G(m3) + 4*G(m0)*g(m2, m3))
  98. t = G(m0)*G(m1)*G(m2)*G(m3)*G(-m1)*G(-m0)
  99. st = tfunc(t)
  100. assert _is_tensor_eq(st, ((D - 4)**2)*G(m2)*G(m3) + (8*D - 16)*g(m2, m3))
  101. t = G(m2)*G(m0)*G(m1)*G(-m2)*G(-m0)
  102. st = tfunc(t)
  103. assert _is_tensor_eq(st, ((-D + 2)*(D - 4) + 4)*G(m1))
  104. t = G(m3)*G(m1)*G(m0)*G(m2)*G(-m3)*G(-m0)*G(-m2)
  105. st = tfunc(t)
  106. assert _is_tensor_eq(st, (-4*D + (-D + 2)**2*(D - 4) + 8)*G(m1))
  107. t = 2*G(m0)*G(m1)*G(m2)*G(m3)*G(-m0)
  108. st = tfunc(t)
  109. assert _is_tensor_eq(st, ((-2*D + 8)*G(m1)*G(m2)*G(m3) - 4*G(m3)*G(m2)*G(m1)))
  110. t = G(m5)*G(m0)*G(m1)*G(m4)*G(m2)*G(-m4)*G(m3)*G(-m0)
  111. st = tfunc(t)
  112. assert _is_tensor_eq(st, (((-D + 2)*(-D + 4))*G(m5)*G(m1)*G(m2)*G(m3) + (2*D - 4)*G(m5)*G(m3)*G(m2)*G(m1)))
  113. t = -G(m0)*G(m1)*G(m2)*G(m3)*G(-m0)*G(m4)
  114. st = tfunc(t)
  115. assert _is_tensor_eq(st, ((D - 4)*G(m1)*G(m2)*G(m3)*G(m4) + 2*G(m3)*G(m2)*G(m1)*G(m4)))
  116. t = G(-m5)*G(m0)*G(m1)*G(m2)*G(m3)*G(m4)*G(-m0)*G(m5)
  117. st = tfunc(t)
  118. result1 = ((-D + 4)**2 + 4)*G(m1)*G(m2)*G(m3)*G(m4) +\
  119. (4*D - 16)*G(m3)*G(m2)*G(m1)*G(m4) + (4*D - 16)*G(m4)*G(m1)*G(m2)*G(m3)\
  120. + 4*G(m2)*G(m1)*G(m4)*G(m3) + 4*G(m3)*G(m4)*G(m1)*G(m2) +\
  121. 4*G(m4)*G(m3)*G(m2)*G(m1)
  122. # Kahane's algorithm yields this result, which is equivalent to `result1`
  123. # in four dimensions, but is not automatically recognized as equal:
  124. result2 = 8*G(m1)*G(m2)*G(m3)*G(m4) + 8*G(m4)*G(m3)*G(m2)*G(m1)
  125. if D == 4:
  126. assert _is_tensor_eq(st, (result1)) or _is_tensor_eq(st, (result2))
  127. else:
  128. assert _is_tensor_eq(st, (result1))
  129. # and a few very simple cases, with no contracted indices:
  130. t = G(m0)
  131. st = tfunc(t)
  132. assert _is_tensor_eq(st, t)
  133. t = -7*G(m0)
  134. st = tfunc(t)
  135. assert _is_tensor_eq(st, t)
  136. t = 224*G(m0)*G(m1)*G(-m2)*G(m3)
  137. st = tfunc(t)
  138. assert _is_tensor_eq(st, t)
  139. def test_kahane_algorithm():
  140. # Wrap this function to convert to and from TIDS:
  141. def tfunc(e):
  142. return _simplify_single_line(e)
  143. execute_gamma_simplify_tests_for_function(tfunc, D=4)
  144. def test_kahane_simplify1():
  145. i0,i1,i2,i3,i4,i5,i6,i7,i8,i9,i10,i11,i12,i13,i14,i15 = tensor_indices('i0:16', LorentzIndex)
  146. mu, nu, rho, sigma = tensor_indices("mu, nu, rho, sigma", LorentzIndex)
  147. D = 4
  148. t = G(i0)*G(i1)
  149. r = kahane_simplify(t)
  150. assert r.equals(t)
  151. t = G(i0)*G(i1)*G(-i0)
  152. r = kahane_simplify(t)
  153. assert r.equals(-2*G(i1))
  154. t = G(i0)*G(i1)*G(-i0)
  155. r = kahane_simplify(t)
  156. assert r.equals(-2*G(i1))
  157. t = G(i0)*G(i1)
  158. r = kahane_simplify(t)
  159. assert r.equals(t)
  160. t = G(i0)*G(i1)
  161. r = kahane_simplify(t)
  162. assert r.equals(t)
  163. t = G(i0)*G(-i0)
  164. r = kahane_simplify(t)
  165. assert r.equals(4*eye(4))
  166. t = G(i0)*G(-i0)
  167. r = kahane_simplify(t)
  168. assert r.equals(4*eye(4))
  169. t = G(i0)*G(-i0)
  170. r = kahane_simplify(t)
  171. assert r.equals(4*eye(4))
  172. t = G(i0)*G(i1)*G(-i0)
  173. r = kahane_simplify(t)
  174. assert r.equals(-2*G(i1))
  175. t = G(i0)*G(i1)*G(-i0)*G(-i1)
  176. r = kahane_simplify(t)
  177. assert r.equals((2*D - D**2)*eye(4))
  178. t = G(i0)*G(i1)*G(-i0)*G(-i1)
  179. r = kahane_simplify(t)
  180. assert r.equals((2*D - D**2)*eye(4))
  181. t = G(i0)*G(-i0)*G(i1)*G(-i1)
  182. r = kahane_simplify(t)
  183. assert r.equals(16*eye(4))
  184. t = (G(mu)*G(nu)*G(-nu)*G(-mu))
  185. r = kahane_simplify(t)
  186. assert r.equals(D**2*eye(4))
  187. t = (G(mu)*G(nu)*G(-nu)*G(-mu))
  188. r = kahane_simplify(t)
  189. assert r.equals(D**2*eye(4))
  190. t = (G(mu)*G(nu)*G(-nu)*G(-mu))
  191. r = kahane_simplify(t)
  192. assert r.equals(D**2*eye(4))
  193. t = (G(mu)*G(nu)*G(-rho)*G(-nu)*G(-mu)*G(rho))
  194. r = kahane_simplify(t)
  195. assert r.equals((4*D - 4*D**2 + D**3)*eye(4))
  196. t = (G(-mu)*G(-nu)*G(-rho)*G(-sigma)*G(nu)*G(mu)*G(sigma)*G(rho))
  197. r = kahane_simplify(t)
  198. assert r.equals((-16*D + 24*D**2 - 8*D**3 + D**4)*eye(4))
  199. t = (G(-mu)*G(nu)*G(-rho)*G(sigma)*G(rho)*G(-nu)*G(mu)*G(-sigma))
  200. r = kahane_simplify(t)
  201. assert r.equals((8*D - 12*D**2 + 6*D**3 - D**4)*eye(4))
  202. # Expressions with free indices:
  203. t = (G(mu)*G(nu)*G(rho)*G(sigma)*G(-mu))
  204. r = kahane_simplify(t)
  205. assert r.equals(-2*G(sigma)*G(rho)*G(nu))
  206. t = (G(mu)*G(nu)*G(rho)*G(sigma)*G(-mu))
  207. r = kahane_simplify(t)
  208. assert r.equals(-2*G(sigma)*G(rho)*G(nu))
  209. def test_gamma_matrix_class():
  210. i, j, k = tensor_indices('i,j,k', LorentzIndex)
  211. # define another type of TensorHead to see if exprs are correctly handled:
  212. A = TensorHead('A', [LorentzIndex])
  213. t = A(k)*G(i)*G(-i)
  214. ts = simplify_gamma_expression(t)
  215. assert _is_tensor_eq(ts, Matrix([
  216. [4, 0, 0, 0],
  217. [0, 4, 0, 0],
  218. [0, 0, 4, 0],
  219. [0, 0, 0, 4]])*A(k))
  220. t = G(i)*A(k)*G(j)
  221. ts = simplify_gamma_expression(t)
  222. assert _is_tensor_eq(ts, A(k)*G(i)*G(j))
  223. execute_gamma_simplify_tests_for_function(simplify_gamma_expression, D=4)
  224. def test_gamma_matrix_trace():
  225. g = LorentzIndex.metric
  226. m0, m1, m2, m3, m4, m5, m6 = tensor_indices('m0:7', LorentzIndex)
  227. n0, n1, n2, n3, n4, n5 = tensor_indices('n0:6', LorentzIndex)
  228. # working in D=4 dimensions
  229. D = 4
  230. # traces of odd number of gamma matrices are zero:
  231. t = G(m0)
  232. t1 = gamma_trace(t)
  233. assert t1.equals(0)
  234. t = G(m0)*G(m1)*G(m2)
  235. t1 = gamma_trace(t)
  236. assert t1.equals(0)
  237. t = G(m0)*G(m1)*G(-m0)
  238. t1 = gamma_trace(t)
  239. assert t1.equals(0)
  240. t = G(m0)*G(m1)*G(m2)*G(m3)*G(m4)
  241. t1 = gamma_trace(t)
  242. assert t1.equals(0)
  243. # traces without internal contractions:
  244. t = G(m0)*G(m1)
  245. t1 = gamma_trace(t)
  246. assert _is_tensor_eq(t1, 4*g(m0, m1))
  247. t = G(m0)*G(m1)*G(m2)*G(m3)
  248. t1 = gamma_trace(t)
  249. t2 = -4*g(m0, m2)*g(m1, m3) + 4*g(m0, m1)*g(m2, m3) + 4*g(m0, m3)*g(m1, m2)
  250. assert _is_tensor_eq(t1, t2)
  251. t = G(m0)*G(m1)*G(m2)*G(m3)*G(m4)*G(m5)
  252. t1 = gamma_trace(t)
  253. t2 = t1*g(-m0, -m5)
  254. t2 = t2.contract_metric(g)
  255. assert _is_tensor_eq(t2, D*gamma_trace(G(m1)*G(m2)*G(m3)*G(m4)))
  256. # traces of expressions with internal contractions:
  257. t = G(m0)*G(-m0)
  258. t1 = gamma_trace(t)
  259. assert t1.equals(4*D)
  260. t = G(m0)*G(m1)*G(-m0)*G(-m1)
  261. t1 = gamma_trace(t)
  262. assert t1.equals(8*D - 4*D**2)
  263. t = G(m0)*G(m1)*G(m2)*G(m3)*G(m4)*G(-m0)
  264. t1 = gamma_trace(t)
  265. t2 = (-4*D)*g(m1, m3)*g(m2, m4) + (4*D)*g(m1, m2)*g(m3, m4) + \
  266. (4*D)*g(m1, m4)*g(m2, m3)
  267. assert _is_tensor_eq(t1, t2)
  268. t = G(-m5)*G(m0)*G(m1)*G(m2)*G(m3)*G(m4)*G(-m0)*G(m5)
  269. t1 = gamma_trace(t)
  270. t2 = (32*D + 4*(-D + 4)**2 - 64)*(g(m1, m2)*g(m3, m4) - \
  271. g(m1, m3)*g(m2, m4) + g(m1, m4)*g(m2, m3))
  272. assert _is_tensor_eq(t1, t2)
  273. t = G(m0)*G(m1)*G(-m0)*G(m3)
  274. t1 = gamma_trace(t)
  275. assert t1.equals((-4*D + 8)*g(m1, m3))
  276. # p, q = S1('p,q')
  277. # ps = p(m0)*G(-m0)
  278. # qs = q(m0)*G(-m0)
  279. # t = ps*qs*ps*qs
  280. # t1 = gamma_trace(t)
  281. # assert t1 == 8*p(m0)*q(-m0)*p(m1)*q(-m1) - 4*p(m0)*p(-m0)*q(m1)*q(-m1)
  282. t = G(m0)*G(m1)*G(m2)*G(m3)*G(m4)*G(m5)*G(-m0)*G(-m1)*G(-m2)*G(-m3)*G(-m4)*G(-m5)
  283. t1 = gamma_trace(t)
  284. assert t1.equals(-4*D**6 + 120*D**5 - 1040*D**4 + 3360*D**3 - 4480*D**2 + 2048*D)
  285. t = G(m0)*G(m1)*G(n1)*G(m2)*G(n2)*G(m3)*G(m4)*G(-n2)*G(-n1)*G(-m0)*G(-m1)*G(-m2)*G(-m3)*G(-m4)
  286. t1 = gamma_trace(t)
  287. tresu = -7168*D + 16768*D**2 - 14400*D**3 + 5920*D**4 - 1232*D**5 + 120*D**6 - 4*D**7
  288. assert t1.equals(tresu)
  289. # checked with Mathematica
  290. # In[1]:= <<Tracer.m
  291. # In[2]:= Spur[l];
  292. # In[3]:= GammaTrace[l, {m0},{m1},{n1},{m2},{n2},{m3},{m4},{n3},{n4},{m0},{m1},{m2},{m3},{m4}]
  293. t = G(m0)*G(m1)*G(n1)*G(m2)*G(n2)*G(m3)*G(m4)*G(n3)*G(n4)*G(-m0)*G(-m1)*G(-m2)*G(-m3)*G(-m4)
  294. t1 = gamma_trace(t)
  295. # t1 = t1.expand_coeff()
  296. c1 = -4*D**5 + 120*D**4 - 1200*D**3 + 5280*D**2 - 10560*D + 7808
  297. c2 = -4*D**5 + 88*D**4 - 560*D**3 + 1440*D**2 - 1600*D + 640
  298. assert _is_tensor_eq(t1, c1*g(n1, n4)*g(n2, n3) + c2*g(n1, n2)*g(n3, n4) + \
  299. (-c1)*g(n1, n3)*g(n2, n4))
  300. p, q = tensor_heads('p,q', [LorentzIndex])
  301. ps = p(m0)*G(-m0)
  302. qs = q(m0)*G(-m0)
  303. p2 = p(m0)*p(-m0)
  304. q2 = q(m0)*q(-m0)
  305. pq = p(m0)*q(-m0)
  306. t = ps*qs*ps*qs
  307. r = gamma_trace(t)
  308. assert _is_tensor_eq(r, 8*pq*pq - 4*p2*q2)
  309. t = ps*qs*ps*qs*ps*qs
  310. r = gamma_trace(t)
  311. assert _is_tensor_eq(r, -12*p2*pq*q2 + 16*pq*pq*pq)
  312. t = ps*qs*ps*qs*ps*qs*ps*qs
  313. r = gamma_trace(t)
  314. assert _is_tensor_eq(r, -32*pq*pq*p2*q2 + 32*pq*pq*pq*pq + 4*p2*p2*q2*q2)
  315. t = 4*p(m1)*p(m0)*p(-m0)*q(-m1)*q(m2)*q(-m2)
  316. assert _is_tensor_eq(gamma_trace(t), t)
  317. t = ps*ps*ps*ps*ps*ps*ps*ps
  318. r = gamma_trace(t)
  319. assert r.equals(4*p2*p2*p2*p2)