B
    [+                 @   s  d Z ddlmZmZ ddlmZ ddlmZ ddlm	Z	m
Z
 ddlmZ ddlmZ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 ddlm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z& edZ'G dd de	Z(G dd de(Z)dd Z*G dd de(Z+G dd de(Z,G dd de(Z-G dd de	Z.G dd  d e	Z/G d!d" d"e(Z0G d#d$ d$e	Z1G d%d& d&e(Z2G d'd( d(e(Z3G d)d* d*e(Z4d+S ),z
This module mainly implements special orthogonal polynomials.

See also functions.combinatorial.numbers which contains some
combinatorial polynomials.

    )print_functiondivision)S)Rational)FunctionArgumentIndexError)Dummy)binomial	factorialRisingFactorial)re)exp)floor)sqrt)cos)gamma)hyper)jacobi_polygegenbauer_polychebyshevt_polychebyshevu_polylaguerre_polyhermite_polylegendre_polyxc               @   s$   e Zd ZdZedd Zdd ZdS )OrthogonalPolynomialz+Base class for orthogonal polynomials.
    c             C   s*   |j r&|dkr&| t|tt|S d S )Nr   )
is_integer_ortho_polyint_xsubs)clsnr    r#   Blib/python3.7/site-packages/sympy/functions/special/polynomials.py_eval_at_order)   s    z#OrthogonalPolynomial._eval_at_orderc             C   s   |  | jd | jd  S )Nr      )funcargs	conjugate)selfr#   r#   r$   _eval_conjugate.   s    z$OrthogonalPolynomial._eval_conjugateN)__name__
__module____qualname____doc__classmethodr%   r+   r#   r#   r#   r$   r   %   s   r   c               @   s6   e Zd ZdZedd ZdddZdd Zd	d
 ZdS )jacobia  
    Jacobi polynomial :math:`P_n^{\left(\alpha, \beta\right)}(x)`

    jacobi(n, alpha, beta, x) gives the nth Jacobi polynomial
    in x, :math:`P_n^{\left(\alpha, \beta\right)}(x)`.

    The Jacobi polynomials are orthogonal on :math:`[-1, 1]` with respect
    to the weight :math:`\left(1-x\right)^\alpha \left(1+x\right)^\beta`.

    Examples
    ========

    >>> from sympy import jacobi, S, conjugate, diff
    >>> from sympy.abc import n,a,b,x

    >>> jacobi(0, a, b, x)
    1
    >>> jacobi(1, a, b, x)
    a/2 - b/2 + x*(a/2 + b/2 + 1)
    >>> jacobi(2, a, b, x)   # doctest:+SKIP
    (a**2/8 - a*b/4 - a/8 + b**2/8 - b/8 + x**2*(a**2/8 + a*b/4 + 7*a/8 +
    b**2/8 + 7*b/8 + 3/2) + x*(a**2/4 + 3*a/4 - b**2/4 - 3*b/4) - 1/2)

    >>> jacobi(n, a, b, x)
    jacobi(n, a, b, x)

    >>> jacobi(n, a, a, x)
    RisingFactorial(a + 1, n)*gegenbauer(n,
        a + 1/2, x)/RisingFactorial(2*a + 1, n)

    >>> jacobi(n, 0, 0, x)
    legendre(n, x)

    >>> jacobi(n, S(1)/2, S(1)/2, x)
    RisingFactorial(3/2, n)*chebyshevu(n, x)/factorial(n + 1)

    >>> jacobi(n, -S(1)/2, -S(1)/2, x)
    RisingFactorial(1/2, n)*chebyshevt(n, x)/factorial(n)

    >>> jacobi(n, a, b, -x)
    (-1)**n*jacobi(n, b, a, x)

    >>> jacobi(n, a, b, 0)
    2**(-n)*gamma(a + n + 1)*hyper((-b - n, -n), (a + 1,), -1)/(factorial(n)*gamma(a + 1))
    >>> jacobi(n, a, b, 1)
    RisingFactorial(a + 1, n)/factorial(n)

    >>> conjugate(jacobi(n, a, b, x))
    jacobi(n, conjugate(a), conjugate(b), conjugate(x))

    >>> diff(jacobi(n,a,b,x), x)
    (a/2 + b/2 + n/2 + 1/2)*jacobi(n - 1, a + 1, b + 1, x)

    See Also
    ========

    gegenbauer,
    chebyshevt_root, chebyshevu, chebyshevu_root,
    legendre, assoc_legendre,
    hermite,
    laguerre, assoc_laguerre,
    sympy.polys.orthopolys.jacobi_poly,
    sympy.polys.orthopolys.gegenbauer_poly
    sympy.polys.orthopolys.chebyshevt_poly
    sympy.polys.orthopolys.chebyshevu_poly
    sympy.polys.orthopolys.hermite_poly
    sympy.polys.orthopolys.legendre_poly
    sympy.polys.orthopolys.laguerre_poly

    References
    ==========

    .. [1] http://en.wikipedia.org/wiki/Jacobi_polynomials
    .. [2] http://mathworld.wolfram.com/JacobiPolynomial.html
    .. [3] http://functions.wolfram.com/Polynomials/JacobiP/
    c             C   sZ  ||kr|t j kr2tt j|t| t|| S |t jkrFt||S |t jkrvtdt j |t|d  t|| S t|d |td| d | t||t j | S n|| kr t	|| d t	|d  d| |d   d| |d   t
|| | S || krTt	|| d t	|d  d| |d   d| |d   t
||| S |jsH| rt j| t||||  S |t jkrd|  t	|| d  t	|d t|  t| | | g|d gd S |t jkrt|d |t| S |t jkrV|jrV|| d|  jr*tdt|| | d |t j S nt||||S d S )N   r&      z,Error. a + b + 2*n should not be an integer.)r   Halfr   r
   
chebyshevtZerolegendre
chebyshevu
gegenbauerr   assoc_legendre	is_Numbercould_extract_minus_signNegativeOner1   r   OneInfinityis_positiver   
ValueErrorr   )r!   r"   abr   r#   r#   r$   eval   s4    


&4JH
, zjacobi.eval   c       
   	   C   s  ddl m} |dkr"t| |n|dkr| j\}}}}td}d|| | | d  }|| d|  d t|| d ||  || t|| | d ||   }	||t|||||	t||||   |d|d fS |dkr| j\}}}}td}d|| | | d  }d||  || d|  d t|| d ||  || t|| | d ||    }	||t|||||	t||||   |d|d fS |dkr| j\}}}}tj|| | d  t|d |d |d | S t| |d S )	Nr   )Sumr&   r3   kr2   r4   rF   )	sympyrG   r   r(   r   r   r1   r   r5   )
r*   argindexrG   r"   rC   rD   r   rH   f1f2r#   r#   r$   fdiff   s*    ($4
2&4
0zjacobi.fdiffc             C   s   ddl m} |js|jdkr$tdtd}t| |t|| | d | t|| d ||  t| d| d |  }dt| |||d|f S )Nr   )rG   Fz*Error: n should be a non-negative integer.rH   r&   r3   )rI   rG   is_negativer   rB   r   r   r
   )r*   r"   rC   rD   r   rG   rH   kernr#   r#   r$   _eval_rewrite_as_polynomial   s    Pz"jacobi._eval_rewrite_as_polynomialc             C   s*   | j \}}}}| || | | S )N)r(   r'   r)   )r*   r"   rC   rD   r   r#   r#   r$   r+      s    zjacobi._eval_conjugateN)rF   )	r,   r-   r.   r/   r0   rE   rM   rP   r+   r#   r#   r#   r$   r1   6   s
   L)

r1   c             C   sz   t d|| d  t| | d t| | d   d|  | | d  t| t| | | d   }t| |||t| S )a  
    Jacobi polynomial :math:`P_n^{\left(\alpha, \beta\right)}(x)`

    jacobi_normalized(n, alpha, beta, x) gives the nth Jacobi polynomial
    in x, :math:`P_n^{\left(\alpha, \beta\right)}(x)`.

    The Jacobi polynomials are orthogonal on :math:`[-1, 1]` with respect
    to the weight :math:`\left(1-x\right)^\alpha \left(1+x\right)^\beta`.

    This functions returns the polynomials normilzed:

    .. math::

        \int_{-1}^{1}
          P_m^{\left(\alpha, \beta\right)}(x)
          P_n^{\left(\alpha, \beta\right)}(x)
          (1-x)^{\alpha} (1+x)^{\beta} \mathrm{d}x
        = \delta_{m,n}

    Examples
    ========

    >>> from sympy import jacobi_normalized
    >>> from sympy.abc import n,a,b,x

    >>> jacobi_normalized(n, a, b, x)
    jacobi(n, a, b, x)/sqrt(2**(a + b + 1)*gamma(a + n + 1)*gamma(b + n + 1)/((a + b + 2*n + 1)*factorial(n)*gamma(a + b + n + 1)))

    See Also
    ========

    gegenbauer,
    chebyshevt_root, chebyshevu, chebyshevu_root,
    legendre, assoc_legendre,
    hermite,
    laguerre, assoc_laguerre,
    sympy.polys.orthopolys.jacobi_poly,
    sympy.polys.orthopolys.gegenbauer_poly
    sympy.polys.orthopolys.chebyshevt_poly
    sympy.polys.orthopolys.chebyshevu_poly
    sympy.polys.orthopolys.hermite_poly
    sympy.polys.orthopolys.legendre_poly
    sympy.polys.orthopolys.laguerre_poly

    References
    ==========

    .. [1] http://en.wikipedia.org/wiki/Jacobi_polynomials
    .. [2] http://mathworld.wolfram.com/JacobiPolynomial.html
    .. [3] http://functions.wolfram.com/Polynomials/JacobiP/
    r3   r&   )r   r   r
   r1   r   )r"   rC   rD   r   Znfactorr#   r#   r$   jacobi_normalized   s    5drQ   c               @   s6   e Zd ZdZedd ZdddZdd Zd	d
 ZdS )r:   a  
    Gegenbauer polynomial :math:`C_n^{\left(\alpha\right)}(x)`

    gegenbauer(n, alpha, x) gives the nth Gegenbauer polynomial
    in x, :math:`C_n^{\left(\alpha\right)}(x)`.

    The Gegenbauer polynomials are orthogonal on :math:`[-1, 1]` with
    respect to the weight :math:`\left(1-x^2\right)^{\alpha-\frac{1}{2}}`.

    Examples
    ========

    >>> from sympy import gegenbauer, conjugate, diff
    >>> from sympy.abc import n,a,x
    >>> gegenbauer(0, a, x)
    1
    >>> gegenbauer(1, a, x)
    2*a*x
    >>> gegenbauer(2, a, x)
    -a + x**2*(2*a**2 + 2*a)
    >>> gegenbauer(3, a, x)
    x**3*(4*a**3/3 + 4*a**2 + 8*a/3) + x*(-2*a**2 - 2*a)

    >>> gegenbauer(n, a, x)
    gegenbauer(n, a, x)
    >>> gegenbauer(n, a, -x)
    (-1)**n*gegenbauer(n, a, x)

    >>> gegenbauer(n, a, 0)
    2**n*sqrt(pi)*gamma(a + n/2)/(gamma(a)*gamma(-n/2 + 1/2)*gamma(n + 1))
    >>> gegenbauer(n, a, 1)
    gamma(2*a + n)/(gamma(2*a)*gamma(n + 1))

    >>> conjugate(gegenbauer(n, a, x))
    gegenbauer(n, conjugate(a), conjugate(x))

    >>> diff(gegenbauer(n, a, x), x)
    2*a*gegenbauer(n - 1, a + 1, x)

    See Also
    ========

    jacobi,
    chebyshevt_root, chebyshevu, chebyshevu_root,
    legendre, assoc_legendre,
    hermite,
    laguerre, assoc_laguerre,
    sympy.polys.orthopolys.jacobi_poly
    sympy.polys.orthopolys.gegenbauer_poly
    sympy.polys.orthopolys.chebyshevt_poly
    sympy.polys.orthopolys.chebyshevu_poly
    sympy.polys.orthopolys.hermite_poly
    sympy.polys.orthopolys.legendre_poly
    sympy.polys.orthopolys.laguerre_poly

    References
    ==========

    .. [1] http://en.wikipedia.org/wiki/Gegenbauer_polynomials
    .. [2] http://mathworld.wolfram.com/GegenbauerPolynomial.html
    .. [3] http://functions.wolfram.com/Polynomials/GegenbauerC3/
    c             C   sN  |j rtjS |tjkr t||S |tjkr4t||S |tjkrDtjS |js>|tjkrrt	|tjkdkrntj
S d S | rtj| t|||  S |tjkrd| ttj t|tj|   td| d t|d  t|  S |tjkrtd| | td| t|d   S |tjkrJ|jrJt||tj S nt|||S d S )NTr3   r&   )rN   r   r7   r5   r8   r?   r9   r>   r<   r   ZComplexInfinityr=   r:   r   Pir   r@   rA   r   r   )r!   r"   rC   r   r#   r#   r$   rE   W  s0    






"&(zgegenbauer.evalr2   c       
      C   s&  ddl m} |dkr"t| |n |dkr| j\}}}td}ddd||    ||  || d|  ||   }d|d  |d|  d| d|  d   d|| d|    }|t||| |t|||  }	||	|d|d fS |dkr| j\}}}d| t|d |d | S t| |d S )Nr   )rG   r&   r3   rH   r4   r2   )rI   rG   r   r(   r   r:   )
r*   rJ   rG   r"   rC   r   rH   Zfactor1Zfactor2rO   r#   r#   r$   rM     s     * 
zgegenbauer.fdiffc             C   sn   ddl m} td}d| t|||  d| |d|    t|t|d|    }|||dt|d fS )Nr   )rG   rH   r4   r3   )rI   rG   r   r   r
   r   )r*   r"   rC   r   rG   rH   rO   r#   r#   r$   rP     s
    (z&gegenbauer._eval_rewrite_as_polynomialc             C   s"   | j \}}}| || | S )N)r(   r'   r)   )r*   r"   rC   r   r#   r#   r$   r+     s    zgegenbauer._eval_conjugateN)r2   )	r,   r-   r.   r/   r0   rE   rM   rP   r+   r#   r#   r#   r$   r:     s
   >+
r:   c               @   s6   e Zd ZdZeeZedd Zd
ddZ	dd Z
d	S )r6   a  
    Chebyshev polynomial of the first kind, :math:`T_n(x)`

    chebyshevt(n, x) gives the nth Chebyshev polynomial (of the first
    kind) in x, :math:`T_n(x)`.

    The Chebyshev polynomials of the first kind are orthogonal on
    :math:`[-1, 1]` with respect to the weight :math:`\frac{1}{\sqrt{1-x^2}}`.

    Examples
    ========

    >>> from sympy import chebyshevt, chebyshevu, diff
    >>> from sympy.abc import n,x
    >>> chebyshevt(0, x)
    1
    >>> chebyshevt(1, x)
    x
    >>> chebyshevt(2, x)
    2*x**2 - 1

    >>> chebyshevt(n, x)
    chebyshevt(n, x)
    >>> chebyshevt(n, -x)
    (-1)**n*chebyshevt(n, x)
    >>> chebyshevt(-n, x)
    chebyshevt(n, x)

    >>> chebyshevt(n, 0)
    cos(pi*n/2)
    >>> chebyshevt(n, -1)
    (-1)**n

    >>> diff(chebyshevt(n, x), x)
    n*chebyshevu(n - 1, x)

    See Also
    ========

    jacobi, gegenbauer,
    chebyshevt_root, chebyshevu, chebyshevu_root,
    legendre, assoc_legendre,
    hermite,
    laguerre, assoc_laguerre,
    sympy.polys.orthopolys.jacobi_poly
    sympy.polys.orthopolys.gegenbauer_poly
    sympy.polys.orthopolys.chebyshevt_poly
    sympy.polys.orthopolys.chebyshevu_poly
    sympy.polys.orthopolys.hermite_poly
    sympy.polys.orthopolys.legendre_poly
    sympy.polys.orthopolys.laguerre_poly

    References
    ==========

    .. [1] http://en.wikipedia.org/wiki/Chebyshev_polynomial
    .. [2] http://mathworld.wolfram.com/ChebyshevPolynomialoftheFirstKind.html
    .. [3] http://mathworld.wolfram.com/ChebyshevPolynomialoftheSecondKind.html
    .. [4] http://functions.wolfram.com/Polynomials/ChebyshevT/
    .. [5] http://functions.wolfram.com/Polynomials/ChebyshevU/
    c             C   s   |j sx| r$tj| t||  S | r8t| |S |tjkrVttjtj | S |tj	krftj	S |tj
krtj
S n |jr| | |S | ||S d S )N)r<   r=   r   r>   r6   r7   r   r5   rR   r?   r@   rN   r%   )r!   r"   r   r#   r#   r$   rE     s    


zchebyshevt.evalr3   c             C   sF   |dkrt | |n.|dkr8| j\}}|t|d | S t | |d S )Nr&   r3   )r   r(   r9   )r*   rJ   r"   r   r#   r#   r$   rM     s    
zchebyshevt.fdiffc             C   sZ   ddl m} td}t|d| |d d |  ||d|    }|||dt|d fS )Nr   )rG   rH   r3   r&   )rI   rG   r   r	   r   )r*   r"   r   rG   rH   rO   r#   r#   r$   rP     s    .z&chebyshevt._eval_rewrite_as_polynomialN)r3   )r,   r-   r.   r/   staticmethodr   r   r0   rE   rM   rP   r#   r#   r#   r$   r6     s
   =
r6   c               @   s6   e Zd ZdZeeZedd Zd
ddZ	dd Z
d	S )r9   a  
    Chebyshev polynomial of the second kind, :math:`U_n(x)`

    chebyshevu(n, x) gives the nth Chebyshev polynomial of the second
    kind in x, :math:`U_n(x)`.

    The Chebyshev polynomials of the second kind are orthogonal on
    :math:`[-1, 1]` with respect to the weight :math:`\sqrt{1-x^2}`.

    Examples
    ========

    >>> from sympy import chebyshevt, chebyshevu, diff
    >>> from sympy.abc import n,x
    >>> chebyshevu(0, x)
    1
    >>> chebyshevu(1, x)
    2*x
    >>> chebyshevu(2, x)
    4*x**2 - 1

    >>> chebyshevu(n, x)
    chebyshevu(n, x)
    >>> chebyshevu(n, -x)
    (-1)**n*chebyshevu(n, x)
    >>> chebyshevu(-n, x)
    -chebyshevu(n - 2, x)

    >>> chebyshevu(n, 0)
    cos(pi*n/2)
    >>> chebyshevu(n, 1)
    n + 1

    >>> diff(chebyshevu(n, x), x)
    (-x*chebyshevu(n, x) + (n + 1)*chebyshevt(n + 1, x))/(x**2 - 1)

    See Also
    ========

    jacobi, gegenbauer,
    chebyshevt, chebyshevt_root, chebyshevu_root,
    legendre, assoc_legendre,
    hermite,
    laguerre, assoc_laguerre,
    sympy.polys.orthopolys.jacobi_poly
    sympy.polys.orthopolys.gegenbauer_poly
    sympy.polys.orthopolys.chebyshevt_poly
    sympy.polys.orthopolys.chebyshevu_poly
    sympy.polys.orthopolys.hermite_poly
    sympy.polys.orthopolys.legendre_poly
    sympy.polys.orthopolys.laguerre_poly

    References
    ==========

    .. [1] http://en.wikipedia.org/wiki/Chebyshev_polynomial
    .. [2] http://mathworld.wolfram.com/ChebyshevPolynomialoftheFirstKind.html
    .. [3] http://mathworld.wolfram.com/ChebyshevPolynomialoftheSecondKind.html
    .. [4] http://functions.wolfram.com/Polynomials/ChebyshevT/
    .. [5] http://functions.wolfram.com/Polynomials/ChebyshevU/
    c             C   s   |j s| r$tj| t||  S | rN|tjkr<tjS t| d | S |tjkrlttjtj | S |tj	krtj	| S |tj
krtj
S n8|jr|tjkrtjS | | d | S n| ||S d S )Nr3   )r<   r=   r   r>   r9   r7   r   r5   rR   r?   r@   rN   r%   )r!   r"   r   r#   r#   r$   rE   U  s$    





zchebyshevu.evalr3   c             C   sd   |dkrt | |nL|dkrV| j\}}|d t|d | |t||  |d d  S t | |d S )Nr&   r3   )r   r(   r6   r9   )r*   rJ   r"   r   r#   r#   r$   rM   t  s    
0zchebyshevu.fdiffc             C   sn   ddl m} td}tj| t||  d| |d|    t|t|d|    }|||dt|d fS )Nr   )rG   rH   r3   )rI   rG   r   r   r>   r
   r   )r*   r"   r   rG   rH   rO   r#   r#   r$   rP     s    Bz&chebyshevu._eval_rewrite_as_polynomialN)r3   )r,   r-   r.   r/   rS   r   r   r0   rE   rM   rP   r#   r#   r#   r$   r9     s
   =
r9   c               @   s   e Zd ZdZedd ZdS )chebyshevt_rootaC  
    chebyshev_root(n, k) returns the kth root (indexed from zero) of
    the nth Chebyshev polynomial of the first kind; that is, if
    0 <= k < n, chebyshevt(n, chebyshevt_root(n, k)) == 0.

    Examples
    ========

    >>> from sympy import chebyshevt, chebyshevt_root
    >>> chebyshevt_root(3, 2)
    -sqrt(3)/2
    >>> chebyshevt(3, chebyshevt_root(3, 2))
    0

    See Also
    ========

    jacobi, gegenbauer,
    chebyshevt, chebyshevu, chebyshevu_root,
    legendre, assoc_legendre,
    hermite,
    laguerre, assoc_laguerre,
    sympy.polys.orthopolys.jacobi_poly
    sympy.polys.orthopolys.gegenbauer_poly
    sympy.polys.orthopolys.chebyshevt_poly
    sympy.polys.orthopolys.chebyshevu_poly
    sympy.polys.orthopolys.hermite_poly
    sympy.polys.orthopolys.legendre_poly
    sympy.polys.orthopolys.laguerre_poly
    c             C   s>   d|kr||k s t d||f ttjd| d  d|  S )Nr   z+must have 0 <= k < n, got k = %s and n = %sr3   r&   )rB   r   r   rR   )r!   r"   rH   r#   r#   r$   rE     s    zchebyshevt_root.evalN)r,   r-   r.   r/   r0   rE   r#   r#   r#   r$   rT     s   rT   c               @   s   e Zd ZdZedd ZdS )chebyshevu_roota-  
    chebyshevu_root(n, k) returns the kth root (indexed from zero) of the
    nth Chebyshev polynomial of the second kind; that is, if 0 <= k < n,
    chebyshevu(n, chebyshevu_root(n, k)) == 0.

    Examples
    ========

    >>> from sympy import chebyshevu, chebyshevu_root
    >>> chebyshevu_root(3, 2)
    -sqrt(2)/2
    >>> chebyshevu(3, chebyshevu_root(3, 2))
    0

    See Also
    ========

    chebyshevt, chebyshevt_root, chebyshevu,
    legendre, assoc_legendre,
    hermite,
    laguerre, assoc_laguerre,
    sympy.polys.orthopolys.jacobi_poly
    sympy.polys.orthopolys.gegenbauer_poly
    sympy.polys.orthopolys.chebyshevt_poly
    sympy.polys.orthopolys.chebyshevu_poly
    sympy.polys.orthopolys.hermite_poly
    sympy.polys.orthopolys.legendre_poly
    sympy.polys.orthopolys.laguerre_poly
    c             C   s:   d|kr||k s t d||f ttj|d  |d  S )Nr   z+must have 0 <= k < n, got k = %s and n = %sr&   )rB   r   r   rR   )r!   r"   rH   r#   r#   r$   rE     s    zchebyshevu_root.evalN)r,   r-   r.   r/   r0   rE   r#   r#   r#   r$   rU     s   rU   c               @   s6   e Zd ZdZeeZedd Zd
ddZ	dd Z
d	S )r8   aX  
    legendre(n, x) gives the nth Legendre polynomial of x, :math:`P_n(x)`

    The Legendre polynomials are orthogonal on [-1, 1] with respect to
    the constant weight 1. They satisfy :math:`P_n(1) = 1` for all n; further,
    :math:`P_n` is odd for odd n and even for even n.

    Examples
    ========

    >>> from sympy import legendre, diff
    >>> from sympy.abc import x, n
    >>> legendre(0, x)
    1
    >>> legendre(1, x)
    x
    >>> legendre(2, x)
    3*x**2/2 - 1/2
    >>> legendre(n, x)
    legendre(n, x)
    >>> diff(legendre(n,x), x)
    n*(x*legendre(n, x) - legendre(n - 1, x))/(x**2 - 1)

    See Also
    ========

    jacobi, gegenbauer,
    chebyshevt, chebyshevt_root, chebyshevu, chebyshevu_root,
    assoc_legendre,
    hermite,
    laguerre, assoc_laguerre,
    sympy.polys.orthopolys.jacobi_poly
    sympy.polys.orthopolys.gegenbauer_poly
    sympy.polys.orthopolys.chebyshevt_poly
    sympy.polys.orthopolys.chebyshevu_poly
    sympy.polys.orthopolys.hermite_poly
    sympy.polys.orthopolys.legendre_poly
    sympy.polys.orthopolys.laguerre_poly

    References
    ==========

    .. [1] http://en.wikipedia.org/wiki/Legendre_polynomial
    .. [2] http://mathworld.wolfram.com/LegendrePolynomial.html
    .. [3] http://functions.wolfram.com/Polynomials/LegendreP/
    .. [4] http://functions.wolfram.com/Polynomials/LegendreP2/
    c             C   s   |j s| r$tj| t||  S | r>t| tj |S |tjkrvttjt	tj
|d  t	tj|d    S |tjkrtjS |tjkrtjS n|jr| tj }| ||S d S )Nr3   )r<   r=   r   r>   r8   r?   r7   r   rR   r   r5   r@   rN   r%   )r!   r"   r   r#   r#   r$   rE     s    
.

zlegendre.evalr3   c             C   s`   |dkrt | |nH|dkrR| j\}}||d d  |t|| t|d |  S t | |d S )Nr&   r3   )r   r(   r8   )r*   rJ   r"   r   r#   r#   r$   rM   &  s    
,zlegendre.fdiffc             C   s^   ddl m} td}d| t||d  d| d ||   d| d |  }|||d|fS )Nr   )rG   rH   r4   r3   r&   )rI   rG   r   r	   )r*   r"   r   rG   rH   rO   r#   r#   r$   rP   2  s    :z$legendre._eval_rewrite_as_polynomialN)r3   )r,   r-   r.   r/   rS   r   r   r0   rE   rM   rP   r#   r#   r#   r$   r8     s
   /
r8   c               @   sB   e Zd ZdZedd Zedd ZdddZd	d
 Zdd Z	dS )r;   a,  
    assoc_legendre(n,m, x) gives :math:`P_n^m(x)`, where n and m are
    the degree and order or an expression which is related to the nth
    order Legendre polynomial, :math:`P_n(x)` in the following manner:

    .. math::
        P_n^m(x) = (-1)^m (1 - x^2)^{\frac{m}{2}}
                   \frac{\mathrm{d}^m P_n(x)}{\mathrm{d} x^m}

    Associated Legendre polynomial are orthogonal on [-1, 1] with:

    - weight = 1            for the same m, and different n.
    - weight = 1/(1-x**2)   for the same n, and different m.

    Examples
    ========

    >>> from sympy import assoc_legendre
    >>> from sympy.abc import x, m, n
    >>> assoc_legendre(0,0, x)
    1
    >>> assoc_legendre(1,0, x)
    x
    >>> assoc_legendre(1,1, x)
    -sqrt(-x**2 + 1)
    >>> assoc_legendre(n,m,x)
    assoc_legendre(n, m, x)

    See Also
    ========

    jacobi, gegenbauer,
    chebyshevt, chebyshevt_root, chebyshevu, chebyshevu_root,
    legendre,
    hermite,
    laguerre, assoc_laguerre,
    sympy.polys.orthopolys.jacobi_poly
    sympy.polys.orthopolys.gegenbauer_poly
    sympy.polys.orthopolys.chebyshevt_poly
    sympy.polys.orthopolys.chebyshevu_poly
    sympy.polys.orthopolys.hermite_poly
    sympy.polys.orthopolys.legendre_poly
    sympy.polys.orthopolys.laguerre_poly

    References
    ==========

    .. [1] http://en.wikipedia.org/wiki/Associated_Legendre_polynomials
    .. [2] http://mathworld.wolfram.com/LegendrePolynomial.html
    .. [3] http://functions.wolfram.com/Polynomials/LegendreP/
    .. [4] http://functions.wolfram.com/Polynomials/LegendreP2/
    c             C   s>   t |tddt|f}d| dtd  t|d  |  S )NT)Zpolysr4   r&   r3   )r   r   Zdiffr   Zas_expr)r!   r"   mPr#   r#   r$   r%   o  s    zassoc_legendre._eval_at_orderc             C   s   |  r:tj|  t|| t||   t|| | S |dkrLt||S |dkrd| ttj td| | d td|| d    S |j	r|j	r|j
r|j
r|jrtd| |f t||krtd| ||f | t|tt|t|S d S )Nr   r3   r&   z3%s : 1st index must be nonnegative integer (got %r)z9%s : abs('2nd index') must be <= '1st index' (got %r, %r))r=   r   r>   r
   r;   r8   r   rR   r   r<   r   rN   rB   absr%   r   r    r   )r!   r"   rV   r   r#   r#   r$   rE   t  s    2
:zassoc_legendre.evalr2   c             C   s   |dkrt | |nn|dkr(t | |nZ|dkrx| j\}}}d|d d  || t||| || t|d ||   S t | |d S )Nr&   r3   r2   )r   r(   r;   )r*   rJ   r"   rV   r   r#   r#   r$   rM     s    <zassoc_legendre.fdiffc             C   s   ddl m} td}td| d|  d| t||  t| t|d|  |   d|  ||| d|    }d|d  |d  |||dt|| tj f S )Nr   )rG   rH   r3   r4   r&   )rI   rG   r   r
   r   r   r5   )r*   r"   rV   r   rG   rH   rO   r#   r#   r$   rP     s    `z*assoc_legendre._eval_rewrite_as_polynomialc             C   s"   | j \}}}| || | S )N)r(   r'   r)   )r*   r"   rV   r   r#   r#   r$   r+     s    zassoc_legendre._eval_conjugateN)r2   )
r,   r-   r.   r/   r0   r%   rE   rM   rP   r+   r#   r#   r#   r$   r;   9  s   4
r;   c               @   s6   e Zd ZdZeeZedd Zd
ddZ	dd Z
d	S )hermitea  
    hermite(n, x) gives the nth Hermite polynomial in x, :math:`H_n(x)`

    The Hermite polynomials are orthogonal on :math:`(-\infty, \infty)`
    with respect to the weight :math:`\exp\left(-x^2\right)`.

    Examples
    ========

    >>> from sympy import hermite, diff
    >>> from sympy.abc import x, n
    >>> hermite(0, x)
    1
    >>> hermite(1, x)
    2*x
    >>> hermite(2, x)
    4*x**2 - 2
    >>> hermite(n, x)
    hermite(n, x)
    >>> diff(hermite(n,x), x)
    2*n*hermite(n - 1, x)
    >>> hermite(n, -x)
    (-1)**n*hermite(n, x)

    See Also
    ========

    jacobi, gegenbauer,
    chebyshevt, chebyshevt_root, chebyshevu, chebyshevu_root,
    legendre, assoc_legendre,
    laguerre, assoc_laguerre,
    sympy.polys.orthopolys.jacobi_poly
    sympy.polys.orthopolys.gegenbauer_poly
    sympy.polys.orthopolys.chebyshevt_poly
    sympy.polys.orthopolys.chebyshevu_poly
    sympy.polys.orthopolys.hermite_poly
    sympy.polys.orthopolys.legendre_poly
    sympy.polys.orthopolys.laguerre_poly

    References
    ==========

    .. [1] http://en.wikipedia.org/wiki/Hermite_polynomial
    .. [2] http://mathworld.wolfram.com/HermitePolynomial.html
    .. [3] http://functions.wolfram.com/Polynomials/HermiteH/
    c             C   s   |j sd| r$tj| t||  S |tjkrRd| ttj ttj	| d  S |tj
krtj
S n |jrxtd| n| ||S d S )Nr3   z0The index n must be nonnegative integer (got %r))r<   r=   r   r>   rY   r7   r   rR   r   r?   r@   rN   rB   r%   )r!   r"   r   r#   r#   r$   rE     s    
$
zhermite.evalr3   c             C   sJ   |dkrt | |n2|dkr<| j\}}d| t|d | S t | |d S )Nr&   r3   )r   r(   rY   )r*   rJ   r"   r   r#   r#   r$   rM     s    
zhermite.fdiffc             C   sh   ddl m} td}d| t|t|d|    d| |d|    }t||||dt|d f S )Nr   )rG   rH   r4   r3   )rI   rG   r   r
   r   )r*   r"   r   rG   rH   rO   r#   r#   r$   rP     s    4z#hermite._eval_rewrite_as_polynomialN)r3   )r,   r-   r.   r/   rS   r   r   r0   rE   rM   rP   r#   r#   r#   r$   rY     s
   .
rY   c               @   s6   e Zd ZdZeeZedd Zd
ddZ	dd Z
d	S )laguerrea  
    Returns the nth Laguerre polynomial in x, :math:`L_n(x)`.

    Parameters
    ==========

    n : int
        Degree of Laguerre polynomial. Must be ``n >= 0``.

    Examples
    ========

    >>> from sympy import laguerre, diff
    >>> from sympy.abc import x, n
    >>> laguerre(0, x)
    1
    >>> laguerre(1, x)
    -x + 1
    >>> laguerre(2, x)
    x**2/2 - 2*x + 1
    >>> laguerre(3, x)
    -x**3/6 + 3*x**2/2 - 3*x + 1

    >>> laguerre(n, x)
    laguerre(n, x)

    >>> diff(laguerre(n, x), x)
    -assoc_laguerre(n - 1, 1, x)

    See Also
    ========

    jacobi, gegenbauer,
    chebyshevt, chebyshevt_root, chebyshevu, chebyshevu_root,
    legendre, assoc_legendre,
    hermite,
    assoc_laguerre,
    sympy.polys.orthopolys.jacobi_poly
    sympy.polys.orthopolys.gegenbauer_poly
    sympy.polys.orthopolys.chebyshevt_poly
    sympy.polys.orthopolys.chebyshevu_poly
    sympy.polys.orthopolys.hermite_poly
    sympy.polys.orthopolys.legendre_poly
    sympy.polys.orthopolys.laguerre_poly

    References
    ==========

    .. [1] http://en.wikipedia.org/wiki/Laguerre_polynomial
    .. [2] http://mathworld.wolfram.com/LaguerrePolynomial.html
    .. [3] http://functions.wolfram.com/Polynomials/LaguerreL/
    .. [4] http://functions.wolfram.com/Polynomials/LaguerreL3/
    c             C   s   |j sb| r&t|t|d |  S |tjkr6tjS |tjkrFtjS |tjkrtj	| tj S n |j
rvtd| n| ||S d S )Nr&   z0The index n must be nonnegative integer (got %r))r<   r=   r   rZ   r   r7   r?   NegativeInfinityr@   r>   rN   rB   r%   )r!   r"   r   r#   r#   r$   rE   9  s    


zlaguerre.evalr3   c             C   sF   |dkrt | |n.|dkr8| j\}}t|d d| S t | |d S )Nr&   r3   )r   r(   assoc_laguerre)r*   rJ   r"   r   r#   r#   r$   rM   P  s    
zlaguerre.fdiffc             C   s\   ddl m} |js|jdkr$tdtd}t| |t|d  ||  }|||d|fS )Nr   )rG   Fz*Error: n should be a non-negative integer.rH   r3   )rI   rG   rN   r   rB   r   r   r
   )r*   r"   r   rG   rH   rO   r#   r#   r$   rP   [  s     z$laguerre._eval_rewrite_as_polynomialN)r3   )r,   r-   r.   r/   rS   r   r   r0   rE   rM   rP   r#   r#   r#   r$   rZ      s
   5
rZ   c               @   s6   e Zd ZdZedd ZdddZdd Zd	d
 ZdS )r\   a1  
    Returns the nth generalized Laguerre polynomial in x, :math:`L_n(x)`.

    Parameters
    ==========

    n : int
        Degree of Laguerre polynomial. Must be ``n >= 0``.

    alpha : Expr
        Arbitrary expression. For ``alpha=0`` regular Laguerre
        polynomials will be generated.

    Examples
    ========

    >>> from sympy import laguerre, assoc_laguerre, diff
    >>> from sympy.abc import x, n, a
    >>> assoc_laguerre(0, a, x)
    1
    >>> assoc_laguerre(1, a, x)
    a - x + 1
    >>> assoc_laguerre(2, a, x)
    a**2/2 + 3*a/2 + x**2/2 + x*(-a - 2) + 1
    >>> assoc_laguerre(3, a, x)
    a**3/6 + a**2 + 11*a/6 - x**3/6 + x**2*(a/2 + 3/2) +
        x*(-a**2/2 - 5*a/2 - 3) + 1

    >>> assoc_laguerre(n, a, 0)
    binomial(a + n, a)

    >>> assoc_laguerre(n, a, x)
    assoc_laguerre(n, a, x)

    >>> assoc_laguerre(n, 0, x)
    laguerre(n, x)

    >>> diff(assoc_laguerre(n, a, x), x)
    -assoc_laguerre(n - 1, a + 1, x)

    >>> diff(assoc_laguerre(n, a, x), a)
    Sum(assoc_laguerre(_k, a, x)/(-a + n), (_k, 0, n - 1))

    See Also
    ========

    jacobi, gegenbauer,
    chebyshevt, chebyshevt_root, chebyshevu, chebyshevu_root,
    legendre, assoc_legendre,
    hermite,
    laguerre,
    sympy.polys.orthopolys.jacobi_poly
    sympy.polys.orthopolys.gegenbauer_poly
    sympy.polys.orthopolys.chebyshevt_poly
    sympy.polys.orthopolys.chebyshevu_poly
    sympy.polys.orthopolys.hermite_poly
    sympy.polys.orthopolys.legendre_poly
    sympy.polys.orthopolys.laguerre_poly

    References
    ==========

    .. [1] http://en.wikipedia.org/wiki/Laguerre_polynomial#Assoc_laguerre_polynomials
    .. [2] http://mathworld.wolfram.com/AssociatedLaguerrePolynomial.html
    .. [3] http://functions.wolfram.com/Polynomials/LaguerreL/
    .. [4] http://functions.wolfram.com/Polynomials/LaguerreL3/
    c             C   s   |t jkrt||S |jsr|t jkr2t|| |S |t jkrV|t jkrVt j| t j S |t jkr|t jkrt jS n |jrt	d| nt
|||S d S )Nz0The index n must be nonnegative integer (got %r))r   r7   rZ   r<   r	   r@   r>   r[   rN   rB   r   )r!   r"   alphar   r#   r#   r$   rE     s    


zassoc_laguerre.evalr2   c             C   s   ddl m} |dkr t| |nt|dkr`| j\}}}td}|t|||||  |d|d fS |dkr| j\}}}t|d |d | S t| |d S )Nr   )rG   r&   r3   rH   r2   )rI   rG   r   r(   r   r\   )r*   rJ   rG   r"   r]   r   rH   r#   r#   r$   rM     s    $zassoc_laguerre.fdiffc             C   s   ddl m} |js|jdkr$tdtd}t| |t|t d t	|  ||  }t|t d t	| |||d|f S )Nr   )rG   Fz*Error: n should be a non-negative integer.rH   r&   )
rI   rG   rN   r   rB   r   r   r   r]   r
   )r*   r"   r   rG   rH   rO   r#   r#   r$   rP     s    ,z*assoc_laguerre._eval_rewrite_as_polynomialc             C   s"   | j \}}}| || | S )N)r(   r'   r)   )r*   r"   r]   r   r#   r#   r$   r+     s    zassoc_laguerre._eval_conjugateN)r2   )	r,   r-   r.   r/   r0   rE   rM   rP   r+   r#   r#   r#   r$   r\   e  s
   C

r\   N)5r/   Z
__future__r   r   Zsympy.core.singletonr   Z
sympy.corer   Zsympy.core.functionr   r   Zsympy.core.symbolr   Z(sympy.functions.combinatorial.factorialsr	   r
   r   Z$sympy.functions.elementary.complexesr   Z&sympy.functions.elementary.exponentialr   Z#sympy.functions.elementary.integersr   Z(sympy.functions.elementary.miscellaneousr   Z(sympy.functions.elementary.trigonometricr   Z'sympy.functions.special.gamma_functionsr   Zsympy.functions.special.hyperr   Zsympy.polys.orthopolysr   r   r   r   r   r   r   r   r   r1   rQ   r:   r6   r9   rT   rU   r8   r;   rY   rZ   r\   r#   r#   r#   r$   <module>   s<   $
 #? ls(,^k\e