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 dd	lmZ dd
lmZmZ ddlmZ ddlmZ ddlmZ ddlmZmZ ddlmZ ddlm Z m!Z!m"Z"m#Z# ddl$m%Z% g dZ&G dd deZ'G dd de'Z(G dd deZ)G dd deZ*dd Z+dd Z,dd  Z-d!d" Z.d#d$ Z/d%d& Z0d4d(d)Z1d*d+ Z2d,d- Z3d.d/ Z4d0d1 Z5d2d3 Z6d'S )5zClebsch-Gordon Coefficients.    )Sum)Add)Expr)expand)Mul)Pow)Eq)S)Wildsymbols)sympify)sqrt)	Piecewise)
prettyForm
stringPict)KroneckerDelta)clebsch_gordan	wigner_3j	wigner_6j	wigner_9j)
PRECEDENCE)CGWigner3jWigner6jWigner9jcg_simpc                   @   s   e Zd ZdZdZdd Zedd Zedd Zed	d
 Z	edd Z
edd Zedd Zedd Zdd Zdd Zdd ZdS )r   a  Class for the Wigner-3j symbols.

    Explanation
    ===========

    Wigner 3j-symbols are coefficients determined by the coupling of
    two angular momenta. When created, they are expressed as symbolic
    quantities that, for numerical parameters, can be evaluated using the
    ``.doit()`` method [1]_.

    Parameters
    ==========

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

    Examples
    ========

    Declare a Wigner-3j coefficient and calculate its value

        >>> from sympy.physics.quantum.cg import Wigner3j
        >>> w3j = Wigner3j(6,0,4,0,2,0)
        >>> w3j
        Wigner3j(6, 0, 4, 0, 2, 0)
        >>> w3j.doit()
        sqrt(715)/143

    See Also
    ========

    CG: Clebsch-Gordan coefficients

    References
    ==========

    .. [1] Varshalovich, D A, Quantum Theory of Angular Momentum. 1988.
    Tc                 C   (   t t||||||f}tj| g|R  S Nmapr   r   __new__)clsj1m1j2m2j3m3args r)   P/var/www/html/zoom/venv/lib/python3.10/site-packages/sympy/physics/quantum/cg.pyr    Q      zWigner3j.__new__c                 C   
   | j d S Nr   r(   selfr)   r)   r*   r"   U      
zWigner3j.j1c                 C   r,   N   r.   r/   r)   r)   r*   r#   Y   r1   zWigner3j.m1c                 C   r,   N   r.   r/   r)   r)   r*   r$   ]   r1   zWigner3j.j2c                 C   r,   N   r.   r/   r)   r)   r*   r%   a   r1   zWigner3j.m2c                 C   r,   N   r.   r/   r)   r)   r*   r&   e   r1   zWigner3j.j3c                 C   r,   N   r.   r/   r)   r)   r*   r'   i   r1   zWigner3j.m3c                 C      t dd | jD  S )Nc                 s       | ]}|j V  qd S r   	is_number.0argr)   r)   r*   	<genexpr>o       z'Wigner3j.is_symbolic.<locals>.<genexpr>allr(   r/   r)   r)   r*   is_symbolicm      zWigner3j.is_symbolicc                    sp  | | j| | jf| | j| | jf| | j| | jffd}d}dgd }tdD ] t fddtdD | < q0d }tdD ]f}d }tdD ]A   | }	|  |		  }
|
d }|
| }t
|	d|  }	t
|	d|  }	|d u r|	}qQt
|d|  }t
||	 }qQ|d u r|}qIt|D ]	}t
|d }qt
|| }qIt
|  }|S )Nr5   r3   r7   c                 3        | ]}  |   V  qd S r   widthrA   ijmr)   r*   rC   z       z#Wigner3j._pretty.<locals>.<genexpr> )_printr"   r#   r$   r%   r&   r'   rangemaxrL   r   rightleftbelowparensr0   printerr(   hsepvsepmaxwDrN   D_rowswdeltawleftwright_r)   rO   r*   _prettyr   s@   
"zWigner3j._prettyc                 G   0   t |j| j| j| j| j| j| jf}dt| S )NzH\left(\begin{array}{ccc} %s & %s & %s \\ %s & %s & %s \end{array}\right))	r   rT   r"   r$   r&   r#   r%   r'   tupler0   r\   r(   labelr)   r)   r*   _latex      zWigner3j._latexc                 K   ,   | j rtdt| j| j| j| j| j| jS NzCoefficients must be numerical)	rG   
ValueErrorr   r"   r$   r&   r#   r%   r'   r0   hintsr)   r)   r*   doit      zWigner3j.doitN)__name__
__module____qualname____doc__is_commutativer    propertyr"   r#   r$   r%   r&   r'   rG   rg   rl   rs   r)   r)   r)   r*   r   &   s*    (






#r   c                   @   s4   e Zd ZdZed d Zdd Zdd Zdd	 Zd
S )r   a  Class for Clebsch-Gordan coefficient.

    Explanation
    ===========

    Clebsch-Gordan coefficients describe the angular momentum coupling between
    two systems. The coefficients give the expansion of a coupled total angular
    momentum state and an uncoupled tensor product state. The Clebsch-Gordan
    coefficients are defined as [1]_:

    .. math ::
        C^{j_3,m_3}_{j_1,m_1,j_2,m_2} = \left\langle j_1,m_1;j_2,m_2 | j_3,m_3\right\rangle

    Parameters
    ==========

    j1, m1, j2, m2 : Number, Symbol
        Angular momenta of states 1 and 2.

    j3, m3: Number, Symbol
        Total angular momentum of the coupled system.

    Examples
    ========

    Define a Clebsch-Gordan coefficient and evaluate its value

        >>> from sympy.physics.quantum.cg import CG
        >>> from sympy import S
        >>> cg = CG(S(3)/2, S(3)/2, S(1)/2, -S(1)/2, 1, 1)
        >>> cg
        CG(3/2, 3/2, 1/2, -1/2, 1, 1)
        >>> cg.doit()
        sqrt(3)/2
        >>> CG(j1=S(1)/2, m1=-S(1)/2, j2=S(1)/2, m2=+S(1)/2, j3=1, m3=0).doit()
        sqrt(2)/2


    Compare [2]_.

    See Also
    ========

    Wigner3j: Wigner-3j symbols

    References
    ==========

    .. [1] Varshalovich, D A, Quantum Theory of Angular Momentum. 1988.
    .. [2] `Clebsch-Gordan Coefficients, Spherical Harmonics, and d Functions
        <https://pdg.lbl.gov/2020/reviews/rpp2020-rev-clebsch-gordan-coefs.pdf>`_
        in P.A. Zyla *et al.* (Particle Data Group), Prog. Theor. Exp. Phys.
        2020, 083C01 (2020).
    r   r3   c                 K   rn   ro   )	rG   rp   r   r"   r$   r&   r#   r%   r'   rq   r)   r)   r*   rs      rt   zCG.doitc                 G   s   |j | j| j| j| jfdd}|j | j| jfdd}t| | }t	|
d }t	|
d }|| ksDt	|d||    }|| ksWt	|d||    }tdd|  }t	|| }t	|| }|S )N,)	delimiterrS   C)
_print_seqr"   r#   r$   r%   r&   r'   rV   rL   r   rX   rW   r   rY   above)r0   r\   r(   bottoppadrb   r)   r)   r*   rg      s   z
CG._prettyc                 G   rh   )NzC^{%s,%s}_{%s,%s,%s,%s})	r   rT   r&   r'   r"   r#   r$   r%   ri   rj   r)   r)   r*   rl      s   z	CG._latexN)	ru   rv   rw   rx   r   
precedencers   rg   rl   r)   r)   r)   r*   r      s    6r   c                   @   s   e Zd ZdZdd Zedd Zedd Zedd	 Zed
d Z	edd Z
edd Zedd Zdd Zdd Zdd ZdS )r   zaClass for the Wigner-6j symbols

    See Also
    ========

    Wigner3j: Wigner-3j symbols

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

    See Also
    ========

    Wigner3j: Wigner-3j symbols

    c
                 C   s.   t t|||||||||	f	}
tj| g|
R  S r   r   )r!   r"   r$   r   r&   j4j34j13j24rP   r(   r)   r)   r*   r    Y  s   zWigner9j.__new__c                 C   r,   r-   r.   r/   r)   r)   r*   r"   ]  r1   zWigner9j.j1c                 C   r,   r2   r.   r/   r)   r)   r*   r$   a  r1   zWigner9j.j2c                 C   r,   r4   r.   r/   r)   r)   r*   r   e  r1   zWigner9j.j12c                 C   r,   r6   r.   r/   r)   r)   r*   r&   i  r1   zWigner9j.j3c                 C   r,   r8   r.   r/   r)   r)   r*   r   m  r1   zWigner9j.j4c                 C   r,   r:   r.   r/   r)   r)   r*   r   q  r1   zWigner9j.j34c                 C   r,   )N   r.   r/   r)   r)   r*   r   u  r1   zWigner9j.j13c                 C   r,   )N   r.   r/   r)   r)   r*   r   y  r1   zWigner9j.j24c                 C   r,   )N   r.   r/   r)   r)   r*   rP   }  r1   z
Wigner9j.jc                 C   r<   )Nc                 s   r=   r   r>   r@   r)   r)   r*   rC     rD   z'Wigner9j.is_symbolic.<locals>.<genexpr>rE   r/   r)   r)   r*   rG     rH   zWigner9j.is_symbolicc                    s  | | j| | j| | jf| | j| | j| | jf| | j| | j| | j	ffd}d}dgd }t
dD ] t fddt
dD | < q?d }t
dD ]f}d }t
dD ]A   | }	|  |	  }
|
d }|
| }t|	d|  }	t|	d|  }	|d u r|	}q`t|d|  }t||	 }q`|d u r|}qXt
|D ]	}t|d }qt|| }qXt|jdd	d
 }|S )Nr5   r3   rI   r7   c                 3   rJ   r   rK   rM   rO   r)   r*   rC     rR   z#Wigner9j._pretty.<locals>.<genexpr>rS   r   r   r   )rT   r"   r&   r   r$   r   r   r   r   rP   rU   rV   rL   r   rW   rX   rY   rZ   r[   r)   rO   r*   rg     sP    
"zWigner9j._prettyc                 G   s<   t |j| j| j| j| j| j| j| j| j	| j
f	}dt| S )NzZ\left\{\begin{array}{ccc} %s & %s & %s \\ %s & %s & %s \\ %s & %s & %s \end{array}\right\})r   rT   r"   r$   r   r&   r   r   r   r   rP   ri   rj   r)   r)   r*   rl     s   zWigner9j._latexc              
   K   s8   | j rtdt| j| j| j| j| j| j| j	| j
| j	S ro   )rG   rp   r   r"   r$   r   r&   r   r   r   r   rP   rq   r)   r)   r*   rs     s   *zWigner9j.doitN)ru   rv   rw   rx   r    rz   r"   r$   r   r&   r   r   r   r   rP   rG   rg   rl   rs   r)   r)   r)   r*   r   P  s4    









&r   c                 C   sb   t | tr	t| S t | trt| S t | tr!tdd | jD  S t | tr/tt| j	| j
S | S )a  Simplify and combine CG coefficients.

    Explanation
    ===========

    This function uses various symmetry and properties of sums and
    products of Clebsch-Gordan coefficients to simplify statements
    involving these terms [1]_.

    Examples
    ========

    Simplify the sum over CG(a,alpha,0,0,a,alpha) for all alpha to
    2*a+1

        >>> from sympy.physics.quantum.cg import CG, cg_simp
        >>> a = CG(1,1,0,0,1,1)
        >>> b = CG(1,0,0,0,1,0)
        >>> c = CG(1,-1,0,0,1,-1)
        >>> cg_simp(a+b+c)
        3

    See Also
    ========

    CG: Clebsh-Gordan coefficients

    References
    ==========

    .. [1] Varshalovich, D A, Quantum Theory of Angular Momentum. 1988.
    c                 S   s   g | ]}t |qS r)   )r   r@   r)   r)   r*   
<listcomp>      zcg_simp.<locals>.<listcomp>)
isinstancer   _cg_simp_addr   _cg_simp_sumr   r(   r   r   baseexper)   r)   r*   r     s   
!


r   c                 C   s  g }g }t | } | jD ]M}|trSt|tr|t| qt|trMd}|jD ]}t|tr7|t|9 }q)||9 }q)|trG|| q|| q|| q|| qt	|\}}|| t
|\}}|| t|\}}|| t| t|  S )a  Takes a sum of terms involving Clebsch-Gordan coefficients and
    simplifies the terms.

    Explanation
    ===========

    First, we create two lists, cg_part, which is all the terms involving CG
    coefficients, and other_part, which is all other terms. The cg_part list
    is then passed to the simplification methods, which return the new cg_part
    and any additional terms that are added to other_part
    r3   )r   r(   hasr   r   r   appendr   r   _check_varsh_871_1_check_varsh_871_2_check_varsh_872_9r   )r   cg_part
other_partrB   termstermotherr)   r)   r*   r     s2   










r   c           
   
   C   s   t td\}}}}|t|||d|| }d| d t|d }|t| }d| d }|| }	t||||| ||||f||f||		S )N)aalphabltr   r5   r3   )r   r
   r   r   abs_check_cg_simp)
	term_listr   r   r   r   exprsimpsign
build_expr
index_exprr)   r)   r*   r     s   $r   c           
   
   C   s   t td\}}}}|t|||| |d }td| d t|d }d||  | t| }d| d }|| }	t||||| ||||f||f||		S )N)r   r   cr   r   r5   r3   rI   )r   r
   r   r   r   r   r   )
r   r   r   r   r   r   r   r   r   r   r)   r)   r*   r     s   $r   c                 C   s  t td\	}}}}}}}}}	|	t||||||d  }
tj}|	t|	 }t|| }t|| }|| d t|||kfdt||f|||kf }|| | }t|
|||	| |||||||	f||||f||	\} }t|| }|| }|d | || d  }|| ||  | | }t|
|||	| |||||||	f||||f||	\} }t||||||t|||||| }
t	||t	|| }tj}t|| }t|| }|| d t|||kfdt||f|||kf }|| | }t|
||tj| ||||||||f||||||f||	\} }t|| }|| }|d | || d  }|| ||  | | }t|
||tj| ||||||||f||||||f||	\} }| || | fS )N)	r   r   alphapr   betabetapr   gammar   r5   r3   r   )
r   r
   r   r	   Oner   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   xyr   r   other1other2other3other4r)   r)   r*   r   )  s8   222$2::r   c	              	      s  d}	d}
|
t k rt|
 | t |  du r|
d7 }
q| js(|
d7 }
q fdd|D }dg|  }t|
t D ]M}t| | |t |t | | ||fd}|du rbq@|||jslq@|| |d|||||||f||||< q@tdd |D std	d |D  }d
d |D }|  |  fdd|D  |D ]}t	|d |krՈ
|d ||d   |d   q|	|||   7 }	n|
d7 }
|
t k s
|	fS )a   Checks for simplifications that can be made, returning a tuple of the
    simplified list of terms and any terms generated by simplification.

    Parameters
    ==========

    expr: expression
        The expression with Wild terms that will be matched to the terms in
        the sum

    simp: expression
        The expression with Wild terms that is substituted in place of the CG
        terms in the case of simplification

    sign: expression
        The expression with Wild terms denoting the sign that is on expr that
        must match

    lt: expression
        The expression with Wild terms that gives the leading term of the
        matched expr

    term_list: list
        A list of all of the terms is the sum to be simplified

    variables: list
        A list of all the variables that appears in expr

    dep_variables: list
        A list of the variables that must match for all the terms in the sum,
        i.e. the dependent variables

    build_index_expr: expression
        Expression with Wild terms giving the number of elements in cg_index

    index_expr: expression
        Expression with Wild terms giving the index terms have when storing
        them to cg_index

    r   Nr3   c                    s   g | ]}| | fqS r)   r)   )rA   r   )sub_1r)   r*   r         z"_check_cg_simp.<locals>.<listcomp>)r   c                 s   s    | ]}|d u V  qd S r   r)   rM   r)   r)   r*   rC     s    z!_check_cg_simp.<locals>.<genexpr>c                 S   s   g | ]}t |d  qS )r5   )r   rA   r   r)   r)   r*   r     r   c                 S   s   g | ]}|d  qS )r   r)   r   r)   r)   r*   r     r   c                    s   g | ]}  |qS r)   )pop)rA   rP   )r   r)   r*   r     s    r5   r7   )len	_check_cgsubsr?   rU   anyminsortreverser   r   )r   r   r   r   r   	variablesdep_variablesbuild_index_exprr   r   rN   sub_depcg_indexrP   sub_2min_ltindicesr   r)   )r   r   r*   r   V  sB   )6D"r   Nc                 C   s^   |  |}|du rdS |dur%t|tstd|d |d |ks%dS t||kr-|S dS )z2Checks whether a term matches the given expressionNzsign must be a tupler   r3   )matchr   ri   	TypeErrorr   r   )cg_termr   lengthr   matchesr)   r)   r*   r     s   

r   c                 C   s   t | } t| } t| } | S r   )_check_varsh_sum_871_1_check_varsh_sum_871_2_check_varsh_sum_872_4r   r)   r)   r*   r     s   r   c              
   C   sr   t d}td}t d}| tt|||d|||| |f}|d ur7t|dkr7d| d t|d |S | S )Nr   r   r   r   r5   r3   )r
   r   r   r   r   r   r   r   )r   r   r   r   r   r)   r)   r*   r     s   &r   c                 C   s   t d}td}t d}| td||  t|||| |d || |f}|d ur@t|dkr@td| d t|d |S | S )Nr   r   r   rI   r   r5   r3   )	r
   r   r   r   r   r   r   r   r   )r   r   r   r   r   r)   r)   r*   r     s   , r   c                 C   s   t d}t d}td}td}td}td}td}td}t||||||}	t||||||}
| t|	|
 || |f|| |f}|d ur\t|d	kr\t||t|| |S | t|	d
 || |f|| |f}|d ur|t|dkr|tj	S | S )Nr   r   r   r   r   cpr   gammapr   r5   r9   )
r   r
   r   r   r   r   r   r   r	   r   )r   r   r   r   r   r   r   r   r   cg1cg2match1match2r)   r)   r*   r     s"   &&r   c                    s   t trfddfS g  d}t ttfstdt tr;jjr;jjr5 fddtjD  nfddfS t tr^jD ]}t |trP 	| qC||9 }qC ||t
| fS d S )Nr3   z term must be CG, Add, Mul or Powc                    s   g | ]}  jqS r)   )r   r   )rA   rf   cgr   r)   r*   r     r   z_cg_list.<locals>.<listcomp>)r   r   r   r   NotImplementedErrorr   r?   rU   r(   r   r   )r   coeffrB   r)   r   r*   _cg_list  s"   




r   r   )7rx   sympy.concrete.summationsr   sympy.core.addr   sympy.core.exprr   sympy.core.functionr   sympy.core.mulr   sympy.core.powerr   sympy.core.relationalr   sympy.core.singletonr	   sympy.core.symbolr
   r   sympy.core.sympifyr   (sympy.functions.elementary.miscellaneousr   $sympy.functions.elementary.piecewiser    sympy.printing.pretty.stringpictr   r   (sympy.functions.special.tensor_functionsr   sympy.physics.wignerr   r   r   r   sympy.printing.precedencer   __all__r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r)   r)   r)   r*   <module>   sD   {VYh-.-
K
