o
    8VaY                     @   s*  d Z ddlmZmZmZmZmZmZmZm	Z	m
Z
mZmZ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 Z$d)ddZ%dd  Z&d!d" Z'd#d$ Z(d%d& Z)d'd( Z*dS )*zClebsch-Gordon Coefficients.    )AddexpandEqExprMul	PiecewisePowsqrtSumsymbolssympifyWild)
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(   :/usr/lib/python3/dist-packages/sympy/physics/quantum/cg.pyr   G      zWigner3j.__new__c                 C   
   | j d S Nr   r'   selfr(   r(   r)   r!   K      
zWigner3j.j1c                 C   r+   N   r-   r.   r(   r(   r)   r"   O   r0   zWigner3j.m1c                 C   r+   N   r-   r.   r(   r(   r)   r#   S   r0   zWigner3j.j2c                 C   r+   N   r-   r.   r(   r(   r)   r$   W   r0   zWigner3j.m2c                 C   r+   N   r-   r.   r(   r(   r)   r%   [   r0   zWigner3j.j3c                 C   r+   N   r-   r.   r(   r(   r)   r&   _   r0   zWigner3j.m3c                 C      t dd | jD  S )Nc                 S      g | ]}|j qS r(   	is_number.0argr(   r(   r)   
<listcomp>e       z(Wigner3j.is_symbolic.<locals>.<listcomp>allr'   r.   r(   r(   r)   is_symbolicc      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 }qt
|| }qIt
|  }|S )Nr4   r2   r6   c                       g | ]
}  |   qS r(   widthr@   ijmr(   r)   rB   p       z$Wigner3j._pretty.<locals>.<listcomp> )_printr!   r"   r#   r$   r%   r&   rangemaxrK   r   rightleftbelowparensr/   printerr'   ZhsepZvsepZmaxwDrM   ZD_rowsZwdeltaZwleftZwright_r(   rN   r)   _prettyh   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   rS   r!   r#   r%   r"   r$   r&   tupler/   r[   r'   Z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)	rF   
ValueErrorr   r!   r#   r%   r"   r$   r&   r/   Zhintsr(   r(   r)   doit      zWigner3j.doitN)__name__
__module____qualname____doc__Zis_commutativer   propertyr!   r"   r#   r$   r%   r&   rF   r_   rc   ri   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   r2   c                 K   re   rf   )	rF   rg   r   r!   r#   r%   r"   r$   r&   rh   r(   r(   r)   ri      rj   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,)Z	delimiterrR   C)Z
_print_seqr!   r"   r#   r$   r%   r&   rU   rK   r   rW   rV   r   rX   Zabove)r/   r[   r'   ZbottopZpadr]   r(   r(   r)   r_      s   z
CG._prettyc                 G   r`   )NzC^{%s,%s}_{%s,%s,%s,%s})	r   rS   r%   r&   r!   r"   r#   r$   ra   rb   r(   r(   r)   rc      s   z	CG._latexN)	rk   rl   rm   rn   r   Z
precedenceri   r_   rc   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%   rO   j23r'   r(   r(   r)   r      r*   zWigner6j.__new__c                 C   r+   r,   r-   r.   r(   r(   r)   r!      r0   zWigner6j.j1c                 C   r+   r1   r-   r.   r(   r(   r)   r#      r0   zWigner6j.j2c                 C   r+   r3   r-   r.   r(   r(   r)   rs     r0   zWigner6j.j12c                 C   r+   r5   r-   r.   r(   r(   r)   r%     r0   zWigner6j.j3c                 C   r+   r7   r-   r.   r(   r(   r)   rO   
  r0   z
Wigner6j.jc                 C   r+   r9   r-   r.   r(   r(   r)   rt     r0   zWigner6j.j23c                 C   r;   )Nc                 S   r<   r(   r=   r?   r(   r(   r)   rB     rC   z(Wigner6j.is_symbolic.<locals>.<listcomp>rD   r.   r(   r(   r)   rF     rG   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 }qt
|| }qIt
|jdd	d
 }|S )Nr4   r2   rH   r6   c                    rI   r(   rJ   rL   rN   r(   r)   rB     rQ   z$Wigner6j._pretty.<locals>.<listcomp>rR   {}rW   rV   )rS   r!   r%   r#   rO   rs   rt   rT   rU   rK   r   rV   rW   rX   rY   rZ   r(   rN   r)   r_     s@   
"zWigner6j._prettyc                 G   r`   )NzJ\left\{\begin{array}{ccc} %s & %s & %s \\ %s & %s & %s \end{array}\right\})	r   rS   r!   r#   rs   r%   rO   rt   ra   rb   r(   r(   r)   rc   :  rd   zWigner6j._latexc                 K   re   rf   )	rF   rg   r   r!   r#   rs   r%   rO   rt   rh   r(   r(   r)   ri   @  rj   zWigner6j.doitN)rk   rl   rm   rn   r   ro   r!   r#   rs   r%   rO   rt   rF   r_   rc   ri   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#   rs   r%   j4j34j13j24rO   r'   r(   r(   r)   r   O  s   zWigner9j.__new__c                 C   r+   r,   r-   r.   r(   r(   r)   r!   S  r0   zWigner9j.j1c                 C   r+   r1   r-   r.   r(   r(   r)   r#   W  r0   zWigner9j.j2c                 C   r+   r3   r-   r.   r(   r(   r)   rs   [  r0   zWigner9j.j12c                 C   r+   r5   r-   r.   r(   r(   r)   r%   _  r0   zWigner9j.j3c                 C   r+   r7   r-   r.   r(   r(   r)   rx   c  r0   zWigner9j.j4c                 C   r+   r9   r-   r.   r(   r(   r)   ry   g  r0   zWigner9j.j34c                 C   r+   )N   r-   r.   r(   r(   r)   rz   k  r0   zWigner9j.j13c                 C   r+   )N   r-   r.   r(   r(   r)   r{   o  r0   zWigner9j.j24c                 C   r+   )N   r-   r.   r(   r(   r)   rO   s  r0   z
Wigner9j.jc                 C   r;   )Nc                 S   r<   r(   r=   r?   r(   r(   r)   rB   y  rC   z(Wigner9j.is_symbolic.<locals>.<listcomp>rD   r.   r(   r(   r)   rF   w  rG   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 }qt|| }qXt|jdd	d
 }|S )Nr4   r2   rH   r6   c                    rI   r(   rJ   rL   rN   r(   r)   rB     rQ   z$Wigner9j._pretty.<locals>.<listcomp>rR   ru   rv   rw   )rS   r!   r%   rz   r#   rx   r{   rs   ry   rO   rT   rU   rK   r   rV   rW   rX   rY   rZ   r(   rN   r)   r_   |  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   rS   r!   r#   rs   r%   rx   ry   rz   r{   rO   ra   rb   r(   r(   r)   rc     s   zWigner9j._latexc              
   K   s8   | j rtdt| j| j| j| j| j| j| j	| j
| j	S rf   )rF   rg   r   r!   r#   rs   r%   rx   ry   rz   r{   rO   rh   r(   r(   r)   ri     s   *zWigner9j.doitN)rk   rl   rm   rn   r   ro   r!   r#   rs   r%   rx   ry   rz   r{   rO   rF   r_   rc   ri   r(   r(   r(   r)   r   F  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)   rB         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
    r2   )r   r'   Zhasr   r   r
   appendr   r   _check_varsh_871_1_check_varsh_871_2_check_varsh_872_9r   )r   Zcg_part
other_partrA   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   r4   r2   )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   r4   r2   rH   )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  }
d}|	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d}t|| }t|| }|| d t|||kfdt||f|||kf }|| | }t|
||td| ||||||||f||||||f||	\} }t|| }|| }|d | || d  }|| ||  | | }t|
||td| ||||||||f||||||f||	\} }| || | fS )N)	r   r   alphapr   betabetapr   gammar   r4   r2   r   )	r   r   r   r   r   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 rt|
 | t |  du r|
d7 }
qt| js*|
d7 }
q fdd|D }dg|  }t|
t D ]O}t| | |t |t | | ||fd}|du rdqBt|||jspqB|| |d|||||||f||||< qBtdd |D rt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   Nr2   c                    s   g | ]}| | fqS r(   r(   )r@   r   )sub_1r(   r)   rB         z"_check_cg_simp.<locals>.<listcomp>)r   c                 s   s    | ]}|d uV  qd S r   r(   rL   r(   r(   r)   	<genexpr>  s    z!_check_cg_simp.<locals>.<genexpr>c                 S   s   g | ]}t |d  qS )r4   )r   r@   r   r(   r(   r)   rB     r   c                 S   s   g | ]}|d  qS )r   r(   r   r(   r(   r)   rB     r   c                    s   g | ]}  |qS r(   )pop)r@   rO   )r   r(   r)   rB     s    r4   r6   )len	_check_cgr   subsr>   rT   rE   minsortreverser   r   )r   r   r   r   r   Z	variablesZdep_variablesZbuild_index_exprr   r   rM   Zsub_depZcg_indexrO   Zsub_2Zmin_ltindicesr   r(   )r   r   r)   r   L  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   r2   )matchr   ra   	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   r4   r2   )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   rH   r   r4   r2   )	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{dS | S )Nr   r   r   r   r   cpr   gammapr|   r4   r8   r2   )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st tstdt trBtjjrBtjjr< fddtjD  nfddfS t trej	D ]}t |trW 
| qJ||9 }qJ ||t| fS d S )Nr2   z term must be CG, Add, Mul or Powc                    s   g | ]}  jqS r(   )r   r   )r@   r^   Zcgr   r(   r)   rB     r   z_cg_list.<locals>.<listcomp>)r   r   r   r   NotImplementedErrorr   r   r>   rT   r'   r   r   )r   ZcoeffrA   r(   r   r)   _cg_list  s"   




r   r   )+rn   Zsympyr   r   r   r   r   r   r   r	   r
   r   r   r   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>   s.   8{VYh-.-
K
