
    \h                     n   S r SSKJr  SSKJr  SSKJr  SSKJr  SSKJ	r	J
r
JrJrJr  SSKJr  SSKJr  SS	KJr  SS
KJrJr  SSKJr  SSKJr  SSKJr  SSKJrJr  SSK J!r!  SSK"J#r#  SSK$J%r%  SSK&J'r'  S/r(S r)S r*S r+S r,S$S jr-S$S jr.S$S jr/S$S jr0S$S jr1S$S jr2 " S S \5      r3S! r4S" r5S# r6g)%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)
.. [Homeier96] 'Some Properties of the Coupling Coefficients of Real
  Spherical Harmonics and Their Relation to Gaunt Coefficients',
  H. H. H. Homeier and E. O. Steinborn J. Mol. Struct., Volume 368,
  pp. 31-37 (1996)

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

- Phil Adam LeMaitre (2022-09-19): added real Gaunt coefficient

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

    )Sum)Add)
int_valued)Function)FloatIIntegerpiRational)S)Dummy)sympify)binomial	factorial)re)exp)sqrt)cossin)Ynm)zeros)ImmutableMatrix)as_int   c                     U [        [        5      :  aN  [        [        [        5      [        U S-   5      5       H%  n[        R	                  [        US-
     U-  5        M'     [        S[        U 5      S-    $ )a  
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     L/var/www/auris/envauris/lib/python3.13/site-packages/sympy/physics/wigner.py_calc_factlistr$   N   s^    6 
S^IBF4BYrAv.34 5\c"gk""    c                    [        U [        5      (       a  U $ [        U 5      [        L a;  U R	                  5       (       a  [        U 5      $ SU -  R	                  5       (       a  U $ O[        U [
        5      (       aF  U R                  S:X  a  U R                  U R                  -  $ U R                  S:X  a  U R                  $ O)[        U [        5      (       a  [        [        U 5      5      $ [        SU -  5      e)z>return Python int unless value is half-int (then return float)   r   z)expecting integer or half-integer, got %s)
isinstancer   typefloat
is_integerr   qpr   _int_or_halfint
ValueError)values    r#   r.   r.   o   s    %	e	u:eG!!L "	E8	$	$77a<77577?"WW\77N 	E5	!	!uU|,,
@5H
IIr%   c                 h   [        [        [        [        XX#XE/5      5      u  pp#pEX4-   U-   S:w  a  [        R                  $ X-   U-
  nUS:  a  [        R                  $ X-
  U-   nUS:  a  [        R                  $ U * U-   U-   nUS:  a  [        R                  $ [        U5      U :  d  [        U5      U:  d  [        U5      U:  a  [        R                  $ [        X-
  5      (       a$  [        X-
  5      (       a  [        X%-
  5      (       d  [        R                  $ [        X-   U-   S-   U [        U5      -   U[        U5      -   U[        U5      -   5      n	[        [        U	5      5        [        [        [        X-   U-
  5         [        [        X-
  U-   5         -  [        [        U * U-   U-   5         -  [        [        X-
  5         -  [        [        X-   5         -  [        [        X-
  5         -  [        [        X-   5         -  [        [        X%-
  5         -  [        [        X%-   5         -  5      [        [        X-   U-   S-   5         -  n
[        U
5      nUR                  (       d  UR                  (       a  UR                  5       S   n[        U* U -   U-   U* U-   U-
  S5      n[!        X-   X-
  X-   U-
  5      nSn[#        [        U5      [        U5      S-   5       H  n[        U   [        [        X-   U -
  U-
  5         -  [        [        X-   U-
  5         -  [        [        X-
  U-
  5         -  [        [        X-   U-
  U-   5         -  [        [        X-   U-
  U-
  5         -  nU[        SU-  5      U-  -   nM     [        S[        X-
  U-
  5      -  5      nX-  U-  nU$ )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
     `m_1  \in \{-|j_1|, \ldots, |j_1|\}`,
     `m_2  \in \{-|j_2|, \ldots, |j_2|\}`,
     `m_3  \in \{-|j_3|, \ldots, |j_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
r   r   )mapr.   r   r   Zeroabsr   maxr$   r   r	   r   r   
is_complexis_infiniteas_real_imagminr   )j_1j_2j_3m_1m_2m_3a1a2a3maxfactargsqrtressqrtiminimaxsumresr"   denprefidress                      r#   	wigner_3jrM      s   t 	OSs*, 	- !Cc y3!vv	SB	Avvv	SB	Avvv
c	B	AvvvC3CHsNC3vvsy!!sy!!sy!!vv#)c/A%sSX~sSX~C."G3w< iCIO 45s39s?345sC4#:#3456 s39~./ s39~.	/
 s39~./ s39~./ s39~./ s39~./ 0 	#ci#o)*+	,G 7mGW00&&(+tczC#c!115Dsy#)SY_5DFCIs4y1}-mc"(S.3./01c#)b.)*+ c#(S.)*+ c"(S.3./0	1
 c#)c/B./01 '2"*-33 . bSS112F

V
#CJr%   c                     [        U 5      n [        U5      n[        U5      n[        U5      n[        U5      n[        U5      n[        XX#XE* 5      nSX-
  U-   -  [        SU-  S-   5      -  U-  $ )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
r2   r'   r   )r   rM   r   )r;   r<   r=   r>   r?   r@   ws          r#   clebsch_gordanrP     ss    d #,C
#,C
#,C
#,C
#,C
#,C#Cc40ACIO$tAGaK'881<<r%   Nc                    [        X-   U-
  5      (       d  [        S5      e[        X-   U-
  5      (       d  [        S5      e[        X-   U -
  5      (       d  [        S5      eX-   U-
  S:  a  [        R                  $ X-   U-
  S:  a  [        R                  $ X-   U -
  S:  a  [        R                  $ [	        X-   U-
  X-   U-
  X-   U -
  X-   U-   S-   5      n[        U5        [        [        [        X-   U-
  5         [        [        X-   U-
  5         -  [        [        X-   U -
  5         -  5      [        [        [        X-   U-   S-   5         5      -  n[        U5      nU(       a"  UR                  U5      R                  5       S   nU$ )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   r4   r6   r$   r	   r   r   r   evalfr9   )aabbccprecrD   rE   rF   s          r#   _big_delta_coeffrW   S  su   B bgl##effbgl##effbgl##eff
"vv
"vv
"vv"'B,"bglBGbL1<LMG7iBGbL 12s27R<012s27R<012 3 		#bglQ./012G
 7mG--%224Q7Nr%   c                    [        XXF5      [        X#XF5      -  [        XXV5      -  [        XXV5      -  nUS:X  a  [        R                  $ [        X-   U-   X#-   U-   X-   U-   X-   U-   5      n[	        X-   U-   U-   X-   U-   U-   X-   U-   U-   5      n	[        U	S-   X-   U-   U-   X-   U-   U-   X-   U-   U-   5      n
[        U
5        Sn[        [        U5      [        U	5      S-   5       H  n[        [        X-
  U-
  U-
  5         [        [        X-
  U-
  U-
  5         -  [        [        X-
  U-
  U-
  5         -  [        [        X-
  U-
  U-
  5         -  [        [        X-   U-   U-   U-
  5         -  [        [        X-   U-   U-   U-
  5         -  [        [        X-   U-   U-   U-
  5         -  nU[        SU-  [        US-      -  5      U-  -   nM     X{-  S[        X-   U-   U-   5      -  -  nU$ )aC  
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   r2   )
rW   r   r4   r6   r:   r$   r   r   r   r	   )rS   rT   rU   ddeeffrV   prefacrG   rH   rD   rI   kkrJ   rL   s                  r#   racahr^     s:   j bb/*+*+ 	*+F {vvrw|RWr\27R<2FDrw|b "'B,"3RWr\B5FGD$(BGbL2-rw|b/@2"$G7FCIs4y1}-BGbL2-./c"'B,+,-.c"'B,+,-. c"'B,+,-. c"'B,+b012	3
 c"'B,+b0123 c"'B,+b0123 '2"*ya/@"@ACGG . /RC"r(9$::
:CJr%   c           
      ~    [        [        XX#XE/5      u  pp#pES[        X-   U-   U-   5      -  [        XXCX%U5      -  nU$ )aR
  
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::

     \begin{aligned}
     \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)
     \end{aligned}

- 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]_.

r2   )r3   r   r   r^   )r;   r<   r=   j_4j_5j_6rV   rL   s           r#   	wigner_6jrc     sT    | $'w3S.$0 Cc#ci#o+,
,c#D12CJr%   c
                 B   [        [        XX#XEXgU/	5      u	  pp#pEpgn[        [        X-   X-   X7-   5      S-  5      n
U
S-  nSn[	        U[        U
5      S-   S5       H@  nXS-   [        XXX-S-  U	5      -  [        X5XqXMS-  U	5      -  [        XXXmS-  U	5      -  -   nMB     U$ )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)
0.05555555555555555555555555555555555555555555555555555555555555555

>>> wigner_9j(1/2,1/2,0, 1/2,3/2,1, 0,1,1, prec=64)
0.1666666666666666666666666666666666666666666666666666666666666667

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   )r3   r   r   r:   r   r^   )r;   r<   r=   r`   ra   rb   j_7j_8j_9rV   rH   rG   rI   r]   s                 r#   	wigner_9jrh   E  s    f 36g3Ss=3?/Cc#Cs39ci3a78D!8DFD#d)a-+6#Cc6489#Cc6489 #Cc6489 9 ,
 Mr%   c                    XX#XE4 Vs/ s H  n[        U5      PM     snu  pp#pEX-   U-
  S:  a  [        R                  $ X-
  U-   S:  a  [        R                  $ U * U-   U-   S:  a  [        R                  $ X4-   U-   S:w  a  [        R                  $ [        U5      U :  d  [        U5      U:  d  [        U5      U:  a  [        R                  $ [	        X-   U-   S5      u  pU	S-  (       a  [        R                  $ [        U* U -   U-   U* U-   U-
  S5      n
[        X-   X-
  X-   U-
  5      n[        [        X-   U-   S-   US-   5      5        [        SU -  S-   SU-  S-   -  SU-  S-   -  [        X-
     -  [        X-      -  [        X-
     -  [        X-      -  [        X%-
     -  [        X%-      -  S[        -  -  5      n[        [        U   [        X-
  U-      -  [        X-
  U-      -  [        X-   U-
     -  5      [        SU-  S-      -  [        X-
     [        X-
     -  [        X-
     -  -  nSn[        [        U
5      [        U5      S-   5       Ht  n[        U   [        X-   U -
  U-
     -  [        X-   U-
     -  [        X-
  U-
     -  [        X-   U-
  U-      -  [        X-   U-
  U-
     -  nU[        SU-  5      U-  -   nMv     X-  U-  [        SX-   U-   U-
  -  5      -  nUb  UR                  U5      nU$ s  snf )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.006895004219221134484332976156744208248842039317638217822322799675

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.
r   r'   r      r2   )r   r   r4   r5   divmodr6   r:   r$   r   r   r
   r	   r   r   n)l_1l_2l_3r>   r?   r@   rV   ibigLremLrG   rH   rF   r\   rI   r"   rJ   rL   s                     r#   gauntrs     s\   R  c9$;9aq	9$; Cc y3vv
y3vvtczC!vv	CAvvC3CHsNC3vv	C+JDaxvvtczC#c!115Dsy#)SY_5D3sy3*D1H56AGaKAGaK0AGaK@#)(346?	6JK#)(346?	6JK 
2 G
 Yt_yS'AAsy3/02;CIO2LM N!d(Q,  
4:		4:	
!*4:!6
78F FCIs4y1}-mi3(<==ci"n%&(1#(S.(ABbhns*+,.7	C"8L.MN '2"*-33	 . 
V
#grtzC7G#7M.N&O
OCeeDkJW$;s   Lc                   ^^^^^ XX#XE4 Vs/ s H  n[        U5      PM     snu  pp#pE[        S X4U4 5       5      S-  (       a  [        R                  $ X-   U-   S-  (       a  [        R                  $ X-   n[	        [        X-
  5      [        [        XE-   5      [        XE-
  5      5      5      n	X-   S-  (       a  U	S-  n	U	[        XS-
  S5      ;  a  [        R                  $ S mS mS mUUU4S jmUUU4S	 jmUUU4S
 jn
Sn[        U * U S-   5       HZ  nU
" X<5      n[        U* US-   5       H;  nU
" XN5      nU
" X\* U-
  5      nU[        X-  U-  5      [        XX,X* U-
  US9-  -   nM=     M\     U$ s  snf )aA  
Calculate the real Gaunt coefficient.

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

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

.. math::
    \begin{aligned}
    \operatorname{RealGaunt}(l_1,l_2,l_3,\mu_1,\mu_2,\mu_3)
    &=\int Z^{\mu_1}_{l_1}(\Omega)
     Z^{\mu_2}_{l_2}(\Omega) Z^{\mu_3}_{l_3}(\Omega) \,d\Omega \\
    \end{aligned}

Alternatively, it can be defined in terms of the standard Gaunt
coefficient by relating the real spherical harmonics to the standard
spherical harmonics via a unitary transformation `U`, i.e.
`Z^{\mu}_{l}(\Omega)=\sum_{m'}U^{\mu}_{m'}Y^{m'}_{l}(\Omega)` [Homeier96]_.
The real Gaunt coefficient is then defined as

.. math::
    \begin{aligned}
    \operatorname{RealGaunt}(l_1,l_2,l_3,\mu_1,\mu_2,\mu_3)
    &=\int Z^{\mu_1}_{l_1}(\Omega)
     Z^{\mu_2}_{l_2}(\Omega) Z^{\mu_3}_{l_3}(\Omega) \,d\Omega \\
    &=\sum_{m'_1 m'_2 m'_3} U^{\mu_1}_{m'_1}U^{\mu_2}_{m'_2}U^{\mu_3}_{m'_3}
     \operatorname{Gaunt}(l_1,l_2,l_3,m'_1,m'_2,m'_3)
    \end{aligned}

The unitary matrix `U` has components

.. math::
    \begin{aligned}
    U^\mu_{m} = \delta_{|\mu||m|}*(\delta_{m0}\delta_{\mu 0} + \frac{1}{\sqrt{2}}\big[\Theta(\mu)\big(\delta_{m\mu}+(-1)^{m}\delta_{m-\mu}\big)
    +i \Theta(-\mu)\big((-1)^{m}\delta_{m\mu}-\delta_{m-\mu}\big)\big])
    \end{aligned}


where `\delta_{ij}` is the Kronecker delta symbol and `\Theta` is a step
function defined as

.. math::
    \begin{aligned}
    \Theta(x) = \begin{cases} 1 \,\text{for}\, x > 0 \\ 0 \,\text{for}\, x \leq 0 \end{cases}
    \end{aligned}

Parameters
==========

l_1, l_2, l_3, mu_1, mu_2, mu_3 :
    Integer degree and order

prec - precision, default: ``None``.
    Providing a precision can
    drastically speed up the calculation.

Returns
=======

Rational number times the square root of a rational number.

Examples
========
>>> from sympy.physics.wigner import real_gaunt
>>> real_gaunt(1,1,2,-1,1,-2)
sqrt(15)/(10*sqrt(pi))
>>> real_gaunt(10,10,20,-9,-9,0,prec=64)
-0.00002480019791932209313156167176797577821140084216297395518482071448

It is an error to use non-integer values for `l` and `\mu`::
    real_gaunt(2.8,0.5,1.3,0,0,0)
    Traceback (most recent call last):
    ...
    ValueError: l values must be integer

    real_gaunt(2,2,4,0.7,1,-3.4)
    Traceback (most recent call last):
    ...
    ValueError: mu values must be integer

Notes
=====

The real Gaunt coefficient inherits from the standard Gaunt coefficient,
the invariance under any permutation of the pairs `(l_i, \mu_i)` and the
requirement that the sum of the `l_i` be even to yield a non-zero value.
It also obeys the following symmetry rules:

- zero for `l_1`, `l_2`, `l_3` not fulfilling the condition
  `l_1 \in \{l_{\text{max}}, l_{\text{max}}-2, \ldots, l_{\text{min}}\}`,
  where `l_{\text{max}} = l_2+l_3`,

  .. math::
      \begin{aligned}
      l_{\text{min}} = \begin{cases} \kappa(l_2, l_3, \mu_2, \mu_3) & \text{if}\,
      \kappa(l_2, l_3, \mu_2, \mu_3) + l_{\text{max}}\, \text{is even} \\
      \kappa(l_2, l_3, \mu_2, \mu_3)+1 & \text{if}\, \kappa(l_2, l_3, \mu_2, \mu_3) +
      l_{\text{max}}\, \text{is odd}\end{cases}
      \end{aligned}

  and `\kappa(l_2, l_3, \mu_2, \mu_3) = \max{\big(|l_2-l_3|, \min{\big(|\mu_2+\mu_3|,
  |\mu_2-\mu_3|\big)}\big)}`

- zero for an odd number of negative `\mu_i`

Algorithms
==========

This function uses the algorithms of [Homeier96]_ and [Rasch03]_ to
calculate the value of the real Gaunt coefficient exactly. Note that
the formula used in [Rasch03]_ contains alternating sums over large
factorials and is therefore unsuitable for finite precision arithmetic
and only useful for a computer algebra system [Rasch03]_. However, this
function can in principle use any algorithm that computes the Gaunt
coefficient, so it is suitable for finite precision arithmetic in so far
as the algorithm which computes the Gaunt coefficient is.
c              3   4   #    U  H  oS :  d  M
  Sv   M     g7f)r   r   N ).0rp   s     r#   	<genexpr>real_gaunt.<locals>.<genexpr>  s     
2(E11(s   		r'   r   c                     X:X  a  S$ S$ )Nr   r   rv   )rp   js     r#   <lambda>real_gaunt.<locals>.<lambda>  s    A.Q.r%   c                     U S-  (       a  S$ S$ )Nr'   r2   r   rv   )es    r#   r}   r~     s    A"$1$r%   c                     U S:  a  S$ S$ )Nr   r   rv   )xs    r#   r}   r~     s    q1u!#!#r%   c                 N   > T" U * 5      T" U5      T" X5      -  T" X* 5      -
  -  $ Nrv   mumkron_delsts     r#   r}   r~     s)    af!x 6!S9I IJr%   c                 L   > T" U 5      T" X5      T" U5      T" X* 5      -  -   -  $ r   rv   r   s     r#   r}   r~     s'    aex1C8H1HHIr%   c                    > T" [        U 5      [        U5      5      T" U S5      T" US5      -  T" X5      [        T" X5      -  -   [        S5      -  -   -  $ )Nr   r'   )r5   r   r   )r   r   ABr   s     r#   r}   r~     sU    hs2wA/8B?XaQR^3SWXY[W_bcfghjfnbnWnptuvpwVw3wxr%   r   )rV   )
r   sumr   r4   r6   r5   r:   r   r   rs   )rm   rn   ro   mu_1mu_2mu_3rV   rp   lmaxlminUugntm1U1m2U2U3r   r   r   r   r   s                    @@@@@r#   
real_gauntr     sr   r  c<'><aq	<'>#Cc 
2t4(
22Q6vv	C1vv9Ds39~s3t{#3S5EFGDq	5ax,,vv.H$A#AJAIAxADSD#a% t[c!e$B4B4BB"RU2X,uSsC"HSW'XXXD % ! K?'>s   E6c                       \ rS rSrS rSrg)Wigner3ji  c                 l    [        S U R                   5       5      (       a  [        U R                  6 $ U $ )Nc              3   8   #    U  H  oR                   v   M     g 7fr   )	is_number)rw   objs     r#   rx    Wigner3j.doit.<locals>.<genexpr>  s     2	}}	s   )allargsrM   )selfhintss     r#   doitWigner3j.doit  s+    2		222dii((Kr%   rv   N)__name__
__module____qualname____firstlineno__r   __static_attributes__rv   r%   r#   r   r     s    r%   r   c                 r   [        U 5      n [        U5      n[        U5      n[        U5      n[        U5      n[        U5      n[        S5      nS n[        R                  X1-   -  [	        [        XcU-   XE5      U" X#XU5      -  S-  US-  U S-  -
  US-  -
  U-   U -
  U-
  -  U[        X -
  5      X -   45      -  $ )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U -  S-   SU-  S-   -  SU-  S-   -  S[        -  -  5      [        X U[        R                  [        R                  [        R                  5      -  [        X XCX* U-
  5      -  $ )Nr'   r   rj   )r   r
   r   r   r4   )lr   r|   r-   r   s        r#   alphadot_rot_grad_Ynm.<locals>.alpha  sq    QqSUQqSUOQqSU+QrT23q!&&!&&!&&9:qQ1-. 	.r%   r'   )r   r   r   NegativeOner   r   r5   )r|   r-   r   r   thetaphir   r   s           r#   dot_rot_grad_Ynmr     s    > 	
A
A
A
AENE
#,Cc
A.
 MMQS!CAsE(?%APQBR(RUV(V
Q$q!t)AqD.
1
Q
) "#SXqs!3%5 5 5r%   c                    [        SU -  S-   5       Vs/ s H  o U-
  PM	     nn[        SU -  S-   5      n[        U5       GH  u  p%[        U5       H  u  pg[        X-
  X-
  /5      n[	        SU* U-
  /5      n	[        [        X-   5      [        X-
  5      -  [        X-   5      -  [        X-
  5      -  5      n
[        XS-   5       Vs/ s Hl  nSX-
  U-
  -  [        X-   X-
  U-
  5      -  [        X-
  U5      -  [        US-  5      SU-  U-   U-   -  -  [        US-  5      SU -  SU-  -
  U-
  U-
  -  -  PMn     nnU
[        U6 -  XBU4'   M     GM     [        U5      $ s  snf s  snf )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)

such that

.. math ::
    d^{(J)}_{m',m}(\beta) = \mathtt{wigner\_d\_small(J,beta)[J-mprime,J-m]}

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             ⎦

r'   r   r   r2   )r   r   	enumerater:   r6   r   r   r   r   r   r   r   )Jbetarp   MdMir|   Mjsigmamaxsigmamindijr   termss                r#   wigner_d_smallr     s   n AaCEl#l1lA#ac!eA 1q\EA AD!$<(HAs2v;'Hyy6 ''07 8C $Hqj9;
 : ADF^adADF+,adA&' a[1Q3r6"9-. a[1Q3qs72:b=1	2
 :  ; #u+oAdG " $ 1- 	$;s   E A3E%c                 |   [        X5      n[        SU -  S-   5       Vs/ s H  oPU-
  PM	     nn[        U5       VVVV	s/ s HU  u  pW[        U5       VV	s/ s H7  u  p[        [        U-  U-  5      XEU4   -  [        [        U	-  U-  5      -  PM9     sn	nPMW     n
nnnn	[        U
5      $ s  snf s  sn	nf s  sn	nnnf )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 figure axis, line of nodes,
    and vertical. See [Edmonds74]_, however note that the symbols alpha
    and gamma are swapped in this implementation.

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)

such that

.. math ::
    \mathcal{D}^{(J)}_{m',m}(\alpha, \beta, \gamma) =
    \mathtt{wigner_d(J, alpha, beta, gamma)[J-mprime,J-m]}

The components are calculated using the general form [Edmonds74]_,
equation 4.1.12, however note that the angles alpha and gamma are swapped
in this implementation.

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⎠⎦

r'   r   )r   r   r   r   r   r   )r   r   r   gammar   rp   r   r   r|   r   Ds              r#   wigner_dr   }  s    v 	qAAaCEl#l1lA# 3<A,	@2>y|
%#ea ad5j/!qD'
!#ad5j/
1#
%2>  	@1	 	$
% 	@s   B+B6
>B0B6
0B6
r   )7__doc__sympy.concrete.summationsr   sympy.core.addr   sympy.core.numbersr   sympy.core.functionr   r   r   r	   r
   r   sympy.core.singletonr   sympy.core.symbolr   sympy.core.sympifyr   (sympy.functions.combinatorial.factorialsr   r   $sympy.functions.elementary.complexesr   &sympy.functions.elementary.exponentialr   (sympy.functions.elementary.miscellaneousr   (sympy.functions.elementary.trigonometricr   r   +sympy.functions.special.spherical_harmonicsr   sympy.matrices.denser   sympy.matrices.immutabler   sympy.utilities.miscr   r   r$   r.   rM   rP   rW   r^   rc   rh   rs   r   r   r   r   r   rv   r%   r#   <module>r      s   4j *  ) ( @ @ " # & J 3 6 9 ? ; & 4 ' C	#BJ&Pf;=|9xNbbJ=@SlWtx -5`M`@r%   