
    Ed|                     0   d 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
 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mZ ddlmZ ddlmZ ddlm Z  dgZ!d Z"d Z#d Z$ddZ%ddZ&ddZ'ddZ(ddZ) G d de          Z*d Z+d Z,d Z-dS ) a   
Wigner, Clebsch-Gordan, Racah, and Gaunt coefficients

Collection of functions for calculating Wigner 3j, 6j, 9j,
Clebsch-Gordan, Racah as well as Gaunt coefficients exactly, all
evaluating to a rational number times the square root of a rational
number [Rasch03]_.

Please see the description of the individual functions for further
details and examples.

References
==========

.. [Regge58] 'Symmetry Properties of Clebsch-Gordan Coefficients',
  T. Regge, Nuovo Cimento, Volume 10, pp. 544 (1958)
.. [Regge59] 'Symmetry Properties of Racah Coefficients',
  T. Regge, Nuovo Cimento, Volume 11, pp. 116 (1959)
.. [Edmonds74] A. R. Edmonds. Angular momentum in quantum mechanics.
  Investigations in physics, 4.; Investigations in physics, no. 4.
  Princeton, N.J., Princeton University Press, 1957.
.. [Rasch03] J. Rasch and A. C. H. Yu, 'Efficient Storage Scheme for
  Pre-calculated Wigner 3j, 6j and Gaunt Coefficients', SIAM
  J. Sci. Comput. Volume 25, Issue 4, pp. 1416-1428 (2003)
.. [Liberatodebrito82] 'FORTRAN program for the integral of three
  spherical harmonics', A. Liberato de Brito,
  Comput. Phys. Commun., Volume 25, pp. 81-85 (1982)

Credits and Copyright
=====================

This code was taken from Sage with the permission of all authors:

https://groups.google.com/forum/#!topic/sage-devel/M4NZdu-7O38

Authors
=======

- Jens Rasch (2009-03-24): initial version for Sage

- Jens Rasch (2009-05-31): updated to sage-4.0

- Oscar Gerardo Lazo Arjona (2017-06-18): added Wigner D matrices

Copyright (C) 2008 Jens Rasch <jyr2000@gmail.com>

    )Sum)Add)Function)IIntegerpi)S)Dummy)sympify)binomial	factorial)exp)sqrt)cossin)Ynm)zeros)ImmutableMatrix   c                 0   | t          t                    k    r`t          t          t                    t          | dz                       D ]-}t                              t          |dz
           |z             .t          dt          |           dz            S )a1  
    Function calculates a list of precomputed factorials in order to
    massively accelerate future calculations of the various
    coefficients.

    Parameters
    ==========

    nn : integer
        Highest factorial to be computed.

    Returns
    =======

    list of integers :
        The list of precomputed factorials.

    Examples
    ========

    Calculate list of factorials::

        sage: from sage.functions.wigner import _calc_factlist
        sage: _calc_factlist(10)
        [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800]
    r   N)len	_Factlistrangeintappend)nniis     4lib/python3.11/site-packages/sympy/physics/wigner.py_calc_factlistr   E   s~    6 
S^^ 5IBF44 	5 	5BYrAv.34444\c"ggk\""    c           	      t	   t          | dz            | dz  k    s2t          |dz            |dz  k    st          |dz            |dz  k    rt          d          t          |dz            |dz  k    s2t          |dz            |dz  k    st          |dz            |dz  k    rt          d          ||z   |z   dk    rt          j        S t	          dt          | |z
  |z
            z            }| }| |z   |z
  }|dk     rt          j        S | |z
  |z   }|dk     rt          j        S |  |z   |z   }	|	dk     rt          j        S t          |          | k    s&t          |          |k    st          |          |k    rt          j        S t          | |z   |z   dz   | t          |          z   |t          |          z   |t          |          z             }
t          t          |
                     t	          t          t          | |z   |z
                     t          t          | |z
  |z                      z  t          t          |  |z   |z                      z  t          t          | |z
                     z  t          t          | |z                      z  t          t          ||z
                     z  t          t          ||z                      z  t          t          ||z
                     z  t          t          ||z                      z            t          t          | |z   |z   dz                      z  }t          |          }|j
        s|j        r|                                d         }t          | | z   |z   | |z   |z
  d          }t          ||z   | |z
  | |z   |z
            }d}t          t          |          t          |          dz             D ]}t          |         t          t          ||z   | z
  |z
                     z  t          t          ||z   |z
                     z  t          t          | |z
  |z
                     z  t          t          ||z   |z
  |z                      z  t          t          | |z   |z
  |z
                     z  }|t	          d|z            |z  z   }||z  |z  }|S )a	  
    Calculate the Wigner 3j symbol `\operatorname{Wigner3j}(j_1,j_2,j_3,m_1,m_2,m_3)`.

    Parameters
    ==========

    j_1, j_2, j_3, m_1, m_2, m_3 :
        Integer or half integer.

    Returns
    =======

    Rational number times the square root of a rational number.

    Examples
    ========

    >>> from sympy.physics.wigner import wigner_3j
    >>> wigner_3j(2, 6, 4, 0, 0, 0)
    sqrt(715)/143
    >>> wigner_3j(2, 6, 4, 0, 0, 1)
    0

    It is an error to have arguments that are not integer or half
    integer values::

        sage: wigner_3j(2.1, 6, 4, 0, 0, 0)
        Traceback (most recent call last):
        ...
        ValueError: j values must be integer or half integer
        sage: wigner_3j(2, 6, 4, 1, 0, -1.1)
        Traceback (most recent call last):
        ...
        ValueError: m values must be integer or half integer

    Notes
    =====

    The Wigner 3j symbol obeys the following symmetry rules:

    - invariant under any permutation of the columns (with the
      exception of a sign change where `J:=j_1+j_2+j_3`):

      .. math::

         \begin{aligned}
         \operatorname{Wigner3j}(j_1,j_2,j_3,m_1,m_2,m_3)
          &=\operatorname{Wigner3j}(j_3,j_1,j_2,m_3,m_1,m_2) \\
          &=\operatorname{Wigner3j}(j_2,j_3,j_1,m_2,m_3,m_1) \\
          &=(-1)^J \operatorname{Wigner3j}(j_3,j_2,j_1,m_3,m_2,m_1) \\
          &=(-1)^J \operatorname{Wigner3j}(j_1,j_3,j_2,m_1,m_3,m_2) \\
          &=(-1)^J \operatorname{Wigner3j}(j_2,j_1,j_3,m_2,m_1,m_3)
         \end{aligned}

    - invariant under space inflection, i.e.

      .. math::

         \operatorname{Wigner3j}(j_1,j_2,j_3,m_1,m_2,m_3)
         =(-1)^J \operatorname{Wigner3j}(j_1,j_2,j_3,-m_1,-m_2,-m_3)

    - symmetric with respect to the 72 additional symmetries based on
      the work by [Regge58]_

    - zero for `j_1`, `j_2`, `j_3` not fulfilling triangle relation

    - zero for `m_1 + m_2 + m_3 \neq 0`

    - zero for violating any one of the conditions
      `j_1 \ge |m_1|`,  `j_2 \ge |m_2|`,  `j_3 \ge |m_3|`

    Algorithm
    =========

    This function uses the algorithm of [Edmonds74]_ to calculate the
    value of the 3j symbol exactly. Note that the formula contains
    alternating sums over large factorials and is therefore unsuitable
    for finite precision arithmetic and only useful for a computer
    algebra system [Rasch03]_.

    Authors
    =======

    - Jens Rasch (2009-03-24): initial version
       z(j values must be integer or half integerz(m values must be integer or half integerr   r   )r   
ValueErrorr	   Zeror   absmaxr   r   r   
is_complexis_infiniteas_real_imagminr   )j_1j_2j_3m_1m_2m_3prefida1a2a3maxfactargsqrtressqrtiminimaxsumresr   denress                      r   	wigner_3jr>   f   s   l 37||sQw E#cAg,,#'"9 EaLLC!G#ECDDD
37||sQw E#cAg,,#'"9 EaLLC!G#ECDDD
Sy3! vbSsS11122F$C	sSB	Av v	sSB	Av v
c	B	Av vC3 CHHsN C3 v#)c/A%sSXX~sSXX~C." "G3w<<   iC#IO 4 45s39s?3345sC4#:#34456 s39~~./ s39~~.	/
 s39~~./ s39~~./ s39~~./ s39~~./ 0 0 	#cCi#o)**+	,G 7mmG ,W0 ,&&((+tczC#c!1155DsSy#)S3Y_55DFCIIs4yy1}-- 4 4mc"s(S.3.//01c#)b.))*+ c#(S.))*+ c"s(S.3.//0	1
 c#)c/B.//01 '2"*--33
F
V
#CJr    c           	          dt          | |z
  |z             z  t          d|z  dz             z  t          | |||||           z  }|S )a  
    Calculates the Clebsch-Gordan coefficient.
    `\left\langle j_1 m_1 \; j_2 m_2 | j_3 m_3 \right\rangle`.

    The reference for this function is [Edmonds74]_.

    Parameters
    ==========

    j_1, j_2, j_3, m_1, m_2, m_3 :
        Integer or half integer.

    Returns
    =======

    Rational number times the square root of a rational number.

    Examples
    ========

    >>> from sympy import S
    >>> from sympy.physics.wigner import clebsch_gordan
    >>> clebsch_gordan(S(3)/2, S(1)/2, 2, S(3)/2, S(1)/2, 2)
    1
    >>> clebsch_gordan(S(3)/2, S(1)/2, 1, S(3)/2, -S(1)/2, 1)
    sqrt(3)/2
    >>> clebsch_gordan(S(3)/2, S(1)/2, 1, -S(1)/2, S(1)/2, 0)
    -sqrt(2)/2

    Notes
    =====

    The Clebsch-Gordan coefficient will be evaluated via its relation
    to Wigner 3j symbols:

    .. math::

        \left\langle j_1 m_1 \; j_2 m_2 | j_3 m_3 \right\rangle
        =(-1)^{j_1-j_2+m_3} \sqrt{2j_3+1}
        \operatorname{Wigner3j}(j_1,j_2,j_3,m_1,m_2,-m_3)

    See also the documentation on Wigner 3j symbols which exhibit much
    higher symmetry relations than the Clebsch-Gordan coefficient.

    Authors
    =======

    - Jens Rasch (2009-03-24): initial version
    r#   r"   r   )r   r   r>   )r,   r-   r.   r/   r0   r1   r=   s          r   clebsch_gordanr@      sU    d '#)c/**
*T!c'A+->->
>#sCcC4001CJr    Nc                    t          | |z   |z
            | |z   |z
  k    rt          d          t          | |z   |z
            | |z   |z
  k    rt          d          t          ||z   | z
            ||z   | z
  k    rt          d          | |z   |z
  dk     rt          j        S | |z   |z
  dk     rt          j        S ||z   | z
  dk     rt          j        S t	          | |z   |z
  | |z   |z
  ||z   | z
  | |z   |z   dz             }t          |           t          t          t          | |z   |z
                     t          t          | |z   |z
                     z  t          t          ||z   | z
                     z            t          t          t          | |z   |z   dz                                z  }t          |          }|r-|	                    |          
                                d         }|S )a  
    Calculates the Delta coefficient of the 3 angular momenta for
    Racah symbols. Also checks that the differences are of integer
    value.

    Parameters
    ==========

    aa :
        First angular momentum, integer or half integer.
    bb :
        Second angular momentum, integer or half integer.
    cc :
        Third angular momentum, integer or half integer.
    prec :
        Precision of the ``sqrt()`` calculation.

    Returns
    =======

    double : Value of the Delta coefficient.

    Examples
    ========

        sage: from sage.functions.wigner import _big_delta_coeff
        sage: _big_delta_coeff(1,1,1)
        1/2*sqrt(1/6)
    zJj values must be integer or half integer and fulfill the triangle relationr   r   )r   r$   r	   r%   r'   r   r   r   r   evalfr*   )aabbccprecr6   r7   r8   s          r   _big_delta_coeffrG   ,  s   > 27R<R"Wr\* gefff
27R<R"Wr\* gefff
27R<R"Wr\* gefff
R" v
R" v
R" v"r'B,R"b2glBGbL1<LMMG7iBGbL 1 12s27R<0012s27R<0012 3 3 		#b2glQ.//0112G
 7mmG 8--%%2244Q7Nr    c                    t          | |||          t          ||||          z  t          | |||          z  t          ||||          z  }|dk    rt          j        S t          | |z   |z   ||z   |z   | |z   |z   ||z   |z             }t	          | |z   |z   |z   | |z   |z   |z   ||z   |z   |z             }	t          |	dz   | |z   |z   |z   | |z   |z   |z   ||z   |z   |z             }
t          |
           d}t          t          |          t          |	          dz             D ]0}t          t          || z
  |z
  |z
                     t          t          ||z
  |z
  |z
                     z  t          t          || z
  |z
  |z
                     z  t          t          ||z
  |z
  |z
                     z  t          t          | |z   |z   |z   |z
                     z  t          t          | |z   |z   |z   |z
                     z  t          t          ||z   |z   |z   |z
                     z  }|t          d|z  t          |dz            z            |z  z   }2||z  dt          | |z   |z   |z             z  z  }|S )a  
    Calculate the Racah symbol `W(a,b,c,d;e,f)`.

    Parameters
    ==========

    a, ..., f :
        Integer or half integer.
    prec :
        Precision, default: ``None``. Providing a precision can
        drastically speed up the calculation.

    Returns
    =======

    Rational number times the square root of a rational number
    (if ``prec=None``), or real number if a precision is given.

    Examples
    ========

    >>> from sympy.physics.wigner import racah
    >>> racah(3,3,3,3,3,3)
    -1/14

    Notes
    =====

    The Racah symbol is related to the Wigner 6j symbol:

    .. math::

       \operatorname{Wigner6j}(j_1,j_2,j_3,j_4,j_5,j_6)
       =(-1)^{j_1+j_2+j_4+j_5} W(j_1,j_2,j_5,j_4,j_3,j_6)

    Please see the 6j symbol for its much richer symmetries and for
    additional properties.

    Algorithm
    =========

    This function uses the algorithm of [Edmonds74]_ to calculate the
    value of the 6j symbol exactly. Note that the formula contains
    alternating sums over large factorials and is therefore unsuitable
    for finite precision arithmetic and only useful for a computer
    algebra system [Rasch03]_.

    Authors
    =======

    - Jens Rasch (2009-03-24): initial version
    r   r   r#   )
rG   r	   r%   r'   r+   r   r   r   r   r   )rC   rD   rE   ddeeffrF   prefacr9   r:   r6   r;   kkr<   r=   s                  r   racahrN   f  s   j b"b$//RT**+RT**+ 	RT**+F { vrBw|R"Wr\27R<b2FFDrBw|b "r'B,"3R"Wr\B5FGGD$(BGbL2-rBw|b/@b2"$ $G7FCIIs4yy1}-- H HBGbL2-../c"r'B,+,,-.c"r'B,+,,-. c"r'B,+,,-. c"r'B,+b0112	3
 c"r'B,+b01123 c"r'B,+b01123 '2"*ya/@"@AACGG
6/RCR"r(9$:$::
:CJr    c           
      h    dt          | |z   |z   |z             z  t          | ||||||          z  }|S )a+  
    Calculate the Wigner 6j symbol `\operatorname{Wigner6j}(j_1,j_2,j_3,j_4,j_5,j_6)`.

    Parameters
    ==========

    j_1, ..., j_6 :
        Integer or half integer.
    prec :
        Precision, default: ``None``. Providing a precision can
        drastically speed up the calculation.

    Returns
    =======

    Rational number times the square root of a rational number
    (if ``prec=None``), or real number if a precision is given.

    Examples
    ========

    >>> from sympy.physics.wigner import wigner_6j
    >>> wigner_6j(3,3,3,3,3,3)
    -1/14
    >>> wigner_6j(5,5,5,5,5,5)
    1/52

    It is an error to have arguments that are not integer or half
    integer values or do not fulfill the triangle relation::

        sage: wigner_6j(2.5,2.5,2.5,2.5,2.5,2.5)
        Traceback (most recent call last):
        ...
        ValueError: j values must be integer or half integer and fulfill the triangle relation
        sage: wigner_6j(0.5,0.5,1.1,0.5,0.5,1.1)
        Traceback (most recent call last):
        ...
        ValueError: j values must be integer or half integer and fulfill the triangle relation

    Notes
    =====

    The Wigner 6j symbol is related to the Racah symbol but exhibits
    more symmetries as detailed below.

    .. math::

       \operatorname{Wigner6j}(j_1,j_2,j_3,j_4,j_5,j_6)
        =(-1)^{j_1+j_2+j_4+j_5} W(j_1,j_2,j_5,j_4,j_3,j_6)

    The Wigner 6j symbol obeys the following symmetry rules:

    - Wigner 6j symbols are left invariant under any permutation of
      the columns:

      .. math::

         \begin{aligned}
         \operatorname{Wigner6j}(j_1,j_2,j_3,j_4,j_5,j_6)
          &=\operatorname{Wigner6j}(j_3,j_1,j_2,j_6,j_4,j_5) \\
          &=\operatorname{Wigner6j}(j_2,j_3,j_1,j_5,j_6,j_4) \\
          &=\operatorname{Wigner6j}(j_3,j_2,j_1,j_6,j_5,j_4) \\
          &=\operatorname{Wigner6j}(j_1,j_3,j_2,j_4,j_6,j_5) \\
          &=\operatorname{Wigner6j}(j_2,j_1,j_3,j_5,j_4,j_6)
         \end{aligned}

    - They are invariant under the exchange of the upper and lower
      arguments in each of any two columns, i.e.

      .. math::

         \operatorname{Wigner6j}(j_1,j_2,j_3,j_4,j_5,j_6)
          =\operatorname{Wigner6j}(j_1,j_5,j_6,j_4,j_2,j_3)
          =\operatorname{Wigner6j}(j_4,j_2,j_6,j_1,j_5,j_3)
          =\operatorname{Wigner6j}(j_4,j_5,j_3,j_1,j_2,j_6)

    - additional 6 symmetries [Regge59]_ giving rise to 144 symmetries
      in total

    - only non-zero if any triple of `j`'s fulfill a triangle relation

    Algorithm
    =========

    This function uses the algorithm of [Edmonds74]_ to calculate the
    value of the 6j symbol exactly. Note that the formula contains
    alternating sums over large factorials and is therefore unsuitable
    for finite precision arithmetic and only useful for a computer
    algebra system [Rasch03]_.

    r#   )r   rN   )r,   r-   r.   j_4j_5j_6rF   r=   s           r   	wigner_6jrS     sE    x #cCi#o+,,
,c3S#sD112CJr    c
                 V   t          t          | |z   ||z   ||z             dz            }
|
dz  }d}t          |t          |
          dz   d          D ]U}||dz   t          | |||||dz  |	          z  t          ||||||dz  |	          z  t          | |||||dz  |	          z  z   }V|S )a?  
    Calculate the Wigner 9j symbol
    `\operatorname{Wigner9j}(j_1,j_2,j_3,j_4,j_5,j_6,j_7,j_8,j_9)`.

    Parameters
    ==========

    j_1, ..., j_9 :
        Integer or half integer.
    prec : precision, default
        ``None``. Providing a precision can
        drastically speed up the calculation.

    Returns
    =======

    Rational number times the square root of a rational number
    (if ``prec=None``), or real number if a precision is given.

    Examples
    ========

    >>> from sympy.physics.wigner import wigner_9j
    >>> wigner_9j(1,1,1, 1,1,1, 1,1,0, prec=64) # ==1/18
    0.05555555...

    >>> wigner_9j(1/2,1/2,0, 1/2,3/2,1, 0,1,1, prec=64) # ==1/6
    0.1666666...

    It is an error to have arguments that are not integer or half
    integer values or do not fulfill the triangle relation::

        sage: wigner_9j(0.5,0.5,0.5, 0.5,0.5,0.5, 0.5,0.5,0.5,prec=64)
        Traceback (most recent call last):
        ...
        ValueError: j values must be integer or half integer and fulfill the triangle relation
        sage: wigner_9j(1,1,1, 0.5,1,1.5, 0.5,1,2.5,prec=64)
        Traceback (most recent call last):
        ...
        ValueError: j values must be integer or half integer and fulfill the triangle relation

    Algorithm
    =========

    This function uses the algorithm of [Edmonds74]_ to calculate the
    value of the 3j symbol exactly. Note that the formula contains
    alternating sums over large factorials and is therefore unsuitable
    for finite precision arithmetic and only useful for a computer
    algebra system [Rasch03]_.
    r"   r   r   )r   r+   r   rN   )r,   r-   r.   rP   rQ   rR   j_7j_8j_9rF   r:   r9   r;   rM   s                 r   	wigner_9jrX     s    f s39cCis33a788D!8DFD#d))a-++ 9 926#sCc264889#sCc264889 #sCc264889 9 Mr    c                    t          |           | k    s&t          |          |k    st          |          |k    rt          d          t          |          |k    s&t          |          |k    st          |          |k    rt          d          | |z   |z   }|dz  }| |z   |z
  }	|	dk     rt          j        S | |z
  |z   }
|
dk     rt          j        S |  |z   |z   }|dk     rt          j        S |dz  rt          j        S ||z   |z   dk    rt          j        S t	          |          | k    s&t	          |          |k    st	          |          |k    rt          j        S t          | | z   |z   | |z   |z
  d          }t          ||z   | |z
  | |z   |z
            }t          | |z   |z   dz   |dz             }t          |           d| z  dz   d|z  dz   z  d|z  dz   z  t          | |z
           z  t          | |z            z  t          ||z
           z  t          ||z            z  t          ||z
           z  t          ||z            z  dt          z  z  }t          |          }t          t          |         t          || z
  |z            z  t          | |z
  |z            z  t          | |z   |z
           z            t          d|z  dz            z  t          || z
           t          ||z
           z  t          ||z
           z  z  }d}t          t          |          t          |          dz             D ]}t          |         t          ||z   | z
  |z
           z  t          ||z   |z
           z  t          | |z
  |z
           z  t          ||z   |z
  |z            z  t          | |z   |z
  |z
           z  }|t          d|z            |z  z   }||z  |z  t          d||z   |z   |z
  z            z  }||                    |          }|S )a*  
    Calculate the Gaunt coefficient.

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

    The Gaunt coefficient is defined as the integral over three
    spherical harmonics:

    .. math::

        \begin{aligned}
        \operatorname{Gaunt}(l_1,l_2,l_3,m_1,m_2,m_3)
        &=\int Y_{l_1,m_1}(\Omega)
         Y_{l_2,m_2}(\Omega) Y_{l_3,m_3}(\Omega) \,d\Omega \\
        &=\sqrt{\frac{(2l_1+1)(2l_2+1)(2l_3+1)}{4\pi}}
         \operatorname{Wigner3j}(l_1,l_2,l_3,0,0,0)
         \operatorname{Wigner3j}(l_1,l_2,l_3,m_1,m_2,m_3)
        \end{aligned}

    Parameters
    ==========

    l_1, l_2, l_3, m_1, m_2, m_3 :
        Integer.
    prec - precision, default: ``None``.
        Providing a precision can
        drastically speed up the calculation.

    Returns
    =======

    Rational number times the square root of a rational number
    (if ``prec=None``), or real number if a precision is given.

    Examples
    ========

    >>> from sympy.physics.wigner import gaunt
    >>> gaunt(1,0,1,1,0,-1)
    -1/(2*sqrt(pi))
    >>> gaunt(1000,1000,1200,9,3,-12).n(64)
    0.00689500421922113448...

    It is an error to use non-integer values for `l` and `m`::

        sage: gaunt(1.2,0,1.2,0,0,0)
        Traceback (most recent call last):
        ...
        ValueError: l values must be integer
        sage: gaunt(1,0,1,1.1,0,-1.1)
        Traceback (most recent call last):
        ...
        ValueError: m values must be integer

    Notes
    =====

    The Gaunt coefficient obeys the following symmetry rules:

    - invariant under any permutation of the columns

      .. math::
        \begin{aligned}
          Y(l_1,l_2,l_3,m_1,m_2,m_3)
          &=Y(l_3,l_1,l_2,m_3,m_1,m_2) \\
          &=Y(l_2,l_3,l_1,m_2,m_3,m_1) \\
          &=Y(l_3,l_2,l_1,m_3,m_2,m_1) \\
          &=Y(l_1,l_3,l_2,m_1,m_3,m_2) \\
          &=Y(l_2,l_1,l_3,m_2,m_1,m_3)
        \end{aligned}

    - invariant under space inflection, i.e.

      .. math::
          Y(l_1,l_2,l_3,m_1,m_2,m_3)
          =Y(l_1,l_2,l_3,-m_1,-m_2,-m_3)

    - symmetric with respect to the 72 Regge symmetries as inherited
      for the `3j` symbols [Regge58]_

    - zero for `l_1`, `l_2`, `l_3` not fulfilling triangle relation

    - zero for violating any one of the conditions: `l_1 \ge |m_1|`,
      `l_2 \ge |m_2|`, `l_3 \ge |m_3|`

    - non-zero only for an even sum of the `l_i`, i.e.
      `L = l_1 + l_2 + l_3 = 2n` for `n` in `\mathbb{N}`

    Algorithms
    ==========

    This function uses the algorithm of [Liberatodebrito82]_ to
    calculate the value of the Gaunt coefficient exactly. Note that
    the formula contains alternating sums over large factorials and is
    therefore unsuitable for finite precision arithmetic and only
    useful for a computer algebra system [Rasch03]_.

    Authors
    =======

    Jens Rasch (2009-03-24): initial version for Sage.
    zl values must be integerzm values must be integerr"   r   r      r#   )r   r$   r	   r%   r&   r'   r+   r   r   r   r   r   r   n)l_1l_2l_3r/   r0   r1   rF   sumLbigLr3   r4   r5   r9   r:   r6   r7   r8   rL   r;   r   r<   r=   s                         r   gauntra   V  sF   P 3xx3 5#c((c/ 5SXX_ 53444
3xx3 5#c((c/ 5SXX_ 534449s?D19D	sSB	Av v	sSB	Av v
c	B	Av vax vc	CA vC3 CHHsN C3 vtczC#c!1155DsSy#)S3Y_55D#)c/A%tax00G73w{q3w{+q3w{;#)(s346?c	6JK#)(s346?c	6JK 
2G 7mmGYt_ysS'AAsSy3/02;C#IO2LM N N!d(Q,  
4#:		4#:	
!*4#:!6
78F FCIIs4yy1}-- 4 4miS3(<==cCi"n%&(1#(S.(ABb3hns*+,.7c	C"8L.MN '2"*--33
F
V
#grtczC7G#7M.N&O&O
OC eeDkkJr    c                       e Zd Zd ZdS )Wigner3jc                 ^    t          d | j        D                       rt          | j         S | S )Nc              3   $   K   | ]}|j         V  d S N)	is_number).0objs     r   	<genexpr>z Wigner3j.doit.<locals>.<genexpr>  s$      22s}222222r    )allargsr>   )selfhintss     r   doitzWigner3j.doit  s5    22	22222 	di((Kr    N)__name__
__module____qualname__ro    r    r   rc   rc     s#            r    rc   c                    t          |           } t          |          }t          |          }t          |          }t          |          }t          |          }t          d          }d }t          j        ||z   z  t	          t          |||z   ||           |||| ||          z  dz  |dz  | dz  z
  |dz  z
  |z   | z
  |z
  z  |t          || z
            || z   f          z  S )a)  
    Returns dot product of rotational gradients of spherical harmonics.

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

    This function returns the right hand side of the following expression:

    .. math ::
        \vec{R}Y{_j^{p}} \cdot \vec{R}Y{_l^{m}} = (-1)^{m+p}
        \sum\limits_{k=|l-j|}^{l+j}Y{_k^{m+p}}  * \alpha_{l,m,j,p,k} *
        \frac{1}{2} (k^2-j^2-l^2+k-j-l)


    Arguments
    =========

    j, p, l, m .... indices in spherical harmonics (expressions or integers)
    theta, phi .... angle arguments in spherical harmonics

    Example
    =======

    >>> from sympy import symbols
    >>> from sympy.physics.wigner import dot_rot_grad_Ynm
    >>> theta, phi = symbols("theta phi")
    >>> dot_rot_grad_Ynm(3, 2, 2, 0, theta, phi).doit()
    3*sqrt(55)*Ynm(5, 2, theta, phi)/(11*sqrt(pi))

    kc           
          t          d| z  dz   d|z  dz   z  d|z  dz   z  dt          z  z            t          || |t          j        t          j        t          j                  z  t          || |||| |z
            z  S )Nr"   r   rZ   )r   r   rc   r	   r%   )lmjpru   s        r   alphazdot_rot_grad_Ynm.<locals>.alpha$  s|    QqSUQqSUOQqSU+QrT233Aq!&!&!&99:Aq!Q1--. 	.r    r"   )r   r
   r	   NegativeOner   r   r&   )ry   rz   rw   rx   thetaphiru   r{   s           r   dot_rot_grad_Ynmr     s    > 	

A

A

A

AENNE
#,,Cc

A. . .
 MQqS!CAqsE3(?(?%%!AaPQBRBR(RUV(V
Q$q!t)AqD.
1
Q
) "#S1XXqs!3%5 %5 5 5r    c           	      V   
  fdt          d z  dz             D             }t          d z  dz             }t          |          D ]\  }
t          |          D ]\  }t          
 z
   z
  g          }t	          d 
z
  g          }t          t           
z             t           
z
            z  t           z             z  t           z
            z            } 
fdt          ||dz             D             }	|t          |	 z  |||f<   t          |          S )u  Return the small Wigner d matrix for angular momentum J.

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

    J : An integer, half-integer, or SymPy symbol for the total angular
        momentum of the angular momentum space being rotated.
    beta : A real number representing the Euler angle of rotation about
        the so-called line of nodes. See [Edmonds74]_.

    Returns
    =======

    A matrix representing the corresponding Euler angle rotation( in the basis
    of eigenvectors of `J_z`).

    .. math ::
        \mathcal{d}_{\beta} = \exp\big( \frac{i\beta}{\hbar} J_y\big)

    The components are calculated using the general form [Edmonds74]_,
    equation 4.1.15.

    Examples
    ========

    >>> from sympy import Integer, symbols, pi, pprint
    >>> from sympy.physics.wigner import wigner_d_small
    >>> half = 1/Integer(2)
    >>> beta = symbols("beta", real=True)
    >>> pprint(wigner_d_small(half, beta), use_unicode=True)
    ⎡   ⎛β⎞      ⎛β⎞⎤
    ⎢cos⎜─⎟   sin⎜─⎟⎥
    ⎢   ⎝2⎠      ⎝2⎠⎥
    ⎢               ⎥
    ⎢    ⎛β⎞     ⎛β⎞⎥
    ⎢-sin⎜─⎟  cos⎜─⎟⎥
    ⎣    ⎝2⎠     ⎝2⎠⎦

    >>> pprint(wigner_d_small(2*half, beta), use_unicode=True)
    ⎡        2⎛β⎞              ⎛β⎞    ⎛β⎞           2⎛β⎞     ⎤
    ⎢     cos ⎜─⎟        √2⋅sin⎜─⎟⋅cos⎜─⎟        sin ⎜─⎟     ⎥
    ⎢         ⎝2⎠              ⎝2⎠    ⎝2⎠            ⎝2⎠     ⎥
    ⎢                                                        ⎥
    ⎢       ⎛β⎞    ⎛β⎞       2⎛β⎞      2⎛β⎞        ⎛β⎞    ⎛β⎞⎥
    ⎢-√2⋅sin⎜─⎟⋅cos⎜─⎟  - sin ⎜─⎟ + cos ⎜─⎟  √2⋅sin⎜─⎟⋅cos⎜─⎟⎥
    ⎢       ⎝2⎠    ⎝2⎠        ⎝2⎠       ⎝2⎠        ⎝2⎠    ⎝2⎠⎥
    ⎢                                                        ⎥
    ⎢        2⎛β⎞               ⎛β⎞    ⎛β⎞          2⎛β⎞     ⎥
    ⎢     sin ⎜─⎟        -√2⋅sin⎜─⎟⋅cos⎜─⎟       cos ⎜─⎟     ⎥
    ⎣         ⎝2⎠               ⎝2⎠    ⎝2⎠           ⎝2⎠     ⎦

    From table 4 in [Edmonds74]_

    >>> pprint(wigner_d_small(half, beta).subs({beta:pi/2}), use_unicode=True)
    ⎡ √2   √2⎤
    ⎢ ──   ──⎥
    ⎢ 2    2 ⎥
    ⎢        ⎥
    ⎢-√2   √2⎥
    ⎢────  ──⎥
    ⎣ 2    2 ⎦

    >>> pprint(wigner_d_small(2*half, beta).subs({beta:pi/2}),
    ... use_unicode=True)
    ⎡       √2      ⎤
    ⎢1/2    ──   1/2⎥
    ⎢       2       ⎥
    ⎢               ⎥
    ⎢-√2         √2 ⎥
    ⎢────   0    ── ⎥
    ⎢ 2          2  ⎥
    ⎢               ⎥
    ⎢      -√2      ⎥
    ⎢1/2   ────  1/2⎥
    ⎣       2       ⎦

    >>> pprint(wigner_d_small(3*half, beta).subs({beta:pi/2}),
    ... use_unicode=True)
    ⎡ √2    √6    √6   √2⎤
    ⎢ ──    ──    ──   ──⎥
    ⎢ 4     4     4    4 ⎥
    ⎢                    ⎥
    ⎢-√6   -√2    √2   √6⎥
    ⎢────  ────   ──   ──⎥
    ⎢ 4     4     4    4 ⎥
    ⎢                    ⎥
    ⎢ √6   -√2   -√2   √6⎥
    ⎢ ──   ────  ────  ──⎥
    ⎢ 4     4     4    4 ⎥
    ⎢                    ⎥
    ⎢-√2    √6   -√6   √2⎥
    ⎢────   ──   ────  ──⎥
    ⎣ 4     4     4    4 ⎦

    >>> pprint(wigner_d_small(4*half, beta).subs({beta:pi/2}),
    ... use_unicode=True)
    ⎡             √6            ⎤
    ⎢1/4   1/2    ──   1/2   1/4⎥
    ⎢             4             ⎥
    ⎢                           ⎥
    ⎢-1/2  -1/2   0    1/2   1/2⎥
    ⎢                           ⎥
    ⎢ √6                     √6 ⎥
    ⎢ ──    0    -1/2   0    ── ⎥
    ⎢ 4                      4  ⎥
    ⎢                           ⎥
    ⎢-1/2  1/2    0    -1/2  1/2⎥
    ⎢                           ⎥
    ⎢             √6            ⎥
    ⎢1/4   -1/2   ──   -1/2  1/4⎥
    ⎣             4             ⎦

    c                     g | ]}|z
  S rs   rs   rh   iJs     r   
<listcomp>z"wigner_d_small.<locals>.<listcomp>      ###1###r    r"   r   r   c                 
   g | ]}d z
  |z
  z  t          z   z
  |z
            z  t          z
  |          z  t          dz            d|z  z   z   z  z  t          dz            dz  d|z  z
  z
  z
  z  z  S )r#   r"   )r   r   r   )rh   sr   MiMjbetas     r   r   z"wigner_d_small.<locals>.<listcomp>  s     ; ; ;
  AbDF^adAbDF++,adA&&' a[[1Q3r6"9-. a[[1Q3qs72:b=1	2 ; ; ;r    )	r   r   	enumerater'   r+   r   r   r   r   )r   r   Mdr   ry   sigmamaxsigmamindijtermsr   r   s   ``        @@r   wigner_d_smallr   -  sh   d 	$###eAaCEll###Aac!eA1 & &2q\\ 	& 	&EAr RCFAbD>**HAqt9~~Hy2y26 2''027 8 8C; ; ; ; ; ; ;
 $Hhqj99; ; ;E #u+oAadGG	&" 1r    c                      t           |           fdt          d z  dz             D             fdt                    D             }t          |          S )u  Return the Wigner D matrix for angular momentum J.

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

    J :
        An integer, half-integer, or SymPy symbol for the total angular
        momentum of the angular momentum space being rotated.
    alpha, beta, gamma - Real numbers representing the Euler.
        Angles of rotation about the so-called vertical, line of nodes, and
        figure axes. See [Edmonds74]_.

    Returns
    =======

    A matrix representing the corresponding Euler angle rotation( in the basis
    of eigenvectors of `J_z`).

    .. math ::
        \mathcal{D}_{\alpha \beta \gamma} =
        \exp\big( \frac{i\alpha}{\hbar} J_z\big)
        \exp\big( \frac{i\beta}{\hbar} J_y\big)
        \exp\big( \frac{i\gamma}{\hbar} J_z\big)

    The components are calculated using the general form [Edmonds74]_,
    equation 4.1.12.

    Examples
    ========

    The simplest possible example:

    >>> from sympy.physics.wigner import wigner_d
    >>> from sympy import Integer, symbols, pprint
    >>> half = 1/Integer(2)
    >>> alpha, beta, gamma = symbols("alpha, beta, gamma", real=True)
    >>> pprint(wigner_d(half, alpha, beta, gamma), use_unicode=True)
    ⎡  ⅈ⋅α  ⅈ⋅γ             ⅈ⋅α  -ⅈ⋅γ         ⎤
    ⎢  ───  ───             ───  ─────        ⎥
    ⎢   2    2     ⎛β⎞       2     2      ⎛β⎞ ⎥
    ⎢ ℯ   ⋅ℯ   ⋅cos⎜─⎟     ℯ   ⋅ℯ     ⋅sin⎜─⎟ ⎥
    ⎢              ⎝2⎠                    ⎝2⎠ ⎥
    ⎢                                         ⎥
    ⎢  -ⅈ⋅α   ⅈ⋅γ          -ⅈ⋅α   -ⅈ⋅γ        ⎥
    ⎢  ─────  ───          ─────  ─────       ⎥
    ⎢    2     2     ⎛β⎞     2      2      ⎛β⎞⎥
    ⎢-ℯ     ⋅ℯ   ⋅sin⎜─⎟  ℯ     ⋅ℯ     ⋅cos⎜─⎟⎥
    ⎣                ⎝2⎠                   ⎝2⎠⎦

    c                     g | ]}|z
  S rs   rs   r   s     r   r   zwigner_d.<locals>.<listcomp>  r   r    r"   r   c                 X    g | ]$\  fd t                    D             %S )c                     g | ]E\  }}t          t          z  z            |f         z  t          t          |z  z            z  FS rs   )r   r   )rh   ry   r   r   r{   r   gammar   s      r   r   z'wigner_d.<locals>.<listcomp>.<listcomp>  s[     
% 
% 
%a ad5j//!AqD'
!#ad5j//
1 
% 
% 
%r    )r   )rh   r   r   r   r{   r   r   s    @@r   r   zwigner_d.<locals>.<listcomp>  sp     	@ 	@ 	@).B
% 
% 
% 
% 
% 
% 
% 
% ||
% 
% 
% 	@ 	@ 	@r    )r   r   r   r   )r   r{   r   r   Dr   r   s   `` ` @@r   wigner_dr     s    f 	q$A####eAaCEll###A	@ 	@ 	@ 	@ 	@ 	@ 	@2;A,,	@ 	@ 	@A1r    rf   ).__doc__sympy.concrete.summationsr   sympy.core.addr   sympy.core.functionr   sympy.core.numbersr   r   r   sympy.core.singletonr	   sympy.core.symbolr
   sympy.core.sympifyr   (sympy.functions.combinatorial.factorialsr   r   &sympy.functions.elementary.exponentialr   (sympy.functions.elementary.miscellaneousr   (sympy.functions.elementary.trigonometricr   r   +sympy.functions.special.spherical_harmonicsr   sympy.matrices.denser   sympy.matrices.immutabler   r   r   r>   r@   rG   rN   rS   rX   ra   rc   r   r   r   rs   r    r   <module>r      s=  . .^ * ) ) ) ) )       ( ( ( ( ( ( / / / / / / / / / / " " " " " " # # # # # # & & & & & & J J J J J J J J 6 6 6 6 6 6 9 9 9 9 9 9 ? ? ? ? ? ? ? ? ; ; ; ; ; ; & & & & & & 4 4 4 4 4 4 C	# # #BL L L^4 4 4n7 7 7 7tN N N Nb^ ^ ^ ^B; ; ; ;|[ [ [ [~    x   -5 -5 -5`F F FR7 7 7 7 7r    