B
    [W                 @   s@  d Z ddlmZm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 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	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 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.    )print_functiondivision)AddexpandEqExprMul	PiecewisePowsqrtSumsymbolssympifyWild)range)
prettyForm
stringPict)KroneckerDelta)clebsch_gordan	wigner_3j	wigner_6j	wigner_9j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

    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'   7lib/python3.7/site-packages/sympy/physics/quantum/cg.pyr   F   s    zWigner3j.__new__c             C   s
   | j d S )Nr   )r&   )selfr'   r'   r(   r    J   s    zWigner3j.j1c             C   s
   | j d S )N   )r&   )r)   r'   r'   r(   r!   N   s    zWigner3j.m1c             C   s
   | j d S )N   )r&   )r)   r'   r'   r(   r"   R   s    zWigner3j.j2c             C   s
   | j d S )N   )r&   )r)   r'   r'   r(   r#   V   s    zWigner3j.m2c             C   s
   | j d S )N   )r&   )r)   r'   r'   r(   r$   Z   s    zWigner3j.j3c             C   s
   | j d S )N   )r&   )r)   r'   r'   r(   r%   ^   s    zWigner3j.m3c             C   s   t dd | jD  S )Nc             S   s   g | ]
}|j qS r'   )	is_number).0argr'   r'   r(   
<listcomp>d   s    z(Wigner3j.is_symbolic.<locals>.<listcomp>)allr&   )r)   r'   r'   r(   is_symbolicb   s    zWigner3j.is_symbolicc                s  | | j| | jf| | j| | jf| | j| | jffd}d}dgd }x0tdD ]$ t fddtdD | < qbW d }xtdD ]}d }xtdD ]   | }	|  |		  }
|
d }|
| }t
|	d|  }	t
|	d|  }	|d kr|	}qt
|d|  }t
||	 }qW |d krB|}qx t|D ]}t
|d }qLW t
|| }qW t
|  }|S )Nr+   r*   r,   c                s   g | ]}  |   qS r'   )width)r0   i)jmr'   r(   r2   o   s    z$Wigner3j._pretty.<locals>.<listcomp> )_printr    r!   r"   r#   r$   r%   r   maxr6   r   rightleftbelowparens)r)   printerr&   hsepvsepmaxwDr7   D_rowswdeltawleftwright_r'   )r8   r9   r(   _prettyg   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   r;   r    r"   r$   r!   r#   r%   tuple)r)   rA   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)	r4   
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%   r4   rL   rO   rR   r'   r'   r'   r(   r      s   $#c               @   s(   e Zd ZdZdd Zdd Zdd ZdS )	r   a1  Class for Clebsch-Gordan coefficient

    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_1,m_1}_{j_2,m_2,j_3,m_3} = \langle j_1,m_1;j_2,m_2 | j_3,m_3\rangle

    Parameters
    ==========

    j1, m1, j2, m2, j3, m3 : Number, Symbol
        Terms determining the angular momentum of coupled angular momentum
        systems.

    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

    See Also
    ========

    Wigner3j: Wigner-3j symbols

    References
    ==========

    .. [1] Varshalovich, D A, Quantum Theory of Angular Momentum. 1988.
    c             K   s,   | j rtdt| j| j| j| j| j| jS )NzCoefficients must be numerical)	r4   rP   r   r    r"   r$   r!   r#   r%   )r)   rQ   r'   r'   r(   rR      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,)Z	delimiterr:   C)Z
_print_seqr    r!   r"   r#   r$   r%   r<   r6   r   r>   r=   r   r?   Zabove)r)   rA   r&   ZbottopZpadrG   r'   r'   r(   rL      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   r;   r$   r%   r    r!   r"   r#   rM   )r)   rA   r&   rN   r'   r'   r(   rO      s    z	CG._latexN)rS   rT   rU   rV   rR   rL   rO   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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 )N)r   r   r   r   )r   r    r"   j12r$   r8   j23r&   r'   r'   r(   r      s    zWigner6j.__new__c             C   s
   | j d S )Nr   )r&   )r)   r'   r'   r(   r       s    zWigner6j.j1c             C   s
   | j d S )Nr*   )r&   )r)   r'   r'   r(   r"      s    zWigner6j.j2c             C   s
   | j d S )Nr+   )r&   )r)   r'   r'   r(   r[      s    zWigner6j.j12c             C   s
   | j d S )Nr,   )r&   )r)   r'   r'   r(   r$      s    zWigner6j.j3c             C   s
   | j d S )Nr-   )r&   )r)   r'   r'   r(   r8      s    z
Wigner6j.jc             C   s
   | j d S )Nr.   )r&   )r)   r'   r'   r(   r\      s    zWigner6j.j23c             C   s   t dd | jD  S )Nc             S   s   g | ]
}|j qS r'   )r/   )r0   r1   r'   r'   r(   r2     s    z(Wigner6j.is_symbolic.<locals>.<listcomp>)r3   r&   )r)   r'   r'   r(   r4     s    zWigner6j.is_symbolicc                s  | | j| | jf| | j| | jf| | j| | jffd}d}dgd }x0tdD ]$ t fddtdD | < qbW d }xtdD ]}d }xtdD ]   | }	|  |		  }
|
d }|
| }t
|	d|  }	t
|	d|  }	|d kr|	}qt
|d|  }t
||	 }qW |d krB|}qx t|D ]}t
|d }qLW t
|| }qW t
|jdd	d
 }|S )Nr+   r*   r5   r,   c                s   g | ]}  |   qS r'   )r6   )r0   r7   )r8   r9   r'   r(   r2     s    z$Wigner6j._pretty.<locals>.<listcomp>r:   {})r>   r=   )r;   r    r$   r"   r8   r[   r\   r   r<   r6   r   r=   r>   r?   r@   )r)   rA   r&   rB   rC   rD   rE   r7   rF   rG   rH   rI   rJ   rK   r'   )r8   r9   r(   rL     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   r;   r    r"   r[   r$   r8   r\   rM   )r)   rA   r&   rN   r'   r'   r(   rO   *  s    zWigner6j._latexc             K   s,   | j rtdt| j| j| j| j| j| jS )NzCoefficients must be numerical)	r4   rP   r   r    r"   r[   r$   r8   r\   )r)   rQ   r'   r'   r(   rR   0  s    zWigner6j.doitN)rS   rT   rU   rV   r   rW   r    r"   r[   r$   r8   r\   r4   rL   rO   rR   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 )N)r   r   r   r   )r   r    r"   r[   r$   j4j34j13j24r8   r&   r'   r'   r(   r   ?  s    zWigner9j.__new__c             C   s
   | j d S )Nr   )r&   )r)   r'   r'   r(   r    C  s    zWigner9j.j1c             C   s
   | j d S )Nr*   )r&   )r)   r'   r'   r(   r"   G  s    zWigner9j.j2c             C   s
   | j d S )Nr+   )r&   )r)   r'   r'   r(   r[   K  s    zWigner9j.j12c             C   s
   | j d S )Nr,   )r&   )r)   r'   r'   r(   r$   O  s    zWigner9j.j3c             C   s
   | j d S )Nr-   )r&   )r)   r'   r'   r(   r_   S  s    zWigner9j.j4c             C   s
   | j d S )Nr.   )r&   )r)   r'   r'   r(   r`   W  s    zWigner9j.j34c             C   s
   | j d S )N   )r&   )r)   r'   r'   r(   ra   [  s    zWigner9j.j13c             C   s
   | j d S )N   )r&   )r)   r'   r'   r(   rb   _  s    zWigner9j.j24c             C   s
   | j d S )N   )r&   )r)   r'   r'   r(   r8   c  s    z
Wigner9j.jc             C   s   t dd | jD  S )Nc             S   s   g | ]
}|j qS r'   )r/   )r0   r1   r'   r'   r(   r2   i  s    z(Wigner9j.is_symbolic.<locals>.<listcomp>)r3   r&   )r)   r'   r'   r(   r4   g  s    zWigner9j.is_symbolicc                s  | | j| | j| | jf| | j| | j| | jf| | j| | j| | j	ffd}d}dgd }x0t
dD ]$ t fddt
dD | < qW d }xt
dD ]}d }xt
dD ]   | }	|  |	  }
|
d }|
| }t|	d|  }	t|	d|  }	|d kr,|	}qt|d|  }t||	 }qW |d kr`|}qx t
|D ]}t|d }qjW t|| }qW t|jdd	d
 }|S )Nr+   r*   r5   r,   c                s   g | ]}  |   qS r'   )r6   )r0   r7   )r8   r9   r'   r(   r2   w  s    z$Wigner9j._pretty.<locals>.<listcomp>r:   r]   r^   )r>   r=   )r;   r    r$   ra   r"   r_   rb   r[   r`   r8   r   r<   r6   r   r=   r>   r?   r@   )r)   rA   r&   rB   rC   rD   rE   r7   rF   rG   rH   rI   rJ   rK   r'   )r8   r9   r(   rL   l  sB    $
$

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   r;   r    r"   r[   r$   r_   r`   ra   rb   r8   rM   )r)   rA   r&   rN   r'   r'   r(   rO     s    zWigner9j._latexc          
   K   s8   | j rtdt| j| j| j| j| j| j| j	| j
| j	S )NzCoefficients must be numerical)r4   rP   r   r    r"   r[   r$   r_   r`   ra   rb   r8   )r)   rQ   r'   r'   r(   rR     s    zWigner9j.doitN)rS   rT   rU   rV   r   rW   r    r"   r[   r$   r_   r`   ra   rb   r8   r4   rL   rO   rR   r'   r'   r'   r(   r   6  s   &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

    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   )r0   r1   r'   r'   r(   r2     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 | } x| jD ]}|trt|tr@|t| qt|trd}x.|jD ]$}t|trr|t|9 }qV||9 }qVW |tr|| q|| q|| q|| qW t	|\}}|| t
|\}}|| t|\}}|| t| t|  S )a  Takes a sum of terms involving Clebsch-Gordan coefficients and
    simplifies the terms.

    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&   Zhasr   rf   r   appendrh   r   _check_varsh_871_1_check_varsh_871_2_check_varsh_872_9r   )rk   Zcg_part
other_partr1   Ztermstermotherr'   r'   r(   rg     s2    








rg   c       
   
   C   s   t td\}}}}|t|||d|| }d| d t|d }|t| }d| d }|| }	t||||| ||||f||f||		S )N)aalphabltr   r+   r*   )r   r   r   r   abs_check_cg_simp)
	term_listrs   rt   ru   rv   exprsimpsign
build_expr
index_exprr'   r'   r(   rm     s    rm   c       
   
   C   s   t td\}}}}|t|||| |d }td| d t|d }d||  | t| }d| d }|| }	t||||| ||||f||f||		S )N)rs   rt   crv   r   r+   r*   r5   )r   r   r   r   r   rw   rx   )
ry   rs   rt   r   rv   rz   r{   r|   r}   r~   r'   r'   r(   rn     s    rn   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)	rs   rt   alphapru   betabetapr   gammarv   r+   r*   r   )	r   r   r   rw   r	   r   rx   r   r   )ry   rs   rt   r   ru   r   r   r   r   rv   rz   r{   r|   xyr}   r~   Zother1Zother2Zother3Zother4r'   r'   r(   ro   	  s:    222$2<<ro   c	          	      s  d}	d}
x|
t k rt|
 | t |  dkr@|
d7 }
qt| jsZ|
d7 }
q fdd|D }dg|  }xt|
t D ]}t| | |t |t | | ||fd}|dkrqt|||jsq|| |d|||||||f||||< qW tdd |D rtd	d |D  }d
d |D }|  |	  fdd|D  xB|D ]:}t
|d |kr|d ||d   |d   qW |	|||   7 }	q|
d7 }
qW |	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'   )r0   r   )sub_1r'   r(   r2   i  s    z"_check_cg_simp.<locals>.<listcomp>)r|   c             s   s   | ]}|d k	V  qd S )Nr'   )r0   r7   r'   r'   r(   	<genexpr>r  s    z!_check_cg_simp.<locals>.<genexpr>c             S   s   g | ]}t |d  qS )r+   )rw   )r0   rq   r'   r'   r(   r2   s  s    c             S   s   g | ]}|d  qS )r   r'   )r0   rq   r'   r'   r(   r2   t  s    c                s   g | ]}  |qS r'   )pop)r0   r8   )ry   r'   r(   r2   w  s    r+   r,   )len	_check_cgr   subsr/   r   r3   minsortreverserw   rl   )rz   r{   r|   rv   ry   Z	variablesZdep_variablesZbuild_index_exprr~   rp   r7   Zsub_depZcg_indexr8   Zsub_2Zmin_ltindicesrq   r'   )r   ry   r(   rx   6  s>    )6F
(rx   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*   )matchrf   rM   	TypeErrorr   r   )Zcg_termrz   lengthr|   Zmatchesr'   r'   r(   r     s    

r   c             C   s   t | } t| } t| } | S )N)_check_varsh_sum_871_1_check_varsh_sum_871_2_check_varsh_sum_872_4)rk   r'   r'   r(   rh     s    rh   c          
   C   sr   t d}td}t d}| tt|||d|||| |f}|d k	rnt|dkrnd| d t|d |S | S )Nrs   rt   ru   r   r+   r*   )r   r   r   r   r   r   r   r   )rk   rs   rt   ru   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 )Nrs   rt   r   r5   r   r+   r*   )	r   r   r   r   r   r   r   r   r   )rk   rs   rt   r   r   r'   r'   r(   r     s    0 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t||||||d
 || |f|| |f}
|
d k	rt|
dkrdS | S )Nrs   rt   ru   r   r   cpr   gammapre   r+   rc   r*   )r   r   r   r   r   r   r   )rk   rs   rt   ru   r   r   r   r   r   Z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rtjjrtjjrx fddtjD  nfddfS t trx,j	D ]"}t |tr 
| q||9 }qW  ||t| fS d S )Nr*   z term must be CG, Add, Mul or Powc                s   g | ]}  jqS r'   )rl   ri   )r0   rK   )cgrq   r'   r(   r2     s    z_cg_list.<locals>.<listcomp>)rf   r   r   r
   NotImplementedErrorr   rj   r/   r   r&   rl   rw   )rq   Zcoeffr1   r'   )r   rq   r(   _cg_list  s     


r   )N).rV   Z
__future__r   r   Zsympyr   r   r   r   r   r	   r
   r   r   r   r   r   Zsympy.core.compatibilityr   Z sympy.printing.pretty.stringpictr   r   Z(sympy.functions.special.tensor_functionsr   Zsympy.physics.wignerr   r   r   r   __all__r   r   r   r   r   rg   rm   rn   ro   rx   r   rh   r   r   r   r   r'   r'   r'   r(   <module>   s6   8xGYh*+-K

