o
    jg                    @   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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 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'm(Z( ddl)m*Z* ddl+m,Z, ddl-m.Z.m/Z/m0Z0 ddl1m2Z2m3Z3m4Z4 ddl5m6Z6 ddl7m8Z8 ddl9m:Z:m;Z; ddl<m=Z= ddl>m?Z? ddl@mAZA g dZBdd ZCG d d! d!ZDG d"d# d#eDe/ZEG d$d% d%eDe/ZFG d&d' d'eDe.ZGG d(d) d)eDe.ZHG d*d+ d+eDe.ZIG d,d- d-eDe.ZJG d.d/ d/e0ZKG d0d1 d1eZLeGd2ZMeHd2ZNeId2ZOeJd2ZPeEd2ZQeFd2ZRG d3d4 d4e4ZSG d5d6 d6eSe3ZTG d7d8 d8eSe2ZUG d9d: d:eSe3ZVG d;d< d<eSe2ZWG d=d> d>eSe3ZXG d?d@ d@eSe2ZYdAdB ZZG dCdD dDeSZ[G dEdF dFe[e3Z\G dGdH dHe[e2Z]G dIdJ dJe[e3Z^G dKdL dLe[e2Z_G dMdN dNe[e3Z`G dOdP dPe[e2Zad\dRdSZbdTdU Zcd]dVdWZddXdY ZedZd[ ZfdQS )^z$Quantum mechanical angular momemtum.    )SumAdd)Tuple)Expr)
int_valued)Mul)IIntegerRationalpi)S)Dummysymbolssympify)binomial	factorial)exp)sqrt)cossin)simplify)zeros)
prettyForm
stringPictpretty_symbol)QExpr)HermitianOperatorOperatorUnitaryOperator)BraKetState)KroneckerDelta)hbar)ComplexSpaceDirectSumHilbertSpace)TensorProductCG)qapply)m_valuesJplusJminusJxJyJzJ2RotationWignerDJxKetJxBraJyKetJyBraJzKetJzBraJzOpJ2OpJxKetCoupledJxBraCoupledJyKetCoupledJyBraCoupledJzKetCoupledJzBraCoupledcoupleuncouplec                    sT   t   d  d }|jr|dkstd  | fddttd  d D fS )N      r   z<Only integer or half-integer values allowed for j, got: : %rc                    s   g | ]} | qS  rH   .0ijrH   R/var/www/html/zoom/venv/lib/python3.10/site-packages/sympy/physics/quantum/spin.py
<listcomp>F       zm_values.<locals>.<listcomp>)r   
is_Integer
ValueErrorrangeint)rM   sizerH   rL   rN   r-   ?   s   &r-   c                   @   s   e Zd ZdZedd Zedd Zdd Zdd	 Z	d
d Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"S )#
SpinOpBasezBase class for spin operators.c                 C   
   t tjS Nr'   r   InfinityclslabelrH   rH   rN   _eval_hilbert_spaceQ      
zSpinOpBase._eval_hilbert_spacec                 C   
   | j d S Nr   argsselfrH   rH   rN   nameV      
zSpinOpBase.namec                 G      d| j | jf S )Nz%s%srf   _coordre   printerrc   rH   rH   rN   _print_contentsZ      zSpinOpBase._print_contentsc                 G   s$   t t| j}t | j}| ||S rX   )r   strrf   rj   _print_subscript_prettyre   rl   rc   abrH   rH   rN   _print_contents_pretty]   s   
z!SpinOpBase._print_contents_prettyc                 G   rh   )Nz%s_%sri   rk   rH   rH   rN   _print_contents_latexb   rn   z SpinOpBase._print_contents_latexc           
   	   K   sh   | dtj}t|\}}t||}t|D ]}t|D ]}| ||| ||| }	|	|||f< qq|S )NrM   )getr   Halfr-   r   rS   matrix_element)
re   basisoptionsrM   rU   mvalsresultpqmerH   rH   rN   _represent_basee   s   
zSpinOpBase._represent_basec                 K   sj   | | j}t|trt|j | }nt|tr"| j|fi |}nt| | }|| | kr0t	| |S rX   )
rewritery   
isinstancer$   r&   mr   _apply_operator_Sumr,   NotImplementedError)re   ket
orig_basisrz   stateretrH   rH   rN   	_apply_opo   s   


zSpinOpBase._apply_opc                 K      | j |dfi |S Nr0   r   re   r   rz   rH   rH   rN   _apply_operator_JxKet}      z SpinOpBase._apply_operator_JxKetc                 K   r   r   r   r   rH   rH   rN   _apply_operator_JxKetCoupled   r   z'SpinOpBase._apply_operator_JxKetCoupledc                 K   r   Nr1   r   r   rH   rH   rN   _apply_operator_JyKet   r   z SpinOpBase._apply_operator_JyKetc                 K   r   r   r   r   rH   rH   rN   _apply_operator_JyKetCoupled   r   z'SpinOpBase._apply_operator_JyKetCoupledc                 K   r   Nr2   r   r   rH   rH   rN   _apply_operator_JzKet   r   z SpinOpBase._apply_operator_JzKetc                 K   r   r   r   r   rH   rH   rN   _apply_operator_JzKetCoupled   r   z'SpinOpBase._apply_operator_JzKetCoupledc                 K   s   t | tttfs
tg }tt|jD ]-}g }||jd |  |	| 
|j|  ||j|d d   |	|j|  qt|  S NrG   )r   JxOpJyOpr<   r   rS   lenrc   extendappend_apply_operator	__class__r   expand)re   tprz   r|   nargrH   rH   rN   _apply_operator_TensorProduct   s   z(SpinOpBase._apply_operator_TensorProductc                 K   s2   t | |j }|| |j krtt|g|jR  S rX   )r,   functionr   r   limits)re   srz   new_funcrH   rH   rN   r      s   zSpinOpBase._apply_operator_Sumc                 K   s   |    S rX   )_represent_default_basistracere   rz   rH   rH   rN   _eval_trace   s   zSpinOpBase._eval_traceN)__name__
__module____qualname____doc__classmethodr^   propertyrf   rm   rt   ru   r   r   r   r   r   r   r   r   r   r   r   rH   rH   rH   rN   rV   N   s(    


rV   c                   @   P   e Zd ZdZdZdZdd Zdd Zdd	 Zd
d Z	dd Z
dd Zdd ZdS )JplusOpzThe J+ operator.+r2   c                 C   s   dt  t| j S NrF   )r&   r<   rf   re   otherrH   rH   rN   _eval_commutator_JminusOp      z!JplusOp._eval_commutator_JminusOpc                 K   sZ   |j }|j}|jr|jr||krtjS tt||tj  ||tj    t||tj  S rX   	rM   r   	is_Numberr   Zeror&   r   Oner:   re   r   rz   rM   r   rH   rH   rN   r      s   4zJplusOp._apply_operator_JzKetc                 K   sj   |j }|j}|j}|j}|jr|jr||krtjS tt||tj	  ||tj	    t
||tj	 || S rX   rM   r   jncouplingr   r   r   r&   r   r   rB   re   r   rz   rM   r   r   r   rH   rH   rN   r      s   8z$JplusOp._apply_operator_JzKetCoupledc                 C   sH   t t||tj  ||tj    }|t||d 9 }|t||9 }|S r   r&   r   r   r   r%   re   rM   r   jpmpr|   rH   rH   rN   rx         $zJplusOp.matrix_elementc                 K      | j di |S NrX   _represent_JzOpr   rH   rH   rN   r      rn   z JplusOp._represent_default_basisc                 K      | j |fi |S rX   r   re   ry   rz   rH   rH   rN   r      r   zJplusOp._represent_JzOpc                 O   s   t |d tt|d   S ra   r   r	   r   re   rc   kwargsrH   rH   rN   _eval_rewrite_as_xyz      zJplusOp._eval_rewrite_as_xyzN)r   r   r   r   rj   ry   r   r   r   rx   r   r   r   rH   rH   rH   rN   r      s    
r   c                   @   sH   e Zd ZdZdZdZdd Zdd Zdd	 Zd
d Z	dd Z
dd ZdS )JminusOpzThe J- operator.-r2   c                 K   s\   |j }|j}|jr|jr|| krtjS tt||tj  ||tj    t||tj  S rX   r   r   rH   rH   rN   r      s   
4zJminusOp._apply_operator_JzKetc                 K   sl   |j }|j}|j}|j}|jr|jr|| krtjS tt||tj	  ||tj	    t
||tj	 || S rX   r   r   rH   rH   rN   r      s   
8z%JminusOp._apply_operator_JzKetCoupledc                 C   sH   t t||tj  ||tj    }|t||d 9 }|t||9 }|S r   r   r   rH   rH   rN   rx      r   zJminusOp.matrix_elementc                 K   r   r   r   r   rH   rH   rN   r      rn   z!JminusOp._represent_default_basisc                 K   r   rX   r   r   rH   rH   rN   r      r   zJminusOp._represent_JzOpc                 O   s   t |d tt|d   S ra   r   r   rH   rH   rN   r      r   zJminusOp._eval_rewrite_as_xyzN)r   r   r   r   rj   ry   r   r   rx   r   r   r   rH   rH   rH   rN   r      s    
r   c                   @   r   )r   zThe Jx operator.xr0   c                 C      t t t| j S rX   )r	   r&   r<   rf   r   rH   rH   rN   _eval_commutator_JyOp	  r   zJxOp._eval_commutator_JyOpc                 C      t  t t| j S rX   r	   r&   r   rf   r   rH   rH   rN   _eval_commutator_JzOp  r   zJxOp._eval_commutator_JzOpc                 K   @   t | jj|fi |}t| jj|fi |}|| td S r   )r   rf   r   r   r
   re   r   rz   r   jmrH   rH   rN   r        zJxOp._apply_operator_JzKetc                 K   r   r   )r   rf   r   r   r
   r   rH   rH   rN   r     r   z!JxOp._apply_operator_JzKetCoupledc                 K   r   r   r   r   rH   rH   rN   r     rn   zJxOp._represent_default_basisc                 K   r   r   )r   rf   r   r   r
   re   ry   rz   r   r   rH   rH   rN   r     r   zJxOp._represent_JzOpc                 O   s   t |d t|d  d S Nr   rF   )r   r   r   rH   rH   rN   _eval_rewrite_as_plusminus!  r   zJxOp._eval_rewrite_as_plusminusN)r   r   r   r   rj   ry   r   r   r   r   r   r   r   rH   rH   rH   rN   r         r   c                   @   r   )r   zThe Jy operator.yr1   c                 C   r   rX   r	   r&   r   rf   r   rH   rH   rN   r   ,  r   zJyOp._eval_commutator_JzOpc                 C   r   rX   )r	   r&   r=   rf   r   rH   rH   rN   _eval_commutator_JxOp/  r   zJyOp._eval_commutator_JxOpc                 K   D   t | jj|fi |}t| jj|fi |}|| tdt  S r   )r   rf   r   r   r
   r	   r   rH   rH   rN   r   2     zJyOp._apply_operator_JzKetc                 K   r   r   )r   rf   r   r   r
   r	   r   rH   rH   rN   r   7  r   z!JyOp._apply_operator_JzKetCoupledc                 K   r   r   r   r   rH   rH   rN   r   <  rn   zJyOp._represent_default_basisc                 K   r   r   )r   rf   r   r   r
   r	   r   rH   rH   rN   r   ?  r   zJyOp._represent_JzOpc                 O   s    t |d t|d  dt  S r   )r   r   r	   r   rH   rH   rN   r   D  s    zJyOp._eval_rewrite_as_plusminusN)r   r   r   r   rj   ry   r   r   r   r   r   r   r   rH   rH   rH   rN   r   %  r   r   c                   @   r   )r<   zThe Jz operator.zr2   c                 C   r   rX   r   r   rH   rH   rN   r   O  r   zJzOp._eval_commutator_JxOpc                 C   r   rX   r   r   rH   rH   rN   r   R  r   zJzOp._eval_commutator_JyOpc                 C   s   t t| j S rX   )r&   r   rf   r   rH   rH   rN   _eval_commutator_JplusOpU     zJzOp._eval_commutator_JplusOpc                 C   s   t  t| j S rX   )r&   r   rf   r   rH   rH   rN   r   X  rn   zJzOp._eval_commutator_JminusOpc                 C   s(   t | }|t||9 }|t||9 }|S rX   r&   r%   r   rH   rH   rN   rx   [  s   zJzOp.matrix_elementc                 K   r   r   r   r   rH   rH   rN   r   a  rn   zJzOp._represent_default_basisc                 K   r   rX   r   r   rH   rH   rN   r   d  r   zJzOp._represent_JzOpN)r   r   r   r   rj   ry   r   r   r   r   rx   r   r   rH   rH   rH   rN   r<   H  s    r<   c                   @   s   e Zd ZdZdZdd Zdd Zdd Zd	d
 Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'S )(r=   zThe J^2 operator.2c                 C      t jS rX   r   r   r   rH   rH   rN   r   m     zJ2Op._eval_commutator_JxOpc                 C   r   rX   r   r   rH   rH   rN   r   p  r   zJ2Op._eval_commutator_JyOpc                 C   r   rX   r   r   rH   rH   rN   r   s  r   zJ2Op._eval_commutator_JzOpc                 C   r   rX   r   r   rH   rH   rN   r   v  r   zJ2Op._eval_commutator_JplusOpc                 C   r   rX   r   r   rH   rH   rN   r   y  r   zJ2Op._eval_commutator_JminusOpc                 K      |j }td | |d  | S NrF   rG   rM   r&   re   r   rz   rM   rH   rH   rN   r   |     zJ2Op._apply_operator_JxKetc                 K   r   r   r   r   rH   rH   rN   r     r   z!J2Op._apply_operator_JxKetCoupledc                 K   r   r   r   r   rH   rH   rN   r     r   zJ2Op._apply_operator_JyKetc                 K   r   r   r   r   rH   rH   rN   r     r   z!J2Op._apply_operator_JyKetCoupledc                 K   r   r   r   r   rH   rH   rN   r     r   zJ2Op._apply_operator_JzKetc                 K   r   r   r   r   rH   rH   rN   r     r   z!J2Op._apply_operator_JzKetCoupledc                 C   s4   t d | |d  }|t||9 }|t||9 }|S r   r   r   rH   rH   rN   rx     s   zJ2Op.matrix_elementc                 K   r   r   r   r   rH   rH   rN   r     rn   zJ2Op._represent_default_basisc                 K   r   rX   r   r   rH   rH   rN   r     r   zJ2Op._represent_JzOpc                 G   s   t t| j}t d}|| S )Nr   )r   ro   rf   rq   rH   rH   rN   rt     s   zJ2Op._print_contents_prettyc                 G   s   dt | j S )Nz%s^2)ro   rf   rk   rH   rH   rN   ru     r   zJ2Op._print_contents_latexc                 O   s0   t |d d t|d d  t|d d  S r   )r   r   r<   r   rH   rH   rN   r     s   0zJ2Op._eval_rewrite_as_xyzc                 O   s:   |d }t |d tjt|t| t|t|    S r   )r<   r   rw   r   r   )re   rc   r   rr   rH   rH   rN   r     s   
$zJ2Op._eval_rewrite_as_plusminusN)r   r   r   r   rj   r   r   r   r   r   r   r   r   r   r   r   rx   r   r   rt   ru   r   r   rH   rH   rH   rN   r=   h  s*    r=   c                   @   s   e Zd Z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d Zedd Zedd Zdd Zdd Zdd Zdd Zd d!d"d#Zd$d% Zd&d' Zd(d) Zd d!d*d+Zd,d- Zd.d/ Zd0d1 Zd2S )3r4   am  Wigner D operator in terms of Euler angles.

    Defines the rotation operator in terms of the Euler angles defined by
    the z-y-z convention for a passive transformation. That is the coordinate
    axes are rotated first about the z-axis, giving the new x'-y'-z' axes. Then
    this new coordinate system is rotated about the new y'-axis, giving new
    x''-y''-z'' axes. Then this new coordinate system is rotated about the
    z''-axis. Conventions follow those laid out in [1]_.

    Parameters
    ==========

    alpha : Number, Symbol
        First Euler Angle
    beta : Number, Symbol
        Second Euler angle
    gamma : Number, Symbol
        Third Euler angle

    Examples
    ========

    A simple example rotation operator:

        >>> from sympy import pi
        >>> from sympy.physics.quantum.spin import Rotation
        >>> Rotation(pi, 0, pi/2)
        R(pi,0,pi/2)

    With symbolic Euler angles and calculating the inverse rotation operator:

        >>> from sympy import symbols
        >>> a, b, c = symbols('a b c')
        >>> Rotation(a, b, c)
        R(a,b,c)
        >>> Rotation(a, b, c).inverse()
        R(-c,-b,-a)

    See Also
    ========

    WignerD: Symbolic Wigner-D function
    D: Wigner-D function
    d: Wigner small-d function

    References
    ==========

    .. [1] Varshalovich, D A, Quantum Theory of Angular Momentum. 1988.
    c                 C   s&   t |}t|dkrtd| |S )N   z 3 Euler angles required, got: %r)r   
_eval_argsr   rR   )r\   rc   rH   rH   rN   r     s   
zRotation._eval_argsc                 C   rW   rX   rY   r[   rH   rH   rN   r^     r_   zRotation._eval_hilbert_spacec                 C   r`   ra   r]   rd   rH   rH   rN   alpha  rg   zRotation.alphac                 C   r`   r   r   rd   rH   rH   rN   beta  rg   zRotation.betac                 C   r`   r   r   rd   rH   rH   rN   gamma  rg   zRotation.gammac                 G      dS )NRrH   rk   rH   rH   rN   _print_operator_name     zRotation._print_operator_namec                 G   s   |j rtdS tdS )Nu   ℛ zR )_use_unicoder   rk   rH   rH   rN   _print_operator_name_pretty   s   z$Rotation._print_operator_name_prettyc                 G   r   )Nz\mathcal{R}rH   rk   rH   rH   rN   _print_operator_name_latex  r   z#Rotation._print_operator_name_latexc                 C   s   t | j | j | j S rX   )r4   r   r   r   rd   rH   rH   rN   _eval_inverse	     zRotation._eval_inversec                 C   s   t ||||||S )ag  Wigner D-function.

        Returns an instance of the WignerD class corresponding to the Wigner-D
        function specified by the parameters.

        Parameters
        ===========

        j : Number
            Total angular momentum
        m : Number
            Eigenvalue of angular momentum along axis after rotation
        mp : Number
            Eigenvalue of angular momentum along rotated axis
        alpha : Number, Symbol
            First Euler angle of rotation
        beta : Number, Symbol
            Second Euler angle of rotation
        gamma : Number, Symbol
            Third Euler angle of rotation

        Examples
        ========

        Return the Wigner-D matrix element for a defined rotation, both
        numerical and symbolic:

            >>> from sympy.physics.quantum.spin import Rotation
            >>> from sympy import pi, symbols
            >>> alpha, beta, gamma = symbols('alpha beta gamma')
            >>> Rotation.D(1, 1, 0,pi, pi/2,-pi)
            WignerD(1, 1, 0, pi, pi/2, -pi)

        See Also
        ========

        WignerD: Symbolic Wigner-D function

        r5   )r\   rM   r   r   r   r   r   rH   rH   rN   D  s   )z
Rotation.Dc                 C   s   t |||d|dS )a  Wigner small-d function.

        Returns an instance of the WignerD class corresponding to the Wigner-D
        function specified by the parameters with the alpha and gamma angles
        given as 0.

        Parameters
        ===========

        j : Number
            Total angular momentum
        m : Number
            Eigenvalue of angular momentum along axis after rotation
        mp : Number
            Eigenvalue of angular momentum along rotated axis
        beta : Number, Symbol
            Second Euler angle of rotation

        Examples
        ========

        Return the Wigner-D matrix element for a defined rotation, both
        numerical and symbolic:

            >>> from sympy.physics.quantum.spin import Rotation
            >>> from sympy import pi, symbols
            >>> beta = symbols('beta')
            >>> Rotation.d(1, 1, 0, pi/2)
            WignerD(1, 1, 0, 0, pi/2, 0)

        See Also
        ========

        WignerD: Symbolic Wigner-D function

        r   r  )r\   rM   r   r   r   rH   rH   rN   d7  s   &z
Rotation.dc                 C   s.   | j |||| j| j| j}|t||9 }|S rX   )r   r  r   r   r   r%   r   rH   rH   rN   rx   _  s
   zRotation.matrix_elementc              	   K   s   t |dtj}t |d}t|\}}t||}t|D ]&}t|D ]}	| ||| |||	 }
|r>|
 |||	f< q%|
|||	f< q%q|S )NrM   doit)	r   rv   r   rw   r-   r   rS   rx   r	  )re   ry   rz   rM   evaluaterU   r{   r|   r}   r~   r   rH   rH   rN   r   f  s   
zRotation._represent_basec                 K   r   r   r   r   rH   rH   rN   r   u  rn   z!Rotation._represent_default_basisc                 K   r   rX   r   r   rH   rH   rN   r   x  r   zRotation._represent_JzOpT)dummyc             	   K   s   | j }| j}| j}|j}|j}	|jr=g }
t|}|d }|D ]}t||	||||}|	 }|

||||  qt|
 S |rDtd}ntd}tt||	||||||| || |fS NrG   r   )r   r   r   rM   r   	is_numberr-   r4   r  r	  r   r   r   r   r   )re   r   r   r  rz   rr   rs   grM   r   r   rU   szr   rr   rH   rH   rN   _apply_operator_uncoupled{  s$   
,z"Rotation._apply_operator_uncoupledc                 K      | j t|fi |S rX   )r  r6   r   rH   rH   rN   r     r   zRotation._apply_operator_JxKetc                 K   r  rX   )r  r8   r   rH   rH   rN   r     r   zRotation._apply_operator_JyKetc                 K   r  rX   )r  r:   r   rH   rH   rN   r     r   zRotation._apply_operator_JzKetc             	   K   s   | j }| j}| j}|j}|j}	|j}
|j}|jrEg }t|}|d }|D ]}t	
||	||||}| }||||||
|  q$t| S |rLtd}ntd}tt	
||	||||||||
| || |fS r  )r   r   r   rM   r   r   r   r  r-   r4   r  r	  r   r   r   r   r   )re   r   r   r  rz   rr   rs   r  rM   r   r   r   r   rU   r  r   r  r   rH   rH   rN   _apply_operator_coupled  s0   

z Rotation._apply_operator_coupledc                 K   r  rX   )r  r>   r   rH   rH   rN   r     r   z%Rotation._apply_operator_JxKetCoupledc                 K   r  rX   )r  r@   r   rH   rH   rN   r     r   z%Rotation._apply_operator_JyKetCoupledc                 K   r  rX   )r  rB   r   rH   rH   rN   r     r   z%Rotation._apply_operator_JzKetCoupledN)r   r   r   r   r   r   r^   r   r   r   r   r   r  r  r  r  r  rx   r   r   r   r  r   r   r   r  r   r   r   rH   rH   rH   rN   r4     s@    3





*
'r4   c                   @   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dd Zdd Zdd Zdd ZdS )r5   a  Wigner-D function

    The Wigner D-function gives the matrix elements of the rotation
    operator in the jm-representation. For the Euler angles `\alpha`,
    `\beta`, `\gamma`, the D-function is defined such that:

    .. math ::
        <j,m| \mathcal{R}(\alpha, \beta, \gamma ) |j',m'> = \delta_{jj'} D(j, m, m', \alpha, \beta, \gamma)

    Where the rotation operator is as defined by the Rotation class [1]_.

    The Wigner D-function defined in this way gives:

    .. math ::
        D(j, m, m', \alpha, \beta, \gamma) = e^{-i m \alpha} d(j, m, m', \beta) e^{-i m' \gamma}

    Where d is the Wigner small-d function, which is given by Rotation.d.

    The Wigner small-d function gives the component of the Wigner
    D-function that is determined by the second Euler angle. That is the
    Wigner D-function is:

    .. math ::
        D(j, m, m', \alpha, \beta, \gamma) = e^{-i m \alpha} d(j, m, m', \beta) e^{-i m' \gamma}

    Where d is the small-d function. The Wigner D-function is given by
    Rotation.D.

    Note that to evaluate the D-function, the j, m and mp parameters must
    be integer or half integer numbers.

    Parameters
    ==========

    j : Number
        Total angular momentum
    m : Number
        Eigenvalue of angular momentum along axis after rotation
    mp : Number
        Eigenvalue of angular momentum along rotated axis
    alpha : Number, Symbol
        First Euler angle of rotation
    beta : Number, Symbol
        Second Euler angle of rotation
    gamma : Number, Symbol
        Third Euler angle of rotation

    Examples
    ========

    Evaluate the Wigner-D matrix elements of a simple rotation:

        >>> from sympy.physics.quantum.spin import Rotation
        >>> from sympy import pi
        >>> rot = Rotation.D(1, 1, 0, pi, pi/2, 0)
        >>> rot
        WignerD(1, 1, 0, pi, pi/2, 0)
        >>> rot.doit()
        sqrt(2)/2

    Evaluate the Wigner-d matrix elements of a simple rotation

        >>> rot = Rotation.d(1, 1, 0, pi/2)
        >>> rot
        WignerD(1, 1, 0, 0, pi/2, 0)
        >>> rot.doit()
        -sqrt(2)/2

    See Also
    ========

    Rotation: Rotation operator

    References
    ==========

    .. [1] Varshalovich, D A, Quantum Theory of Angular Momentum. 1988.
    Tc                 O   sX   t |dkstd| t|}|dd}|r#tj| g|R   S tj| g|R  S )N   z6 parameters expected, got %sr
  F)r   rR   r   rv   r   __new___eval_wignerd)r\   rc   hintsr
  rH   rH   rN   r    s   zWignerD.__new__c                 C   r`   ra   rb   rd   rH   rH   rN   rM     rg   z	WignerD.jc                 C   r`   r   rb   rd   rH   rH   rN   r     rg   z	WignerD.mc                 C   r`   r   rb   rd   rH   rH   rN   r     rg   z
WignerD.mpc                 C   r`   Nr   rb   rd   rH   rH   rN   r   #  rg   zWignerD.alphac                 C   r`   )N   rb   rd   rH   rH   rN   r   '  rg   zWignerD.betac                 C   r`   )N   rb   rd   rH   rH   rN   r   +  rg   zWignerD.gammac              	   G   s   | j dkr"| jdkr"d|| j|| j|| j|| jf S d|| j|| j|| j|| j || j|| jf S )Nr   zd^{%s}_{%s,%s}\left(%s\right)z#D^{%s}_{%s,%s}\left(%s,%s,%s\right))r   r   _printrM   r   r   r   rk   rH   rH   rN   _latex/  s"   

zWignerD._latexc                 G   s  | | j}| | j}t|d }t|| | j }t| | }t|d }t|d }|| krHt|d||    }|| kr[t|d||    }| j	dkrt| j
dkrt| | j}tdd|  }n2| | j	}t|d }t|| | j }t|d }t|| | j
 }tdd|  }t|  }t|| }t|| }t|| }|S )N, r   r  r  )r  rM   r   r   rightr   maxwidthleftr   r   r   r   parensabovebelow)re   rl   rc   topbotpadr   rH   rH   rN   _pretty<  s2   zWignerD._prettyc                 K   s   d|d< t | ji |S )NTr
  )r5   rc   )re   r  rH   rH   rN   r	  \  s   zWignerD.doitc              
   C   s  | j }| j}| j}| j}| j}| j}|dkr#|dkr#|dkr#t||S |js,td| d}|t	d krt
d| d D ].}||| ksP||| ksP||| k rQq<|tj| t|| | t|| || |  7 }q<|tj||  d|  tt|| t||  t|| t||    9 }nLt|\}	}
|
D ]-}|t|||t	d  t| | tt| |    t||| t	d   7 }q|td| | |   dd|   }t|}|tt | | tt | |  9 }|S )Nr   z1j parameter must be numerical to evaluate, got %srF   rG   )rM   r   r   r   r   r   r%   r  rR   r   rS   r   NegativeOner   r   r   r-   r4   r  r	  r   r	   r   r   r   )re   rM   r   r   r   r   r   r  krU   r{   mpprH   rH   rN   r  `  s@   
$4"
	8$(zWignerD._eval_wignerdN)r   r   r   r   is_commutativer  r   rM   r   r   r   r   r   r  r)  r	  r  rH   rH   rH   rN   r5     s(    O	





 r5   Jc                   @   s   e Zd ZdZdZ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d Zdd Zdd Zdd Zdd Zdd ZdS )	SpinStatez'Base class for angular momentum states.r  c                 C   s   t |}t |}|jr%d| td| krtd| |dk r%td| |jr8d| td| kr8td| |jr^|jr^t||krLtd||f t|| || kr^td||f t| ||S )NrF   z*j must be integer or half-integer, got: %sr   zj must be >= 0, got: %sz*m must be integer or half-integer, got: %sz7Allowed values for m are -j <= m <= j, got j, m: %s, %sz>Both j and m must be integer or half-integer, got j, m: %s, %s)r   r  rT   rR   absr$   r  )r\   rM   r   rH   rH   rN   r    s(   zSpinState.__new__c                 C   r`   ra   r   rd   rH   rH   rN   rM     rg   zSpinState.jc                 C   r`   r   r   rd   rH   rH   rN   r     rg   zSpinState.mc                 C   s   t d|d  d S )NrF   r   rG   r'   r[   rH   rH   rN   r^     s   zSpinState._eval_hilbert_spacec              	   K   s   | j }| j}t|dd}t|dd}t|dd}t|\}}t|d}	t|D ]*\}
}|jrGt	| j || j|||
 |	|
df< q-t	| j || j||||	|
df< q-|	S )Nr   r   r   r   rG   )rM   r   r   rv   r-   r   	enumerater  r4   r  r	  )re   rz   rM   r   r   r   r   rU   r{   r|   r}   mvalrH   rH   rN   r     s$   


zSpinState._represent_basec                 O   2   t | tr| jttfi |S | jttfi |S rX   )r   r"   _rewrite_basisr0   r7   r6   re   rc   rz   rH   rH   rN   _eval_rewrite_as_Jx     
zSpinState._eval_rewrite_as_Jxc                 O   r5  rX   )r   r"   r6  r1   r9   r8   r7  rH   rH   rN   _eval_rewrite_as_Jy  r9  zSpinState._eval_rewrite_as_Jyc                 O   r5  rX   )r   r"   r6  r2   r;   r:   r7  rH   rH   rN   _eval_rewrite_as_Jz  r9  zSpinState._eval_rewrite_as_Jzc                    s  ddl m} | j| jdd   jret| tr2tkr#d nd d d d  d nd|| fd|i|t fddt	d d D  }t| trc|
d	d
u rct|S |S d}td}| |d| kr~|d7 }td| }	 t| trd|df}nd|f}t| tr|| j| |dd jdd }	n|| j| |dd jd jdd }	|	dkr| S |g R  }
tj|| jg|	R  }t||
 | fS )Nr   )	representrF   rG   r  ry   c                    s.   g | ]}|  | g R   qS rH   rH   rI   rc   evectrM   startvectrH   rN   rO     s   . z,SpinState._rewrite_basis.<locals>.<listcomp>coupledFmizmi%d)r   r   )ry   r   r  )r   r   r   )sympy.physics.quantum.representr<  rM   rc   r  r   CoupledSpinStaterT   r   rS   rv   rE   r   subsr#   r   r4   r  r   r   )re   ry   r>  rz   r<  r|   rK   rB  	test_argsanglesr   ltrH   r=  rN   r6    sb   

$


zSpinState._rewrite_basisc                 K   ^   t | j|j}| | jur|| d |j|j  9 }|S |t | j|jt | j|j 9 }|S rX   )r%   rM   
dual_classr   _represent_JxOpr   re   brar  r|   rH   rH   rN   _eval_innerproduct_JxBra     z"SpinState._eval_innerproduct_JxBrac                 K   rI  rX   )r%   rM   rJ  r   _represent_JyOpr   rL  rH   rH   rN   _eval_innerproduct_JyBra  rO  z"SpinState._eval_innerproduct_JyBrac                 K   rI  rX   )r%   rM   rJ  r   r   r   rL  rH   rH   rN   _eval_innerproduct_JzBra  rO  z"SpinState._eval_innerproduct_JzBrac                 K   s   ||    S rX   )r	  )re   rM  r  rH   rH   rN   r   &  s   zSpinState._eval_traceN)r   r   r   r   _label_separatorr  r   rM   r   r   r^   r   r8  r:  r;  r6  rN  rQ  rR  r   rH   rH   rH   rN   r0    s&    


,			r0  c                   @   H   e Zd ZdZedd Zedd Zdd Zdd	 Zd
d Z	dd Z
dS )r6   zEigenket of Jx.

    See JzKet for the usage of spin eigenstates.

    See Also
    ========

    JzKet: Usage of spin states

    c                 C      t S rX   r7   rd   rH   rH   rN   rJ  B     zJxKet.dual_classc                 C   rU  rX   r>   rd   rH   rH   rN   coupled_classF  rW  zJxKet.coupled_classc                 K   r   r   )rK  r   rH   rH   rN   r   J  rn   zJxKet._represent_default_basisc                 K      | j di |S NrH   r   r   rH   rH   rN   rK  M  rn   zJxKet._represent_JxOpc                 K      | j ddttdd i|S Nr   r   rF   rH   r   r   r   r   rH   rH   rN   rP  P     zJxKet._represent_JyOpc                 K      | j ddtd i|S Nr   rF   rH   r   r   r   rH   rH   rN   r   S  r  zJxKet._represent_JzOpNr   r   r   r   r   rJ  rY  r   rK  rP  r   rH   rH   rH   rN   r6   6      

r6   c                   @   (   e Zd ZdZedd Zedd ZdS )r7   zEigenbra of Jx.

    See JzKet for the usage of spin eigenstates.

    See Also
    ========

    JzKet: Usage of spin states

    c                 C   rU  rX   r6   rd   rH   rH   rN   rJ  c  rW  zJxBra.dual_classc                 C   rU  rX   r?   rd   rH   rH   rN   rY  g  rW  zJxBra.coupled_classNr   r   r   r   r   rJ  rY  rH   rH   rH   rN   r7   W      
r7   c                   @   rT  )r8   zEigenket of Jy.

    See JzKet for the usage of spin eigenstates.

    See Also
    ========

    JzKet: Usage of spin states

    c                 C   rU  rX   r9   rd   rH   rH   rN   rJ  x  rW  zJyKet.dual_classc                 C   rU  rX   r@   rd   rH   rH   rN   rY  |  rW  zJyKet.coupled_classc                 K   r   r   )rP  r   rH   rH   rN   r     rn   zJyKet._represent_default_basisc                 K   r`  Nr   rF   rH   rb  r   rH   rH   rN   rK    r  zJyKet._represent_JxOpc                 K   rZ  r[  r   r   rH   rH   rN   rP    rn   zJyKet._represent_JyOpc                 K   ,   | j dttdd t d td d|S Nr   rF   )r   r   r   rH   r^  r   rH   rH   rN   r        ,zJyKet._represent_JzOpNrc  rH   rH   rH   rN   r8   l  rd  r8   c                   @   re  )r9   zEigenbra of Jy.

    See JzKet for the usage of spin eigenstates.

    See Also
    ========

    JzKet: Usage of spin states

    c                 C   rU  rX   r8   rd   rH   rH   rN   rJ    rW  zJyBra.dual_classc                 C   rU  rX   rA   rd   rH   rH   rN   rY    rW  zJyBra.coupled_classNrh  rH   rH   rH   rN   r9     ri  r9   c                   @   rT  )r:   aN  Eigenket of Jz.

    Spin state which is an eigenstate of the Jz operator. Uncoupled states,
    that is states representing the interaction of multiple separate spin
    states, are defined as a tensor product of states.

    Parameters
    ==========

    j : Number, Symbol
        Total spin angular momentum
    m : Number, Symbol
        Eigenvalue of the Jz spin operator

    Examples
    ========

    *Normal States:*

    Defining simple spin states, both numerical and symbolic:

        >>> from sympy.physics.quantum.spin import JzKet, JxKet
        >>> from sympy import symbols
        >>> JzKet(1, 0)
        |1,0>
        >>> j, m = symbols('j m')
        >>> JzKet(j, m)
        |j,m>

    Rewriting the JzKet in terms of eigenkets of the Jx operator:
    Note: that the resulting eigenstates are JxKet's

        >>> JzKet(1,1).rewrite("Jx")
        |1,-1>/2 - sqrt(2)*|1,0>/2 + |1,1>/2

    Get the vector representation of a state in terms of the basis elements
    of the Jx operator:

        >>> from sympy.physics.quantum.represent import represent
        >>> from sympy.physics.quantum.spin import Jx, Jz
        >>> represent(JzKet(1,-1), basis=Jx)
        Matrix([
        [      1/2],
        [sqrt(2)/2],
        [      1/2]])

    Apply innerproducts between states:

        >>> from sympy.physics.quantum.innerproduct import InnerProduct
        >>> from sympy.physics.quantum.spin import JxBra
        >>> i = InnerProduct(JxBra(1,1), JzKet(1,1))
        >>> i
        <1,1|1,1>
        >>> i.doit()
        1/2

    *Uncoupled States:*

    Define an uncoupled state as a TensorProduct between two Jz eigenkets:

        >>> from sympy.physics.quantum.tensorproduct import TensorProduct
        >>> j1,m1,j2,m2 = symbols('j1 m1 j2 m2')
        >>> TensorProduct(JzKet(1,0), JzKet(1,1))
        |1,0>x|1,1>
        >>> TensorProduct(JzKet(j1,m1), JzKet(j2,m2))
        |j1,m1>x|j2,m2>

    A TensorProduct can be rewritten, in which case the eigenstates that make
    up the tensor product is rewritten to the new basis:

        >>> TensorProduct(JzKet(1,1),JxKet(1,1)).rewrite('Jz')
        |1,1>x|1,-1>/2 + sqrt(2)*|1,1>x|1,0>/2 + |1,1>x|1,1>/2

    The represent method for TensorProduct's gives the vector representation of
    the state. Note that the state in the product basis is the equivalent of the
    tensor product of the vector representation of the component eigenstates:

        >>> represent(TensorProduct(JzKet(1,0),JzKet(1,1)))
        Matrix([
        [0],
        [0],
        [0],
        [1],
        [0],
        [0],
        [0],
        [0],
        [0]])
        >>> represent(TensorProduct(JzKet(1,1),JxKet(1,1)), basis=Jz)
        Matrix([
        [      1/2],
        [sqrt(2)/2],
        [      1/2],
        [        0],
        [        0],
        [        0],
        [        0],
        [        0],
        [        0]])

    See Also
    ========

    JzKetCoupled: Coupled eigenstates
    sympy.physics.quantum.tensorproduct.TensorProduct: Used to specify uncoupled states
    uncouple: Uncouples states given coupling parameters
    couple: Couples uncoupled states

    c                 C   rU  rX   r;   rd   rH   rH   rN   rJ    rW  zJzKet.dual_classc                 C   rU  rX   rB   rd   rH   rH   rN   rY    rW  zJzKet.coupled_classc                 K   r   r   r   r   rH   rH   rN   r     rn   zJzKet._represent_default_basisc                 K   r\  Nr   r   rF   rH   r^  r   rH   rH   rN   rK    r_  zJzKet._represent_JxOpc                 K   *   | j dttdd td td d|S rn  r^  r   rH   rH   rN   rP       *zJzKet._represent_JyOpc                 K   rZ  r[  r   r   rH   rH   rN   r   "  rn   zJzKet._represent_JzOpNrc  rH   rH   rH   rN   r:     s    n

r:   c                   @   re  )r;   zEigenbra of Jz.

    See the JzKet for the usage of spin eigenstates.

    See Also
    ========

    JzKet: Usage of spin states

    c                 C   rU  rX   r:   rd   rH   rH   rN   rJ  2  rW  zJzBra.dual_classc                 C   rU  rX   rC   rd   rH   rH   rN   rY  6  rW  zJzBra.coupled_classNrh  rH   rH   rH   rN   r;   &  ri  r;   c           	      C   s   dd t |D }g }g }| D ]/\}}}|| |||d  ||d  f t||d  ||d   }|||d d < q||fS )Nc                 S      g | ]}|d  gqS rG   rH   rJ   r   rH   rH   rN   rO   @      z"_build_coupled.<locals>.<listcomp>rG   r   )rS   r   sorted)		jcouplinglengthn_list
coupled_jn	coupled_nn1n2j_newn_sortrH   rH   rN   _build_coupled?  s   
r  c                   @   s   e Zd ZdZdd Zdd Zd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dd Zdd Zdd Zdd ZdS )rD  z/Base class for coupled angular momentum states.c              	      s  t || t|dkr5g }tdt D ]}|d|t fddt|D  f q|dt |f nt|dkr@|d }n
tdt|d  t ttt	fsZtd j
j t|ttt	fsjtd	|j
j td
d |D swtdt d t|kstdt d t|f tdd |D stdt|}t|}t	dd  D   t	dd |D  }tdd  D rtd  tdd |D rtdt fdd|D rtdtdd |D rtdt|t \}}t }t|D ]f\}\}	}
|t|	d  }|t|
d  }|| }t|jr[t|jr[t|jr[|| |k r>td|d |||f t|| |krStd|d |||f t|| r[	 ||t|	|
 d < qt|dkrz|d d |krztdt| || |S )Nr   rF   rG   c                       g | ]} | qS rH   rH   rI   r   rH   rN   rO   V  rP   z,CoupledSpinState.__new__.<locals>.<listcomp>z5CoupledSpinState only takes 3 or 4 arguments, got: %sr   z'jn must be Tuple, list or tuple, got %sz.jcoupling must be Tuple, list or tuple, got %sc                 s   s     | ]}t |tttfV  qd S rX   )r   listtupler   rJ   termrH   rH   rN   	<genexpr>d      z+CoupledSpinState.__new__.<locals>.<genexpr>z6All elements of jcoupling must be list, tuple or Tuplez(jcoupling must have length of %d, got %dc                 s       | ]	}t |d kV  qdS )r   Nr   rJ   r   rH   rH   rN   r  j      z,All elements of jcoupling must have length 3c                 S      g | ]}t |qS rH   r   rJ   jirH   rH   rN   rO   o  rP   c                 S   s*   g | ]\}}}t t|t|t|qS rH   )r   r   )rJ   r  r  r  rH   rH   rN   rO   p  s    c                 s   s*    | ]}|j rd | td | kV  qdS rF   Nr  rT   r  rH   rH   rN   r  s  s   ( z;All elements of jn must be integer or half-integer, got: %sc                 s   s.    | ]\}}}|t |kp|t |kV  qd S rX   )rT   rJ   r  r  _rH   rH   rN   r  u  s   , z%Indices in jcoupling must be integersc                 3   s>    | ]\}}}|d k p|d k p|t  kp|t  kV  qdS )rG   Nr  r  r  rH   rN   r  w  s   < z?Indices must be between 1 and the number of coupled spin spacesc                 s   s0    | ]\}}}|j rd | td | kV  qdS r  r  )rJ   r  r  rH   rH   rN   r  y  s   . zGAll coupled j values in coupling scheme must be integer or half-integerzQAll couplings must have j1+j2 >= j3, in coupling number %d got j1,j2,j3: %d,%d,%dzSAll couplings must have |j1+j2| <= j3, in coupling number %d got j1,j2,j3: %d,%d,%dr*  z>Last j value coupled together must be the final j of the state)r0  r   rS   r   r   	TypeErrorr   r  r  r   r   r   allrR   r   anyr  r3  minr  r1  r   r$   r  )r\   rM   r   r   r~  r   r  r  jvalsr  r  j1j2j3rH   r  rN   r  N  s|   
(
$ zCoupledSpinState.__new__c           	   
   G   s   | | j| | jg}t| jddD ]\}}|d|| |f  qt| jd d | jd d D ]\}\}}|dd	dd t
|| D | |f  q3d	|S )	NrG   r?  zj%d=%sr*  zj(%s)=%sr  c                 s       | ]}t |V  qd S rX   ro   rI   rH   rH   rN   r        z0CoupledSpinState._print_label.<locals>.<genexpr>)r  rM   r   r3  r   r   zipr  r  joinr}  )	re   rl   rc   r]   rK   r  r   r  r  rH   rH   rN   _print_label  s   

*"

zCoupledSpinState._print_labelc                 G   s   | j | jg}t| jddD ]!\}}d| }t|}t|d }t||| }|| qt	| j
d d | jd d D ]+\}\}	}
ddd t|	|
 D }td	| d }t||| }|| q>| j|| j|g|R  S )
NrG   r  j%d=r*  r  c                 s   s     | ]}t d | d V  qdS )r  r*  Nr   rI   rH   rH   rN   r    r  z7CoupledSpinState._print_label_pretty.<locals>.<genexpr>rM   )rM   r   r3  r   r   r   r  r  r   r  r  r  r  r}  _print_sequence_prettyrS  )re   rl   rc   r]   rK   r  symbitemr   r  r  r   rH   rH   rN   _print_label_pretty  s"   *z$CoupledSpinState._print_label_prettyc           
      G   s   |j | jg|R  |j | jg|R  g}t| jddD ]\}}|d||j |g|R  f  qt| jd d | jd d D ]$\}\}}d	dd t
|| D }	|d|	|j |g|R  f  q?| j	|S )	NrG   r  z	j_{%d}=%sr*  r  c                 s   r  rX   r  rI   rH   rH   rN   r    r  z6CoupledSpinState._print_label_latex.<locals>.<genexpr>z	j_{%s}=%s)r  rM   r   r3  r   r   r  r  r  r  r}  rS  )
re   rl   rc   r]   rK   r  r   r  r  r   rH   rH   rN   _print_label_latex  s   "*"z#CoupledSpinState._print_label_latexc                 C   r`   r   r   rd   rH   rH   rN   r     rg   zCoupledSpinState.jnc                 C   r`   r  r   rd   rH   rH   rN   r     rg   zCoupledSpinState.couplingc                 C      t | jd t| jd d S )Nr   rF   rG   r  r]   r   rd   rH   rH   rN   r       zCoupledSpinState.coupled_jnc                 C   r  )Nr   rF   r   r  rd   rH   rH   rN   r    r  zCoupledSpinState.coupled_nc                 C   sH   t |d  }|jrtdd ttd| d ddD  S td| d S )NrF   c                 S   r  rH   r2  r  rH   rH   rN   rO     rP   z8CoupledSpinState._eval_hilbert_space.<locals>.<listcomp>rG   r   )r   r  r(   rS   rT   r'   )r\   r]   rM   rH   rH   rN   r^     s   &z$CoupledSpinState._eval_hilbert_spacec                 K   s   |   }| jjstd| jjjstdt| jjd}| jt| jkr*| jd }nd| j d dd| j   d }|| j| jj	di ||||d| j  d df< |S )Nz1State must not have symbolic j value to representz2State must not have symbolic j values to representrG   rF   r  r   rH   )
uncoupled_classrM   r  rR   hilbert_space	dimensionr   rT   r   r   )re   rz   r>  r|   r?  rH   rH   rN   _represent_coupled_base  s*   
  z(CoupledSpinState._represent_coupled_basec                 O   r5  rX   )r   r"   r6  r0   r?   r>   r7  rH   rH   rN   r8    r9  z$CoupledSpinState._eval_rewrite_as_Jxc                 O   r5  rX   )r   r"   r6  r1   rA   r@   r7  rH   rH   rN   r:    r9  z$CoupledSpinState._eval_rewrite_as_Jyc                 O   r5  rX   )r   r"   r6  r2   rC   rB   r7  rH   rH   rN   r;    r9  z$CoupledSpinState._eval_rewrite_as_JzN)r   r   r   r   r  r  r  r  r   r   r   r  r  r   r^   r  r8  r:  r;  rH   rH   rH   rN   rD  K  s(    B




rD  c                   @   rT  )r>   zCoupled eigenket of Jx.

    See JzKetCoupled for the usage of coupled spin eigenstates.

    See Also
    ========

    JzKetCoupled: Usage of coupled spin states

    c                 C   rU  rX   rg  rd   rH   rH   rN   rJ    rW  zJxKetCoupled.dual_classc                 C   rU  rX   rf  rd   rH   rH   rN   r    rW  zJxKetCoupled.uncoupled_classc                 K   r   r   r   r   rH   rH   rN   r     rn   z%JxKetCoupled._represent_default_basisc                 K   rZ  r[  r  r   rH   rH   rN   rK    rn   zJxKetCoupled._represent_JxOpc                 K   r\  r]  r  r   r   r   rH   rH   rN   rP    r_  zJxKetCoupled._represent_JyOpc                 K   r`  ra  r  r   r   rH   rH   rN   r     r  zJxKetCoupled._represent_JzOpNr   r   r   r   r   rJ  r  r   rK  rP  r   rH   rH   rH   rN   r>     rd  r>   c                   @   re  )r?   zCoupled eigenbra of Jx.

    See JzKetCoupled for the usage of coupled spin eigenstates.

    See Also
    ========

    JzKetCoupled: Usage of coupled spin states

    c                 C   rU  rX   rX  rd   rH   rH   rN   rJ  $  rW  zJxBraCoupled.dual_classc                 C   rU  rX   rV  rd   rH   rH   rN   r  (  rW  zJxBraCoupled.uncoupled_classNr   r   r   r   r   rJ  r  rH   rH   rH   rN   r?     ri  r?   c                   @   rT  )r@   zCoupled eigenket of Jy.

    See JzKetCoupled for the usage of coupled spin eigenstates.

    See Also
    ========

    JzKetCoupled: Usage of coupled spin states

    c                 C   rU  rX   rq  rd   rH   rH   rN   rJ  9  rW  zJyKetCoupled.dual_classc                 C   rU  rX   rp  rd   rH   rH   rN   r  =  rW  zJyKetCoupled.uncoupled_classc                 K   r   r   r   r   rH   rH   rN   r   A  rn   z%JyKetCoupled._represent_default_basisc                 K   r`  rl  r  r   rH   rH   rN   rK  D  r  zJyKetCoupled._represent_JxOpc                 K   rZ  r[  r  r   rH   rH   rN   rP  G  rn   zJyKetCoupled._represent_JyOpc                 K   rm  rn  r  r   rH   rH   rN   r   J  ro  zJyKetCoupled._represent_JzOpNr  rH   rH   rH   rN   r@   -  rd  r@   c                   @   re  )rA   zCoupled eigenbra of Jy.

    See JzKetCoupled for the usage of coupled spin eigenstates.

    See Also
    ========

    JzKetCoupled: Usage of coupled spin states

    c                 C   rU  rX   rk  rd   rH   rH   rN   rJ  Z  rW  zJyBraCoupled.dual_classc                 C   rU  rX   rj  rd   rH   rH   rN   r  ^  rW  zJyBraCoupled.uncoupled_classNr  rH   rH   rH   rN   rA   N  ri  rA   c                   @   rT  )rB   a  Coupled eigenket of Jz

    Spin state that is an eigenket of Jz which represents the coupling of
    separate spin spaces.

    The arguments for creating instances of JzKetCoupled are ``j``, ``m``,
    ``jn`` and an optional ``jcoupling`` argument. The ``j`` and ``m`` options
    are the total angular momentum quantum numbers, as used for normal states
    (e.g. JzKet).

    The other required parameter in ``jn``, which is a tuple defining the `j_n`
    angular momentum quantum numbers of the product spaces. So for example, if
    a state represented the coupling of the product basis state
    `\left|j_1,m_1\right\rangle\times\left|j_2,m_2\right\rangle`, the ``jn``
    for this state would be ``(j1,j2)``.

    The final option is ``jcoupling``, which is used to define how the spaces
    specified by ``jn`` are coupled, which includes both the order these spaces
    are coupled together and the quantum numbers that arise from these
    couplings. The ``jcoupling`` parameter itself is a list of lists, such that
    each of the sublists defines a single coupling between the spin spaces. If
    there are N coupled angular momentum spaces, that is ``jn`` has N elements,
    then there must be N-1 sublists. Each of these sublists making up the
    ``jcoupling`` parameter have length 3. The first two elements are the
    indices of the product spaces that are considered to be coupled together.
    For example, if we want to couple `j_1` and `j_4`, the indices would be 1
    and 4. If a state has already been coupled, it is referenced by the
    smallest index that is coupled, so if `j_2` and `j_4` has already been
    coupled to some `j_{24}`, then this value can be coupled by referencing it
    with index 2. The final element of the sublist is the quantum number of the
    coupled state. So putting everything together, into a valid sublist for
    ``jcoupling``, if `j_1` and `j_2` are coupled to an angular momentum space
    with quantum number `j_{12}` with the value ``j12``, the sublist would be
    ``(1,2,j12)``, N-1 of these sublists are used in the list for
    ``jcoupling``.

    Note the ``jcoupling`` parameter is optional, if it is not specified, the
    default coupling is taken. This default value is to coupled the spaces in
    order and take the quantum number of the coupling to be the maximum value.
    For example, if the spin spaces are `j_1`, `j_2`, `j_3`, `j_4`, then the
    default coupling couples `j_1` and `j_2` to `j_{12}=j_1+j_2`, then,
    `j_{12}` and `j_3` are coupled to `j_{123}=j_{12}+j_3`, and finally
    `j_{123}` and `j_4` to `j=j_{123}+j_4`. The jcoupling value that would
    correspond to this is:

        ``((1,2,j1+j2),(1,3,j1+j2+j3))``

    Parameters
    ==========

    args : tuple
        The arguments that must be passed are ``j``, ``m``, ``jn``, and
        ``jcoupling``. The ``j`` value is the total angular momentum. The ``m``
        value is the eigenvalue of the Jz spin operator. The ``jn`` list are
        the j values of argular momentum spaces coupled together. The
        ``jcoupling`` parameter is an optional parameter defining how the spaces
        are coupled together. See the above description for how these coupling
        parameters are defined.

    Examples
    ========

    Defining simple spin states, both numerical and symbolic:

        >>> from sympy.physics.quantum.spin import JzKetCoupled
        >>> from sympy import symbols
        >>> JzKetCoupled(1, 0, (1, 1))
        |1,0,j1=1,j2=1>
        >>> j, m, j1, j2 = symbols('j m j1 j2')
        >>> JzKetCoupled(j, m, (j1, j2))
        |j,m,j1=j1,j2=j2>

    Defining coupled spin states for more than 2 coupled spaces with various
    coupling parameters:

        >>> JzKetCoupled(2, 1, (1, 1, 1))
        |2,1,j1=1,j2=1,j3=1,j(1,2)=2>
        >>> JzKetCoupled(2, 1, (1, 1, 1), ((1,2,2),(1,3,2)) )
        |2,1,j1=1,j2=1,j3=1,j(1,2)=2>
        >>> JzKetCoupled(2, 1, (1, 1, 1), ((2,3,1),(1,2,2)) )
        |2,1,j1=1,j2=1,j3=1,j(2,3)=1>

    Rewriting the JzKetCoupled in terms of eigenkets of the Jx operator:
    Note: that the resulting eigenstates are JxKetCoupled

        >>> JzKetCoupled(1,1,(1,1)).rewrite("Jx")
        |1,-1,j1=1,j2=1>/2 - sqrt(2)*|1,0,j1=1,j2=1>/2 + |1,1,j1=1,j2=1>/2

    The rewrite method can be used to convert a coupled state to an uncoupled
    state. This is done by passing coupled=False to the rewrite function:

        >>> JzKetCoupled(1, 0, (1, 1)).rewrite('Jz', coupled=False)
        -sqrt(2)*|1,-1>x|1,1>/2 + sqrt(2)*|1,1>x|1,-1>/2

    Get the vector representation of a state in terms of the basis elements
    of the Jx operator:

        >>> from sympy.physics.quantum.represent import represent
        >>> from sympy.physics.quantum.spin import Jx
        >>> from sympy import S
        >>> represent(JzKetCoupled(1,-1,(S(1)/2,S(1)/2)), basis=Jx)
        Matrix([
        [        0],
        [      1/2],
        [sqrt(2)/2],
        [      1/2]])

    See Also
    ========

    JzKet: Normal spin eigenstates
    uncouple: Uncoupling of coupling spin states
    couple: Coupling of uncoupled spin states

    c                 C   rU  rX   rx  rd   rH   rH   rN   rJ    rW  zJzKetCoupled.dual_classc                 C   rU  rX   rw  rd   rH   rH   rN   r    rW  zJzKetCoupled.uncoupled_classc                 K   r   r   r   r   rH   rH   rN   r     rn   z%JzKetCoupled._represent_default_basisc                 K   r\  rt  r  r   rH   rH   rN   rK    r_  zJzKetCoupled._represent_JxOpc                 K   ru  rn  r  r   rH   rH   rN   rP    rv  zJzKetCoupled._represent_JyOpc                 K   rZ  r[  r  r   rH   rH   rN   r     rn   zJzKetCoupled._represent_JzOpNr  rH   rH   rH   rN   rB   c  s    t

rB   c                   @   re  )rC   zCoupled eigenbra of Jz.

    See the JzKetCoupled for the usage of coupled spin eigenstates.

    See Also
    ========

    JzKetCoupled: Usage of coupled spin states

    c                 C   rU  rX   rs  rd   rH   rH   rN   rJ    rW  zJzBraCoupled.dual_classc                 C   rU  rX   rr  rd   rH   rH   rN   r    rW  zJzBraCoupled.uncoupled_classNr  rH   rH   rH   rN   rC     ri  rC   Nc                    s`   |  t}|D ]& tdd  jD sqt fdd jD s$td|  t |} q| S )a	   Couple a tensor product of spin states

    This function can be used to couple an uncoupled tensor product of spin
    states. All of the eigenstates to be coupled must be of the same class. It
    will return a linear combination of eigenstates that are subclasses of
    CoupledSpinState determined by Clebsch-Gordan angular momentum coupling
    coefficients.

    Parameters
    ==========

    expr : Expr
        An expression involving TensorProducts of spin states to be coupled.
        Each state must be a subclass of SpinState and they all must be the
        same class.

    jcoupling_list : list or tuple
        Elements of this list are sub-lists of length 2 specifying the order of
        the coupling of the spin spaces. The length of this must be N-1, where N
        is the number of states in the tensor product to be coupled. The
        elements of this sublist are the same as the first two elements of each
        sublist in the ``jcoupling`` parameter defined for JzKetCoupled. If this
        parameter is not specified, the default value is taken, which couples
        the first and second product basis spaces, then couples this new coupled
        space to the third product space, etc

    Examples
    ========

    Couple a tensor product of numerical states for two spaces:

        >>> from sympy.physics.quantum.spin import JzKet, couple
        >>> from sympy.physics.quantum.tensorproduct import TensorProduct
        >>> couple(TensorProduct(JzKet(1,0), JzKet(1,1)))
        -sqrt(2)*|1,1,j1=1,j2=1>/2 + sqrt(2)*|2,1,j1=1,j2=1>/2


    Numerical coupling of three spaces using the default coupling method, i.e.
    first and second spaces couple, then this couples to the third space:

        >>> couple(TensorProduct(JzKet(1,1), JzKet(1,1), JzKet(1,0)))
        sqrt(6)*|2,2,j1=1,j2=1,j3=1,j(1,2)=2>/3 + sqrt(3)*|3,2,j1=1,j2=1,j3=1,j(1,2)=2>/3

    Perform this same coupling, but we define the coupling to first couple
    the first and third spaces:

        >>> couple(TensorProduct(JzKet(1,1), JzKet(1,1), JzKet(1,0)), ((1,3),(1,2)) )
        sqrt(2)*|2,2,j1=1,j2=1,j3=1,j(1,3)=1>/2 - sqrt(6)*|2,2,j1=1,j2=1,j3=1,j(1,3)=2>/6 + sqrt(3)*|3,2,j1=1,j2=1,j3=1,j(1,3)=2>/3

    Couple a tensor product of symbolic states:

        >>> from sympy import symbols
        >>> j1,m1,j2,m2 = symbols('j1 m1 j2 m2')
        >>> couple(TensorProduct(JzKet(j1,m1), JzKet(j2,m2)))
        Sum(CG(j1, m1, j2, m2, j, m1 + m2)*|j,m1 + m2,j1=j1,j2=j2>, (j, m1 + m2, j1 + j2))

    c                 s   s    | ]}t |tV  qd S rX   )r   r0  rJ   r   rH   rH   rN   r  C  s    zcouple.<locals>.<genexpr>c                 3   s"    | ]}|j  jd  j u V  qdS )r   N)r   rc   r  r   rH   rN   r  F  s     z!All states must be the same basis)atomsr)   r  rc   r  rE  _couple)exprjcoupling_listrr   rH   r  rN   rD     s   
:rD   c           !         s  | j }|d  }|d u r"g }tdt|D ]}|d|d f qt|t|d ks:tdt|d t|f tdd |D sGtdtdd |D rTtdtd	d |D rdgt| }|D ]!\}}||d  d
ksz||d  d
kr~tdd
|t	||d < qfdd |D  dd |D g }dd tt|D }	|D ]*}
|
\}}t
|	|d  }t
|	|d  }|||f t|| |	t||d < qtdd |D r fdd|D }g }t|d
 d D ]}t|}t|| d |}t|D ]}t|||}tdd t||D rqg }t
 }g }t||D ]X\\}}}|t|d  }|t|d  }|| | }||t|| d < tfdd|D  }tfdd|D  }|| }|||||||f |t|t||f q%tdd |D rqtdd |D rqtdd |D rqtdd |D  }||| |}|||  qqt| S g }g }g }t
 }|D ]|\}}|t|d  }|t|d  }t|| t|krtd}ndddd || D  } t| }||t|| d < tfdd|D  }tfdd|D  }|| }|||||||f |t|t||f ||||| f qtdd |D  }||| |}t|| g|R  S )Nr   rG   z(jcoupling_list must be length %d, got %dc                 s   r  r  r  rJ   r   rH   rH   rN   r  Z  r  z_couple.<locals>.<genexpr>z"Each coupling must define 2 spacesc                 s   s    | ]	\}}||kV  qd S rX   rH   rJ   r  r  rH   rH   rN   r  \  r  z'Spin spaces cannot couple to themselvesc                 s   s(    | ]\}}t |jot |jV  qd S rX   )r   r  r  rH   rH   rN   r  ^     & r*  z8Spaces coupling j_n's are referenced by smallest n valuec                 S      g | ]}|j qS rH   rL   r  rH   rH   rN   rO   f      z_couple.<locals>.<listcomp>c                 S   r  rH   )r   r  rH   rH   rN   rO   g  r  c                 S   ry  rz  rH   rI   rH   rH   rN   rO   l  r|  c                 s   s     | ]}|j jo|jjV  qd S rX   )rM   r  r   r  rH   rH   rN   r  w  r  c                    s0   g | ]}t  fd d|d |d  D  qS )c                    s$   g | ]} |d   |d    qS rz  rH   r{  r   mnrH   rN   rO   z     $ z&_couple.<locals>.<listcomp>.<listcomp>r   rG   r   r  r  rH   rN   rO   z  s
    c                 s       | ]	\}}||kV  qd S rX   rH   )rJ   r  r   rH   rH   rN   r    r  c                       g | ]} |d   qS rz  rH   r  r  rH   rN   rO         c                    r  rz  rH   r  r  rH   rN   rO     r  c                 s   s$    | ]}t |d  |d kV  qdS )r  r  Nr1  r  rH   rH   rN   r    s   " c                 s   s(    | ]}|d  |d  |d k V  qdS r   rF   r  NrH   r  rH   rH   rN   r    r  c                 s   s,    | ]}t |d  |d  |d kV  qdS r  r  r  rH   rH   rN   r    s   * c                 S      g | ]}t |  qS rH   r+   r	  r  rH   rH   rN   rO     r  rM    c                 S   s   g | ]}d | qS )z%srH   r{  rH   rH   rN   rO     rP   c                    r  rz  rH   r  r  rH   rN   rO     r  c                    r  rz  rH   r  r  rH   rN   rO     r  c                 S      g | ]}t | qS rH   r*   r  rH   rH   rN   rO     rP   )rc   rY  rS   r   r   r  r  rR   r  r   r  r}  r  r   _confignum_to_difflistr  r   r   r   r  r   )!r   r  statescoupled_evectr   j_testr  r  coupling_listr  
j_couplingj1_nj2_ndiff_maxr|   difftot
config_num	diff_listcg_terms	coupled_jr~  coupling_diffr  r  r  m1m2m3coeffr   	sum_termsj3_namerH   r  rN   r  L  s     
r  c                 C   s,   |  t}|D ]}| |t|||} q| S )a   Uncouple a coupled spin state

    Gives the uncoupled representation of a coupled spin state. Arguments must
    be either a spin state that is a subclass of CoupledSpinState or a spin
    state that is a subclass of SpinState and an array giving the j values
    of the spaces that are to be coupled

    Parameters
    ==========

    expr : Expr
        The expression containing states that are to be coupled. If the states
        are a subclass of SpinState, the ``jn`` and ``jcoupling`` parameters
        must be defined. If the states are a subclass of CoupledSpinState,
        ``jn`` and ``jcoupling`` will be taken from the state.

    jn : list or tuple
        The list of the j-values that are coupled. If state is a
        CoupledSpinState, this parameter is ignored. This must be defined if
        state is not a subclass of CoupledSpinState. The syntax of this
        parameter is the same as the ``jn`` parameter of JzKetCoupled.

    jcoupling_list : list or tuple
        The list defining how the j-values are coupled together. If state is a
        CoupledSpinState, this parameter is ignored. This must be defined if
        state is not a subclass of CoupledSpinState. The syntax of this
        parameter is the same as the ``jcoupling`` parameter of JzKetCoupled.

    Examples
    ========

    Uncouple a numerical state using a CoupledSpinState state:

        >>> from sympy.physics.quantum.spin import JzKetCoupled, uncouple
        >>> from sympy import S
        >>> uncouple(JzKetCoupled(1, 0, (S(1)/2, S(1)/2)))
        sqrt(2)*|1/2,-1/2>x|1/2,1/2>/2 + sqrt(2)*|1/2,1/2>x|1/2,-1/2>/2

    Perform the same calculation using a SpinState state:

        >>> from sympy.physics.quantum.spin import JzKet
        >>> uncouple(JzKet(1, 0), (S(1)/2, S(1)/2))
        sqrt(2)*|1/2,-1/2>x|1/2,1/2>/2 + sqrt(2)*|1/2,1/2>x|1/2,-1/2>/2

    Uncouple a numerical state of three coupled spaces using a CoupledSpinState state:

        >>> uncouple(JzKetCoupled(1, 1, (1, 1, 1), ((1,3,1),(1,2,1)) ))
        |1,-1>x|1,1>x|1,1>/2 - |1,0>x|1,0>x|1,1>/2 + |1,1>x|1,0>x|1,0>/2 - |1,1>x|1,1>x|1,-1>/2

    Perform the same calculation using a SpinState state:

        >>> uncouple(JzKet(1, 1), (1, 1, 1), ((1,3,1),(1,2,1)) )
        |1,-1>x|1,1>x|1,1>/2 - |1,0>x|1,0>x|1,1>/2 + |1,1>x|1,0>x|1,0>/2 - |1,1>x|1,1>x|1,-1>/2

    Uncouple a symbolic state using a CoupledSpinState state:

        >>> from sympy import symbols
        >>> j,m,j1,j2 = symbols('j m j1 j2')
        >>> uncouple(JzKetCoupled(j, m, (j1, j2)))
        Sum(CG(j1, m1, j2, m2, j, m)*|j1,m1>x|j2,m2>, (m1, -j1, j1), (m2, -j2, j2))

    Perform the same calculation using a SpinState state

        >>> uncouple(JzKet(j, m), (j1, j2))
        Sum(CG(j1, m1, j2, m2, j, m)*|j1,m1>x|j2,m2>, (m1, -j1, j1), (m2, -j2, j2))

    )r  r0  rE  	_uncouple)r  r   r  rr   r   rH   rH   rN   rE     s   
DrE   c              
      sD  t | tr| j| j}| j}|  nit | trxd u r tdt tt	fs+t
d|d u rRg }tdtD ]}|dd| tfddt|d D  f q8t |tt	fs]t
dt|td ksktdt|t\}}| jnt
d| j| jg }t}t||D ]*\}\}	}
||	d	 d  }||
d	 d  }||	|
|||f ||t|	|
 d < qjrMjrMd
d D }t  }t}t|| d |}g }t|D ]h}t||| tdd t |D rqg }|D ]0}|\}}}}}t fdd|D  }t fdd|D  }|| }|||||||f qtdd |D  }tfddt D  } |||   qt| S dtd  }t|fdd|d d D }|jfdd|d fD   tdd |D  }dd tD }tfddtD  } t||  g|R  S )Nz'Must specify j-values for coupled statezjn must be list or tuplerG   c                    r  rH   rH   )rJ   rM   r  rH   rN   rO     rP   z_uncouple.<locals>.<listcomp>z!jcoupling must be a list or tuplez?Must specify 2 fewer coupling terms than the number of j valueszstate must be a spin stater   c                 S   s   g | ]}d | qS )rF   rH   r  rH   rH   rN   rO   0  rP   c                 s   r  rX   rH   )rJ   r  r}   rH   rH   rN   r  9  r  z_uncouple.<locals>.<genexpr>c                    $   g | ]}|d    |d    qS rz  rH   r  r  r   rH   rN   rO   ?  r  c                    r  rz  rH   r  r  rH   rN   rO   @  r  c                 S   r  rH   r  r  rH   rH   rN   rO   C  r  c                    s   g | ]\}} ||| qS rH   rH   )rJ   rM   r  r>  rH   rN   rO   E  s    zm1:%dc                    s`   g | ],\}}}}}|t  fd d|D  |t  fdd|D  |t  fdd|| D  fqS )c                    r  rz  rH   r{  r{   rH   rN   rO   L  r  (_uncouple.<locals>.<listcomp>.<listcomp>c                    r  rz  rH   r{  r  rH   rN   rO   M  r  c                    r  rz  rH   r{  r  rH   rN   rO   N  r  r   rJ   r  r  r  r  r  r  rH   rN   rO   L  s    r*  c              	      sJ   g | ]!\}}}}}|t fd d|D  |t fdd|D   fqS )c                    r  rz  rH   r{  r  rH   rN   rO   O  r  r  c                    r  rz  rH   r{  r  rH   rN   rO   P  r  r   r  )rM   r   r{   rH   rN   rO   O  s    c                 S   r  rH   r*   )rJ   cg_termrH   rH   rN   rO   R  rP   c                 S   s   g | ]
\}}|| |fqS rH   rH   rJ   rM   r   rH   rH   rN   rO   S  s    c                    s   g | ]	\}} ||qS rH   rH   r  r  rH   rN   rO   T  s    )r   rD  r   r  r  r  r0  rR   r  r  r  rS   r   r   r   r  r   rM   r   r  r  r  r   r  r  r   r)   r   r   )r   r   r  r  r  rK   r  j_listr  r  r  r  r  r  r  r   r  r|   r  r  r   r  r  r  r  r  r  m_strcg_coeffr  rH   )r  r>  rM   r   r   r{   rN   r    s   


&

r  c                 C   sx   g }t |D ]3}|}|| d }t|| d |}| |kr2| |8 } |d8 }t|| d |}| |ks|||  q|S r   )rS   r   r   )r  r  list_lenr  r   	prev_diff	rem_spotsrem_configsrH   rH   rN   r  X  s   r  rX   )NN)gr   sympy.concrete.summationsr   sympy.core.addr   sympy.core.containersr   sympy.core.exprr   sympy.core.numbersr   sympy.core.mulr   r	   r
   r   r   sympy.core.singletonr   sympy.core.symbolr   r   sympy.core.sympifyr   (sympy.functions.combinatorial.factorialsr   r   &sympy.functions.elementary.exponentialr   (sympy.functions.elementary.miscellaneousr   (sympy.functions.elementary.trigonometricr   r   sympy.simplify.simplifyr   sympy.matricesr    sympy.printing.pretty.stringpictr   r   &sympy.printing.pretty.pretty_symbologyr   sympy.physics.quantum.qexprr   sympy.physics.quantum.operatorr   r    r!   sympy.physics.quantum.stater"   r#   r$   (sympy.functions.special.tensor_functionsr%   sympy.physics.quantum.constantsr&   sympy.physics.quantum.hilbertr'   r(   #sympy.physics.quantum.tensorproductr)   sympy.physics.quantum.cgr+   sympy.physics.quantum.qapplyr,   __all__r-   rV   r   r   r   r   r<   r=   r4   r5   r0   r1   r2   r3   r.   r/   r0  r6   r7   r8   r9   r:   r;   r  rD  r>   r?   r@   rA   rB   rC   rD   r  rE   r  r  rH   rH   rH   rN   <module>   s    _,)## I   Q !!  -!! 
F
qJQ