
    \h[                        S r SSKJr  SSKJr  SSKJr  SSKJr  SSK	J
r
  SSKJr  SSKJr  SS	KJr  SS
KJrJr  SSKJr  SSKJr  SSKJr  SSKJrJr  SSKJr  SSKJ r J!r!J"r"J#r#  SSK$J%r%  / SQr& " S S\5      r' " S S\'5      r( " S S\5      r) " S S\5      r*S r+S r,S r-S r.S r/S  r0S(S" jr1S# r2S$ r3S% r4S& r5S' r6g!))zClebsch-Gordon Coefficients.    )Sum)Add)Expr)expand)Mul)Pow)Eq)S)Wildsymbols)sympify)sqrt)	Piecewise)
prettyForm
stringPict)KroneckerDelta)clebsch_gordan	wigner_3j	wigner_6j	wigner_9j)
PRECEDENCE)CGWigner3jWigner6jWigner9jcg_simpc                       \ rS rSrSrSrS r\S 5       r\S 5       r	\S 5       r
\S 5       r\S	 5       r\S
 5       r\S 5       rS rS rS rSrg)r   &   a8  Class 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           	      V    [        [        XX4XV45      n[        R                  " U /UQ76 $ Nmapr   r   __new__)clsj1m1j2m2j3m3argss           P/var/www/auris/envauris/lib/python3.13/site-packages/sympy/physics/quantum/cg.pyr#   Wigner3j.__new__Q   s)    7RRR45||C'$''    c                      U R                   S   $ Nr   r+   selfs    r,   r%   Wigner3j.j1U       yy|r.   c                      U R                   S   $ N   r1   r2   s    r,   r&   Wigner3j.m1Y   r5   r.   c                      U R                   S   $ N   r1   r2   s    r,   r'   Wigner3j.j2]   r5   r.   c                      U R                   S   $ N   r1   r2   s    r,   r(   Wigner3j.m2a   r5   r.   c                      U R                   S   $ N   r1   r2   s    r,   r)   Wigner3j.j3e   r5   r.   c                      U R                   S   $ N   r1   r2   s    r,   r*   Wigner3j.m3i   r5   r.   c                 D    [        S U R                   5       5      (       + $ )Nc              3   8   #    U  H  oR                   v   M     g 7fr    	is_number.0args     r,   	<genexpr>'Wigner3j.is_symbolic.<locals>.<genexpr>o        :	}}	   allr+   r2   s    r,   is_symbolicWigner3j.is_symbolicm       :		::::r.   c                   ^^ UR                  U R                  5      UR                  U R                  5      4UR                  U R                  5      UR                  U R                  5      4UR                  U R
                  5      UR                  U R                  5      44mSnSnS/S-  n[        S5       H%  m[        UU4S j[        S5       5       5      UT'   M'     S n[        S5       H  nS n[        S5       H  mTT   U   n	UT   U	R                  5       -
  n
U
S-  nX-
  n[        U	R                  SU-  5      6 n	[        U	R                  SU-  5      6 n	Uc  U	nMe  [        UR                  SU-  5      6 n[        UR                  U	5      6 nM     Uc  UnM  [        U5       H  n[        UR                  S5      6 nM     [        UR                  U5      6 nM     [        UR                  5       6 nU$ )Nr<   r8   r@   c              3   P   >#    U  H  nTT   U   R                  5       v   M     g 7fr    widthrO   ijms     r,   rQ   #Wigner3j._pretty.<locals>.<genexpr>z   !     <8a!A$q'--//8   #& )_printr%   r&   r'   r(   r)   r*   rangemaxr^   r   rightleftbelowparensr3   printerr+   hsepvsepmaxwDr`   D_rowswdeltawleftwright_ra   rb   s                 @@r,   _prettyWigner3j._prettyr   s   nnTWW%w~~dgg'>?^^DGG$gnnTWW&=>^^DGG$gnnTWW&=>@ tAvqA<58<<DG qAE1XaDGa1779,
F
 34s5y 12=E"EKKD$9:"EKKN3  y4[- !AGGEN+A+ , 
#r.   c           	          [        UR                  U R                  U R                  U R                  U R
                  U R                  U R                  45      nS[        U5      -  $ )NzH\left(\begin{array}{ccc} %s & %s & %s \\ %s & %s & %s \end{array}\right))	r"   rg   r%   r'   r)   r&   r(   r*   tupler3   ro   r+   labels       r,   _latexWigner3j._latex   sP    GNNTWWdggtwwGGTWWdgg%/ 0Z%L 	r.   c                     U R                   (       a  [        S5      e[        U R                  U R                  U R
                  U R                  U R                  U R                  5      $ NzCoefficients must be numerical)	rW   
ValueErrorr   r%   r'   r)   r&   r(   r*   r3   hintss     r,   doitWigner3j.doit   sD    =>>$''477DGGTWWdggNNr.    N)__name__
__module____qualname____firstlineno____doc__is_commutativer#   propertyr%   r&   r'   r(   r)   r*   rW   rz   r   r   __static_attributes__r   r.   r,   r   r   &   s    &P N(             ; ;!FOr.   r   c                   :    \ rS rSrSr\S   S-
  rS rS rS r	Sr
g	)
r      a_  Class 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
    <https://pdg.lbl.gov/2020/reviews/rpp2020-rev-clebsch-gordan-coefs.pdf>`_
    in P.A. Zyla *et al.* (Particle Data Group), Prog. Theor. Exp. Phys.
    2020, 083C01 (2020).
r   r8   c                     U R                   (       a  [        S5      e[        U R                  U R                  U R
                  U R                  U R                  U R                  5      $ r   )	rW   r   r   r%   r'   r)   r&   r(   r*   r   s     r,   r   CG.doit   sD    =>>dggtww$''477SSr.   c                    UR                  U R                  U R                  U R                  U R                  4SS9nUR                  U R
                  U R                  4SS9n[        UR                  5       UR                  5       5      n[        UR                  S5      6 n[        UR                  S5      6 nXSR                  5       :X  d*  [        UR                  SXSR                  5       -
  -  5      6 nXTR                  5       :X  d*  [        UR                  SXTR                  5       -
  -  5      6 n[        SSU-  -   5      n[        UR                  U5      6 n[        UR                  U5      6 nU$ )N,)	delimiterrf   C)
_print_seqr%   r&   r'   r(   r)   r*   ri   r^   r   rk   rj   r   rl   above)r3   ro   r+   bottoppadru   s          r,   rz   
CG._pretty   s)     WWdggtww0C ! A  $''477!3s C#))+syy{+#((3-(#((3-(iik!ciiS99;->(?@ACiik!ciiS99;->(?@ACsSW}%%%r.   c           	          [        UR                  U R                  U R                  U R                  U R
                  U R                  U R                  45      nS[        U5      -  $ )NzC^{%s,%s}_{%s,%s,%s,%s})	r"   rg   r)   r*   r%   r&   r'   r(   r}   r~   s       r,   r   	CG._latex   sK    GNNTWWdggtwwGGTWWdgg%/ 0)E%L88r.   r   N)r   r   r   r   r   r   
precedencer   rz   r   r   r   r.   r,   r   r      s)    5l E"Q&JT
$9r.   r   c                       \ rS rSrSrS r\S 5       r\S 5       r\S 5       r	\S 5       r
\S 5       r\S	 5       r\S
 5       rS rS rS rSrg)r      zQClass for the Wigner-6j symbols

See Also
========

Wigner3j: Wigner-3j symbols

c           	      V    [        [        XX4XV45      n[        R                  " U /UQ76 $ r    r!   )r$   r%   r'   j12r)   ra   j23r+   s           r,   r#   Wigner6j.__new__   s)    7RSa56||C'$''r.   c                      U R                   S   $ r0   r1   r2   s    r,   r%   Wigner6j.j1  r5   r.   c                      U R                   S   $ r7   r1   r2   s    r,   r'   Wigner6j.j2  r5   r.   c                      U R                   S   $ r;   r1   r2   s    r,   r   Wigner6j.j12  r5   r.   c                      U R                   S   $ r?   r1   r2   s    r,   r)   Wigner6j.j3  r5   r.   c                      U R                   S   $ rC   r1   r2   s    r,   ra   
Wigner6j.j  r5   r.   c                      U R                   S   $ rG   r1   r2   s    r,   r   Wigner6j.j23  r5   r.   c                 D    [        S U R                   5       5      (       + $ )Nc              3   8   #    U  H  oR                   v   M     g 7fr    rL   rN   s     r,   rQ   'Wigner6j.is_symbolic.<locals>.<genexpr>  rS   rT   rU   r2   s    r,   rW   Wigner6j.is_symbolic  rY   r.   c                   ^^ UR                  U R                  5      UR                  U R                  5      4UR                  U R                  5      UR                  U R                  5      4UR                  U R
                  5      UR                  U R                  5      44mSnSnS/S-  n[        S5       H%  m[        UU4S j[        S5       5       5      UT'   M'     S n[        S5       H  nS n[        S5       H  mTT   U   n	UT   U	R                  5       -
  n
U
S-  nX-
  n[        U	R                  SU-  5      6 n	[        U	R                  SU-  5      6 n	Uc  U	nMe  [        UR                  SU-  5      6 n[        UR                  U	5      6 nM     Uc  UnM  [        U5       H  n[        UR                  S5      6 nM     [        UR                  U5      6 nM     [        UR                  SSS	96 nU$ )
Nr<   r8   r[   r@   c              3   P   >#    U  H  nTT   U   R                  5       v   M     g 7fr    r]   r_   s     r,   rQ   #Wigner6j._pretty.<locals>.<genexpr>)  rd   re   rf   {}rk   rj   )rg   r%   r)   r'   ra   r   r   rh   ri   r^   r   rj   rk   rl   rm   rn   s                 @@r,   rz   Wigner6j._pretty!  s   nnTWW%w~~dgg'>?^^DGG$gnnTVV&<=^^DHH%w~~dhh'?@B tAvqA<58<<DG qAE1XaDGa1779,
F
 34s5y 12=E"EKKD$9:"EKKN3  y4[- !AGGEN+A+ , c56r.   c           	          [        UR                  U R                  U R                  U R                  U R
                  U R                  U R                  45      nS[        U5      -  $ )NzJ\left\{\begin{array}{ccc} %s & %s & %s \\ %s & %s & %s \end{array}\right\})	r"   rg   r%   r'   r   r)   ra   r   r}   r~   s       r,   r   Wigner6j._latexD  sP    GNNTWWdggtxxGGTVVTXX%/ 0\%L 	r.   c                     U R                   (       a  [        S5      e[        U R                  U R                  U R
                  U R                  U R                  U R                  5      $ r   )	rW   r   r   r%   r'   r   r)   ra   r   r   s     r,   r   Wigner6j.doitJ  sD    =>>$''488TWWdffdhhOOr.   r   N)r   r   r   r   r   r#   r   r%   r'   r   r)   ra   r   rW   rz   r   r   r   r   r.   r,   r   r      s    (             ; ;!FPr.   r   c                       \ rS rSrSrS r\S 5       r\S 5       r\S 5       r	\S 5       r
\S 5       r\S	 5       r\S
 5       r\S 5       r\S 5       r\S 5       rS rS rS rSrg)r   iP  zQClass for the Wigner-9j symbols

See Also
========

Wigner3j: Wigner-3j symbols

c
                 Z    [        [        XX4XVXxU	4	5      n
[        R                  " U /U
Q76 $ r    r!   )r$   r%   r'   r   r)   j4j34j13j24ra   r+   s              r,   r#   Wigner9j.__new__Y  s-    7RSbsCD||C'$''r.   c                      U R                   S   $ r0   r1   r2   s    r,   r%   Wigner9j.j1]  r5   r.   c                      U R                   S   $ r7   r1   r2   s    r,   r'   Wigner9j.j2a  r5   r.   c                      U R                   S   $ r;   r1   r2   s    r,   r   Wigner9j.j12e  r5   r.   c                      U R                   S   $ r?   r1   r2   s    r,   r)   Wigner9j.j3i  r5   r.   c                      U R                   S   $ rC   r1   r2   s    r,   r   Wigner9j.j4m  r5   r.   c                      U R                   S   $ rG   r1   r2   s    r,   r   Wigner9j.j34q  r5   r.   c                      U R                   S   $ )N   r1   r2   s    r,   r   Wigner9j.j13u  r5   r.   c                      U R                   S   $ )N   r1   r2   s    r,   r   Wigner9j.j24y  r5   r.   c                      U R                   S   $ )N   r1   r2   s    r,   ra   
Wigner9j.j}  r5   r.   c                 D    [        S U R                   5       5      (       + $ )Nc              3   8   #    U  H  oR                   v   M     g 7fr    rL   rN   s     r,   rQ   'Wigner9j.is_symbolic.<locals>.<genexpr>  rS   rT   rU   r2   s    r,   rW   Wigner9j.is_symbolic  rY   r.   c                   ^^ UR                  U R                  5      UR                  U R                  5      UR                  U R                  5      4UR                  U R                  5      UR                  U R
                  5      UR                  U R                  5      4UR                  U R                  5      UR                  U R                  5      UR                  U R                  5      44mSnSnS/S-  n[        S5       H%  m[        UU4S j[        S5       5       5      UT'   M'     S n[        S5       H  nS n[        S5       H  mTT   U   n	UT   U	R                  5       -
  n
U
S-  nX-
  n[        U	R                  SU-  5      6 n	[        U	R                  SU-  5      6 n	Uc  U	nMe  [        UR                  SU-  5      6 n[        UR                  U	5      6 nM     Uc  UnM  [        U5       H  n[        UR!                  S5      6 nM     [        UR!                  U5      6 nM     [        UR#                  SSS	96 nU$ )
Nr<   r8   r[   r@   c              3   P   >#    U  H  nTT   U   R                  5       v   M     g 7fr    r]   r_   s     r,   rQ   #Wigner9j._pretty.<locals>.<genexpr>  rd   re   rf   r   r   r   )rg   r%   r)   r   r'   r   r   r   r   ra   rh   ri   r^   r   rj   rk   rl   rm   rn   s                 @@r,   rz   Wigner9j._pretty  s   ^^!..17>>$((3KM^^!..17>>$((3KM^^DHH%w~~dhh'?PTPVPVAWXZ tAvqA<58<<DG qAE1XaDGa1779,
F
 34s5y 12=E"EKKD$9:"EKKN3  y4[- !AGGEN+A+ , c56r.   c                    [        UR                  U R                  U R                  U R                  U R
                  U R                  U R                  U R                  U R                  U R                  4	5      nS[        U5      -  $ )NzZ\left\{\begin{array}{ccc} %s & %s & %s \\ %s & %s & %s \\ %s & %s & %s \end{array}\right\})r"   rg   r%   r'   r   r)   r   r   r   r   ra   r}   r~   s       r,   r   Wigner9j._latex  sc    GNNTWWdggtxx488TXXtvv%? @l%L 	r.   c                    U R                   (       a  [        S5      e[        U R                  U R                  U R
                  U R                  U R                  U R                  U R                  U R                  U R                  5	      $ r   )rW   r   r   r%   r'   r   r)   r   r   r   r   ra   r   s     r,   r   Wigner9j.doit  s_    =>>$''488TWWdggtxxQUQYQY[_[c[ceiekekllr.   r   N)r   r   r   r   r   r#   r   r%   r'   r   r)   r   r   r   r   ra   rW   rz   r   r   r   r   r.   r,   r   r   P  s    (                   ; ;$Lmr.   r   c                    [        U [        5      (       a  [        U 5      $ [        U [        5      (       a  [	        U 5      $ [        U [
        5      (       a,  [        U R                   Vs/ s H  n[        U5      PM     sn6 $ [        U [        5      (       a)  [        [        U R                  5      U R                  5      $ U $ s  snf )ao  Simplify 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_sumr   r+   r   r   baseexp)erP   s     r,   r   r     s    B !SA	As		A	As		QVV4VcWS\V455	As		7166?AEE**	 5s   )Cc                 $   / n/ n[        U 5      n U R                   GH  nUR                  [        5      (       a  [	        U[
        5      (       a  UR                  [        U5      5        MO  [	        U[        5      (       a~  SnUR                   H,  n[	        U[
        5      (       a  U[        U5      -  nM(  XE-  nM.     UR                  [        5      (       a  UR                  U5        M  UR                  U5        M  UR                  U5        M  UR                  U5        GM	     [        U5      u  pUR                  U5        [        U5      u  pUR                  U5        [        U5      u  pUR                  U5        [        U6 [        U6 -   $ )a  Takes 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
r8   )r   r+   hasr   r   r   appendr   r   _check_varsh_871_1_check_varsh_871_2_check_varsh_872_9r   )r   cg_part
other_partrP   termstermothers          r,   r   r     s<    GJq	Avv772;;#s##!!,s"34C%%HHD!$,,d!33	 %
 99R==NN5)%%e,s#c"% ( (0NGe'0NGe'0NGe=3
+++r.   c                     [        [        S5      u  pp4U[        XUSX5      -  nSU-  S-   [        US5      -  nU[	        U5      -  nSU-  S-   nX-   n	[        XVXtXX#U4X4X5	      $ )N)aalphabltr   r<   r8   )r"   r   r   r   abs_check_cg_simp)
	term_listr   r   r  r  exprsimpsign
build_expr
index_exprs
             r,   r   r     s    $ 9:OAab1a**DaC!G^Aq))Dc"g:D1qJJ$d	u;LqfV`mmr.   c                     [        [        S5      u  pp4U[        XX* US5      -  n[        SU-  S-   5      [	        US5      -  nSX-
  -  U-  [        U5      -  nSU-  S-   nX-   n	[        XVXtXX#U4X4X5	      $ )N)r   r   cr  r   r<   r8   r[   )r"   r   r   r   r   r  r  )
r  r   r   r  r  r  r  r  r	  r
  s
             r,   r   r     s    $ 9:OAab1fa++D!a=1--D!)RB'D1qJJ$d	u;LqfV`mmr.   c                    [        [        S5      u	  pp4pVpxn	U	[        XXEXx5      S-  -  n
[        R                  nU	[        U	5      -  n[        X-
  5      n[        X%-   5      nX-   S-   [        XU:  4S[        X5      4XU:  45      -
  nX-   U-
  n[        XXXX$XWX4XXE4UU5	      u  n n[        X-
  5      nX-   nUS-   U-
  X-   S-   -  nX}-
  X-   -  U-   U-   n[        XXXX$XWX4XXE4UU5	      u  n n[        XXEXx5      [        XXFXx5      -  n
[        X#5      [        XV5      -  n[        R                  n[        X-
  5      n[        X%-   5      nX-   S-   [        XU:  4S[        X5      4XU:  45      -
  nX-   U-
  n[        XU[        R                  XX#XEXgU4XX4XV4UU5	      u  n n[        X-
  5      nX-   nUS-   U-
  X-   S-   -  nX}-
  X-   -  U-   U-   n[        XU[        R                  XX#XEXgU4XX4XV4UU5	      u  n nU UU-   U-   4$ )N)	r   r   alphapr  betabetapr  gammar  r<   r8   r   )
r"   r   r   r
   Oner  r   r	   r  r   )r  r   r   r  r  r  r  r  r  r  r  r  r  xyr	  r
  other1other2other3other4s                        r,   r   r   )  s   58 @J 6K2Afa
 b1A-q00D55Dc"g:DAE
AELAYq5zAr!x=1!e*MMJJ&t4YEVZ_dHilmvwk~  AK  MW  XIv 	AE
A	Aa%!)aeai(J%!%1$u,J&t4YEVZ_dHilmvwk~  AK  MW  XIv
 a*2a1+LLD%()DDD55DAE
AELAYq5zAr!x=1!e*MMJJ&t4	u^_glqvKwz{  EK  PT  z\  ^h  jt  uIv 	AE
A	Aa%!)aeai(J%!%1$u,J&t4	u^_glqvKwz{  EK  PT  z\  ^h  jt  uIvfvo...r.   c	           
         Sn	Sn
U
[        U5      :  Ga  [        XJ   U [        U5      5      nUc  U
S-  n
M2  UR                  U5      R                  (       d  U
S-  n
MY  U Vs/ s H  oX   4PM
     nnS/UR                  U5      -  n[	        U
[        U5      5       GH  n[        XO   U R                  U5      [        U5      [        U5      -
  UR                  U5      UR                  U5      4S9nUc  M[  UR                  U5      R                  U5      R                  (       d  M  XR                  US5      R                  U5      R                  U5      UR                  U5      UR                  U5      R                  U5      4XR                  U5      R                  U5      '   GM     [        S U 5       5      (       d  [        U Vs/ s H  n[        US   5      PM     sn6 nU Vs/ s H  nUS   PM
     nnUR                  5         UR                  5         U Vs/ s H  oR                  U5      PM       nU H:  n[        US   5      U:  d  M  UR                  US   UUS   -  -
  US   -  5        M<     U	UX!-  R                  U5      -  -  n	OU
S-  n
U
[        U5      :  a  GM  XI4$ s  snf s  snf s  snf s  snf )aL  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

r   Nr8   )r  c              3   (   #    U  H  oS L v   M
     g 7fr    r   )rO   r`   s     r,   rQ   !_check_cg_simp.<locals>.<genexpr>  s     /h9hs   r<   r@   )len	_check_cgsubsrM   rh   anyminr  sortreversepopr   )r  r  r  r  r  	variablesdep_variablesbuild_index_exprr
  r   r`   sub_1r  sub_depcg_indexra   sub_2r   min_ltindicess                       r,   r  r  V  s   R J	A
c)n
),c)n==FA$$U+55FA*78-Qux=-86*//66q#i.)AilDIIg,>IQTUbQc@ckoktktuzk{  ~B  ~G  ~G  HO  ~P  kQ  RE}??7+007AA=>		"a@P@U@UV]@^@c@cdi@jlnlslstylz  }A  }F  }F  GN  }O  }T  }T  UZ  }[  >[H__W-2259: * /h///X?XTCQLX?@F,45HDQHG5LLNOO(/11mmA1 tAw<&($$tAwQ'?a&HJ ! &$)!1!1%!888JFA9 c)n
:   + 9 @5 2s   "J3J8.J=#KNc                     U R                  U5      nUc  gUb<  [        U[        5      (       d  [        S5      eUS   US   R	                  U5      :X  d  g[        U5      U:X  a  U$ g)z2Checks whether a term matches the given expressionNzsign must be a tupler   r8   )matchr   r}   	TypeErrorr  r  )cg_termr  lengthr  matchess        r,   r  r    sk     mmD!G$&&233Aw47..11
7|v r.   c                 H    [        U 5      n [        U 5      n [        U 5      n U $ r    )_check_varsh_sum_871_1_check_varsh_sum_871_2_check_varsh_sum_872_4)r   s    r,   r   r     s%    q!Aq!Aq!AHr.   c                    [        S5      n[        S5      n[        S5      nU R                  [        [	        XUSX5      X!* U45      5      nUb3  [        U5      S:X  a$  SU-  S-   [        US5      -  R                  U5      $ U $ )Nr   r   r  r   r<   r8   )r   r   r.  r   r   r  r   r  )r   r   r   r  r.  s        r,   r4  r4    s~    S	AGES	AGGC1Q14ub!nEFESZ1_1q.A..44U;;Hr.   c                 ,   [        S5      n[        S5      n[        S5      nU R                  [        SX-
  -  [	        XX* US5      -  X!* U45      5      nUb<  [        U5      S:X  a-  [        SU-  S-   5      [        US5      -  R                  U5      $ U $ )Nr   r   r  r[   r   r<   r8   )	r   r   r.  r   r   r  r   r   r  )r   r   r   r  r.  s        r,   r5  r5    s    S	AGES	AGGR19b1fa;;eR^LNESZ1_QqS1WnQ2288??Hr.   c           	      0   [        S5      n[        S5      n[        S5      n[        S5      n[        S5      n[        S5      n[        S5      n[        S5      n[        X1XBXW5      n	[        X1XBXh5      n
U R                  [	        X-  X* U4X$* U45      5      nUb5  [        U5      S	:X  a&  [        XV5      [        Xx5      -  R                  U5      $ U R                  [	        U	S
-  X* U4X$* U45      5      nUb  [        U5      S:X  a  [        R                  $ U $ )Nr   r  r   r  r  cpr  gammapr   r<   rD   )
r   r   r   r.  r   r  r   r  r
   r  )r   r   r  r   r  r  r:  r  r;  cg1cg2match1match2s                r,   r6  r6    s   GE6?DS	AS	AS	A	dBME(^F
Qq
)C
Qq
+CWWS5"a.4Q-@AFc&kQ.q%nU&CCII&QQWWSa%Q$A?@Fc&kQ.uuHr.   c                 ~   [        U [        5      (       a  U 4SS4$ / nSn[        U [        [        45      (       d  [	        S5      e[        U [        5      (       az  U R
                  R                  (       a_  U R
                  R                  (       a>  [        U R
                  5       Vs/ s H  o1R                  U R                  5      PM       nOU 4SS4$ [        U [        5      (       aO  U R                   H/  n[        U[        5      (       a  UR                  U5        M+  X$-  nM1     XU[        U5      -  4$ g s  snf )Nr8   z term must be CG, Add, Mul or Pow)r   r   r   r   NotImplementedErrorr   rM   rh   r   r   r+   r  )r   cgcoeffry   rP   s        r,   _cg_listrD    s    $w1}	BEdS#J''!"DEE$!3!388,1$((O=Oqii		"O=7Aq= $99C#r""		#	 
 %E
***  >s   ($D:r    )7r   sympy.concrete.summationsr   sympy.core.addr   sympy.core.exprr   sympy.core.functionr   sympy.core.mulr   sympy.core.powerr   sympy.core.relationalr	   sympy.core.singletonr
   sympy.core.symbolr   r   sympy.core.sympifyr   (sympy.functions.elementary.miscellaneousr   $sympy.functions.elementary.piecewiser    sympy.printing.pretty.stringpictr   r   (sympy.functions.special.tensor_functionsr   sympy.physics.wignerr   r   r   r   sympy.printing.precedencer   __all__r   r   r   r   r   r   r   r   r   r  r  r   r4  r5  r6  rD  r   r.   r,   <module>rV     s   
 # )    &    $ " - & 9 : C C P P 0xOt xOvS9 S9lVPt VPremt emP*Z+,\nn*/ZH!V(+r.   