ó
Ąź\c           @  s  d  d l  m Z m Z d  d l m Z 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 d  d
 l m Z d  d l m Z m Z m Z e d  Z d e
 f d     YZ d   Z d e
 f d     YZ d S(   i˙˙˙˙(   t   print_functiont   division(   t   pit   I(   t   Dummyt   sympify(   t   Functiont   ArgumentIndexError(   t   S(   t   assoc_legendre(   t	   factorial(   t   Abs(   t   exp(   t   sqrt(   t   sint   cost   cott   xt   Ynmc           B  st   e  Z d  Z e d    Z d   Z d d  Z d   Z d   Z d   Z	 d   Z
 e d	  Z d
   Z d   Z RS(   s
  
    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)

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

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

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

    as well as for the angles

    >>> 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)

    >>> 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] http://mathworld.wolfram.com/SphericalHarmonic.html
    .. [3] http://functions.wolfram.com/Polynomials/SphericalHarmonicY/
    .. [4] http://dlmf.nist.gov/14.30
    c         C  sä   g  | | | | f D] } t  |  ^ q \ } } } } | j   r~ | } t j | t d t | |  t | | | |  S| j   r¤ | } t | | | |  S| j   rŕ | } t d t | |  t | | | |  Sd  S(   Niţ˙˙˙(   R   t   could_extract_minus_signR   t   NegativeOneR   R   R   (   t   clst   nt   mt   thetat   phiR   (    (    sJ   lib/python2.7/site-packages/sympy/functions/special/spherical_harmonics.pyt   eval   s    74c         K  s    |  j  \ } } } } t d | d d t t | |  t | |   t t | |  t | | t |   } | j t t |  d d  t	 |   S(   Ni   i   i   (
   t   argsR   R   R
   R   R   R	   R   t   subsR   (   t   selft   hintsR   R   R   R   t   rv(    (    sJ   lib/python2.7/site-packages/sympy/functions/special/spherical_harmonics.pyt   _eval_expand_funcĄ   s    `i   c         C  s  | d k r t  |  |   në | d k r< t  |  |   nÍ | d k rž |  j \ } } } } | t |  t | | | |  t | | | | d  t t |  t | | d | |  S| d k rú |  j \ } } } } t | t | | | |  St  |  |   d  S(   Ni   i   i   i   (   R   R   R   R   R   R   R   (   R   t   argindexR   R   R   R   (    (    sJ   lib/python2.7/site-packages/sympy/functions/special/spherical_harmonics.pyt   fdiff¨   s     Ac         K  s   |  j  d t  S(   Nt   func(   t   expandt   True(   R   R   R   R   R   t   kwargs(    (    sJ   lib/python2.7/site-packages/sympy/functions/special/spherical_harmonics.pyt   _eval_rewrite_as_polynomialť   s    c         K  s   |  j  t  S(   N(   t   rewriteR   (   R   R   R   R   R   R&   (    (    sJ   lib/python2.7/site-packages/sympy/functions/special/spherical_harmonics.pyt   _eval_rewrite_as_sinŔ   s    c   	      K  sf   d d l  m } m } | |  j d t   } | j i t |  t t |   6 } | | |   S(   Ni˙˙˙˙(   t   simplifyt   trigsimpR#   (   t   sympy.simplifyR*   R+   R$   R%   t   xreplaceR   R   (	   R   R   R   R   R   R&   R*   R+   t   term(    (    sJ   lib/python2.7/site-packages/sympy/functions/special/spherical_harmonics.pyt   _eval_rewrite_as_cosĂ   s    (c         C  s7   |  j  \ } } } } t j | |  j | | | |  S(   N(   R   R   R   R#   (   R   R   R   R   R   (    (    sJ   lib/python2.7/site-packages/sympy/functions/special/spherical_harmonics.pyt   _eval_conjugateÍ   s    c   	      K  s×   |  j  \ } } } } t d | d d t t | |  t | |   t | |  t | | t |   } t d | d d t t | |  t | |   t | |  t | | t |   } | | f S(   Ni   i   i   (   R   R   R   R
   R   R	   R   (	   R   t   deepR   R   R   R   R   t   ret   im(    (    sJ   lib/python2.7/site-packages/sympy/functions/special/spherical_harmonics.pyt   as_real_imagŇ   s    \\c   
   	   C  sš   d d l  m } m } d d l m } |  j d j |  } |  j d j |  } |  j d j |  } |  j d j |  } | |   | j | | | |  }	 Wd  QX| j |	 |  S(   Ni˙˙˙˙(   t   mpt   workprec(   t   Expri    i   i   i   (	   t   mpmathR5   R6   t   sympyR7   R   t
   _to_mpmatht	   spherharmt   _from_mpmath(
   R   t   precR5   R6   R7   R   R   R   R   t   res(    (    sJ   lib/python2.7/site-packages/sympy/functions/special/spherical_harmonics.pyt   _eval_evalfŰ   s    c         C  sY   d d  l  j } | j |  j d j   |  j d j   |  j d j   |  j d j    S(   Ni˙˙˙˙i    i   i   i   (   t   sage.allt   allt   spherical_harmonicR   t   _sage_(   R   t   sage(    (    sJ   lib/python2.7/site-packages/sympy/functions/special/spherical_harmonics.pyRC   é   s
    (   t   __name__t
   __module__t   __doc__t   classmethodR   R    R"   R'   R)   R/   R0   R%   R4   R?   RC   (    (    (    sJ   lib/python2.7/site-packages/sympy/functions/special/spherical_harmonics.pyR      s   ~				
			c         C  s)   d d l  m } | t |  | | |   S(   s  Conjugate spherical harmonics defined as

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

    See Also
    ========

    Ynm, Znm

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Spherical_harmonics
    .. [2] http://mathworld.wolfram.com/SphericalHarmonic.html
    .. [3] http://functions.wolfram.com/Polynomials/SphericalHarmonicY/
    i˙˙˙˙(   t	   conjugate(   R9   RI   R   (   R   R   R   R   RI   (    (    sJ   lib/python2.7/site-packages/sympy/functions/special/spherical_harmonics.pyt   Ynm_cń   s    t   Znmc           B  s   e  Z d  Z e d    Z RS(   s  
    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}

    See Also
    ========

    Ynm, Ynm_c

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Spherical_harmonics
    .. [2] http://mathworld.wolfram.com/SphericalHarmonic.html
    .. [3] http://functions.wolfram.com/Polynomials/SphericalHarmonicY/
    c   	      C  sŮ   g  | | | | f D] } t  |  ^ q \ } } } } | j rv t | | | |  t | | | |  t d  } | S| j r t | | | |  S| j rŐ t | | | |  t | | | |  t d  t } | Sd  S(   Ni   (   R   t   is_positiveR   RJ   R   t   is_zerot   is_negativeR   (	   R   R   R   R   R   R   t   tht   pht   zz(    (    sJ   lib/python2.7/site-packages/sympy/functions/special/spherical_harmonics.pyR   ,  s    7	2		6(   RE   RF   RG   RH   R   (    (    (    sJ   lib/python2.7/site-packages/sympy/functions/special/spherical_harmonics.pyRK     s   #N(    t
   __future__R    R   R9   R   R   t
   sympy.coreR   R   t   sympy.core.functionR   R   t   sympy.core.singletonR   t   sympy.functionsR	   t(   sympy.functions.combinatorial.factorialsR
   t$   sympy.functions.elementary.complexesR   t&   sympy.functions.elementary.exponentialR   t(   sympy.functions.elementary.miscellaneousR   t(   sympy.functions.elementary.trigonometricR   R   R   t   _xR   RJ   RK   (    (    (    sJ   lib/python2.7/site-packages/sympy/functions/special/spherical_harmonics.pyt   <module>   s   á	