o
    Ïe#[  ã                   @   s†  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„ deƒZ'G dd„ de'ƒZ(G dd„ deƒZ)G dd„ deƒZ*dd„ Z+dd„ Z,dd „ Z-d!d"„ Z.d#d$„ Z/d%d&„ Z0d4d(d)„Z1d*d+„ Z2d,d-„ Z3d.d/„ Z4d0d1„ Z5d2d3„ Z6d'S )5z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                   @   sˆ   e Zd ZdZdZdd„ Zedd„ ƒZedd„ ƒZed	d
„ ƒZ	edd„ ƒZ
edd„ ƒZedd„ ƒZedd„ ƒZdd„ Zdd„ Zd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                 C   ó(   t t||||||fƒ}tj| g|¢R Ž S ©N©Úmapr   r   Ú__new__)ÚclsÚj1Úm1Új2Úm2Új3Úm3Úargs© r)   úHD:\Projects\ConvertPro\env\Lib\site-packages\sympy/physics/quantum/cg.pyr    Q   ó   zWigner3j.__new__c                 C   ó
   | j d S ©Nr   ©r(   ©Úselfr)   r)   r*   r"   U   ó   
zWigner3j.j1c                 C   r,   ©Né   r.   r/   r)   r)   r*   r#   Y   r1   zWigner3j.m1c                 C   r,   ©Né   r.   r/   r)   r)   r*   r$   ]   r1   zWigner3j.j2c                 C   r,   ©Né   r.   r/   r)   r)   r*   r%   a   r1   zWigner3j.m2c                 C   r,   ©Né   r.   r/   r)   r)   r*   r&   e   r1   zWigner3j.j3c                 C   r,   ©Né   r.   r/   r)   r)   r*   r'   i   r1   zWigner3j.m3c                 C   ó   t dd„ | jD ƒƒ S )Nc                 s   ó   � | ]}|j V  qd S r   ©Ú	is_number©Ú.0Úargr)   r)   r*   Ú	<genexpr>o   ó   € z'Wigner3j.is_symbolic.<locals>.<genexpr>©Úallr(   r/   r)   r)   r*   Úis_symbolicm   ó   zWigner3j.is_symbolicc                    sp  |  | j¡|  | j¡f|  | j¡|  | j¡f|  | j¡|  | j¡ff‰d}d}dgd }tdƒD ]‰ t‡ ‡fdd„tdƒD ƒƒ|ˆ < q0d }tdƒD ]f}d }tdƒD ]A‰ ˆˆ  | }	|ˆ  |	 	¡  }
|
d }|
| }t
|	 d| ¡Ž }	t
|	 d| ¡Ž }	|d u r‚|	}qQt
| d| ¡Ž }t
| |	¡Ž }qQ|d u rš|}qIt|ƒD ]	}t
| d¡Ž }qžt
| |¡Ž }qIt
| ¡ Ž }|S )Nr5   r3   éÿÿÿÿr7   c                    ó   g | ]
}ˆˆ  |   ¡ ‘qS r)   ©Úwidth©rA   Úi©ÚjÚmr)   r*   Ú
<listcomp>z   ó    z$Wigner3j._pretty.<locals>.<listcomp>ú )Ú_printr"   r#   r$   r%   r&   r'   ÚrangeÚmaxrL   r   ÚrightÚleftÚbelowÚparens©r0   Úprinterr(   ZhsepZvsepZmaxwÚDrN   ZD_rowÚsZwdeltaZwleftZwrightÚ_r)   rO   r*   Ú_prettyr   s@   þ
"zWigner3j._prettyc                 G   ó0   t |j| j| j| j| j| j| jfƒ}dt|ƒ S )NzH\left(\begin{array}{ccc} %s & %s & %s \\ %s & %s & %s \end{array}\right))	r   rU   r"   r$   r&   r#   r%   r'   Útuple©r0   r]   r(   Úlabelr)   r)   r*   Ú_latex•   ó   ÿÿzWigner3j._latexc                 K   ó,   | j rtdƒ‚t| j| j| j| j| j| jƒS ©NzCoefficients must be numerical)	rG   Ú
ValueErrorr   r"   r$   r&   r#   r%   r'   ©r0   Úhintsr)   r)   r*   Údoit›   ó   zWigner3j.doitN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Zis_commutativer    Úpropertyr"   r#   r$   r%   r&   r'   rG   ra   rf   rm   r)   r)   r)   r*   r   &   s*    (






#r   c                   @   s4   e Zd ZdZed d Zdd„ Zdd„ Zd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   r3   c                 K   rh   ri   )	rG   rj   r   r"   r$   r&   r#   r%   r'   rk   r)   r)   r*   rm   Ú   rn   zCG.doitc                 G   sÞ   |j | j| j| j| jfdd�}|j | j| jfdd�}t| ¡ | ¡ ƒ}t	| 
d¡Ž }t	| 
d¡Ž }|| ¡ ksDt	| d|| ¡   ¡Ž }|| ¡ ksWt	| d|| ¡   ¡Ž }tdd|  ƒ}t	| |¡Ž }t	| |¡Ž }|S )Nú,)Ú	delimiterrT   ÚC)Z
_print_seqr"   r#   r$   r%   r&   r'   rW   rL   r   rY   rX   r   rZ   Zabove)r0   r]   r(   ZbotÚtopÚpadr_   r)   r)   r*   ra   ß   s   ÿz
CG._prettyc                 G   rb   )NzC^{%s,%s}_{%s,%s,%s,%s})	r   rU   r&   r'   r"   r#   r$   r%   rc   rd   r)   r)   r*   rf   ñ   s   ÿz	CG._latexN)	ro   rp   rq   rr   r   Ú
precedencerm   ra   rf   r)   r)   r)   r*   r   ¡   s    6r   c                   @   s„   e Zd ZdZdd„ Zedd„ ƒZedd„ ƒZedd	„ ƒZed
d„ ƒZ	edd„ ƒZ
edd„ ƒZedd„ ƒZdd„ Zdd„ Zdd„ ZdS )r   zaClass for the Wigner-6j symbols

    See Also
    ========

    Wigner3j: Wigner-3j symbols

    c                 C   r   r   r   )r!   r"   r$   Új12r&   rP   Új23r(   r)   r)   r*   r       r+   zWigner6j.__new__c                 C   r,   r-   r.   r/   r)   r)   r*   r"     r1   zWigner6j.j1c                 C   r,   r2   r.   r/   r)   r)   r*   r$     r1   zWigner6j.j2c                 C   r,   r4   r.   r/   r)   r)   r*   rz     r1   zWigner6j.j12c                 C   r,   r6   r.   r/   r)   r)   r*   r&     r1   zWigner6j.j3c                 C   r,   r8   r.   r/   r)   r)   r*   rP     r1   z
Wigner6j.jc                 C   r,   r:   r.   r/   r)   r)   r*   r{     r1   zWigner6j.j23c                 C   r<   )Nc                 s   r=   r   r>   r@   r)   r)   r*   rC     rD   z'Wigner6j.is_symbolic.<locals>.<genexpr>rE   r/   r)   r)   r*   rG     rH   zWigner6j.is_symbolicc                    sv  |  | j¡|  | j¡f|  | j¡|  | j¡f|  | j¡|  | j¡ff‰d}d}dgd }tdƒD ]‰ t‡ ‡fdd„tdƒD ƒƒ|ˆ < q0d }tdƒD ]f}d }tdƒD ]A‰ ˆˆ  | }	|ˆ  |	 	¡  }
|
d }|
| }t
|	 d| ¡Ž }	t
|	 d| ¡Ž }	|d u r‚|	}qQt
| d| ¡Ž }t
| |	¡Ž }qQ|d u rš|}qIt|ƒD ]	}t
| d¡Ž }qžt
| |¡Ž }qIt
|jdd	d
�Ž }|S )Nr5   r3   rI   r7   c                    rJ   r)   rK   rM   rO   r)   r*   rR   )  rS   z$Wigner6j._pretty.<locals>.<listcomp>rT   Ú{Ú}©rY   rX   )rU   r"   r&   r$   rP   rz   r{   rV   rW   rL   r   rX   rY   rZ   r[   r\   r)   rO   r*   ra   !  s@   þ
"zWigner6j._prettyc                 G   rb   )NzJ\left\{\begin{array}{ccc} %s & %s & %s \\ %s & %s & %s \end{array}\right\})	r   rU   r"   r$   rz   r&   rP   r{   rc   rd   r)   r)   r*   rf   D  rg   zWigner6j._latexc                 K   rh   ri   )	rG   rj   r   r"   r$   rz   r&   rP   r{   rk   r)   r)   r*   rm   J  rn   zWigner6j.doitN)ro   rp   rq   rr   r    rs   r"   r$   rz   r&   rP   r{   rG   ra   rf   rm   r)   r)   r)   r*   r   ÷   s(    






#r   c                   @   s¨   e Zd ZdZdd„ Zedd„ ƒZedd„ ƒZedd	„ ƒZed
d„ ƒZ	edd„ ƒZ
edd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZdd„ Zdd„ Zdd„ ZdS )r   zaClass for the Wigner-9j symbols

    See Also
    ========

    Wigner3j: Wigner-3j symbols

    c
                 C   s.   t t|||||||||	f	ƒ}
tj| g|
¢R Ž S r   r   )r!   r"   r$   rz   r&   Új4Új34Új13Új24rP   r(   r)   r)   r*   r    Y  s   zWigner9j.__new__c                 C   r,   r-   r.   r/   r)   r)   r*   r"   ]  r1   zWigner9j.j1c                 C   r,   r2   r.   r/   r)   r)   r*   r$   a  r1   zWigner9j.j2c                 C   r,   r4   r.   r/   r)   r)   r*   rz   e  r1   zWigner9j.j12c                 C   r,   r6   r.   r/   r)   r)   r*   r&   i  r1   zWigner9j.j3c                 C   r,   r8   r.   r/   r)   r)   r*   r   m  r1   zWigner9j.j4c                 C   r,   r:   r.   r/   r)   r)   r*   r€   q  r1   zWigner9j.j34c                 C   r,   )Né   r.   r/   r)   r)   r*   r�   u  r1   zWigner9j.j13c                 C   r,   )Né   r.   r/   r)   r)   r*   r‚   y  r1   zWigner9j.j24c                 C   r,   )Né   r.   r/   r)   r)   r*   rP   }  r1   z
Wigner9j.jc                 C   r<   )Nc                 s   r=   r   r>   r@   r)   r)   r*   rC   ƒ  rD   z'Wigner9j.is_symbolic.<locals>.<genexpr>rE   r/   r)   r)   r*   rG   �  rH   zWigner9j.is_symbolicc                    s”  |  | j¡|  | j¡|  | j¡f|  | j¡|  | j¡|  | j¡f|  | j¡|  | j¡|  | j	¡ff‰d}d}dgd }t
dƒD ]‰ t‡ ‡fdd„t
dƒD ƒƒ|ˆ < q?d }t
dƒD ]f}d }t
dƒD ]A‰ ˆˆ  | }	|ˆ  |	 ¡  }
|
d }|
| }t|	 d| ¡Ž }	t|	 d| ¡Ž }	|d u r‘|	}q`t| d| ¡Ž }t| |	¡Ž }q`|d u r©|}qXt
|ƒD ]	}t| d¡Ž }q­t| |¡Ž }qXt|jdd	d
�Ž }|S )Nr5   r3   rI   r7   c                    rJ   r)   rK   rM   rO   r)   r*   rR   ‘  rS   z$Wigner9j._pretty.<locals>.<listcomp>rT   r|   r}   r~   )rU   r"   r&   r�   r$   r   r‚   rz   r€   rP   rV   rW   rL   r   rX   rY   rZ   r[   r\   r)   rO   r*   ra   †  sP   ÿÿÿÿ û
"zWigner9j._prettyc                 G   s<   t |j| j| j| j| j| j| j| j| j	| j
f	ƒ}dt|ƒ S )NzZ\left\{\begin{array}{ccc} %s & %s & %s \\ %s & %s & %s \\ %s & %s & %s \end{array}\right\})r   rU   r"   r$   rz   r&   r   r€   r�   r‚   rP   rc   rd   r)   r)   r*   rf   ¬  s   ÿÿzWigner9j._latexc              
   K   s8   | j rtdƒ‚t| j| j| j| j| j| j| j	| j
| jƒ	S ri   )rG   rj   r   r"   r$   rz   r&   r   r€   r�   r‚   rP   rk   r)   r)   r*   rm   ²  s   *zWigner9j.doitN)ro   rp   rq   rr   r    rs   r"   r$   rz   r&   r   r€   r�   r‚   rP   rG   ra   rf   rm   r)   r)   r)   r*   r   P  s4    









&r   c                 C   sb   t | tƒr	t| ƒS t | tƒrt| ƒS t | tƒr!td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                 S   s   g | ]}t |ƒ‘qS r)   )r   r@   r)   r)   r*   rR   Þ  ó    zcg_simp.<locals>.<listcomp>)Ú
isinstancer   Ú_cg_simp_addr   Ú_cg_simp_sumr   r(   r   r   ÚbaseÚexp©Úer)   r)   r*   r   ¸  s   
!


r   c                 C   s  g }g }t | ƒ} | jD ]M}| t¡rSt|tƒr| t|ƒ¡ qt|tƒrMd}|jD ]}t|tƒr7|t|ƒ9 }q)||9 }q)| t¡rG| |¡ q| |¡ q| |¡ q| |¡ qt	|ƒ\}}| |¡ t
|ƒ\}}| |¡ t|ƒ\}}| |¡ t|Ž t|Ž  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
    r3   )r   r(   Úhasr   r‡   r   Úappendr‰   r   Ú_check_varsh_871_1Ú_check_varsh_871_2Ú_check_varsh_872_9r   )r�   Zcg_partÚ
other_partrB   ZtermsÚtermÚotherr)   r)   r*   rˆ   å  s2   










rˆ   c           
   
   C   s‚   t tdƒ\}}}}|t|||d||ƒ }d| d t|dƒ }|t|ƒ }d| d }|| }	t||||| ||||f||f||	ƒ	S )N)ÚaÚalphaÚbÚltr   r5   r3   )r   r
   r   r   ÚabsÚ_check_cg_simp)
Ú	term_listr–   r—   r˜   r™   ÚexprÚsimpÚsignÚ
build_exprÚ
index_exprr)   r)   r*   r�     s   $r�   c           
   
   C   s”   t tdƒ\}}}}|t|||| |dƒ }td| d ƒt|dƒ }d||  | t|ƒ }d| d }|| }	t||||| ||||f||f||	ƒ	S )N)r–   r—   Úcr™   r   r5   r3   rI   )r   r
   r   r   r   rš   r›   )
rœ   r–   r—   r¢   r™   r�   rž   rŸ   r    r¡   r)   r)   r*   r‘     s   $r‘   c                 C   s¢  t tdƒ\	}}}}}}}}}	|	t||||||ƒd  }
tj}|	t|	ƒ }t|| ƒ}t|| ƒ}|| d t|||kfdt||ƒf|||kfƒ }|| | }t|
|||	| |||||||	f||||f||ƒ	\} }t|| ƒ}|| }|d | || d  }|| ||  | | }t|
|||	| |||||||	f||||f||ƒ	\} }t||||||ƒt||||||ƒ }
t	||ƒt	||ƒ }tj}t|| ƒ}t|| ƒ}|| d t|||kfdt||ƒf|||kfƒ }|| | }t|
||tj| ||||||||f||||||f||ƒ	\} }t|| ƒ}|| }|d | || d  }|| ||  | | }t|
||tj| ||||||||f||||||f||ƒ	\} }| || | fS )N)	r–   r—   Úalphapr˜   ÚbetaÚbetapr¢   Úgammar™   r5   r3   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¡   Zother1Zother2Zother3Zother4r)   r)   r*   r’   )  s8   222$2::r’   c	              	      sà  d}	d}
|
t ˆƒk rìtˆ|
 | t |ƒƒ‰ ˆ du r|
d7 }
q| ˆ ¡js(|
d7 }
q‡ fdd„|D ƒ}dg| ˆ ¡ }t|
t ˆƒƒD ]M}tˆ| |  |¡t |ƒt |ƒ | ˆ ¡| |¡fd�}|du rbq@| |¡ |¡jslq@||  |d¡ |¡ |¡| |¡| |¡ |¡f|| |¡ |¡< q@tdd„ |D ƒƒsâtd	d„ |D ƒŽ }d
d„ |D ƒ}| ¡  | ¡  ‡fdd„|D ƒ |D ]}t	|d ƒ|krÕˆ 
|d ||d   |d  ¡ qº|	|||  ˆ ¡ 7 }	n|
d7 }
|
t ˆƒk s
ˆ|	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   Nr3   c                    s   g | ]}|ˆ | f‘qS r)   r)   )rA   r¨   )Úsub_1r)   r*   rR   ‰  ó    z"_check_cg_simp.<locals>.<listcomp>)rŸ   c                 s   s   � | ]}|d u V  qd S r   r)   rM   r)   r)   r*   rC   ’  s   € z!_check_cg_simp.<locals>.<genexpr>c                 S   s   g | ]}t |d  ƒ‘qS )r5   )rš   ©rA   r”   r)   r)   r*   rR   “  r«   c                 S   s   g | ]}|d  ‘qS )r   r)   r¬   r)   r)   r*   rR   ”  r†   c                    s   g | ]}ˆ   |¡‘qS r)   )Úpop)rA   rP   )rœ   r)   r*   rR   —  s    r5   r7   )ÚlenÚ	_check_cgÚsubsr?   rV   ÚanyÚminÚsortÚreverserš   r�   )r�   rž   rŸ   r™   rœ   Ú	variablesZdep_variablesZbuild_index_exprr¡   r“   rN   Zsub_depZcg_indexrP   Zsub_2Zmin_ltÚindicesr”   r)   )rª   rœ   r*   r›   V  sB   )6D"€är›   Nc                 C   s^   |   |¡}|du rdS |dur%t|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   r3   )Úmatchr‡   rc   Ú	TypeErrorr°   r®   )Zcg_termr�   ÚlengthrŸ   Úmatchesr)   r)   r*   r¯   ¡  s   

ÿr¯   c                 C   s   t | ƒ} t| ƒ} t| ƒ} | S r   )Ú_check_varsh_sum_871_1Ú_check_varsh_sum_871_2Ú_check_varsh_sum_872_4rŒ   r)   r)   r*   r‰   °  s   r‰   c              
   C   sr   t dƒ}tdƒ}t dƒ}|  tt|||d||ƒ|| |fƒ¡}|d ur7t|ƒdkr7d| d t|dƒ  |¡S | S )Nr–   r—   r˜   r   r5   r3   )r
   r   r·   r   r   r®   r   r°   )r�   r–   r—   r˜   r·   r)   r)   r*   r»   ·  s   &r»   c                 C   s„   t dƒ}tdƒ}t dƒ}|  td||  t|||| |dƒ || |fƒ¡}|d ur@t|ƒdkr@td| d ƒt|dƒ  |¡S | S )Nr–   r—   r¢   rI   r   r5   r3   )	r
   r   r·   r   r   r®   r   r   r°   )r�   r–   r—   r¢   r·   r)   r)   r*   r¼   Á  s   ,ÿ r¼   c                 C   sü   t dƒ}t dƒ}tdƒ}tdƒ}tdƒ}tdƒ}tdƒ}tdƒ}t||||||ƒ}	t||||||ƒ}
|  t|	|
 || |f|| |fƒ¡}|d ur\t|ƒd	kr\t||ƒt||ƒ  |¡S |  t|	d
 || |f|| |fƒ¡}|d ur|t|ƒdkr|tj	S | S )Nr—   r¤   r–   r˜   r¢   Úcpr¦   Úgammaprƒ   r5   r9   )
r   r
   r   r·   r   r®   r   r°   r	   r§   )r�   r—   r¤   r–   r˜   r¢   r¾   r¦   r¿   Zcg1Zcg2Zmatch1Zmatch2r)   r)   r*   r½   Ì  s"   &&r½   c                    sÀ   t ˆtƒrˆfddfS g ‰ d}t ˆttfƒstdƒ‚t ˆtƒr;ˆjjr;ˆjjr5‡ ‡fdd„tˆjƒD ƒ nˆfddfS t ˆtƒr^ˆjD ]}t |tƒrPˆ  	|¡ qC||9 }qCˆ ||t
|ƒ fS d S )Nr3   z term must be CG, Add, Mul or Powc                    s   g | ]}ˆ   ˆj¡‘qS r)   )r�   rŠ   )rA   r`   ©Zcgr”   r)   r*   rR   é  r«   z_cg_list.<locals>.<listcomp>)r‡   r   r   r   ÚNotImplementedErrorr‹   r?   rV   r(   r�   rš   )r”   ZcoeffrB   r)   rÀ   r*   Ú_cg_listà  s"   




úrÂ   r   )7rr   Zsympy.concrete.summationsr   Zsympy.core.addr   Zsympy.core.exprr   Zsympy.core.functionr   Zsympy.core.mulr   Zsympy.core.powerr   Zsympy.core.relationalr   Zsympy.core.singletonr	   Zsympy.core.symbolr
   r   Zsympy.core.sympifyr   Z(sympy.functions.elementary.miscellaneousr   Z$sympy.functions.elementary.piecewiser   Z sympy.printing.pretty.stringpictr   r   Z(sympy.functions.special.tensor_functionsr   Zsympy.physics.wignerr   r   r   r   Zsympy.printing.precedencer   Ú__all__r   r   r   r   r   rˆ   r�   r‘   r’   r›   r¯   r‰   r»   r¼   r½   rÂ   r)   r)   r)   r*   Ú<module>   sD   {VYh-.-
K
