o
    à8Va–z  ã                   @   sÐ   d Z ddlmZmZmZmZmZmZmZm	Z	m
Z
mZmZmZmZmZmZmZmZmZ dgZdd„ Zdd„ Zdd	„ Zddd„Zddd„Zddd„Zddd„Zddd„ZG dd„ deƒZdd„ Zdd„ Zd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>

é    )ÚIntegerÚpiÚsqrtÚsympifyÚDummyÚSÚSumÚYnmÚzerosÚFunctionÚsinÚcosÚexpÚIÚ	factorialÚbinomialÚAddÚImmutableMatrixé   c                 C   sR   | t tƒkrtt tƒt| d ƒƒD ]}t t|d  | ¡ qtdt| ƒd … 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Úii© r   ú6/usr/lib/python3/dist-packages/sympy/physics/wigner.pyÚ_calc_factlist:   s   r   c                 C   sB  t | d ƒ| d kst |d ƒ|d kst |d ƒ|d kr"tdƒ‚t |d ƒ|d ks@t |d ƒ|d ks@t |d ƒ|d krDtdƒ‚|| | dkrNdS tdt | | | ƒ ƒ}| }| | | }|dk ridS | | | }|dk rudS |  | | }	|	dk r‚dS t|ƒ| ks”t|ƒ|ks”t|ƒ|kr–dS t| | | d | t|ƒ |t|ƒ |t|ƒ ƒ}
tt |
ƒƒ ttt | | | ƒ tt | | | ƒ  tt |  | | ƒ  tt | | ƒ  tt | | ƒ  tt || ƒ  tt || ƒ  tt || ƒ  tt || ƒ  ƒtt | | | d ƒ  }t|ƒ}|js|j	r$| 
¡ d }t| |  | | | | dƒ}t|| | | | | | ƒ}d}tt |ƒt |ƒd ƒD ]I}t| tt || |  | ƒ  tt || | ƒ  tt | | | ƒ  tt || | | ƒ  tt | | | | ƒ  }|td| ƒ|  }qO|| | }|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   ÚabsÚmaxr   r   r   Z
is_complexZis_infiniteÚas_real_imagÚminr   )Új_1Új_2Új_3Úm_1Úm_2Úm_3ZprefidÚa1Úa2Úa3ÚmaxfactÚargsqrtÚressqrtÚiminÚimaxÚsumresr   ÚdenÚresr   r   r   Ú	wigner_3j[   s†   (Vÿ(ÿ$$
ÿÿþýüûúùø	÷ ÿþýüûr7   c                 C   s<   dt | | | ƒ td| d ƒ t| ||||| ƒ }|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   r7   )r&   r'   r(   r)   r*   r+   r6   r   r   r   Úclebsch_gordanê   s   "2ÿr8   Nc                 C   s`  t | | | ƒ| | | krtdƒ‚t | | | ƒ| | | kr$tdƒ‚t || |  ƒ|| |  kr6tdƒ‚| | | dk r@dS | | | dk rJdS || |  dk rTdS t| | | | | | || |  | | | d ƒ}t|ƒ ttt | | | ƒ tt | | | ƒ  tt || |  ƒ  ƒttt | | | d ƒ ƒ }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   Zevalfr$   )ÚaaÚbbÚccÚprecr/   r0   r1   r   r   r   Ú_big_delta_coeff!  s2   2ÿþýr=   c                 C   sø  t | |||ƒt ||||ƒ t | |||ƒ t ||||ƒ }|dkr"dS t| | | || | | | | || | ƒ}t| | | | | | | | || | | ƒ}	t|	d | | | | | | | | || | | ƒ}
t|
ƒ d}tt|ƒt|	ƒd ƒD ]l}tt||  | | ƒ tt|| | | ƒ  tt||  | | ƒ  tt|| | | ƒ  tt| | | | | ƒ  tt| | | | | ƒ  tt|| | | | ƒ  }|td| t|d   ƒ|  }q}|| dt| | | | ƒ  }|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    )r=   r#   r%   r   r   r   r   r   )r9   r:   r;   ÚddÚeeZffr<   Úprefacr2   r3   r/   r4   Úkkr5   r6   r   r   r   Úracah[  sB   5ÿþý.0$ÿÿþýüûú" rB   c              	   C   s0   dt | | | | ƒ t| ||||||ƒ }|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   rB   )r&   r'   r(   Új_4Új_5Új_6r<   r6   r   r   r   Ú	wigner_6j¬  s   \ÿrF   c
                 C   sž   t t| | || || ƒd ƒ}
|
d }d}t|t |
ƒd dƒD ],}||d t| |||||d |	ƒ t||||||d |	ƒ t| |||||d |	ƒ  }q |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   rB   )r&   r'   r(   rC   rD   rE   Zj_7Zj_8Zj_9r<   r3   r2   r4   rA   r   r   r   Ú	wigner_9j  s    3ÿþýrG   c                 C   s  t | ƒ| kst |ƒ|kst |ƒ|krtdƒ‚t |ƒ|ks(t |ƒ|ks(t |ƒ|kr,tdƒ‚| | | }|d }| | | }	|	dk rBdS | | | }
|
dk rNdS |  | | }|dk r[dS |d radS || | dkrkdS t|ƒ| ks}t|ƒ|ks}t|ƒ|krdS t| |  | | | | dƒ}t|| | | | | | ƒ}t| | | d |d ƒ}t|ƒ d|  d d| d  d| d  t| |   t| |   t||   t||   t||   t||   dt  }t|ƒ}t	t| t||  |   t| | |   t| | |   ƒtd| d   t||   t||   t||    }d}t
t |ƒt |ƒd ƒD ]?}t| t|| |  |   t|| |   t| | |   t|| | |   t| | | |   }|t	d| ƒ|  }q1|| | t	d|| | |  ƒ }|dur| |¡}|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    N)r   r!   r"   r#   r%   r   r   r   r   r   r   Ún)Zl_1Zl_2Zl_3r)   r*   r+   r<   ZsumLZbigLr,   r-   r.   r2   r3   r/   r0   r1   r@   r4   r   r5   r6   r   r   r   ÚgauntK  sŒ   $h$$ "
ÿ
ÿ
ÿ
þ
þ
þýÿÿþ

ÿ
ÿýÿÿþþ$

rJ   c                   @   s   e Zd Zdd„ ZdS )ÚWigner3jc                 K   s"   t dd„ | jD ƒƒrt| jŽ S | S )Nc                 s   s    | ]}|j V  qd S ©N)Z	is_number)Ú.0Úobjr   r   r   Ú	<genexpr>í  s   € z Wigner3j.doit.<locals>.<genexpr>)ÚallÚargsr7   )ÚselfZhintsr   r   r   Údoitì  s   
zWigner3j.doitN)Ú__name__Ú
__module__Ú__qualname__rS   r   r   r   r   rK   ê  s    rK   c              	   C   s°   t | ƒ} t |ƒ}t |ƒ}t |ƒ}t |ƒ}t |ƒ}tdƒ}dd„ }tj||  tt||| ||ƒ|||| ||ƒ d |d | d  |d  | |  |  |t||  ƒ||  fƒ 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              	   S   s`   t d|  d d| d  d| d  dt  ƒt|| |tjtjtjƒ t|| |||| | ƒ S )Nr   r   rH   )r   r   rK   r   ZZero)ÚlÚmÚjÚprW   r   r   r   Úalpha  s
   .ÿþzdot_rot_grad_Ynm.<locals>.alphar   )r   r   r   ZNegativeOner   r	   r"   )rZ   r[   rX   rY   ZthetaZphirW   r\   r   r   r   Údot_rot_grad_Ynmò  s   2"ÿÿr]   c           
         sê   ‡ fdd„t dˆ  d ƒD ƒ}tdˆ  d ƒ}t|ƒD ]U\}‰t|ƒD ]L\}‰tˆ ˆ ˆ ˆ gƒ}tdˆ ˆ gƒ}ttˆ ˆ ƒtˆ ˆ ƒ tˆ ˆ ƒ tˆ ˆ ƒ ƒ}‡ ‡‡‡fdd„t ||d ƒD ƒ}	|t|	Ž  |||f< q#q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 | ]}ˆ | ‘qS r   r   ©rM   Úi©ÚJr   r   Ú
<listcomp>”  ó    z"wigner_d_small.<locals>.<listcomp>r   r   r   c                    s€   g | ]<}d ˆ ˆ |  t ˆ ˆ ˆ ˆ | ƒ t ˆ ˆ |ƒ tˆd ƒd| ˆ ˆ   tˆd ƒdˆ  d|  ˆ ˆ   ‘qS )r    r   )r   r   r   )rM   Ús©rb   ÚMiÚMjÚbetar   r   rc   Ÿ  s    ûÿþý"ü)	r   r
   Ú	enumerater#   r%   r   r   r   r   )
rb   ri   ÚMÚdr`   rZ   ZsigmamaxZsigmaminZdijZtermsr   rf   r   Úwigner_d_small"  s"   r
ÿ
ÿûñrm   c                    sL   t ˆ |ƒ‰‡ fdd„tdˆ  d ƒD ƒ‰‡‡‡‡fd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                    r^   r   r   r_   ra   r   r   rc   ß  rd   zwigner_d.<locals>.<listcomp>r   r   c                    s.   g | ]\‰‰ ‡ ‡‡‡‡fd d„t ˆƒD ƒ‘qS )c                    s<   g | ]\}}t tˆ  ˆ ƒˆˆ|f  t t| ˆ ƒ ‘qS r   )r   r   )rM   rZ   rh   )rg   r\   rl   Úgammar`   r   r   rc   à  s    0ÿz'wigner_d.<locals>.<listcomp>.<listcomp>)rj   )rM   )rk   r\   rl   rn   )rg   r`   r   rc   à  s
    ÿ
ÿ)rm   r   rj   r   )rb   r\   ri   rn   ÚDr   )rb   rk   r\   rl   rn   r   Úwigner_d«  s   
3ÿrp   rL   )!Ú__doc__Zsympyr   r   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r7   r8   r=   rB   rF   rG   rJ   rK   r]   rm   rp   r   r   r   r   Ú<module>   s$   P/! 
7
:
Q
a
>  0 
