K i[dZddlmZddlmZddlmZddlmZddl m Z ddl m Z ddl mZdd lmZdd lmZmZdd lmZdd lmZdd lmZddlmZmZddlmZddlm Z m!Z!m"Z"m#Z#ddl$m%Z%gdZ&GddeZ'Gdde'Z(GddeZ)GddeZ*dZ+dZ,dZ-dZ.dZ/d Z0d(d"Z1d#Z2d$Z3d%Z4d&Z5d'Z6y!))zClebsch-Gordon Coefficients.)Sum)Add)Expr)expand)Mul)Pow)Eq)S)Wildsymbols)sympify)sqrt) Piecewise) prettyForm stringPict)KroneckerDelta)clebsch_gordan wigner_3j wigner_6j wigner_9j) PRECEDENCE)CGWigner3jWigner6jWigner9jcg_simpceZdZdZdZdZedZedZedZ edZ edZ ed Z ed Z d Zd Zd Zy)raClass for the Wigner-3j symbols. Explanation =========== Wigner 3j-symbols are coefficients determined by the coupling of two angular momenta. When created, they are expressed as symbolic quantities that, for numerical parameters, can be evaluated using the ``.doit()`` method [1]_. Parameters ========== j1, m1, j2, m2, j3, m3 : Number, Symbol Terms determining the angular momentum of coupled angular momentum systems. Examples ======== Declare a Wigner-3j coefficient and calculate its value >>> from sympy.physics.quantum.cg import Wigner3j >>> w3j = Wigner3j(6,0,4,0,2,0) >>> w3j Wigner3j(6, 0, 4, 0, 2, 0) >>> w3j.doit() sqrt(715)/143 See Also ======== CG: Clebsch-Gordan coefficients References ========== .. [1] Varshalovich, D A, Quantum Theory of Angular Momentum. 1988. Tc Ztt||||||f}tj|g|SNmapr r__new__)clsj1m1j2m2j3m3argss ^/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/physics/quantum/cg.pyr"zWigner3j.__new__Qs/7RRR45||C'$''c |jdSNrr*selfs r+r$z Wigner3j.j1Uyy|r,c |jdSNr/r0s r+r%z Wigner3j.m1Yr2r,c |jdSNr/r0s r+r&z Wigner3j.j2]r2r,c |jdSNr/r0s r+r'z Wigner3j.m2ar2r,c |jdSNr/r0s r+r(z Wigner3j.j3er2r,c |jdSNr/r0s r+r)z Wigner3j.m3ir2r,c<td|jD S)Nc34K|]}|jywr is_number.0args r+ z'Wigner3j.is_symbolic..o:s}}:allr*r0s r+ is_symboliczWigner3j.is_symbolicm: ::::r,c|j|j|j|jf|j|j|j|jf|j|j |j|j ffd}d}dgdz}tdD]#tfdtdD|<%d}tdD]}d}tdD]|} || jz } | dz} | | z } t| jd| z} t| jd| z} || }dt|jd|z}t|j| }||}t|D]} t|jd}t|j|}t|j}|S)Nr8r5r;c3JK|]}|jywrwidthrGijms r+rIz#Wigner3j._pretty..z ? ^^DGG $gnnTWW&= > ^^DGG $gnnTWW&= > @tAvq =A<58< >$''477DGGTWWdggNNr,N)__name__ __module__ __qualname____doc__is_commutativer"propertyr$r%r&r'r(r)rNrortrzr,r+rr&s&PN(;;!F Or,rc2eZdZdZeddz ZdZdZdZy)raClass for Clebsch-Gordan coefficient. Explanation =========== Clebsch-Gordan coefficients describe the angular momentum coupling between two systems. The coefficients give the expansion of a coupled total angular momentum state and an uncoupled tensor product state. The Clebsch-Gordan coefficients are defined as [1]_: .. math :: C^{j_3,m_3}_{j_1,m_1,j_2,m_2} = \left\langle j_1,m_1;j_2,m_2 | j_3,m_3\right\rangle Parameters ========== j1, m1, j2, m2 : Number, Symbol Angular momenta of states 1 and 2. j3, m3: Number, Symbol Total angular momentum of the coupled system. Examples ======== Define a Clebsch-Gordan coefficient and evaluate its value >>> from sympy.physics.quantum.cg import CG >>> from sympy import S >>> cg = CG(S(3)/2, S(3)/2, S(1)/2, -S(1)/2, 1, 1) >>> cg CG(3/2, 3/2, 1/2, -1/2, 1, 1) >>> cg.doit() sqrt(3)/2 >>> CG(j1=S(1)/2, m1=-S(1)/2, j2=S(1)/2, m2=+S(1)/2, j3=1, m3=0).doit() sqrt(2)/2 Compare [2]_. See Also ======== Wigner3j: Wigner-3j symbols References ========== .. [1] Varshalovich, D A, Quantum Theory of Angular Momentum. 1988. .. [2] `Clebsch-Gordan Coefficients, Spherical Harmonics, and d Functions `_ in P.A. Zyla *et al.* (Particle Data Group), Prog. Theor. Exp. Phys. 2020, 083C01 (2020). rr5c |jr tdt|j|j|j |j |j|jSrv) rNrwrr$r&r(r%r'r)rxs r+rzzCG.doitsD   => >dggtww$''477SSr,c|j|j|j|j|jfd}|j|j |j fd}t|j|j}t|jd}t|jd}||jk(s+t|jd||jz z}||jk(s+t|jd||jz z}tdd|zz}t|j|}t|j|}|S)N,) delimiterr[C) _print_seqr$r%r&r'r(r)r^rTrr`r_rraabove)r1rdr*bottoppadrjs r+roz CG._prettys1  WWdggtww 0C!A  $''477!3s C#))+syy{+#((3-(#((3-(ciik!ciiS399;->(?@ACciik!ciiS399;->(?@AC sSW} %  %  %r,c t|j|j|j|j|j |j |jf}dt|zS)NzC^{%s,%s}_{%s,%s,%s,%s}) r!r\r(r)r$r%r&r'rqrrs r+rtz CG._latexsKGNNTWWdggtwwGGTWWdgg%/0)E%L88r,N) r{r|r}r~r precedencerzrortrr,r+rrs)5lE"Q&JT $9r,rceZdZdZdZedZedZedZedZ edZ edZ ed Z d Z d Zd Zy )rzaClass for the Wigner-6j symbols See Also ======== Wigner3j: Wigner-3j symbols c Ztt||||||f}tj|g|Srr )r#r$r&j12r(rWj23r*s r+r"zWigner6j.__new__s/7RS"a56||C'$''r,c |jdSr.r/r0s r+r$z Wigner6j.j1r2r,c |jdSr4r/r0s r+r&z Wigner6j.j2r2r,c |jdSr7r/r0s r+rz Wigner6j.j12 r2r,c |jdSr:r/r0s r+r(z Wigner6j.j3r2r,c |jdSr=r/r0s r+rWz Wigner6j.jr2r,c |jdSr@r/r0s r+rz Wigner6j.j23r2r,c<td|jD S)Nc34K|]}|jywrrDrFs r+rIz'Wigner6j.is_symbolic..rJrKrLr0s r+rNzWigner6j.is_symbolicrOr,c|j|j|j|jf|j|j|j|jf|j|j |j|j ffd}d}dgdz}tdD]#tfdtdD|<%d}tdD]}d}tdD]|} || jz } | dz} | | z } t| jd| z} t| jd| z} || }dt|jd|z}t|j| }||}t|D]} t|jd}t|j|}t|jdd }|S) Nr8r5rQr;c3JK|]}|jywrrSrUs r+rIz#Wigner6j._pretty..)rYrZr[{}r`r_)r\r$r(r&rWrrr]r^rTrr_r`rarbrcs @@r+rozWigner6j._pretty!snnTWW%w~~dgg'> ? ^^DGG $gnnTVV&< = ^^DHH %w~~dhh'? @ BtAvq =A<58< >$''488TWWdffdhhOOr,N)r{r|r}r~r"rr$r&rr(rWrrNrortrzrr,r+rrs(;;!F Pr,rceZdZdZdZedZedZedZedZ edZ edZ ed Z ed Z ed Zed Zd ZdZdZy)rzaClass for the Wigner-9j symbols See Also ======== Wigner3j: Wigner-3j symbols c `tt||||||||| f } tj|g| Srr ) r#r$r&rr(j4j34j13j24rWr*s r+r"zWigner9j.__new__Ys57RS"b#sCCD||C'$''r,c |jdSr.r/r0s r+r$z Wigner9j.j1]r2r,c |jdSr4r/r0s r+r&z Wigner9j.j2ar2r,c |jdSr7r/r0s r+rz Wigner9j.j12er2r,c |jdSr:r/r0s r+r(z Wigner9j.j3ir2r,c |jdSr=r/r0s r+rz Wigner9j.j4mr2r,c |jdSr@r/r0s r+rz Wigner9j.j34qr2r,c |jdS)Nr/r0s r+rz Wigner9j.j13ur2r,c |jdS)Nr/r0s r+rz Wigner9j.j24yr2r,c |jdS)Nr/r0s r+rWz Wigner9j.j}r2r,c<td|jD S)Nc34K|]}|jywrrDrFs r+rIz'Wigner9j.is_symbolic..rJrKrLr0s r+rNzWigner9j.is_symbolicrOr,c|j|j|j|j|j|jf|j|j|j|j |j|j f|j|j|j|j|j|jffd}d}dgdz}tdD]#tfdtdD|<%d}tdD]}d}tdD]|} || jz } | dz} | | z } t| jd| z} t| jd| z} || }dt|jd|z}t|j| }||}t|D]} t|j!d}t|j!|}t|j#dd }|S) Nr8r5rQr;c3JK|]}|jywrrSrUs r+rIz#Wigner9j._pretty..rYrZr[rrr)r\r$r(rr&rrrrrWr]r^rTrr_r`rarbrcs @@r+rozWigner9j._prettys ^^!..17>>$((3K M ^^!..17>>$((3K M ^^DHH %w~~dhh'?PTPVPVAW X  Z tAvq =A<58< >$''488TWWdggtxxQUQYQY[_[c[ceiekekllr,N)r{r|r}r~r"rr$r&rr(rrrrrWrNrortrzrr,r+rrPs(;;$L mr,rc`t|tr t|St|tr t |St|t r)t |j Dcgc] }t|c}St|tr)tt|j|jS|Scc}w)aSimplify and combine CG coefficients. Explanation =========== This function uses various symmetry and properties of sums and products of Clebsch-Gordan coefficients to simplify statements involving these terms [1]_. Examples ======== Simplify the sum over CG(a,alpha,0,0,a,alpha) for all alpha to 2*a+1 >>> from sympy.physics.quantum.cg import CG, cg_simp >>> a = CG(1,1,0,0,1,1) >>> b = CG(1,0,0,0,1,0) >>> c = CG(1,-1,0,0,1,-1) >>> cg_simp(a+b+c) 3 See Also ======== CG: Clebsh-Gordan coefficients References ========== .. [1] Varshalovich, D A, Quantum Theory of Angular Momentum. 1988. ) isinstancer _cg_simp_addr _cg_simp_sumrr*rrbaseexp)erHs r+rrsB!SA As A As QVV4cWS\455 As 7166?AEE** 5sB+cg}g}t|}|jD]}|jtrt |t r|j t|Ct |trpd}|jD]&}t |t r|t|z}"||z}(|jtr|j ||j ||j ||j |t|\}}|j |t|\}}|j |t|\}}|j |t|t|zS)aTakes a sum of terms involving Clebsch-Gordan coefficients and simplifies the terms. Explanation =========== First, we create two lists, cg_part, which is all the terms involving CG coefficients, and other_part, which is all other terms. The cg_part list is then passed to the simplification methods, which return the new cg_part and any additional terms that are added to other_part r5) rr*hasrrrappendrr_check_varsh_871_1_check_varsh_871_2_check_varsh_872_9r)rcg_part other_partrHtermstermothers r+rrsAGJq Avv# 772;#s#!!,s"34C%HH&D!$,d!33  & 99R=NN5)%%e,s#   c "%#((0NGUe'0NGUe'0NGUe =3 + ++r,c ttd\}}}}|t|||d||z}d|zdzt|dz}|t |z }d|zdz}||z} t |||||||||f||f|| S)N)aalphabltrr8r5)r!r rrabs_check_cg_simp) term_listrrrrexprsimpsign build_expr index_exprs r+rrs$ 9:OAua bE1aE* *D aC!G^Aq) )D c"g:D1qJUJ $dB Aua;LqRSfV`bl mmr,c  ttd\}}}}|t|||| |dz}td|zdzt |dz}d||z z|zt |z }d|zdz}||z} t |||||||||f||f|| S)N)rrcrrr8r5rQ)r!r rrrrr) rrrrrrrrrrs r+rrs$ 9:OAua bE1ufa+ +D !a=1- -D !e) R B 'D1qJUJ $dB Aua;LqRSfV`bl mmr,cttd\ }}}}}}}}} | t||||||dzz} tj} | t | z } t ||z } t ||z}||zdzt | | |kDfdt| |f||| kDfz }||z|z }t| | | | |||||||| f||||f|| \}}t ||z } ||z}|dz| z | |zdzz}|| z | |zz|z|z}t| | | | |||||||| f||||f|| \}}t||||||t||||||z} t||t||z} tj} t ||z } t ||z}||zdzt | | |kDfdt| |f||| kDfz }||z|z }t| | | tj|||||||||f||||||f|| \}}t ||z } ||z}|dz| z | |zdzz}|| z | |zz|z|z}t| | | tj|||||||||f||||||f|| \}}|||z|zfS)N) rralphaprbetabetaprgammarr8r5r) r!r rr Onerrr rr)rrrrrrrrrrrrrxyrrother1other2other3other4s r+rr)s58@J6K2Aufaua bE1dAu-q0 0D 55D c"g:D AE A EDLAQY1q5zAr!Qx=1a!e*MMJQJ&tT4YESTVZ\]_dfhHilmotvwy}k~AKMWXIv AE A AAa%!)a!eai(Ja%!a%1$u,J&tT4YESTVZ\]_dfhHilmotvwy}k~AKMWXIv a4E *2aE1e+L LD % (e)D DD 55D AE A EDLAQY1q5zAr!Qx=1a!e*MMJQJ&tT4 AuV\^_aeglnoqvKwz{~CEKMNPTV[z\^hjtuIv AE A AAa%!)a!eai(Ja%!a%1$u,J&tT4 AuV\^_aeglnoqvKwz{~CEKMNPTV[z\^hjtuIv fvo. ..r,c Td} d} | t|kr~t|| |t|} | | dz } 0|j| js| dz } Q|D cgc] } | | | f } } dg|j| z}t | t|D] }t|||j| t|t|z |j| |j| f}|]|j| j|js||j|dj| j||j||j| j|f||j| j|< t d|Dst |Dcgc]}t|dc}}|Dcgc]}|d }}|j|j|Dcgc]}|j|c}|D]7}t|d|kDs|j|d||dzz |dz9| |||zj| zz } n| dz } | t|kr~|| fScc} wcc}wcc}wcc}w)a Checks for simplifications that can be made, returning a tuple of the simplified list of terms and any terms generated by simplification. Parameters ========== expr: expression The expression with Wild terms that will be matched to the terms in the sum simp: expression The expression with Wild terms that is substituted in place of the CG terms in the case of simplification sign: expression The expression with Wild terms denoting the sign that is on expr that must match lt: expression The expression with Wild terms that gives the leading term of the matched expr term_list: list A list of all of the terms is the sum to be simplified variables: list A list of all the variables that appears in expr dep_variables: list A list of the variables that must match for all the terms in the sum, i.e. the dependent variables build_index_expr: expression Expression with Wild terms giving the number of elements in cg_index index_expr: expression Expression with Wild terms giving the index terms have when storing them to cg_index rNr5)rc3$K|]}|du ywrr)rGrVs r+rIz!_check_cg_simp..s/19/sr8r;) len _check_cgsubsrEr]anyminrsortreversepopr)rrrrr variables dep_variablesbuild_index_exprrrrVsub_1rsub_depcg_indexrWsub_2rmin_ltindicess r+rrVsRJ A c)n )A,c)n= = FA $$U+55 FA *78QAuQx=886*//66q#i.) [AilDIIg,>IQTUbQc@ckoktktuzk{~B~G~GHO~PkQRE}??7+007AA=> "a@P@U@UV]@^@c@cdi@jlnlslstylz}A}F}FGN}O}T}TUZ}[>[HZ__W-2259 :  [/h//X?TCQL?@F,45DQ5G5 LLN OO (/ 11immA 1  KtAw<&($$tAwQ'?a&HJ K &$t)!1!1%!88 8J FA9 c)n : j  +9@5 2sJ<J J  J%Nc|j|}|y|6t|ts td|d|dj |k(syt ||k(r|Sy)z2Checks whether a term matches the given expressionNzsign must be a tuplerr5)matchrrq TypeErrorrr)cg_termrlengthrmatchess r+rrshmmD!G $&23 3Aw47..11  7|vr,cHt|}t|}t|}|Sr)_check_varsh_sum_871_1_check_varsh_sum_871_2_check_varsh_sum_872_4)rs r+rrs%q!Aq!Aq!A Hr,c  td}td}td}|jtt |||d|||| |f}|2t |dk(r$d|zdzt |dzj|S|S)Nrrrrr8r5)r r rrrrrr)rrrrrs r+rrs S A G E S A GGC1eQ1e4uqb!nE FE SZ1_1q.A..44U;; Hr,c 0td}td}td}|jtd||z zt |||| |dz|| |f}|;t |dk(r-t d|zdzt|dzj|S|S)NrrrrQrr8r5) r r rrrrrrr)rrrrrs r+rrs S A G E S A GG R1u9 bE1ufa; ;eaR^L NE SZ1_QqS1W nQ2288?? Hr,c Btd}td}td}td}td}td}td}td}t||||||} t||||||} |jt | | z|| |f|| |f} | 6t | d k(r(t ||t ||zj| S|jt | d z|| |f|| |f} | t | d k(rtjS|S) Nrrrrrcprgammaprr8r>) r r rrrrrrr r) rrrrrrrrrcg1cg2match1match2s r+rrs G E 6?D S A S A S A dB ME (^F Qq$5 )C Qq$F +C WWSS51"a.4!Q-@ AF c&kQ.q"%nUF&CCII&QQ WWSa%!Q$A? @F c&kQ.uu Hr,c4t|tr|fddfSg}d}t|ttfs t dt|tro|j j rY|j j r=t|j Dcgc]}|j|jc}n|fddfSt|trI|jD])}t|tr|j|%||z}+|||t|z fSycc}w)Nr5z term must be CG, Add, Mul or Pow) rrrrNotImplementedErrorrrEr]rrr*r)rcgcoeffrnrHs r+_cg_listrs$w1} B E dS#J '!"DEE$!3!3 88  ,1$((O =qbii " =7Aq= $99 C#r" #    5%E ***  >s"Dr)7r~sympy.concrete.summationsrsympy.core.addrsympy.core.exprrsympy.core.functionrsympy.core.mulrsympy.core.powerrsympy.core.relationalr sympy.core.singletonr sympy.core.symbolr r sympy.core.sympifyr (sympy.functions.elementary.miscellaneousr$sympy.functions.elementary.piecewiser sympy.printing.pretty.stringpictrr(sympy.functions.special.tensor_functionsrsympy.physics.wignerrrrrsympy.printing.precedencer__all__rrrrrrrrrrrrrrrrrr,r+r#s #) & $"-&9:CCPP0 xOtxOvS9S9lVPtVPremtemP*Z+,\nn*/ZH!V     (+r,