
    g*                         d dl mZ d dlmZmZ d dlmZmZ d dlm	Z	 d dl
mZ d dlmZ d dlmZ d dlmZmZ d d	lmZ d d
lmZ d dlmZmZmZ  ed          Z G d de          Zd Z G d de          ZdS )    )Expr)FunctionArgumentIndexError)Ipi)S)Dummy)assoc_legendre)	factorial)Abs	conjugate)exp)sqrt)sincoscotxc                   \    e Zd ZdZed             Zd ZddZd Zd Z	d Z
d	 ZddZd ZdS )Ynma4  
    Spherical harmonics defined as

    .. math::
        Y_n^m(\theta, \varphi) := \sqrt{\frac{(2n+1)(n-m)!}{4\pi(n+m)!}}
                                  \exp(i m \varphi)
                                  \mathrm{P}_n^m\left(\cos(\theta)\right)

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

    ``Ynm()`` gives the spherical harmonic function of order $n$ and $m$
    in $\theta$ and $\varphi$, $Y_n^m(\theta, \varphi)$. The four
    parameters are as follows: $n \geq 0$ an integer and $m$ an integer
    such that $-n \leq m \leq n$ holds. The two angles are real-valued
    with $\theta \in [0, \pi]$ and $\varphi \in [0, 2\pi]$.

    Examples
    ========

    >>> from sympy import Ynm, Symbol, simplify
    >>> from sympy.abc import n,m
    >>> theta = Symbol("theta")
    >>> phi = Symbol("phi")

    >>> Ynm(n, m, theta, phi)
    Ynm(n, m, theta, phi)

    Several symmetries are known, for the order:

    >>> Ynm(n, -m, theta, phi)
    (-1)**m*exp(-2*I*m*phi)*Ynm(n, m, theta, phi)

    As well as for the angles:

    >>> Ynm(n, m, -theta, phi)
    Ynm(n, m, theta, phi)

    >>> Ynm(n, m, theta, -phi)
    exp(-2*I*m*phi)*Ynm(n, m, theta, phi)

    For specific integers $n$ and $m$ we can evaluate the harmonics
    to more useful expressions:

    >>> simplify(Ynm(0, 0, theta, phi).expand(func=True))
    1/(2*sqrt(pi))

    >>> simplify(Ynm(1, -1, theta, phi).expand(func=True))
    sqrt(6)*exp(-I*phi)*sin(theta)/(4*sqrt(pi))

    >>> simplify(Ynm(1, 0, theta, phi).expand(func=True))
    sqrt(3)*cos(theta)/(2*sqrt(pi))

    >>> simplify(Ynm(1, 1, theta, phi).expand(func=True))
    -sqrt(6)*exp(I*phi)*sin(theta)/(4*sqrt(pi))

    >>> simplify(Ynm(2, -2, theta, phi).expand(func=True))
    sqrt(30)*exp(-2*I*phi)*sin(theta)**2/(8*sqrt(pi))

    >>> simplify(Ynm(2, -1, theta, phi).expand(func=True))
    sqrt(30)*exp(-I*phi)*sin(2*theta)/(8*sqrt(pi))

    >>> simplify(Ynm(2, 0, theta, phi).expand(func=True))
    sqrt(5)*(3*cos(theta)**2 - 1)/(4*sqrt(pi))

    >>> simplify(Ynm(2, 1, theta, phi).expand(func=True))
    -sqrt(30)*exp(I*phi)*sin(2*theta)/(8*sqrt(pi))

    >>> simplify(Ynm(2, 2, theta, phi).expand(func=True))
    sqrt(30)*exp(2*I*phi)*sin(theta)**2/(8*sqrt(pi))

    We can differentiate the functions with respect
    to both angles:

    >>> from sympy import Ynm, Symbol, diff
    >>> from sympy.abc import n,m
    >>> theta = Symbol("theta")
    >>> phi = Symbol("phi")

    >>> diff(Ynm(n, m, theta, phi), theta)
    m*cot(theta)*Ynm(n, m, theta, phi) + sqrt((-m + n)*(m + n + 1))*exp(-I*phi)*Ynm(n, m + 1, theta, phi)

    >>> diff(Ynm(n, m, theta, phi), phi)
    I*m*Ynm(n, m, theta, phi)

    Further we can compute the complex conjugation:

    >>> from sympy import Ynm, Symbol, conjugate
    >>> from sympy.abc import n,m
    >>> theta = Symbol("theta")
    >>> phi = Symbol("phi")

    >>> conjugate(Ynm(n, m, theta, phi))
    (-1)**(2*m)*exp(-2*I*m*phi)*Ynm(n, m, theta, phi)

    To get back the well known expressions in spherical
    coordinates, we use full expansion:

    >>> from sympy import Ynm, Symbol, expand_func
    >>> from sympy.abc import n,m
    >>> theta = Symbol("theta")
    >>> phi = Symbol("phi")

    >>> expand_func(Ynm(n, m, theta, phi))
    sqrt((2*n + 1)*factorial(-m + n)/factorial(m + n))*exp(I*m*phi)*assoc_legendre(n, m, cos(theta))/(2*sqrt(pi))

    See Also
    ========

    Ynm_c, Znm

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Spherical_harmonics
    .. [2] https://mathworld.wolfram.com/SphericalHarmonic.html
    .. [3] https://functions.wolfram.com/Polynomials/SphericalHarmonicY/
    .. [4] https://dlmf.nist.gov/14.30

    c                    |                                 rC| }t          j        |z  t          dt          z  |z  |z            z  t          ||||          z  S |                                 r| }t          ||||          S |                                 r3| }t          dt          z  |z  |z            t          ||||          z  S d S )N)could_extract_minus_signr   NegativeOner   r   r   )clsnmthetaphis        g/var/www/html/ai-engine/env/lib/python3.11/site-packages/sympy/functions/special/spherical_harmonics.pyevalzYnm.eval   s     %%'' 	NA=!#c"Q$q&*oo5Aq%8M8MMM))++ 	)FEq!UC((('')) 	;$Cr!tAvcz??SAuc%:%:::	; 	;    c                    | j         \  }}}}t          d|z  dz   dt          z  z  t          ||z
            z  t          ||z             z            t	          t
          |z  |z            z  t          ||t          |                    z  }|                    t          t          |          dz   dz             t          |                    S N         )
argsr   r   r   r   r   r
   r   subsr   )selfhintsr   r   r   r   rvs          r   _eval_expand_funczYnm._eval_expand_func   s    91eSAaC!Gad#iA&6&66yQ7G7GGHHAaCG-aCJJ??@ wwtSZZ]NQ.//U<<<r!   r&   c                    |dk    rt          | |          |dk    rt          | |          |dk    r|| j        \  }}}}|t          |          z  t          ||||          z  t	          ||z
  ||z   dz   z            t          t           |z            z  t          ||dz   ||          z  z   S |dk    r)| j        \  }}}}t          |z  t          ||||          z  S t          | |          )Nr%   r$      r&   )r   r'   r   r   r   r   r   )r)   argindexr   r   r   r   s         r   fdiffz	Ynm.fdiff   s   q==$T8444]]$T8444]]#yAq%E

NSAuc%:%::!a%!a%!),--QBsF;c!QUESV>W>WWX Y]]#yAq%q53q!UC0000$T8444r!   c                 .    |                      d          S )NTfunc)expandr)   r   r   r   r   kwargss         r   _eval_rewrite_as_polynomialzYnm._eval_rewrite_as_polynomial   s     {{{%%%r!   c                 6    |                      t                    S N)rewriter   r5   s         r   _eval_rewrite_as_sinzYnm._eval_rewrite_as_sin   s    ||C   r!   c                     ddl m}m}  ||                     d                    }|                    t          t          |                    t          |          i          } | ||                    S )Nr   )simplifytrigsimpTr2   )sympy.simplifyr=   r>   r4   xreplacer   r   )	r)   r   r   r   r   r6   r=   r>   terms	            r   _eval_rewrite_as_coszYnm._eval_rewrite_as_cos   sy    55555555 x..//}}c#e**ooc%jj9::x'''r!   c                 l    | j         \  }}}}t          j        |z  |                     || ||          z  S r9   )r'   r   r   r3   )r)   r   r   r   r   s        r   _eval_conjugatezYnm._eval_conjugate   s8    91eS}a$))Ar5#">">>>r!   Tc                    | j         \  }}}}t          d|z  dz   dt          z  z  t          ||z
            z  t          ||z             z            t	          ||z            z  t          ||t	          |                    z  }t          d|z  dz   dt          z  z  t          ||z
            z  t          ||z             z            t          ||z            z  t          ||t	          |                    z  }||fS r#   )r'   r   r   r   r   r
   r   )	r)   deepr*   r   r   r   r   reims	            r   as_real_imagzYnm.as_real_imag   s    91eSAaC!Gad#iA&6&66yQ7G7GGHH!C%jj)!QE

;;<AaC!Gad#iA&6&66yQ7G7GGHH!C%jj)!QE

;;<Bxr!   c                    ddl m}m} | j        d                             |          }| j        d                             |          }| j        d                             |          }| j        d                             |          } ||          5  |                    ||||          }d d d            n# 1 swxY w Y   t          j        ||          S )Nr   )mpworkprecr%   r$   r.   )mpmathrK   rL   r'   
_to_mpmath	spherharmr   _from_mpmath)	r)   precrK   rL   r   r   r   r   ress	            r   _eval_evalfzYnm._eval_evalf   s    	('''''''IaL##D))IaL##D))	!''--il%%d++Xd^^ 	1 	1,,q!UC00C	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 d+++s   B99B= B=N)r&   )T)__name__
__module____qualname____doc__classmethodr    r,   r0   r7   r;   rB   rD   rI   rS    r!   r   r   r      s        w wr 
; 
; [
;= = =5 5 5 5&& & &
! ! !( ( (? ? ?
   , , , , ,r!   r   c                 @    t          t          | |||                    S )a0  
    Conjugate spherical harmonics defined as

    .. math::
        \overline{Y_n^m(\theta, \varphi)} := (-1)^m Y_n^{-m}(\theta, \varphi).

    Examples
    ========

    >>> from sympy import Ynm_c, Symbol, simplify
    >>> from sympy.abc import n,m
    >>> theta = Symbol("theta")
    >>> phi = Symbol("phi")
    >>> Ynm_c(n, m, theta, phi)
    (-1)**(2*m)*exp(-2*I*m*phi)*Ynm(n, m, theta, phi)
    >>> Ynm_c(n, m, -theta, phi)
    (-1)**(2*m)*exp(-2*I*m*phi)*Ynm(n, m, theta, phi)

    For specific integers $n$ and $m$ we can evaluate the harmonics
    to more useful expressions:

    >>> simplify(Ynm_c(0, 0, theta, phi).expand(func=True))
    1/(2*sqrt(pi))
    >>> simplify(Ynm_c(1, -1, theta, phi).expand(func=True))
    sqrt(6)*exp(I*(-phi + 2*conjugate(phi)))*sin(theta)/(4*sqrt(pi))

    See Also
    ========

    Ynm, Znm

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Spherical_harmonics
    .. [2] https://mathworld.wolfram.com/SphericalHarmonic.html
    .. [3] https://functions.wolfram.com/Polynomials/SphericalHarmonicY/

    )r   r   )r   r   r   r   s       r   Ynm_cr[      s!    P SAuc**+++r!   c                   (    e Zd ZdZed             ZdS )Znma{  
    Real spherical harmonics defined as

    .. math::

        Z_n^m(\theta, \varphi) :=
        \begin{cases}
          \frac{Y_n^m(\theta, \varphi) + \overline{Y_n^m(\theta, \varphi)}}{\sqrt{2}} &\quad m > 0 \\
          Y_n^m(\theta, \varphi) &\quad m = 0 \\
          \frac{Y_n^m(\theta, \varphi) - \overline{Y_n^m(\theta, \varphi)}}{i \sqrt{2}} &\quad m < 0 \\
        \end{cases}

    which gives in simplified form

    .. math::

        Z_n^m(\theta, \varphi) =
        \begin{cases}
          \frac{Y_n^m(\theta, \varphi) + (-1)^m Y_n^{-m}(\theta, \varphi)}{\sqrt{2}} &\quad m > 0 \\
          Y_n^m(\theta, \varphi) &\quad m = 0 \\
          \frac{Y_n^m(\theta, \varphi) - (-1)^m Y_n^{-m}(\theta, \varphi)}{i \sqrt{2}} &\quad m < 0 \\
        \end{cases}

    Examples
    ========

    >>> from sympy import Znm, Symbol, simplify
    >>> from sympy.abc import n, m
    >>> theta = Symbol("theta")
    >>> phi = Symbol("phi")
    >>> Znm(n, m, theta, phi)
    Znm(n, m, theta, phi)

    For specific integers n and m we can evaluate the harmonics
    to more useful expressions:

    >>> simplify(Znm(0, 0, theta, phi).expand(func=True))
    1/(2*sqrt(pi))
    >>> simplify(Znm(1, 1, theta, phi).expand(func=True))
    -sqrt(3)*sin(theta)*cos(phi)/(2*sqrt(pi))
    >>> simplify(Znm(2, 1, theta, phi).expand(func=True))
    -sqrt(15)*sin(2*theta)*cos(phi)/(4*sqrt(pi))

    See Also
    ========

    Ynm, Ynm_c

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Spherical_harmonics
    .. [2] https://mathworld.wolfram.com/SphericalHarmonic.html
    .. [3] https://functions.wolfram.com/Polynomials/SphericalHarmonicY/

    c                 @   |j         r7t          ||||          t          ||||          z   t          d          z  }|S |j        rt          ||||          S |j        r?t          ||||          t          ||||          z
  t          d          t          z  z  }|S d S )Nr$   )is_positiver   r[   r   is_zerois_negativer   )r   r   r   r   r   zzs         r   r    zZnm.evalE  s    = 	aE3''%1eS*A*AAT!WWLBIY 	q!UC(((] 	aE3''%1eS*A*AAd1ggaiPBI	 	r!   N)rT   rU   rV   rW   rX   r    rY   r!   r   r]   r]     s:        7 7r   [  r!   r]   N)sympy.core.exprr   sympy.core.functionr   r   sympy.core.numbersr   r   sympy.core.singletonr   sympy.core.symbolr	   sympy.functionsr
   (sympy.functions.combinatorial.factorialsr   $sympy.functions.elementary.complexesr   r   &sympy.functions.elementary.exponentialr   (sympy.functions.elementary.miscellaneousr   (sympy.functions.elementary.trigonometricr   r   r   _xr   r[   r]   rY   r!   r   <module>ro      sw               < < < < < < < < $ $ $ $ $ $ $ $ " " " " " " # # # # # # * * * * * * > > > > > > ? ? ? ? ? ? ? ? 6 6 6 6 6 6 9 9 9 9 9 9 B B B B B B B B B B
U3ZZN, N, N, N, N,( N, N, N,b(, (, (,VC C C C C( C C C C Cr!   