U
    9%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% dddddg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% Z0d3d'd(Z1d)d* Z2d+d, Z3d-d. Z4d/d0 Z5d1d2 Z6d&S )4z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   s&   t t||||||f}tj| f| S Nmapr   r   __new__)clsj1m1j2m2j3m3args r(   W/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/sympy/physics/quantum/cg.pyr   Q   s    zWigner3j.__new__c                 C   s
   | j d S Nr   r'   selfr(   r(   r)   r!   U   s    zWigner3j.j1c                 C   s
   | j d S N   r+   r,   r(   r(   r)   r"   Y   s    zWigner3j.m1c                 C   s
   | j d S N   r+   r,   r(   r(   r)   r#   ]   s    zWigner3j.j2c                 C   s
   | j d S N   r+   r,   r(   r(   r)   r$   a   s    zWigner3j.m2c                 C   s
   | j d S N   r+   r,   r(   r(   r)   r%   e   s    zWigner3j.j3c                 C   s
   | j d S N   r+   r,   r(   r(   r)   r&   i   s    zWigner3j.m3c                 C   s   t dd | jD  S )Nc                 s   s   | ]}|j V  qd S r   	is_number.0argr(   r(   r)   	<genexpr>o   s     z'Wigner3j.is_symbolic.<locals>.<genexpr>allr'   r,   r(   r(   r)   is_symbolicm   s    zWigner3j.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 | < q`d }tdD ]}d }tdD ]   | }	|  |		  }
|
d }|
| }t
|	d|  }	t
|	d|  }	|d kr|	}qt
|d|  }t
||	 }q|d kr8|}qt|D ]}t
|d }q@t
|| }qt
|  }|S )Nr1   r/   r3   c                    s   g | ]}  |   qS r(   widthr;   ijmr(   r)   
<listcomp>z   s     z$Wigner3j._pretty.<locals>.<listcomp> )_printr!   r"   r#   r$   r%   r&   rangemaxrC   r   rightleftbelowparensr-   printerr'   ZhsepZvsepZmaxwDrE   ZD_rowsZwdeltaZwleftZwright_r(   rF   r)   _prettyr   s@    
"

zWigner3j._prettyc                 G   s0   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   rK   r!   r#   r%   r"   r$   r&   tupler-   rS   r'   labelr(   r(   r)   _latex   s      zWigner3j._latexc                 K   s,   | j rtdt| j| j| j| j| j| jS NzCoefficients must be numerical)	r@   
ValueErrorr   r!   r#   r%   r"   r$   r&   r-   hintsr(   r(   r)   doit   s    zWigner3j.doitN)__name__
__module____qualname____doc__Zis_commutativer   propertyr!   r"   r#   r$   r%   r&   r@   rW   r[   r`   r(   r(   r(   r)   r   &   s(   (






#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   r/   c                 K   s,   | j rtdt| j| j| j| j| j| jS r\   )	r@   r]   r   r!   r#   r%   r"   r$   r&   r^   r(   r(   r)   r`      s    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st	|d||    }|| kst	|d||    }tdd|  }t	|| }t	|| }|S )N,)	delimiterrJ   C)Z
_print_seqr!   r"   r#   r$   r%   r&   rM   rC   r   rO   rN   r   rP   Zabove)r-   rS   r'   ZbottoppadrU   r(   r(   r)   rW      s      z
CG._prettyc                 G   s0   t |j| j| j| j| j| j| jf}dt| S )NzC^{%s,%s}_{%s,%s,%s,%s})	r   rK   r%   r&   r!   r"   r#   r$   rX   rY   r(   r(   r)   r[      s      z	CG._latexN)	ra   rb   rc   rd   r   
precedencer`   rW   r[   r(   r(   r(   r)   r      s
   6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   s&   t t||||||f}tj| f| S r   r   )r    r!   r#   j12r%   rG   j23r'   r(   r(   r)   r      s    zWigner6j.__new__c                 C   s
   | j d S r*   r+   r,   r(   r(   r)   r!     s    zWigner6j.j1c                 C   s
   | j d S r.   r+   r,   r(   r(   r)   r#     s    zWigner6j.j2c                 C   s
   | j d S r0   r+   r,   r(   r(   r)   rl     s    zWigner6j.j12c                 C   s
   | j d S r2   r+   r,   r(   r(   r)   r%     s    zWigner6j.j3c                 C   s
   | j d S r4   r+   r,   r(   r(   r)   rG     s    z
Wigner6j.jc                 C   s
   | j d S r6   r+   r,   r(   r(   r)   rm     s    zWigner6j.j23c                 C   s   t dd | jD  S )Nc                 s   s   | ]}|j V  qd S r   r8   r:   r(   r(   r)   r=     s     z'Wigner6j.is_symbolic.<locals>.<genexpr>r>   r,   r(   r(   r)   r@     s    zWigner6j.is_symbolicc                    s|  | | 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 | < q`d }tdD ]}d }tdD ]   | }	|  |		  }
|
d }|
| }t
|	d|  }	t
|	d|  }	|d kr|	}qt
|d|  }t
||	 }q|d kr8|}qt|D ]}t
|d }q@t
|| }qt
|jdd	d
 }|S )Nr1   r/   rA   r3   c                    s   g | ]}  |   qS r(   rB   rD   rF   r(   r)   rI   )  s     z$Wigner6j._pretty.<locals>.<listcomp>rJ   {}rO   rN   )rK   r!   r%   r#   rG   rl   rm   rL   rM   rC   r   rN   rO   rP   rQ   rR   r(   rF   r)   rW   !  s@    
"

zWigner6j._prettyc                 G   s0   t |j| j| j| j| j| j| jf}dt| S )NzJ\left\{\begin{array}{ccc} %s & %s & %s \\ %s & %s & %s \end{array}\right\})	r   rK   r!   r#   rl   r%   rG   rm   rX   rY   r(   r(   r)   r[   D  s      zWigner6j._latexc                 K   s,   | j rtdt| j| j| j| j| j| jS r\   )	r@   r]   r   r!   r#   rl   r%   rG   rm   r^   r(   r(   r)   r`   J  s    zWigner6j.doitN)ra   rb   rc   rd   r   re   r!   r#   rl   r%   rG   rm   r@   rW   r[   r`   r(   r(   r(   r)   r      s&   






#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| f|
 S r   r   )r    r!   r#   rl   r%   j4j34j13j24rG   r'   r(   r(   r)   r   Y  s    zWigner9j.__new__c                 C   s
   | j d S r*   r+   r,   r(   r(   r)   r!   ]  s    zWigner9j.j1c                 C   s
   | j d S r.   r+   r,   r(   r(   r)   r#   a  s    zWigner9j.j2c                 C   s
   | j d S r0   r+   r,   r(   r(   r)   rl   e  s    zWigner9j.j12c                 C   s
   | j d S r2   r+   r,   r(   r(   r)   r%   i  s    zWigner9j.j3c                 C   s
   | j d S r4   r+   r,   r(   r(   r)   rq   m  s    zWigner9j.j4c                 C   s
   | j d S r6   r+   r,   r(   r(   r)   rr   q  s    zWigner9j.j34c                 C   s
   | j d S )N   r+   r,   r(   r(   r)   rs   u  s    zWigner9j.j13c                 C   s
   | j d S )N   r+   r,   r(   r(   r)   rt   y  s    zWigner9j.j24c                 C   s
   | j d S )N   r+   r,   r(   r(   r)   rG   }  s    z
Wigner9j.jc                 C   s   t dd | jD  S )Nc                 s   s   | ]}|j V  qd S r   r8   r:   r(   r(   r)   r=     s     z'Wigner9j.is_symbolic.<locals>.<genexpr>r>   r,   r(   r(   r)   r@     s    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 ]}d }t
dD ]   | }	|  |	  }
|
d }|
| }t|	d|  }	t|	d|  }	|d kr$|	}qt|d|  }t||	 }q|d krV|}qt
|D ]}t|d }q^t|| }qt|jdd	d
 }|S )Nr1   r/   rA   r3   c                    s   g | ]}  |   qS r(   rB   rD   rF   r(   r)   rI     s     z$Wigner9j._pretty.<locals>.<listcomp>rJ   rn   ro   rp   )rK   r!   r%   rs   r#   rq   rt   rl   rr   rG   rL   rM   rC   r   rN   rO   rP   rQ   rR   r(   rF   r)   rW     sT    
 

 
 
"

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   rK   r!   r#   rl   r%   rq   rr   rs   rt   rG   rX   rY   r(   r(   r)   r[     s        zWigner9j._latexc              
   K   s8   | j rtdt| j| j| j| j| j| j| j	| j
| j	S r\   )r@   r]   r   r!   r#   rl   r%   rq   rr   rs   rt   rG   r^   r(   r(   r)   r`     s    zWigner9j.doitN)ra   rb   rc   rd   r   re   r!   r#   rl   r%   rq   rr   rs   rt   rG   r@   rW   r[   r`   r(   r(   r(   r)   r   P  s2   









&c                 C   sf   t | trt| S t | tr$t| S t | trBtdd | jD  S t | tr^tt| j	| j
S | S d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)   rI     s     zcg_simp.<locals>.<listcomp>N)
isinstancer   _cg_simp_addr   _cg_simp_sumr   r'   r   r   baseexper(   r(   r)   r     s    !



c                 C   s  g }g }t | } | jD ]}|trt|tr>|t| qt|trd}|jD ]$}t|trn|t|9 }qR||9 }qR|tr|| 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
    r/   )r   r'   hasr   rx   r   appendrz   r   _check_varsh_871_1_check_varsh_871_2_check_varsh_872_9r   )r~   Zcg_part
other_partr<   Ztermstermotherr(   r(   r)   ry     s2    










ry   c           
   
   C   s   t td\}}}}|t|||d|| }d| d t|d }|t| }d| d }|| }	t||||| ||||f||f||		S )N)aalphabltr   r1   r/   )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   r1   r/   rA   )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   r1   r/   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 rt|
 | t |  dkr<|
d7 }
q| jsR|
d7 }
q fdd|D }dg|  }t|
t D ]}t| | |t |t | | ||fd}|dkrq|||jsq|| |d|||||||f||||< qtdd |D std	d |D  }d
d |D }|  |  fdd|D  |D ]:}t	|d |krx
|d ||d   |d   qx|	|||   7 }	q|
d7 }
q|	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   Nr/   c                    s   g | ]}| | fqS r(   r(   )r;   r   )sub_1r(   r)   rI     s     z"_check_cg_simp.<locals>.<listcomp>)r   c                 s   s   | ]}|d kV  qd S r   r(   rD   r(   r(   r)   r=     s     z!_check_cg_simp.<locals>.<genexpr>c                 S   s   g | ]}t |d  qS )r1   )r   r;   r   r(   r(   r)   rI     s     c                 S   s   g | ]}|d  qS )r   r(   r   r(   r(   r)   rI     s     c                    s   g | ]}  |qS r(   )pop)r;   rG   )r   r(   r)   rI     s     r1   r3   )len	_check_cgsubsr9   rL   anyminsortreverser   r   )r   r   r   r   r   	variablesZdep_variablesZbuild_index_exprr   r   rE   Zsub_depZcg_indexrG   Zsub_2Zmin_ltindicesr   r(   )r   r   r)   r   V  s>    )6D&
r   Nc                 C   s^   |  |}|dkrdS |dk	rJt|ts0td|d |d |ksJdS t||krZ|S dS )z2Checks whether a term matches the given expressionNzsign must be a tupler   r/   )matchrx   rX   	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)   rz     s    rz   c              
   C   sr   t d}td}t d}| tt|||d|||| |f}|d k	rnt|dkrnd| d t|d |S | S )Nr   r   r   r   r1   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 || |f}|d k	rt|dkrtd| d t|d |S | S )Nr   r   r   rA   r   r1   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 d}t d}td}td}td}td}td}td}t||||||}	t||||||}
| t|	|
 || |f|| |f}|d k	rt|d	krt||t|| |S | t|	d
 || |f|| |f}|d k	rt|dkrtj	S | S )Nr   r   r   r   r   cpr   gammapru   r1   r5   )
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s4tdt trvjjrvjjrj fddtjD  nfddfS t trjD ]"}t |tr 	| q||9 }q ||t
| fS d S )Nr/   z term must be CG, Add, Mul or Powc                    s   g | ]}  jqS r(   )r   r{   )r;   rV   Zcgr   r(   r)   rI     s     z_cg_list.<locals>.<listcomp>)rx   r   r   r   NotImplementedErrorr|   r9   rL   r'   r   r   )r   Zcoeffr<   r(   r   r)   _cg_list  s     




r   )N)7rd   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   ry   r   r   r   r   r   rz   r   r   r   r   r(   r(   r(   r)   <module>   sL   {VYh-.-K

