
    Ed"[                        d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZ dd	lmZ dd
lmZmZ ddlmZ ddlmZ ddlmZ ddlmZmZ ddlmZ ddlm Z m!Z!m"Z"m#Z# ddl$m%Z% g dZ& G d de          Z' G d de'          Z( G d de          Z) G d de          Z*d Z+d Z,d Z-d Z.d Z/d  Z0d(d"Z1d# Z2d$ Z3d% Z4d& Z5d' Z6d!S ))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                       e Zd ZdZdZd Zed             Zed             Zed             Z	ed             Z
ed             Zed	             Zed
             Zd Zd Zd ZdS )r   a  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           	      \    t          t          ||||||f          }t          j        | g|R  S Nmapr   r   __new__)clsj1m1j2m2j3m3argss           8lib/python3.11/site-packages/sympy/physics/quantum/cg.pyr"   zWigner3j.__new__Q   s6    7RRR455|C'$''''    c                     | j         d         S Nr   r*   selfs    r+   r$   zWigner3j.j1U       y|r,   c                     | j         d         S N   r/   r0   s    r+   r%   zWigner3j.m1Y   r2   r,   c                     | j         d         S N   r/   r0   s    r+   r&   zWigner3j.j2]   r2   r,   c                     | j         d         S N   r/   r0   s    r+   r'   zWigner3j.m2a   r2   r,   c                     | j         d         S N   r/   r0   s    r+   r(   zWigner3j.j3e   r2   r,   c                     | j         d         S N   r/   r0   s    r+   r)   zWigner3j.m3i   r2   r,   c                 @    t          d | j        D                        S )Nc              3   $   K   | ]}|j         V  d S r   	is_number.0args     r+   	<genexpr>z'Wigner3j.is_symbolic.<locals>.<genexpr>o   $      ::s}::::::r,   allr*   r0   s    r+   is_symboliczWigner3j.is_symbolicm   $    ::	:::::::r,   c                 ^   |                     | j                  |                     | j                  f|                     | j                  |                     | j                  f|                     | j                  |                     | j                  ffd}d}dgdz  }t          d          D ].t          fdt          d          D                       |<   /d }t          d          D ]}d }t          d          D ]         |         }	|         |		                                z
  }
|
dz  }|
|z
  }t          |	                    d|z             }	t          |	                    d|z             }	||	}zt          |                    d|z             }t          |                    |	           }||}t          |          D ]}t          |                    d           }t          |                    |           }t          |                                 }|S )Nr8   r5   r;   c                 P    g | ]"}         |                                          #S  widthrG   ijms     r+   
<listcomp>z$Wigner3j._pretty.<locals>.<listcomp>z   )    ???AaDGMMOO???r,    )_printr$   r%   r&   r'   r(   r)   rangemaxrT   r   rightleftbelowparensr1   printerr*   hsepvsepmaxwDrV   D_rowswdeltawleftwright_rW   rX   s                 @@r+   _prettyzWigner3j._prettyr   s   nnTW%%w~~dg'>'>?^^DG$$gnnTW&=&=>^^DG$$gnnTW&=&=>@ tAvq 	A 	AA?????U1XX???@@DGGq 	, 	,AE1XX 4 4aDGa17799,
%F
 3 34s5y 1 12 E"EKKD$9$9:"EKKNN3 4[[ . .-AGGENN+AA

#r,   c           	          t          |j        | j        | j        | j        | j        | j        | j        f          }dt          |          z  S )NzH\left(\begin{array}{ccc} %s & %s & %s \\ %s & %s & %s \end{array}\right))	r!   r\   r$   r&   r(   r%   r'   r)   tupler1   rd   r*   labels       r+   _latexzWigner3j._latex   sI    GNTWdgtwGTWdg%/ 0 0Z%LL 	r,   c                     | j         rt          d          t          | j        | j        | j        | j        | j        | j                  S NzCoefficients must be numerical)	rM   
ValueErrorr   r$   r&   r(   r%   r'   r)   r1   hintss     r+   doitzWigner3j.doit   s@     	?=>>>$'47DGTWdgNNNr,   N)__name__
__module____qualname____doc__is_commutativer"   propertyr$   r%   r&   r'   r(   r)   rM   ro   rt   rz   rR   r,   r+   r   r   &   s%       & &P N( ( (   X   X   X   X   X   X ; ; X;! ! !F  O O O O Or,   r   c                   :    e Zd ZdZed         dz
  Zd Zd Zd ZdS )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   r5   c                     | j         rt          d          t          | j        | j        | j        | j        | j        | j                  S rv   )	rM   rw   r   r$   r&   r(   r%   r'   r)   rx   s     r+   rz   zCG.doit   s@     	?=>>>dgtw$'47SSSr,   c                 L   |                     | j        | j        | j        | j        fd          }|                     | j        | j        fd          }t          |                                |                                          }t          |
                    d           }t          |
                    d           }||                                k    s4t          |                    d||                                z
  z             }||                                k    s4t          |                    d||                                z
  z             }t          dd|z  z             }t          |                    |           }t          |                    |           }|S )N,)	delimiterr[   C)
_print_seqr$   r%   r&   r'   r(   r)   r^   rT   r   r`   r_   r   ra   above)r1   rd   r*   bottoppadrj   s          r+   ro   z
CG._pretty   sZ     Wdgtw0C ! A A  $'47!3s CC#))++syy{{++#((3--(#((3--(ciikk! 	BciiS399;;->(?@@ACciikk! 	BciiS399;;->(?@@ACsSW}%%%%r,   c           	          t          |j        | j        | j        | j        | j        | j        | j        f          }dt          |          z  S )NzC^{%s,%s}_{%s,%s,%s,%s})	r!   r\   r(   r)   r$   r%   r&   r'   rq   rr   s       r+   rt   z	CG._latex   sD    GNTWdgtwGTWdg%/ 0 0)E%LL88r,   N)	r{   r|   r}   r~   r   
precedencerz   ro   rt   rR   r,   r+   r   r      s`        5 5l E"Q&JT T T
  $9 9 9 9 9r,   r   c                       e Zd ZdZd Zed             Zed             Zed             Zed             Z	ed             Z
ed             Zed	             Zd
 Zd Zd ZdS )r   zaClass for the Wigner-6j symbols

    See Also
    ========

    Wigner3j: Wigner-3j symbols

    c           	      \    t          t          ||||||f          }t          j        | g|R  S r   r    )r#   r$   r&   j12r(   rW   j23r*   s           r+   r"   zWigner6j.__new__   s6    7RS"a566|C'$''''r,   c                     | j         d         S r.   r/   r0   s    r+   r$   zWigner6j.j1  r2   r,   c                     | j         d         S r4   r/   r0   s    r+   r&   zWigner6j.j2  r2   r,   c                     | j         d         S r7   r/   r0   s    r+   r   zWigner6j.j12  r2   r,   c                     | j         d         S r:   r/   r0   s    r+   r(   zWigner6j.j3  r2   r,   c                     | j         d         S r=   r/   r0   s    r+   rW   z
Wigner6j.j  r2   r,   c                     | j         d         S r@   r/   r0   s    r+   r   zWigner6j.j23  r2   r,   c                 @    t          d | j        D                        S )Nc              3   $   K   | ]}|j         V  d S r   rD   rF   s     r+   rI   z'Wigner6j.is_symbolic.<locals>.<genexpr>  rJ   r,   rK   r0   s    r+   rM   zWigner6j.is_symbolic  rN   r,   c                 d   |                     | j                  |                     | j                  f|                     | j                  |                     | j                  f|                     | j                  |                     | j                  ffd}d}dgdz  }t          d          D ].t          fdt          d          D                       |<   /d }t          d          D ]}d }t          d          D ]         |         }	|         |		                                z
  }
|
dz  }|
|z
  }t          |	                    d|z             }	t          |	                    d|z             }	||	}zt          |                    d|z             }t          |                    |	           }||}t          |          D ]}t          |                    d           }t          |                    |           }t          |                    dd	           }|S )
Nr8   r5   rP   r;   c                 P    g | ]"}         |                                          #S rR   rS   rU   s     r+   rY   z$Wigner6j._pretty.<locals>.<listcomp>)  rZ   r,   r[   {}r`   r_   )r\   r$   r(   r&   rW   r   r   r]   r^   rT   r   r_   r`   ra   rb   rc   s                 @@r+   ro   zWigner6j._pretty!  s$   nnTW%%w~~dg'>'>?^^DG$$gnnTV&<&<=^^DH%%w~~dh'?'?@B tAvq 	A 	AA?????U1XX???@@DGGq 	, 	,AE1XX 4 4aDGa17799,
%F
 3 34s5y 1 12 E"EKKD$9$9:"EKKNN3 4[[ . .-AGGENN+AAc556r,   c           	          t          |j        | j        | j        | j        | j        | j        | j        f          }dt          |          z  S )NzJ\left\{\begin{array}{ccc} %s & %s & %s \\ %s & %s & %s \end{array}\right\})	r!   r\   r$   r&   r   r(   rW   r   rq   rr   s       r+   rt   zWigner6j._latexD  sI    GNTWdgtxGTVTX%/ 0 0\%LL 	r,   c                     | j         rt          d          t          | j        | j        | j        | j        | j        | j                  S rv   )	rM   rw   r   r$   r&   r   r(   rW   r   rx   s     r+   rz   zWigner6j.doitJ  s@     	?=>>>$'48TWdfdhOOOr,   N)r{   r|   r}   r~   r"   r   r$   r&   r   r(   rW   r   rM   ro   rt   rz   rR   r,   r+   r   r      s        ( ( (   X   X   X   X   X   X ; ; X;! ! !F  P P P P Pr,   r   c                      e Zd ZdZd Zed             Zed             Zed             Zed             Z	ed             Z
ed             Zed	             Zed
             Zed             Zed             Zd Zd Zd ZdS )r   zaClass for the Wigner-9j symbols

    See Also
    ========

    Wigner3j: Wigner-3j symbols

    c
                 b    t          t          |||||||||	f	          }
t          j        | g|
R  S r   r    )r#   r$   r&   r   r(   j4j34j13j24rW   r*   s              r+   r"   zWigner9j.__new__Y  s<    7RS"b#sCCDD|C'$''''r,   c                     | j         d         S r.   r/   r0   s    r+   r$   zWigner9j.j1]  r2   r,   c                     | j         d         S r4   r/   r0   s    r+   r&   zWigner9j.j2a  r2   r,   c                     | j         d         S r7   r/   r0   s    r+   r   zWigner9j.j12e  r2   r,   c                     | j         d         S r:   r/   r0   s    r+   r(   zWigner9j.j3i  r2   r,   c                     | j         d         S r=   r/   r0   s    r+   r   zWigner9j.j4m  r2   r,   c                     | j         d         S r@   r/   r0   s    r+   r   zWigner9j.j34q  r2   r,   c                     | j         d         S )N   r/   r0   s    r+   r   zWigner9j.j13u  r2   r,   c                     | j         d         S )N   r/   r0   s    r+   r   zWigner9j.j24y  r2   r,   c                     | j         d         S )N   r/   r0   s    r+   rW   z
Wigner9j.j}  r2   r,   c                 @    t          d | j        D                        S )Nc              3   $   K   | ]}|j         V  d S r   rD   rF   s     r+   rI   z'Wigner9j.is_symbolic.<locals>.<genexpr>  rJ   r,   rK   r0   s    r+   rM   zWigner9j.is_symbolic  rN   r,   c                    |                     | j                  |                     | j                  |                     | j                  f|                     | j                  |                     | j                  |                     | j                  f|                     | j                  |                     | j                  |                     | j	                  ffd}d}dgdz  }t          d          D ].t          fdt          d          D                       |<   /d }t          d          D ]}d }t          d          D ]         |         }	|         |	                                z
  }
|
dz  }|
|z
  }t          |	                    d|z             }	t          |	                    d|z             }	||	}zt          |                    d|z             }t          |                    |	           }||}t          |          D ]}t          |                    d           }t          |                    |           }t          |                    dd	           }|S )
Nr8   r5   rP   r;   c                 P    g | ]"}         |                                          #S rR   rS   rU   s     r+   rY   z$Wigner9j._pretty.<locals>.<listcomp>  rZ   r,   r[   r   r   r   )r\   r$   r(   r   r&   r   r   r   r   rW   r]   r^   rT   r   r_   r`   ra   rb   rc   s                 @@r+   ro   zWigner9j._pretty  sh   ^^ !..117>>$(3K3KM^^ !..117>>$(3K3KM^^DH%%w~~dh'?'?PTPVAWAWXZ tAvq 	A 	AA?????U1XX???@@DGGq 	, 	,AE1XX 4 4aDGa17799,
%F
 3 34s5y 1 12 E"EKKD$9$9:"EKKNN3 4[[ . .-AGGENN+AAc556r,   c                     t          |j        | j        | j        | j        | j        | j        | j        | j        | j	        | j
        f	          }dt          |          z  S )NzZ\left\{\begin{array}{ccc} %s & %s & %s \\ %s & %s & %s \\ %s & %s & %s \end{array}\right\})r!   r\   r$   r&   r   r(   r   r   r   r   rW   rq   rr   s       r+   rt   zWigner9j._latex  sW    GNTWdgtx48TXtv%? @ @l%LL 	r,   c                     | j         rt          d          t          | j        | j        | j        | j        | j        | j        | j	        | j
        | j        	  	        S rv   )rM   rw   r   r$   r&   r   r(   r   r   r   r   rW   rx   s     r+   rz   zWigner9j.doit  sR     	?=>>>$'48TWdgtxQUQY[_[ceieklllr,   N)r{   r|   r}   r~   r"   r   r$   r&   r   r(   r   r   r   r   rW   rM   ro   rt   rz   rR   r,   r+   r   r   P  sn        ( ( (   X   X   X   X   X   X   X   X   X ; ; X;$ $ $L  m m m m mr,   r   c                 h   t          | t                    rt          |           S t          | t                    rt	          |           S t          | t
                    rt          d | j        D              S t          | t                    r't          t          | j	                  | j
                  S | S )a  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.
    c                 ,    g | ]}t          |          S rR   )r   rF   s     r+   rY   zcg_simp.<locals>.<listcomp>  s    444cWS\\444r,   )
isinstancer   _cg_simp_addr   _cg_simp_sumr   r*   r   r   baseexpes    r+   r   r     s    B !S 	A	As		 A	As		 44QV44455	As		 716??AE***r,   c                 r   g }g }t          |           } | j        D ]}|                    t                    rt	          |t
                    r#|                    t          |                     Ut	          |t                    rd}|j        D ]/}t	          |t
                    r|t          |          z  }*||z  }0|                    t                    r|                    |           |                    |           |                    |           |                    |           t          |          \  }}|                    |           t          |          \  }}|                    |           t          |          \  }}|                    |           t          | t          | z   S )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
    r5   )r   r*   hasr   r   r   appendr   r   _check_varsh_871_1_check_varsh_871_2_check_varsh_872_9r   )r   cg_part
other_partrH   termstermothers          r+   r   r     s    GJq		Av # #772;; 	##s## $!!,s"3"34444C%% $H & &D!$,, &d!3!3399R== -NN5))))%%e,,,,s####c""""'00NGUe'00NGUe'00NGUe=3
+++r,   c                    t          t          d          \  }}}}|t          |||d||          z  }d|z  dz   t          |d          z  }|t	          |          z  }d|z  dz   }||z   }	t          ||||| ||||f||f||		  	        S )N)aalphabltr   r8   r5   )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::OAuabE1aE***DaC!G^Aq)))Dc"gg:D1qJUJ$dB	Aua;LqRSfV`blmmmr,   c                 <   t          t          d          \  }}}}|t          |||| |d          z  }t          d|z  dz             t	          |d          z  }d||z
  z  |z  t          |          z  }d|z  dz   }||z   }	t          ||||| ||||f||f||		  	        S )N)r   r   cr   r   r8   r5   rP   )r!   r   r   r   r   r   r   )
r   r   r   r   r   r   r   r   r   r   s
             r+   r   r     s    $ 9::OAuabE1ufa+++D!a==1---D!e)RB'D1qJUJ$dB	Aua;LqRSfV`blmmmr,   c                 8   t          t          d          \	  }}}}}}}}}	|	t          ||||||          dz  z  }
t          j        }|	t          |	          z  }t          ||z
            }t          ||z             }||z   dz   t          |||k    fdt          ||          f|||k    f          z
  }||z   |z
  }t          |
|||	| |||||||	f||||f||	  	        \  } }t          ||z
            }||z   }|dz   |z
  ||z   dz   z  }||z
  ||z   z  |z   |z   }t          |
|||	| |||||||	f||||f||	  	        \  } }t          ||||||          t          ||||||          z  }
t          ||          t          ||          z  }t          j        }t          ||z
            }t          ||z             }||z   dz   t          |||k    fdt          ||          f|||k    f          z
  }||z   |z
  }t          |
||t          j        | ||||||||f||||||f||	  	        \  } }t          ||z
            }||z   }|dz   |z
  ||z   dz   z  }||z
  ||z   z  |z   |z   }t          |
||t          j        | ||||||||f||||||f||	  	        \  } }| ||z   |z   fS )N)	r   r   alphapr   betabetapr   gammar   r8   r5   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 6K2Aufaua
 bE1dAu--q00D5Dc"gg:DAE

AEDLAQY1q5zAr!Qxx=1a!e*MMMJQJ&tT4YESTVZ\]_dfhHilmotvwy}k~  AK  MW  X  XIv 	AE

A	AAa%!)a!eai(Ja%!a%1$u,J&tT4YESTVZ\]_dfhHilmotvwy}k~  AK  MW  X  XIv
 a4E**2aE1e+L+LLD%((e)D)DDD5DAE

AEDLAQY1q5zAr!Qxx=1a!e*MMMJQJ&tT4	AuV\^_aeglnoqvKwz{  ~C  EK  MN  PT  V[  z\  ^h  jt  u  uIv 	AE

A	AAa%!)a!eai(Ja%!a%1$u,J&tT4	AuV\^_aeglnoqvKwz{  ~C  EK  MN  PT  V[  z\  ^h  jt  u  uIvfvo...r,   c	           
         d}	d}
|
t                    k     rt          |
         | t          |                    |
dz  }
@|                              j        s|
dz  }
`fd|D             }dg|                              z  }t	          |
t                              D ]G}t          |         |                     |          t          |          t          |          z
  |                              |                    |          f          }|w|                    |                              |          j        s||                     |d                              |                              |          |                    |          |                    |                              |          f||                    |                              |          <   It          d |D                       st          d |D              }d |D             }|                                 |                                 fd	|D              |D ]K}t          |d
                   |k    r0
                    |d
         ||d         z  z
  |d         z             L|	|||z                                z  z  }	n|
dz  }
|
t                    k     ͉|	fS )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

    r   Nr5   c                 $    g | ]}||         fS rR   rR   )rG   r   sub_1s     r+   rY   z"_check_cg_simp.<locals>.<listcomp>  s!    888QAuQx=888r,   )r   c              3      K   | ]}|d u V  	d S r   rR   )rG   rV   s     r+   rI   z!_check_cg_simp.<locals>.<genexpr>  s&      //19//////r,   c                 8    g | ]}t          |d                    S )r8   )r   rG   r   s     r+   rY   z"_check_cg_simp.<locals>.<listcomp>  s"    ???TCQLL???r,   c                     g | ]
}|d          S )r   rR   r   s     r+   rY   z"_check_cg_simp.<locals>.<listcomp>  s    555DQ555r,   c                 :    g | ]}                     |          S rR   )pop)rG   rW   r   s     r+   rY   z"_check_cg_simp.<locals>.<listcomp>  s%    1111immA111r,   r8   r;   )len	_check_cgsubsrE   r]   anyminsortreverser   r   )r   r   r   r   r   	variablesdep_variablesbuild_index_exprr   r   rV   sub_depcg_indexrW   sub_2min_ltindicesr   r   s       `             @r+   r   r   V  sg   R J	A
c)nn
 )A,c)nn== 	FA$$U++5 	FA8888-8886*//666q#i..)) 	[ 	[AilDIIg,>,>IQTUbQcQc@ckoktktuzk{k{  ~B  ~G  ~G  HO  ~P  ~P  kQ  R  R  RE ??7++0077A =>		"a@P@P@U@UV]@^@^@c@cdi@j@jlnlslstylzlz  }A  }F  }F  GN  }O  }O  }T  }T  UZ  }[  }[  >[HZ__W--22599:://h///// 	??X???@F55H555GLLNNNOO11111111  K KtAw<<&( K$$tAwQ'?a&HJJJ&$t)!1!1%!8!888JJFA9 c)nn
 : j  r,   Nc                     |                      |          }|dS |Kt          |t                    st          d          |d         |d                             |          k    sdS t          |          |k    r|S dS )z2Checks whether a term matches the given expressionNzsign must be a tupler   r5   )matchr   rq   	TypeErrorr   r   )cg_termr   lengthr   matchess        r+   r   r     s     mmD!!G  $&& 	42333Aw47..111 	F
7||v  r,   c                 `    t          |           } t          |           } t          |           } | S r   )_check_varsh_sum_871_1_check_varsh_sum_871_2_check_varsh_sum_872_4r   s    r+   r   r     s.    q!!Aq!!Aq!!AHr,   c                 T   t          d          }t          d          }t          d          }|                     t          t	          |||d||          || |f                    }|?t          |          dk    r,d|z  dz   t          |d          z                      |          S | S )Nr   r   r   r   r8   r5   )r   r   r  r   r   r   r   r   )r   r   r   r   r  s        r+   r  r    s    S		AGES		AGGC1eQ1e44uqb!nEEFFE <SZZ1_ <1q.A...44U;;;Hr,   c                    t          d          }t          d          }t          d          }|                     t          d||z
  z  t	          |||| |d          z  || |f                    }|Lt          |          dk    r9t          d|z  dz             t          |d          z                      |          S | S )Nr   r   r   rP   r   r8   r5   )	r   r   r  r   r   r   r   r   r   )r   r   r   r   r  s        r+   r  r    s    S		AGES		AGGR1u9bE1ufa;;;eaR^LLN NE @SZZ1_ @QqS1WnQ22288???Hr,   c           	         t          d          }t          d          }t          d          }t          d          }t          d          }t          d          }t          d          }t          d          }t          ||||||          }	t          ||||||          }
|                     t	          |	|
z  || |f|| |f                    }|Gt          |          d	k    r4t          ||          t          ||          z                      |          S |                     t	          |	d
z  || |f|| |f                    }|t          |          dk    rt          j	        S | S )Nr   r   r   r   r   cpr   gammapr   r8   r>   )
r   r   r   r  r   r   r   r   r
   r   )r   r   r   r   r   r   r  r   r  cg1cg2match1match2s                r+   r	  r	    s[   GE6??DS		AS		AS		A	dBMME(^^F
Qq$5
)
)C
Qq$F
+
+CWWSS51"a.4!Q-@@AAF Rc&kkQ. Rq"%%nUF&C&CCII&QQQWWSa%!Q$A??@@F c&kkQ. uHr,   c                     t           t                    r fddfS g d}t           t          t          f          st	          d          t           t                    r@ j        j        r4 j        j        r" fdt           j                  D              n fddfS t           t                    rO j        D ]2}t          |t                    r	                    |           -||z  }3||t          |          z  fS d S )Nr5   z term must be CG, Add, Mul or Powc                 D    g | ]}                     j                  S rR   )r   r   )rG   rn   cgr   s     r+   rY   z_cg_list.<locals>.<listcomp>  s'    ===qbii	""===r,   )r   r   r   r   NotImplementedErrorr   rE   r]   r*   r   r   )r   coeffrH   r  s   `  @r+   _cg_listr    s(   $ w1}	BEdS#J'' F!"DEEE$ !!3 !8 	!=====E$(OO=====7Aq= $ +9 	 	C#r"" 		#5%E

***+ +r,   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   r  r  r	  r  rR   r,   r+   <module>r*     s  
 # " ) ) ) ) ) )                   & & & & & &                   $ $ $ $ $ $ " " " " " " - - - - - - - - & & & & & & 9 9 9 9 9 9 : : : : : : C C C C C C C C C C C C C C P P P P P P P P P P P P 0 0 0 0 0 0  xO xO xO xO xOt xO xO xOvS9 S9 S9 S9 S9 S9 S9 S9lVP VP VP VP VPt VP VP VPrem em em em emt em em emP* * *Z+, +, +,\n n nn n n*/ */ */ZH! H! H!V           (+ + + + +r,   