K i^dZddlmZddlmZddlmZddlmZddl m Z m Z m Z m Z mZmZmZmZe ddd Zed egej&d d ZdZdZddZdZdZdZdZdZy )a Module to handle gamma matrices expressed as tensor objects. Examples ======== >>> from sympy.physics.hep.gamma_matrices import GammaMatrix as G, LorentzIndex >>> from sympy.tensor.tensor import tensor_indices >>> i = tensor_indices('i', LorentzIndex) >>> G(i) GammaMatrix(i) Note that there is already an instance of GammaMatrixHead in four dimensions: GammaMatrix, which is simply declare as >>> from sympy.physics.hep.gamma_matrices import GammaMatrix >>> from sympy.tensor.tensor import tensor_indices >>> i = tensor_indices('i', LorentzIndex) >>> GammaMatrix(i) GammaMatrix(i) To access the metric tensor >>> LorentzIndex.metric metric(LorentzIndex,LorentzIndex) )Mul)S)eye)trace)TensorIndexType TensorIndexTensMulTensAdd tensor_mulTensor TensorHeadTensorSymmetry LorentzIndexL)dim dummy_name GammaMatrixN)commc$t|tr|g}n(t|tr |j}n t dt j }t j }|D],}t|tr|j|k(r||z}(||z}.||fS)z Extract from a ``TensExpr`` all tensors with `component`. Returns two tensor expressions: * the first contains all ``Tensor`` of having `component`. * the second contains all remaining. z wrong type) isinstancer r args ValueErrorrOne component) expressionrspnew_expr residual_expris f/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/physics/hep/gamma_matrices.pyextract_type_tensr#.s*f%\ J ( __&&uuHEEM  a Q[[I%= MH Q M  ] ""cHt|t\}}t|}||zS)N)r#r_simplify_single_line)rextracted_exprr res_exprs r"simplify_gamma_expressionr)Ks($5j+$N!NM$^4H m ##r$cTd}|r|j} ||}||k7r|}n|S)a simplify products ``G(i)*p(-i)*G(j)*p(-j) -> p(i)*p(-i)`` Examples ======== >>> from sympy.physics.hep.gamma_matrices import GammaMatrix as G, LorentzIndex, simplify_gpgp >>> from sympy.tensor.tensor import tensor_indices, tensor_heads >>> p, q = tensor_heads('p, q', [LorentzIndex]) >>> i0,i1,i2,i3,i4,i5 = tensor_indices('i0:6', LorentzIndex) >>> ps = p(i0)*G(-i0) >>> qs = q(i0)*G(-i0) >>> simplify_gpgp(ps*qs*qs) GammaMatrix(-L_0)*p(L_0)*q(L_1)*q(-L_1) c  |j}g}g}t|D]$\}}|j|g|jz&|jDcgc]}|d|d||d||df}}t t |D]k}||tk7r|D]W}|d|k(r|d}n|d|k(r|d}n ||} | jdk(s5| jdk(sE|j||fYm|s|St} g} d} tj} d}| r&d} t|ddD]\}}|d| vr|d||dzddz k7r$||d|||dzdk7r<| j|d| j|d| j||dzd| j||dzd|s |j}tdt }d} |dk(r |j"} ||d||d| z}t |dk(r|d z}| j|n| r&| r@t|Dcgc] \}}|| vs |}}}|j| t%|| z}|S|Scc}wcc}}w) NrrTFmur) components enumerateextendrankdumrangelenrrappendsetrraddsplitrrcoeffr )exr0acomp_mapr!compr4dxp_pos1comp1elimtvhitr;taair/txjxts r"_simplify_gpgpz%simplify_gpgp.._simplify_gpgpbs]]  , +GAt OOQC M * +EGVVL!adHQqTNHQqTN;LLs:' *A!} + *a5A:UFUaZUF"6*::?uzzQHHa[) * *Iu  C"1Sb6* 2a5D=a5Aa!eHQK!O+be$ 1QU8A;(??AA1q5!%1q5!%B$T<8B6HHE&Z1&r*+<:be+tq! >A> HHRLAu$AHIkM^?sJ J J )sorted_components)r<sortrLrKs r" simplify_gpgprOQs>";z  ! ! # 2  7BH r$ct|tr+t|jDcgc] }t|c}}|St |}t |}|Scc}w)aY trace of a single line of gamma matrices Examples ======== >>> from sympy.physics.hep.gamma_matrices import GammaMatrix as G, gamma_trace, LorentzIndex >>> from sympy.tensor.tensor import tensor_indices, tensor_heads >>> p, q = tensor_heads('p, q', [LorentzIndex]) >>> i0,i1,i2,i3,i4,i5 = tensor_indices('i0:6', LorentzIndex) >>> ps = p(i0)*G(-i0) >>> qs = q(i0)*G(-i0) >>> gamma_trace(G(i0)*G(i1)) 4*metric(i0, i1) >>> gamma_trace(ps*ps) - 4*p(i0)*p(-i0) 0 >>> gamma_trace(ps*qs + ps*ps) - 4*p(i0)*p(-i0) - 4*p(i0)*q(-i0) 0 )rr r gamma_tracer&_trace_single_line)rKrJress r"rQrQsN,!W71 A78 a A Q C J 8sAcVt|t\}}|dk7r t|}||z}|S)a Simplify single-line product of gamma matrices. Examples ======== >>> from sympy.physics.hep.gamma_matrices import GammaMatrix as G, LorentzIndex, _simplify_single_line >>> from sympy.tensor.tensor import tensor_indices, TensorHead >>> p = TensorHead('p', [LorentzIndex]) >>> i0,i1 = tensor_indices('i0:2', LorentzIndex) >>> _simplify_single_line(G(i0)*G(i1)*p(-i1)*G(-i0)) + 2*G(i0)*p(-i0) 0 r)r#rkahane_simplify)rt1t2rSs r"r&r&s3 z; 7FB Qw R  R%C Jr$c*d}|j}t|tr4|jDcgc]}|||jz}}t|St|t t fr|j||z}|St|Scc}w)a Evaluate the trace of a single gamma matrix line inside a ``TensExpr``. Notes ===== If there are ``DiracSpinorIndex.auto_left`` and ``DiracSpinorIndex.auto_right`` indices trace over them; otherwise traces are not implied (explain) Examples ======== >>> from sympy.physics.hep.gamma_matrices import GammaMatrix as G, LorentzIndex, _trace_single_line >>> from sympy.tensor.tensor import tensor_indices, TensorHead >>> p = TensorHead('p', [LorentzIndex]) >>> i0,i1,i2,i3,i4,i5 = tensor_indices('i0:6', LorentzIndex) >>> _trace_single_line(G(i0)*G(i1)) 4*metric(i0, i1) >>> _trace_single_line(G(i0)*p(-i0)*G(i1)*p(-i1)) - 4*p(i0)*p(-i0) 0 cf|j}|j}t|}tj}d}t |D]}||t k(sd}nt |z|D]}||t k7sn|}||z }|dk(r|j}|r |jS|S|dzdk(r&tjtjgggS|dkDr|j} | |jd} | |dzjd} | d|| |dzdz} t| || | z} | j!|} | g}d}t |dz|D]u}| }| |jd} | d|| |dz|z| |dzdz} |t| z|| | z}|j!|}t#|d}|j%|wt'|}t)|}|S|j} t+| ||} | d|| |dz}t|}| |z}|s|S|j!|}|S)Nrrr,rF)rMr0r6rmetricr5rr;nocoeffr from_datarZeror: get_indicesr contract_metricrOr7r rR _gamma_trace1)rKr0ncompsgrEr!rInumGtcoeffr=ind1ind2aarVrsignkrWt3a2s r"_trace_single_line1z/_trace_single_line.._trace_single_line1s   !\\ Z   v A!} +  q3w' A!} + A1u 19WWF &199 -A - !8q=$$QVVRR8 8 AX AQ4##%a(DQU8'')!,D2A1q56"BR4.B##A&B4DD1q5!_ ut'')!,rUQq1uQZ'!AEF)3*b/)!D$-7''*"2u- B $B#B'BI A!A'B2A12BRBBB ##A&BIr$)expandrr rr;r r r)rKrlrJr=rs r"rRrRs25n  A!W56VV <  #AGG + < <{ A( ) GG'* *Qx =sBc&d}tj}|s|St|}|dzdk(rtjS|dk(r8|dj d}|dj d}||||zS|dk(r|dj d}|dj d}|dj d}|dj d}|||||||z||||||zz ||||||zzzSy)Nrr,rrr-)rrZr6rr]r^)r=gctrrbnind0rerfind3s r"r`r`:s: DA   AAsaxvv Avt!!$t!!$AdDM!!Avt!!$t!!$t!!$t!!$QtT]1T4=0 T4=4 &')*4qt})DEF F r$c t|tr|St|tr)t|jDcgc] }t |c}St|t r|St|t sJ|j}|D]}|jtk(rJ|j}g}|jD]5}|j|dtk(s|j|d|df7t|}t|dk(r|St!t#t |}t|t|dzz}t|} dg|z} |D] } | d| | d<dg|z} t%|D] \} } d| | d<t'||D cic]} | g}} d}dg|z}d}t(j*}gg}d}t%| D]\} }| |kr |r9|dz }|dkDr|| dz j| || j| dz n||dzrdndz}|dk(r(| |k7r#|dk(rd| z dzg|d| z <d| z g|d| z dz<| |z |vrQ| |k7r|| j| |z |dk7r0| |z t| kr| | |z r|| |z j| d}||| <|D]} | d}| d}||j|||j||||z}|||z}||k\rK||k\rQ||krW||kr]d|z |vrd|z }d|z |vrd|z }|dk\r| |s|}|dk\r| |s|}|||vr||j||||vs||j||}d} ||vr|j-|}nnL||vr|j/||}|r|d}nn,||k(rn&|dk\r | ||D]}|j| |c|r|dz }t!|j1}|}g} ||vr|j-|}nnL||vrt|dkDr|j/||}|r|d}||k\r| ||j3d| |ct|dk(r|dz}n3|}t5t7|} |D!cgc]}|| fD]}!|!|z }}}!|r|| |z dzdzrdndz}|d| zz}|Dcgc] }| d||z }}t(j8}"|D],} t(j*}#| D]}$|#t|$z}#|"|#z }".||"z}%d}&t|%tr|%jd}&nt|%t r|%}&|&r |%St;d|%z}%|%Scc}wcc} wcc}!}wcc}w) a This function cancels contracted elements in a product of four dimensional gamma matrices, resulting in an expression equal to the given one, without the contracted gamma matrices. Parameters ========== `expression` the tensor expression containing the gamma matrices to simplify. Notes ===== If spinor indices are given, the matrices must be given in the order given in the product. Algorithm ========= The idea behind the algorithm is to use some well-known identities, i.e., for contractions enclosing an even number of `\gamma` matrices `\gamma^\mu \gamma_{a_1} \cdots \gamma_{a_{2N}} \gamma_\mu = 2 (\gamma_{a_{2N}} \gamma_{a_1} \cdots \gamma_{a_{2N-1}} + \gamma_{a_{2N-1}} \cdots \gamma_{a_1} \gamma_{a_{2N}} )` for an odd number of `\gamma` matrices `\gamma^\mu \gamma_{a_1} \cdots \gamma_{a_{2N+1}} \gamma_\mu = -2 \gamma_{a_{2N+1}} \gamma_{a_{2N}} \cdots \gamma_{a_{1}}` Instead of repeatedly applying these identities to cancel out all contracted indices, it is possible to recognize the links that would result from such an operation, the problem is thus reduced to a simple rearrangement of free gamma matrices. Examples ======== When using, always remember that the original expression coefficient has to be handled separately >>> from sympy.physics.hep.gamma_matrices import GammaMatrix as G, LorentzIndex >>> from sympy.physics.hep.gamma_matrices import kahane_simplify >>> from sympy.tensor.tensor import tensor_indices >>> i0, i1, i2 = tensor_indices('i0:3', LorentzIndex) >>> ta = G(i0)*G(-i0) >>> kahane_simplify(ta) Matrix([ [4, 0, 0, 0], [0, 4, 0, 0], [0, 0, 4, 0], [0, 0, 0, 4]]) >>> tb = G(i0)*G(i1)*G(-i0) >>> kahane_simplify(tb) -2*GammaMatrix(i1) >>> t = G(i0)*G(-i0) >>> kahane_simplify(t) Matrix([ [4, 0, 0, 0], [0, 4, 0, 0], [0, 0, 4, 0], [0, 0, 0, 4]]) >>> t = G(i0)*G(-i0) >>> kahane_simplify(t) Matrix([ [4, 0, 0, 0], [0, 4, 0, 0], [0, 0, 4, 0], [0, 0, 0, 4]]) If there are no contractions, the same expression is returned >>> tc = G(i0)*G(i1) >>> kahane_simplify(tc) GammaMatrix(i0)*GammaMatrix(i1) References ========== [1] Algorithm for Reducing Contracted Products of gamma Matrices, Joseph Kahane, Journal of Mathematical Physics, Vol. 9, No. 10, October 1968. rrr,NFTr.r)rrr rrUr r rrfreer4 index_typesrr7sortedr6minmapr1r5rrpopremovekeysinsertlistreversedr]r)'rarggammasgammarur4dum_pair first_dum_pos total_numbernumber_of_contractionsfree_posr! index_is_freeindxlinkscum_sign cum_sign_listblock_free_countresulting_coeffresulting_indicesconnected_componentsis_freepos1pos2linkpos1linkpos2pointerprevious_pointer next_onesriprepend_indicesexpr1expr2expriresulting_expr temp_exprrIrKrVs' r"rUrUQsb*c"*g&I#-IJJ*f% j' ** * __F.+---. ??D CNN3  ! !(1+ ., > JJ Xa[1 23 +C 3x1}C &Mt9s3xz)L Xvl"H 11 GL(MT?&4!% d1g&"-> ?qQU ?E ?HF<'MeeO   .&$ 7 }     ! !#a!e ##A&aA& .2 ;H1$m);r>#%a46(E"Q$K%'TFE"Q$q&MH & %!HOOAL1#q(8|c-&88(X6!!h,/66q9 # aM&$^1-tt d 4  d 4 ------ | #  | #  m #  m #  xKE !({H xKE !({H q=x!8H a< h 7H 5? * (O " "8 , 5? * (O " "8 ,c1-nG  e  '*I  y (   - ." lG  & &  Q;8G,8' - (7+, -' J !ejjl#"%!IIg. 9,y>A%$$%56& #A,-'HW,=,I&&q(7*;rs6"$2EEE~1E 333A6TC #:$ Vr<.XvF.{ r$