a
    a                      @   s  d Z ddlZddlmZmZmZmZmZmZm	Z	m
Z
mZmZmZ ddlmZ ddlmZ ddlmZ ddlmZ ejZddlmZ g d	Zd
dddddddddddddddZg dZeee  e ddg ZG dd dejZdd  Z dnd"d#Z!dod$d%Z"dpd&d'Z#dqd(d)Z$drd*d+Z%dsd,d-Z&dtd.d/Z'dud0d1Z(dvd2d3Z)dwd5d6Z*d7d8 Z+d9d: Z,d;d< Z-d=d> Z.dxd?d@Z/dAdB Z0dydCdDZ1dzdEdFZ2d{dGdHZ3d|dIdJZ4d}dKdLZ5d~dMdNZ6ddOdPZ7ddQdRZ8ddSdTZ9ddUdVZ:ddWdXZ;ddYdZZ<dd[d\Z=dd]d^Z>dd_d`Z?ddadbZ@ddcddZAddedfZBddgdhZCddidjZDddkdlZEejFZFddmlmGZGmHZHmIZImJZJmKZKmLZLmMZMmNZNmOZOmPZPmQZQmRZRmSZSmTZTmUZUmVZV eW ZXeY D ] \ZZZ[eXeZ eXe[< e\e[ qdS )aI  
A collection of functions to find the weights and abscissas for
Gaussian Quadrature.

These calculations are done by finding the eigenvalues of a
tridiagonal matrix whose entries are dependent on the coefficients
in the recursion formula for the orthogonal polynomials with the
corresponding weighting function over the interval.

Many recursion relations for orthogonal polynomials are given:

.. math::

    a1n f_{n+1} (x) = (a2n + a3n x ) f_n (x) - a4n f_{n-1} (x)

The recursion relation of interest is

.. math::

    P_{n+1} (x) = (x - A_n) P_n (x) - B_n P_{n-1} (x)

where :math:`P` has a different normalization than :math:`f`.

The coefficients can be found as:

.. math::

    A_n = -a2n / a3n
    \qquad
    B_n = ( a4n / a3n \sqrt{h_n-1 / h_n})^2

where

.. math::

    h_n = \int_a^b w(x) f_n(x)^2

assume:

.. math::

    P_0 (x) = 1
    \qquad
    P_{-1} (x) == 0

For the mathematical background, see [golub.welsch-1969-mathcomp]_ and
[abramowitz.stegun-1965]_.

References
----------
.. [golub.welsch-1969-mathcomp]
   Golub, Gene H, and John H Welsch. 1969. Calculation of Gauss
   Quadrature Rules. *Mathematics of Computation* 23, 221-230+s1--s10.

.. [abramowitz.stegun-1965]
   Abramowitz, Milton, and Irene A Stegun. (1965) *Handbook of
   Mathematical Functions: with Formulas, Graphs, and Mathematical
   Tables*. Gaithersburg, MD: National Bureau of Standards.
   http://www.math.sfu.ca/~cbm/aands/

.. [townsend.trogdon.olver-2014]
   Townsend, A. and Trogdon, T. and Olver, S. (2014)
   *Fast computation of Gauss quadrature nodes and
   weights on the whole real line*. :arXiv:`1410.5286`.

.. [townsend.trogdon.olver-2015]
   Townsend, A. and Trogdon, T. and Olver, S. (2015)
   *Fast computation of Gauss quadrature nodes and
   weights on the whole real line*.
   IMA Journal of Numerical Analysis
   :doi:`10.1093/imanum/drv002`.
    N)expinfpisqrtfloorsincosaroundhstackarccosarange)linalg)airy   )_ufuncs)specfun)legendrechebytchebyuchebycchebysjacobilaguerregenlaguerrehermitehermitenorm
gegenbauersh_legendre	sh_chebyt	sh_chebyu	sh_jacobiZp_rootsZt_rootsZu_rootsZc_rootsZs_rootsZj_rootsZl_rootsZla_rootsZh_rootsZhe_rootsZcg_rootsZps_rootsZts_rootsZus_rootsZjs_roots)roots_legendreroots_chebytroots_chebyuroots_chebycroots_chebysroots_jacobiroots_laguerreroots_genlaguerreroots_hermiteroots_hermitenormroots_gegenbauerroots_sh_legendreroots_sh_chebytroots_sh_chebyuroots_sh_jacobi)eval_legendreeval_chebyteval_chebyueval_chebyceval_chebyseval_jacobieval_laguerreeval_genlaguerreeval_hermiteeval_hermitenormeval_gegenbauereval_sh_legendreeval_sh_chebyteval_sh_chebyueval_sh_jacobipochbinomc                   @   s&   e Zd Zd
ddZdd Zdd	 ZdS )orthopoly1dN      ?Fc	                    s   fddt tD }	t|}
|rT|  rD| fdd}|
t| }
d}tjdd}tj| |jt|  t	t
t|	| _| _|| _|
| _|| _d S )Nc                    s    g | ]}|  |  qS  rC   ).0k)rootsweightswfuncrC   h/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/site-packages/scipy/special/orthogonal.py
<listcomp>~   s   z(orthopoly1d.__init__.<locals>.<listcomp>c                    s    |  S NrC   x)evfknnrC   rI   <lambda>       z&orthopoly1d.__init__.<locals>.<lambda>rB   T)r)rangelenr   absnppoly1d__init__ZcoeffsfloatarraylistziprG   Zweight_funclimitsnormcoef
_eval_func)selfrF   rG   hnknrH   r]   monic	eval_funcZequiv_weightsmuZpolyrC   )rN   rO   rF   rG   rH   rI   rX   |   s$    
zorthopoly1d.__init__c                 C   s.   | j rt|tjs|  |S tj| |S d S rK   )r_   
isinstancerV   rW   __call__)r`   vrC   rC   rI   rg      s    
zorthopoly1d.__call__c                    sF   dkrd S |  j 9  _ | j  r4 fdd| _|  j9  _d S )NrB   c                    s    |  S rK   rC   rL   rN   prC   rI   rP      rQ   z$orthopoly1d._scale.<locals>.<lambda>)Z_coeffsr_   r^   )r`   rj   rC   ri   rI   _scale   s    zorthopoly1d._scale)NrB   rB   NNFN)__name__
__module____qualname__rX   rg   rk   rC   rC   rC   rI   rA   z   s
     
rA   c                 C   s  t j| dd}t d| f}	||dd |	dddf< |||	dddf< tj|	dd}
|| |
}|| |
}|
|| 8 }
|| d |
}|t |  }|t |  }d	||  }|r||ddd
  d }|
|
ddd
  d }
|||  9 }|r|
||fS |
|fS dS )a  [x,w] = gen_roots_and_weights(n,an_func,sqrt_bn_func,mu)

    Returns the roots (x) of an nth order orthogonal polynomial,
    and weights (w) to use in appropriate Gaussian quadrature with that
    orthogonal polynomial.

    The polynomials have the recurrence relation
          P_n+1(x) = (x - A_n) P_n(x) - B_n P_n-1(x)

    an_func(n)          should return A_n
    sqrt_bn_func(n)     should return sqrt(B_n)
    mu ( = h_0 )        is the integral of the weight over the orthogonal
                        interval
    d)Zdtype   r   Nr   T)Zoverwrite_a_bandrB   )rV   r   Zzerosr   Zeigvals_bandedrU   maxsum)nmu0an_funcbn_funcfdfZ
symmetrizere   rE   crM   yZdyZfmwrC   rC   rI   _gen_roots_and_weights   s&    


r}   Fc           
   	      s   t | }| dk s| |kr td|dks0|dkr8td|dkrR|dkrRt||S ||krjt||d |S d|| d  t|d |d  }| |  dkr fdd	}n fd
d	} fdd	} fdd	} fdd	}	t||||||	d|S )an  Gauss-Jacobi quadrature.

    Compute the sample points and weights for Gauss-Jacobi
    quadrature. The sample points are the roots of the nth degree
    Jacobi polynomial, :math:`P^{\alpha, \beta}_n(x)`. These sample
    points and weights correctly integrate polynomials of degree
    :math:`2n - 1` or less over the interval :math:`[-1, 1]` with
    weight function :math:`w(x) = (1 - x)^{\alpha} (1 +
    x)^{\beta}`. See 22.2.1 in [AS]_ for details.

    Parameters
    ----------
    n : int
        quadrature order
    alpha : float
        alpha must be > -1
    beta : float
        beta must be > -1
    mu : bool, optional
        If True, return the sum of the weights, optional.

    Returns
    -------
    x : ndarray
        Sample points
    w : ndarray
        Weights
    mu : float
        Sum of the weights

    See Also
    --------
    scipy.integrate.quadrature
    scipy.integrate.fixed_quad

    References
    ----------
    .. [AS] Milton Abramowitz and Irene A. Stegun, eds.
        Handbook of Mathematical Functions with Formulas,
        Graphs, and Mathematical Tables. New York: Dover, 1972.

    r   n must be a positive integer.rq   z'alpha and beta must be greater than -1.              ?       @c                    s"   t | dk  d    dS )Nr   rp   r   rV   whererE   abrC   rI   rP     rQ   zroots_jacobi.<locals>.<lambda>c                    sR   t | dk  d         d|     d|     d   S )Nr   rp   r   r   r   r   rC   rI   rP     s   2c              
      sv   dd|      t |   |   d|     d   t | dkdt | |     d|     d   S )Nr   rp   r   rB   )rV   r   r   r   r   rC   rI   rP     s   <6c                    s   t |  |S rK   cephesr5   rt   rM   r   rC   rI   rP     rQ   c                    s0   d|    d  t | d  d d | S )Nr   r   r   r   r   rC   rI   rP     s   F)int
ValueErrorr!   r+   r   betar}   )
rt   alphar   re   mru   rv   rw   rx   ry   rC   r   rI   r&      s&    +
$r&   c                    s  dk rt d fdd}dkr@tg g dd|d|tjdS t dd	\}}}  d }d
| d
 |  t  d  }	|	t d td  t|  9 }	td
 | d  td  t|  }
t|||	|
|d| fdd}|S )a  Jacobi polynomial.

    Defined to be the solution of

    .. math::
        (1 - x^2)\frac{d^2}{dx^2}P_n^{(\alpha, \beta)}
          + (\beta - \alpha - (\alpha + \beta + 2)x)
            \frac{d}{dx}P_n^{(\alpha, \beta)}
          + n(n + \alpha + \beta + 1)P_n^{(\alpha, \beta)} = 0

    for :math:`\alpha, \beta > -1`; :math:`P_n^{(\alpha, \beta)}` is a
    polynomial of degree :math:`n`.

    Parameters
    ----------
    n : int
        Degree of the polynomial.
    alpha : float
        Parameter, must be greater than -1.
    beta : float
        Parameter, must be greater than -1.
    monic : bool, optional
        If `True`, scale the leading coefficient to be 1. Default is
        `False`.

    Returns
    -------
    P : orthopoly1d
        Jacobi polynomial.

    Notes
    -----
    For fixed :math:`\alpha, \beta`, the polynomials
    :math:`P_n^{(\alpha, \beta)}` are orthogonal over :math:`[-1, 1]`
    with weight function :math:`(1 - x)^\alpha(1 + x)^\beta`.

    r   n must be nonnegative.c                    s   d|    d|    S Nr   rC   rL   )r   r   rC   rI   rP   D  rQ   zjacobi.<locals>.<lambda>rB   rq   r   rd   Tre   rp   r   r   c                    s   t  | S rK   )r5   rL   r   r   rt   rC   rI   rP   O  rQ   )r   rA   rV   	ones_liker&   _gam)rt   r   r   rc   rH   rM   r|   re   Zab1ra   rb   rj   rC   r   rI   r     s     &$,0r   c                 C   sv   || dks|dkrt dt| || |d d\}}}|d d }d| }|| }|| }|rj|||fS ||fS dS )	ap  Gauss-Jacobi (shifted) quadrature.

    Compute the sample points and weights for Gauss-Jacobi (shifted)
    quadrature. The sample points are the roots of the nth degree
    shifted Jacobi polynomial, :math:`G^{p,q}_n(x)`. These sample
    points and weights correctly integrate polynomials of degree
    :math:`2n - 1` or less over the interval :math:`[0, 1]` with
    weight function :math:`w(x) = (1 - x)^{p-q} x^{q-1}`. See 22.2.2
    in [AS]_ for details.

    Parameters
    ----------
    n : int
        quadrature order
    p1 : float
        (p1 - q1) must be > -1
    q1 : float
        q1 must be > 0
    mu : bool, optional
        If True, return the sum of the weights, optional.

    Returns
    -------
    x : ndarray
        Sample points
    w : ndarray
        Weights
    mu : float
        Sum of the weights

    See Also
    --------
    scipy.integrate.quadrature
    scipy.integrate.fixed_quad

    References
    ----------
    .. [AS] Milton Abramowitz and Irene A. Stegun, eds.
        Handbook of Mathematical Functions with Formulas,
        Graphs, and Mathematical Tables. New York: Dover, 1972.

    rq   r   z>(p - q) must be greater than -1, and q must be greater than 0.r   Trp   r   N)r   r&   )rt   p1Zq1re   rM   r|   r   ZscalerC   rC   rI   r/   U  s    +
r/   c                    s    dk rt dfdd} dkr@tg g dd|d|tjdS  }t|\}}t d t   t   t   d  }|d	   td	   d	   }d}	t||||	|d
| fddd}
|
S )a  Shifted Jacobi polynomial.

    Defined by

    .. math::

        G_n^{(p, q)}(x)
          = \binom{2n + p - 1}{n}^{-1}P_n^{(p - q, q - 1)}(2x - 1),

    where :math:`P_n^{(\cdot, \cdot)}` is the nth Jacobi polynomial.

    Parameters
    ----------
    n : int
        Degree of the polynomial.
    p : float
        Parameter, must have :math:`p > q - 1`.
    q : float
        Parameter, must be greater than 0.
    monic : bool, optional
        If `True`, scale the leading coefficient to be 1. Default is
        `False`.

    Returns
    -------
    G : orthopoly1d
        Shifted Jacobi polynomial.

    Notes
    -----
    For fixed :math:`p, q`, the polynomials :math:`G_n^{(p, q)}` are
    orthogonal over :math:`[0, 1]` with weight function :math:`(1 -
    x)^{p - q}x^{q - 1}`.

    r   r   c                    s   d|     | d   S NrB   rC   rL   )rj   qrC   rI   rP     rQ   zsh_jacobi.<locals>.<lambda>rB   r   r   r   rp   r   r   c                    s   t  | S rK   )r>   rL   rt   rj   r   rC   rI   rP     rQ   rH   r]   rc   rd   )r   rA   rV   r   r/   r   )rt   rj   r   rc   rH   n1rM   r|   ra   rb   pprC   r   rI   r      s     $8$r    c              	      s   t | }| dk s| |kr td dk r0tdt d }|dkr|t d gd}t|gd}|rt|||fS ||fS  fdd} fd	d} fd
d}	 fdd}
t|||||	|
d|S )a[  Gauss-generalized Laguerre quadrature.

    Compute the sample points and weights for Gauss-generalized
    Laguerre quadrature. The sample points are the roots of the nth
    degree generalized Laguerre polynomial, :math:`L^{\alpha}_n(x)`.
    These sample points and weights correctly integrate polynomials of
    degree :math:`2n - 1` or less over the interval :math:`[0,
    \infty]` with weight function :math:`w(x) = x^{\alpha}
    e^{-x}`. See 22.3.9 in [AS]_ for details.

    Parameters
    ----------
    n : int
        quadrature order
    alpha : float
        alpha must be > -1
    mu : bool, optional
        If True, return the sum of the weights, optional.

    Returns
    -------
    x : ndarray
        Sample points
    w : ndarray
        Weights
    mu : float
        Sum of the weights

    See Also
    --------
    scipy.integrate.quadrature
    scipy.integrate.fixed_quad

    References
    ----------
    .. [AS] Milton Abramowitz and Irene A. Stegun, eds.
        Handbook of Mathematical Functions with Formulas,
        Graphs, and Mathematical Tables. New York: Dover, 1972.

    r   r~   rq   zalpha must be greater than -1.rB   ro   c                    s   d|    d S Nrp   r   rC   r   r   rC   rI   rP     rQ   z#roots_genlaguerre.<locals>.<lambda>c                    s   t | |     S rK   rV   r   r   r   rC   rI   rP     rQ   c                    s   t |  |S rK   r   r7   r   r   rC   rI   rP     rQ   c                    s0   | t |  | |   t | d  |  | S r   r   r   r   rC   rI   rP      s   F)r   r   r   gammarV   rZ   r}   )rt   r   re   r   ru   rM   r|   rv   rw   rx   ry   rC   r   rI   r(     s"    )
r(   c           
         s    dkrt ddk r t ddkr2d }n}t| \}} fdd}dkrbg g  }}t  d td  }d td  }t|||||dtf| fdd}	|	S )	a  Generalized (associated) Laguerre polynomial.

    Defined to be the solution of

    .. math::
        x\frac{d^2}{dx^2}L_n^{(\alpha)}
          + (\alpha + 1 - x)\frac{d}{dx}L_n^{(\alpha)}
          + nL_n^{(\alpha)} = 0,

    where :math:`\alpha > -1`; :math:`L_n^{(\alpha)}` is a polynomial
    of degree :math:`n`.

    Parameters
    ----------
    n : int
        Degree of the polynomial.
    alpha : float
        Parameter, must be greater than -1.
    monic : bool, optional
        If `True`, scale the leading coefficient to be 1. Default is
        `False`.

    Returns
    -------
    L : orthopoly1d
        Generalized Laguerre polynomial.

    Notes
    -----
    For fixed :math:`\alpha`, the polynomials :math:`L_n^{(\alpha)}`
    are orthogonal over :math:`[0, \infty)` with weight function
    :math:`e^{-x}x^\alpha`.

    The Laguerre polynomials are the special case where :math:`\alpha
    = 0`.

    See Also
    --------
    laguerre : Laguerre polynomial.

    rq   zalpha must be > -1r   r   r   c                    s   t |  |    S rK   r   rL   r   rC   rI   rP   9  rQ   zgenlaguerre.<locals>.<lambda>c                    s   t  | S rK   )r7   rL   r   rt   rC   rI   rP   ?  rQ   )r   r(   r   rA   r   )
rt   r   rc   r   rM   r|   rH   ra   rb   rj   rC   r   rI   r     s"    *

r   c                 C   s   t | d|dS )a  Gauss-Laguerre quadrature.

    Compute the sample points and weights for Gauss-Laguerre
    quadrature. The sample points are the roots of the nth degree
    Laguerre polynomial, :math:`L_n(x)`. These sample points and
    weights correctly integrate polynomials of degree :math:`2n - 1`
    or less over the interval :math:`[0, \infty]` with weight function
    :math:`w(x) = e^{-x}`. See 22.2.13 in [AS]_ for details.

    Parameters
    ----------
    n : int
        quadrature order
    mu : bool, optional
        If True, return the sum of the weights, optional.

    Returns
    -------
    x : ndarray
        Sample points
    w : ndarray
        Weights
    mu : float
        Sum of the weights

    See Also
    --------
    scipy.integrate.quadrature
    scipy.integrate.fixed_quad
    numpy.polynomial.laguerre.laggauss

    References
    ----------
    .. [AS] Milton Abramowitz and Irene A. Stegun, eds.
        Handbook of Mathematical Functions with Formulas,
        Graphs, and Mathematical Tables. New York: Dover, 1972.

    r   r   )r(   )rt   re   rC   rC   rI   r'   E  s    'r'   c                    s    dk rt d dkr" d }n }t|\}} dkrDg g  }}d}d  t d  }t||||dd dtf| fdd}|S )	aR  Laguerre polynomial.

    Defined to be the solution of

    .. math::
        x\frac{d^2}{dx^2}L_n + (1 - x)\frac{d}{dx}L_n + nL_n = 0;

    :math:`L_n` is a polynomial of degree :math:`n`.

    Parameters
    ----------
    n : int
        Degree of the polynomial.
    monic : bool, optional
        If `True`, scale the leading coefficient to be 1. Default is
        `False`.

    Returns
    -------
    L : orthopoly1d
        Laguerre Polynomial.

    Notes
    -----
    The polynomials :math:`L_n` are orthogonal over :math:`[0,
    \infty)` with weight function :math:`e^{-x}`.

    r   r   r   rB   rq   c                 S   s
   t |  S rK   r   rL   rC   rC   rI   rP     rQ   zlaguerre.<locals>.<lambda>c                    s
   t  | S rK   )r6   rL   rt   rC   rI   rP     rQ   )r   r'   r   rA   r   rt   rc   r   rM   r|   ra   rb   rj   rC   r   rI   r   o  s    


r   c           
   	   C   s   t | }| dk s| |kr tdttj}| dkrhdd }dd }tj}dd }t||||||d|S t|\}}	|r||	|fS ||	fS d	S )
a=  Gauss-Hermite (physicist's) quadrature.

    Compute the sample points and weights for Gauss-Hermite
    quadrature. The sample points are the roots of the nth degree
    Hermite polynomial, :math:`H_n(x)`. These sample points and
    weights correctly integrate polynomials of degree :math:`2n - 1`
    or less over the interval :math:`[-\infty, \infty]` with weight
    function :math:`w(x) = e^{-x^2}`. See 22.2.14 in [AS]_ for
    details.

    Parameters
    ----------
    n : int
        quadrature order
    mu : bool, optional
        If True, return the sum of the weights, optional.

    Returns
    -------
    x : ndarray
        Sample points
    w : ndarray
        Weights
    mu : float
        Sum of the weights

    Notes
    -----
    For small n up to 150 a modified version of the Golub-Welsch
    algorithm is used. Nodes are computed from the eigenvalue
    problem and improved by one step of a Newton iteration.
    The weights are computed from the well-known analytical formula.

    For n larger than 150 an optimal asymptotic algorithm is applied
    which computes nodes and weights in a numerically stable manner.
    The algorithm has linear runtime making computation for very
    large n (several thousand or more) feasible.

    See Also
    --------
    scipy.integrate.quadrature
    scipy.integrate.fixed_quad
    numpy.polynomial.hermite.hermgauss
    roots_hermitenorm

    References
    ----------
    .. [townsend.trogdon.olver-2014]
        Townsend, A. and Trogdon, T. and Olver, S. (2014)
        *Fast computation of Gauss quadrature nodes and
        weights on the whole real line*. :arXiv:`1410.5286`.
    .. [townsend.trogdon.olver-2015]
        Townsend, A. and Trogdon, T. and Olver, S. (2015)
        *Fast computation of Gauss quadrature nodes and
        weights on the whole real line*.
        IMA Journal of Numerical Analysis
        :doi:`10.1093/imanum/drv002`.
    .. [AS] Milton Abramowitz and Irene A. Stegun, eds.
        Handbook of Mathematical Functions with Formulas,
        Graphs, and Mathematical Tables. New York: Dover, 1972.

    r   r~      c                 S   s   d|  S Nr   rC   r   rC   rC   rI   rP     rQ   zroots_hermite.<locals>.<lambda>c                 S   s   t | d S Nr   r   r   rC   rC   rI   rP     rQ   c                 S   s   d|  t | d | S )Nr   r   )r   r8   r   rC   rC   rI   rP     rQ   TN)	r   r   rV   r   r   r   r8   r}   _roots_hermite_asy
rt   re   r   ru   rv   rw   rx   ry   nodesrG   rC   rC   rI   r)     s    ?
r)      c                    s   | d d }dt | d  d|  d t dt | d  d|  d    fdd}dd }dt }t|D ]}|||||  }ql|S )	a  Helper function for Tricomi initial guesses

    For details, see formula 3.1 in lemma 3.1 in the
    original paper.

    Parameters
    ----------
    n : int
        Quadrature order
    k : ndarray of type int
        Index of roots :math:`	au_k` to compute
    maxit : int
        Number of Newton maxit performed, the default
        value of 5 is sufficient.

    Returns
    -------
    tauk : ndarray
        Roots of equation 3.1

    See Also
    --------
    initial_nodes_a
    roots_hermite_asy
    rp   r         @r         @c                    s   | t |    S rK   )r   rL   rz   rC   rI   rP     rQ   z_compute_tauk.<locals>.<lambda>c                 S   s   dt |  S r   )r   rL   rC   rC   rI   rP     rQ   )r   r   rS   )rt   rE   maxitr   rx   ry   xiirC   r   rI   _compute_tauk  s    <r   c                 C   s~   t | |}td| d }| d d }dt| d  d|  d }|| dd|  ddd| d   dd|   d   }|S )	ae  Tricomi initial guesses

    Computes an initial approximation to the square of the `k`-th
    (positive) root :math:`x_k` of the Hermite polynomial :math:`H_n`
    of order :math:`n`. The formula is the one from lemma 3.1 in the
    original paper. The guesses are accurate except in the region
    near :math:`\sqrt{2n + 1}`.

    Parameters
    ----------
    n : int
        Quadrature order
    k : ndarray of type int
        Index of roots to compute

    Returns
    -------
    xksq : ndarray
        Square of the approximate roots

    See Also
    --------
    initial_nodes
    roots_hermite_asy
    r   rp   r   r   rB   r   g      @      ?)r   r   r   )rt   rE   ZtaukZsigkr   nuxksqrC   rC   rI   _initial_nodes_a  s    
8r   c                 C   s   | d d }dt | d  d|  d }t| dd ddd }|d	| |d
   d|d  |d   dd|d   |d   d| d|d   d	 |d   d|d  d|d   d |d   }|S )ab  Gatteschi initial guesses

    Computes an initial approximation to the square of the kth
    (positive) root :math:`x_k` of the Hermite polynomial :math:`H_n`
    of order :math:`n`. The formula is the one from lemma 3.2 in the
    original paper. The guesses are accurate in the region just
    below :math:`\sqrt{2n + 1}`.

    Parameters
    ----------
    n : int
        Quadrature order
    k : ndarray of type int
        Index of roots to compute

    Returns
    -------
    xksq : ndarray
        Square of the approximate root

    See Also
    --------
    initial_nodes
    roots_hermite_asy
    rp   r   r   r   r   r   Nrq   g<n=e?UUUUUU?g<=a ?gUUUUUUտgPuPu?gE'危?   g      gHg΄?g`A?   gg^PTxt?r   gJ_?gr(?g)r   r   Zairyzorr   )rt   rE   r   r   Zakr   rC   rC   rI   _initial_nodes_b8  s    "r   c                 C   s   d|  d }t |t}tdtt| d d }|ddd }t| |d|d  }t| ||d d }tt||g}| d dkrtd|g}|S )	a  Initial guesses for the Hermite roots

    Computes an initial approximation to the non-negative
    roots :math:`x_k` of the Hermite polynomial :math:`H_n`
    of order :math:`n`. The Tricomi and Gatteschi initial
    guesses are used in the region where they are accurate.

    Parameters
    ----------
    n : int
        Quadrature order

    Returns
    -------
    xk : ndarray
        Approximate roots

    See Also
    --------
    roots_hermite_asy
    g_Dji?g
Ϯ@r   r   Nrq   rp   r   )	r	   Zastyper   r   r   r   r   r   r
   )rt   ZfitZturnoveriaibZxasqZxbsqivrC   rC   rI   _initial_nodes`  s    r   c           6      C   s  t |}t|}d|  d }d| d| |  }d| d d  }| |d  d }d}d}	d	}
d
}d}d}d}d}d}d}d}d}|tdd }d}d|dddf  d|  d }d|dddf  d|dddf   d d }d|dddf  d|d ddf   d!|d"ddf   d#|dddf   d$|  d% }d&|d'ddf  d(|d)ddf   d*|d+ddf   d,|dddf   d-|dddf   d. d/ }d0|d1ddf  d2|d3ddf   d4|d5ddf   d6|dddf   d7|d ddf   d8|d"ddf   d9|dddf   d:|  d; }d}d|dddf  d|  d }d<|dddf  d=|dddf   d> d }d|dddf  d|d ddf   d?|d"ddf   d@|dddf   d$|  d% }dA|d'ddf  dB|d)ddf   dC|d+ddf   dD|dddf   dE|dddf   dF d/ }d0|d1ddf  d2|d3ddf   dG|d5ddf   dH|dddf   dI|d ddf   dJ|d"ddf   dK|dddf   dL|  d; } t|d | \}!}"}#}$dtt |dM  | }%|td+dNd+d }&|| }'|| |&dOddf | |  |&dPddf | |  |d  }(|| |&dOddf | |  |&dPddf | |  |&dddf | |  |&dddf | |  |d+  })|	| |&dOddf | |   |d  }*|| |&dOddf |
 |  |&dPddf |	 |  |&dddf | |   |d"  }+|| |&dOddf | |  |&dPddf | |  |&dddf |
 |  |&dddf |	 |  |&dddf | |   |d)  },|%|!|'|(|d   |)|dQ    |"|*|+|d   |,|dQ    |dR    }-tdt |dS  | }.|| |&dOddf | |   | }/|| |&dOddf | |  |&dPddf | |  |&dddf | |   |d  }0|| |&dOddf | |  |&dPddf | |  |&dddf | |  |&dddf | |  |&dddf | |    |d   }1|| }2|
| |&dOddf |	 |  |&dPddf | |  |d  }3|| |&dOddf | |  |&dPddf |
 |  |&dddf |	 |  |&dddf | |  |d+  }4|.|!|/|0|d   |1|dQ    |d  |"|2|3|d   |4|dQ      }5|-|5fS )Ta  Asymptotic series expansion of parabolic cylinder function

    The implementation is based on sections 3.2 and 3.3 from the
    original paper. Compared to the published version this code
    adds one more term to the asymptotic series. The detailed
    formulas can be found at [parabolic-asymptotics]_. The evaluation
    is done in a transformed variable :math:`\theta := \arccos(t)`
    where :math:`t := x / \mu` and :math:`\mu := \sqrt{2n + 1}`.

    Parameters
    ----------
    n : int
        Quadrature order
    theta : ndarray
        Transformed position variable

    Returns
    -------
    U : ndarray
        Value of the parabolic cylinder function :math:`U(a, \theta)`.
    Ud : ndarray
        Value of the derivative :math:`U^{\prime}(a, \theta)` of
        the parabolic cylinder function.

    See Also
    --------
    roots_hermite_asy

    References
    ----------
    .. [parabolic-asymptotics]
       https://dlmf.nist.gov/12.10#vii
    r   rB   r   r   gUUUUUU?rp   r   g?g98c?gHxi?gd?g_cJ6?g¿grqGgk~XX¿g@9SsMԿg:(,a(   r   r   Ng      @g      8@g      "r   g      o@g      b@g      @g     	   g    @@   g    @r   g    ؍Ag    ЦAg     PAg    @@
   g    A   g    0A   g   8JAg   H;Ag    \hAg     Ag   夓A   g   djA   g  m6A   g   Ag  P/Ag  f!Bg  ݱ;Bg  hdBg    Ag      .@g     pt@g     a@g    `@g    Ag    g    j Ag    Ag   H$iAg   Ai g  .Ag  (Ag  ӍAg  5e Bg  d{>Bl   +2V gUUUUUU?   r   r   r   gUUUUUU?r   )r   r   r   Zreshaper   r   r   )6rt   thetastctre   etazetaphiZa0Za1Za2a3Za4Za5Zb0b1b2Zb3Zb4Zb5ZctpZu0u1u2u3Zu4Zu5Zv0v1v2Zv3Zv4Zv5ZAiZAipZBiZBipPZphipZA0A1A2B0ZB1ZB2UZPdZC0ZC1ZC2ZD0ZD1ZD2ZUdrC   rC   rI   _pbcf  s    " 0\lb 0\lb@p*Z &&Z@p(r   c                 C   s   t d|  d }|| }t|}t|D ]J}t| |\}}|t d| t| |  }	||	 }tt|	dk r( qtq(|t| }
| d dkrd|
d< t|
d  d|d   }|
|fS )a+  Newton iteration for polishing the asymptotic approximation
    to the zeros of the Hermite polynomials.

    Parameters
    ----------
    n : int
        Quadrature order
    x_initial : ndarray
        Initial guesses for the roots
    maxit : int
        Maximal number of Newton iterations.
        The default 5 is sufficient, usually
        only one or two steps are needed.

    Returns
    -------
    nodes : ndarray
        Quadrature nodes
    weights : ndarray
        Quadrature weights

    See Also
    --------
    roots_hermite_asy
    r   rB   g+=rp   r   r   r   )	r   r   rS   r   r   rr   rU   r   r   )rt   Z	x_initialr   re   tr   r   uZudZdthetarM   r|   rC   rC   rI   _newton  s    r   c                 C   s   t | }t| |\}}| d dkrRt|ddd  |g}t|ddd |g}n.t|ddd  |g}t|ddd |g}|ttt| 9 }||fS )a,  Gauss-Hermite (physicist's) quadrature for large n.

    Computes the sample points and weights for Gauss-Hermite quadrature.
    The sample points are the roots of the nth degree Hermite polynomial,
    :math:`H_n(x)`. These sample points and weights correctly integrate
    polynomials of degree :math:`2n - 1` or less over the interval
    :math:`[-\infty, \infty]` with weight function :math:`f(x) = e^{-x^2}`.

    This method relies on asymptotic expansions which work best for n > 150.
    The algorithm has linear runtime making computation for very large n
    feasible.

    Parameters
    ----------
    n : int
        quadrature order

    Returns
    -------
    nodes : ndarray
        Quadrature nodes
    weights : ndarray
        Quadrature weights

    See Also
    --------
    roots_hermite

    References
    ----------
    .. [townsend.trogdon.olver-2014]
       Townsend, A. and Trogdon, T. and Olver, S. (2014)
       *Fast computation of Gauss quadrature nodes and
       weights on the whole real line*. :arXiv:`1410.5286`.

    .. [townsend.trogdon.olver-2015]
       Townsend, A. and Trogdon, T. and Olver, S. (2015)
       *Fast computation of Gauss quadrature nodes and
       weights on the whole real line*.
       IMA Journal of Numerical Analysis
       :doi:`10.1093/imanum/drv002`.
    rp   r   Nrq   )r   r   r
   r   r   rs   )rt   r   r   rG   rC   rC   rI   r   $  s    +r   c           	         s    dk rt d dkr" d }n }t|\}}dd } dkrLg g  }}d  t d  tt }d  }t|||||t tf| fdd}|S )a/  Physicist's Hermite polynomial.

    Defined by

    .. math::

        H_n(x) = (-1)^ne^{x^2}\frac{d^n}{dx^n}e^{-x^2};

    :math:`H_n` is a polynomial of degree :math:`n`.

    Parameters
    ----------
    n : int
        Degree of the polynomial.
    monic : bool, optional
        If `True`, scale the leading coefficient to be 1. Default is
        `False`.

    Returns
    -------
    H : orthopoly1d
        Hermite polynomial.

    Notes
    -----
    The polynomials :math:`H_n` are orthogonal over :math:`(-\infty,
    \infty)` with weight function :math:`e^{-x^2}`.

    Examples
    --------
    >>> from scipy import special
    >>> import matplotlib.pyplot as plt
    >>> import numpy as np

    >>> p_monic = special.hermite(3, monic=True)
    >>> p_monic
    poly1d([ 1. ,  0. , -1.5,  0. ])
    >>> p_monic(1)
    -0.49999999999999983
    >>> x = np.linspace(-3, 3, 400)
    >>> y = p_monic(x)
    >>> plt.plot(x, y)
    >>> plt.title("Monic Hermite polynomial of degree 3")
    >>> plt.xlabel("x")
    >>> plt.ylabel("H_3(x)")
    >>> plt.show()

    r   r   r   c                 S   s   t |  |  S rK   r   rL   rC   rC   rI   rP     rQ   zhermite.<locals>.<lambda>rp   c                    s
   t  | S rK   )r8   rL   r   rC   rI   rP     rQ   )r   r)   r   r   r   rA   r   	rt   rc   r   rM   r|   rH   ra   rb   rj   rC   r   rI   r   ]  s    1


r   c           
   	   C   s   t | }| dk s| |kr tdtdtj }| dkrldd }dd }tj}dd }t||||||d	|S t|\}}	|td
9 }|	td
9 }	|r||	|fS ||	fS dS )aL  Gauss-Hermite (statistician's) quadrature.

    Compute the sample points and weights for Gauss-Hermite
    quadrature. The sample points are the roots of the nth degree
    Hermite polynomial, :math:`He_n(x)`. These sample points and
    weights correctly integrate polynomials of degree :math:`2n - 1`
    or less over the interval :math:`[-\infty, \infty]` with weight
    function :math:`w(x) = e^{-x^2/2}`. See 22.2.15 in [AS]_ for more
    details.

    Parameters
    ----------
    n : int
        quadrature order
    mu : bool, optional
        If True, return the sum of the weights, optional.

    Returns
    -------
    x : ndarray
        Sample points
    w : ndarray
        Weights
    mu : float
        Sum of the weights

    Notes
    -----
    For small n up to 150 a modified version of the Golub-Welsch
    algorithm is used. Nodes are computed from the eigenvalue
    problem and improved by one step of a Newton iteration.
    The weights are computed from the well-known analytical formula.

    For n larger than 150 an optimal asymptotic algorithm is used
    which computes nodes and weights in a numerical stable manner.
    The algorithm has linear runtime making computation for very
    large n (several thousand or more) feasible.

    See Also
    --------
    scipy.integrate.quadrature
    scipy.integrate.fixed_quad
    numpy.polynomial.hermite_e.hermegauss

    References
    ----------
    .. [AS] Milton Abramowitz and Irene A. Stegun, eds.
        Handbook of Mathematical Functions with Formulas,
        Graphs, and Mathematical Tables. New York: Dover, 1972.

    r   r~   r   r   c                 S   s   d|  S r   rC   r   rC   rC   rI   rP     rQ   z#roots_hermitenorm.<locals>.<lambda>c                 S   s
   t | S rK   r   r   rC   rC   rI   rP     rQ   c                 S   s   | t | d | S r   )r   r9   r   rC   rC   rI   rP     rQ   Trp   N)	r   r   rV   r   r   r   r9   r}   r   r   rC   rC   rI   r*     s     4
r*   c           	         s    dk rt d dkr" d }n }t|\}}dd } dkrLg g  }}tdt t d  }d}t|||||t tf| fddd	}|S )
ab  Normalized (probabilist's) Hermite polynomial.

    Defined by

    .. math::

        He_n(x) = (-1)^ne^{x^2/2}\frac{d^n}{dx^n}e^{-x^2/2};

    :math:`He_n` is a polynomial of degree :math:`n`.

    Parameters
    ----------
    n : int
        Degree of the polynomial.
    monic : bool, optional
        If `True`, scale the leading coefficient to be 1. Default is
        `False`.

    Returns
    -------
    He : orthopoly1d
        Hermite polynomial.

    Notes
    -----

    The polynomials :math:`He_n` are orthogonal over :math:`(-\infty,
    \infty)` with weight function :math:`e^{-x^2/2}`.

    r   r   r   c                 S   s   t |  |  d S r   r   rL   rC   rC   rI   rP     rQ   zhermitenorm.<locals>.<lambda>rp   rB   c                    s
   t  | S rK   )r9   rL   r   rC   rI   rP     rQ   r   )r   r*   r   r   r   rA   r   r   rC   r   rI   r     s    


r   c           	   	      s   t | }| dk s| |kr td dk r2tdn dkrDt| |S ttjt d  t d  }dd } fd	d} fd
d} fdd}t||||||d|S )aG  Gauss-Gegenbauer quadrature.

    Compute the sample points and weights for Gauss-Gegenbauer
    quadrature. The sample points are the roots of the nth degree
    Gegenbauer polynomial, :math:`C^{\alpha}_n(x)`. These sample
    points and weights correctly integrate polynomials of degree
    :math:`2n - 1` or less over the interval :math:`[-1, 1]` with
    weight function :math:`w(x) = (1 - x^2)^{\alpha - 1/2}`. See
    22.2.3 in [AS]_ for more details.

    Parameters
    ----------
    n : int
        quadrature order
    alpha : float
        alpha must be > -0.5
    mu : bool, optional
        If True, return the sum of the weights, optional.

    Returns
    -------
    x : ndarray
        Sample points
    w : ndarray
        Weights
    mu : float
        Sum of the weights

    See Also
    --------
    scipy.integrate.quadrature
    scipy.integrate.fixed_quad

    References
    ----------
    .. [AS] Milton Abramowitz and Irene A. Stegun, eds.
        Handbook of Mathematical Functions with Formulas,
        Graphs, and Mathematical Tables. New York: Dover, 1972.

    r   r~   g      z alpha must be greater than -0.5.r   r   c                 S   s   d|  S r   rC   r   rC   rC   rI   rP   W  rQ   z"roots_gegenbauer.<locals>.<lambda>c                    s2   t | | d   d  d|    |   d   S )Nrp   r   r   r   r   r   rC   rI   rP   X  s   c                    s   t |  |S rK   r   r:   r   r   rC   rI   rP   Z  rQ   c                    sF   |  | t |  | | d   d t | d  |  d|d   S r   r   r   r   rC   rI   rP   [  s    
T)	r   r   r"   rV   r   r   r   r   r}   )	rt   r   re   r   ru   rv   rw   rx   ry   rC   r   rI   r+   !  s    )

(r+   c                    sz   t  d  d |d}|r |S td   t d  td   t d   }||  fdd|jd< |S )am  Gegenbauer (ultraspherical) polynomial.

    Defined to be the solution of

    .. math::
        (1 - x^2)\frac{d^2}{dx^2}C_n^{(\alpha)}
          - (2\alpha + 1)x\frac{d}{dx}C_n^{(\alpha)}
          + n(n + 2\alpha)C_n^{(\alpha)} = 0

    for :math:`\alpha > -1/2`; :math:`C_n^{(\alpha)}` is a polynomial
    of degree :math:`n`.

    Parameters
    ----------
    n : int
        Degree of the polynomial.
    alpha : float
        Parameter, must be greater than -0.5.
    monic : bool, optional
        If `True`, scale the leading coefficient to be 1. Default is
        `False`.

    Returns
    -------
    C : orthopoly1d
        Gegenbauer polynomial.

    Notes
    -----
    The polynomials :math:`C_n^{(\alpha)}` are orthogonal over
    :math:`[-1,1]` with weight function :math:`(1 - x^2)^{(\alpha -
    1/2)}`.

    Examples
    --------
    >>> from scipy import special
    >>> import matplotlib.pyplot as plt

    We can initialize a variable ``p`` as a Gegenbauer polynomial using the
    `gegenbauer` function and evaluate at a point ``x = 1``.

    >>> p = special.gegenbauer(3, 0.5, monic=False)
    >>> p
    poly1d([ 2.5,  0. , -1.5,  0. ])
    >>> p(1)
    1.0

    To evaluate ``p`` at various points ``x`` in the interval ``(-3, 3)``,
    simply pass an array ``x`` to ``p`` as follows:

    >>> x = np.linspace(-3, 3, 400)
    >>> y = p(x)

    We can then visualize ``x, y`` using `matplotlib.pyplot`.

    >>> fig, ax = plt.subplots()
    >>> ax.plot(x, y)
    >>> ax.set_title("Gegenbauer (ultraspherical) polynomial of degree 3")
    >>> ax.set_xlabel("x")
    >>> ax.set_ylabel("G_3(x)")
    >>> plt.show()

    r   rc   rp   c                    s   t t | S rK   )r:   rY   rL   r   rC   rI   rP     rQ   zgegenbauer.<locals>.<lambda>r_   )r   r   rk   __dict__)rt   r   rc   basefactorrC   r   rI   r   `  s    @

r   c                 C   sl   t | }| dk s| |kr tdtt| d |dd|  }t|t| }|r`||tfS ||fS dS )aM  Gauss-Chebyshev (first kind) quadrature.

    Computes the sample points and weights for Gauss-Chebyshev
    quadrature. The sample points are the roots of the nth degree
    Chebyshev polynomial of the first kind, :math:`T_n(x)`. These
    sample points and weights correctly integrate polynomials of
    degree :math:`2n - 1` or less over the interval :math:`[-1, 1]`
    with weight function :math:`w(x) = 1/\sqrt{1 - x^2}`. See 22.2.4
    in [AS]_ for more details.

    Parameters
    ----------
    n : int
        quadrature order
    mu : bool, optional
        If True, return the sum of the weights, optional.

    Returns
    -------
    x : ndarray
        Sample points
    w : ndarray
        Weights
    mu : float
        Sum of the weights

    See Also
    --------
    scipy.integrate.quadrature
    scipy.integrate.fixed_quad
    numpy.polynomial.chebyshev.chebgauss

    References
    ----------
    .. [AS] Milton Abramowitz and Irene A. Stegun, eds.
        Handbook of Mathematical Functions with Formulas,
        Graphs, and Mathematical Tables. New York: Dover, 1972.

    r   r~   rp   N)r   r   r   Z_sinpirV   r   Z	full_liker   )rt   re   r   rM   r|   rC   rC   rI   r"     s    ("
r"   c           
         s    dk rt ddd } dkr>tg g td|d| fddS  }t|dd	\}}}td
 }d
 d  }t|||||d| fdd}	|	S )a  Chebyshev polynomial of the first kind.

    Defined to be the solution of

    .. math::
        (1 - x^2)\frac{d^2}{dx^2}T_n - x\frac{d}{dx}T_n + n^2T_n = 0;

    :math:`T_n` is a polynomial of degree :math:`n`.

    Parameters
    ----------
    n : int
        Degree of the polynomial.
    monic : bool, optional
        If `True`, scale the leading coefficient to be 1. Default is
        `False`.

    Returns
    -------
    T : orthopoly1d
        Chebyshev polynomial of the first kind.

    Notes
    -----
    The polynomials :math:`T_n` are orthogonal over :math:`[-1, 1]`
    with weight function :math:`(1 - x^2)^{-1/2}`.

    See Also
    --------
    chebyu : Chebyshev polynomial of the second kind.

    r   r   c                 S   s   dt d| |    S )NrB   r   r   rL   rC   rC   rI   rP     rQ   zchebyt.<locals>.<lambda>rB   r   c                    s
   t  | S rK   r1   rL   r   rC   rI   rP   	  rQ   Tr   rp   r   c                    s
   t  | S rK   r   rL   r   rC   rI   rP     rQ   )r   rA   r   r"   )
rt   rc   rH   r   rM   r|   re   ra   rb   rj   rC   r   rI   r     s    !

r   c                 C   s|   t | }| dk s| |kr tdt|ddt |d  }t|}tt|d  |d  }|rp||td fS ||fS dS )a  Gauss-Chebyshev (second kind) quadrature.

    Computes the sample points and weights for Gauss-Chebyshev
    quadrature. The sample points are the roots of the nth degree
    Chebyshev polynomial of the second kind, :math:`U_n(x)`. These
    sample points and weights correctly integrate polynomials of
    degree :math:`2n - 1` or less over the interval :math:`[-1, 1]`
    with weight function :math:`w(x) = \sqrt{1 - x^2}`. See 22.2.5 in
    [AS]_ for details.

    Parameters
    ----------
    n : int
        quadrature order
    mu : bool, optional
        If True, return the sum of the weights, optional.

    Returns
    -------
    x : ndarray
        Sample points
    w : ndarray
        Weights
    mu : float
        Sum of the weights

    See Also
    --------
    scipy.integrate.quadrature
    scipy.integrate.fixed_quad

    References
    ----------
    .. [AS] Milton Abramowitz and Irene A. Stegun, eds.
        Handbook of Mathematical Functions with Formulas,
        Graphs, and Mathematical Tables. New York: Dover, 1972.

    r   r~   r   rq   rp   N)r   r   rV   r   r   r   r   )rt   re   r   r   rM   r|   rC   rC   rI   r#     s    '
r#   c                 C   sJ   t | dd|d}|r|S ttd t| d  t| d  }|| |S )a  Chebyshev polynomial of the second kind.

    Defined to be the solution of

    .. math::
        (1 - x^2)\frac{d^2}{dx^2}U_n - 3x\frac{d}{dx}U_n
          + n(n + 2)U_n = 0;

    :math:`U_n` is a polynomial of degree :math:`n`.

    Parameters
    ----------
    n : int
        Degree of the polynomial.
    monic : bool, optional
        If `True`, scale the leading coefficient to be 1. Default is
        `False`.

    Returns
    -------
    U : orthopoly1d
        Chebyshev polynomial of the second kind.

    Notes
    -----
    The polynomials :math:`U_n` are orthogonal over :math:`[-1, 1]`
    with weight function :math:`(1 - x^2)^{1/2}`.

    See Also
    --------
    chebyt : Chebyshev polynomial of the first kind.

    r   r   r   rp         ?)r   r   r   r   rk   rt   rc   r   r   rC   rC   rI   r   I  s    "$
r   c                 C   sB   t | d\}}}|d9 }|d9 }|d9 }|r6|||fS ||fS dS )a)  Gauss-Chebyshev (first kind) quadrature.

    Compute the sample points and weights for Gauss-Chebyshev
    quadrature. The sample points are the roots of the nth degree
    Chebyshev polynomial of the first kind, :math:`C_n(x)`. These
    sample points and weights correctly integrate polynomials of
    degree :math:`2n - 1` or less over the interval :math:`[-2, 2]`
    with weight function :math:`w(x) = 1 / \sqrt{1 - (x/2)^2}`. See
    22.2.6 in [AS]_ for more details.

    Parameters
    ----------
    n : int
        quadrature order
    mu : bool, optional
        If True, return the sum of the weights, optional.

    Returns
    -------
    x : ndarray
        Sample points
    w : ndarray
        Weights
    mu : float
        Sum of the weights

    See Also
    --------
    scipy.integrate.quadrature
    scipy.integrate.fixed_quad

    References
    ----------
    .. [AS] Milton Abramowitz and Irene A. Stegun, eds.
        Handbook of Mathematical Functions with Formulas,
        Graphs, and Mathematical Tables. New York: Dover, 1972.

    Trp   Nr"   rt   re   rM   r|   r   rC   rC   rI   r$   u  s    '
r$   c              	      s    dk rt d dkr" d }n }t|\}} dkrDg g  }}dt  dkd  }d}t||||dd d|d	}|s|d
|d   fdd|jd< |S )ag  Chebyshev polynomial of the first kind on :math:`[-2, 2]`.

    Defined as :math:`C_n(x) = 2T_n(x/2)`, where :math:`T_n` is the
    nth Chebychev polynomial of the first kind.

    Parameters
    ----------
    n : int
        Degree of the polynomial.
    monic : bool, optional
        If `True`, scale the leading coefficient to be 1. Default is
        `False`.

    Returns
    -------
    C : orthopoly1d
        Chebyshev polynomial of the first kind on :math:`[-2, 2]`.

    Notes
    -----
    The polynomials :math:`C_n(x)` are orthogonal over :math:`[-2, 2]`
    with weight function :math:`1/\sqrt{1 - (x/2)^2}`.

    See Also
    --------
    chebyt : Chebyshev polynomial of the first kind.

    References
    ----------
    .. [1] Abramowitz and Stegun, "Handbook of Mathematical Functions"
           Section 22. National Bureau of Standards, 1972.

    r   r   r   r   rB   c                 S   s   dt d| |  d   S )NrB   r   r   r   rL   rC   rC   rI   rP     rQ   zchebyc.<locals>.<lambda>rp   rH   r]   rc   r   rp   c                    s
   t  | S rK   )r3   rL   r   rC   rI   rP     rQ   r_   )r   r$   r   rA   rk   r   r   rC   r   rI   r     s$    "


r   c                 C   sB   t | d\}}}|d9 }|d9 }|d9 }|r6|||fS ||fS dS )a'  Gauss-Chebyshev (second kind) quadrature.

    Compute the sample points and weights for Gauss-Chebyshev
    quadrature. The sample points are the roots of the nth degree
    Chebyshev polynomial of the second kind, :math:`S_n(x)`. These
    sample points and weights correctly integrate polynomials of
    degree :math:`2n - 1` or less over the interval :math:`[-2, 2]`
    with weight function :math:`w(x) = \sqrt{1 - (x/2)^2}`. See 22.2.7
    in [AS]_ for more details.

    Parameters
    ----------
    n : int
        quadrature order
    mu : bool, optional
        If True, return the sum of the weights, optional.

    Returns
    -------
    x : ndarray
        Sample points
    w : ndarray
        Weights
    mu : float
        Sum of the weights

    See Also
    --------
    scipy.integrate.quadrature
    scipy.integrate.fixed_quad

    References
    ----------
    .. [AS] Milton Abramowitz and Irene A. Stegun, eds.
        Handbook of Mathematical Functions with Formulas,
        Graphs, and Mathematical Tables. New York: Dover, 1972.

    Trp   N)r#   r   rC   rC   rI   r%     s    '
r%   c           	   	      s    dk rt d dkr" d }n }t|\}} dkrDg g  }}t}d}t||||dd d|d}|s d |d	 }||  fd
d|jd< |S )af  Chebyshev polynomial of the second kind on :math:`[-2, 2]`.

    Defined as :math:`S_n(x) = U_n(x/2)` where :math:`U_n` is the
    nth Chebychev polynomial of the second kind.

    Parameters
    ----------
    n : int
        Degree of the polynomial.
    monic : bool, optional
        If `True`, scale the leading coefficient to be 1. Default is
        `False`.

    Returns
    -------
    S : orthopoly1d
        Chebyshev polynomial of the second kind on :math:`[-2, 2]`.

    Notes
    -----
    The polynomials :math:`S_n(x)` are orthogonal over :math:`[-2, 2]`
    with weight function :math:`\sqrt{1 - (x/2)}^2`.

    See Also
    --------
    chebyu : Chebyshev polynomial of the second kind

    References
    ----------
    .. [1] Abramowitz and Stegun, "Handbook of Mathematical Functions"
           Section 22. National Bureau of Standards, 1972.

    r   r   r   rB   c                 S   s   t d| |  d  S )Nr   r   r   rL   rC   rC   rI   rP   ?  rQ   zchebys.<locals>.<lambda>r   r   rp   c                    s
   t  | S rK   )r4   rL   r   rC   rI   rP   D  rQ   r_   )r   r%   r   rA   rk   r   )	rt   rc   r   rM   r|   ra   rb   rj   r   rC   r   rI   r     s&    "



r   c                 C   s(   t | |}|d d d f|dd  S )a3  Gauss-Chebyshev (first kind, shifted) quadrature.

    Compute the sample points and weights for Gauss-Chebyshev
    quadrature. The sample points are the roots of the nth degree
    shifted Chebyshev polynomial of the first kind, :math:`T_n(x)`.
    These sample points and weights correctly integrate polynomials of
    degree :math:`2n - 1` or less over the interval :math:`[0, 1]`
    with weight function :math:`w(x) = 1/\sqrt{x - x^2}`. See 22.2.8
    in [AS]_ for more details.

    Parameters
    ----------
    n : int
        quadrature order
    mu : bool, optional
        If True, return the sum of the weights, optional.

    Returns
    -------
    x : ndarray
        Sample points
    w : ndarray
        Weights
    mu : float
        Sum of the weights

    See Also
    --------
    scipy.integrate.quadrature
    scipy.integrate.fixed_quad

    References
    ----------
    .. [AS] Milton Abramowitz and Irene A. Stegun, eds.
        Handbook of Mathematical Functions with Formulas,
        Graphs, and Mathematical Tables. New York: Dover, 1972.

    r   r   rp   Nr   )rt   re   ZxwrC   rC   rI   r-   J  s    '
r-   c                 C   s@   t | dd|d}|r|S | dkr.d|  d }nd}|| |S )aX  Shifted Chebyshev polynomial of the first kind.

    Defined as :math:`T^*_n(x) = T_n(2x - 1)` for :math:`T_n` the nth
    Chebyshev polynomial of the first kind.

    Parameters
    ----------
    n : int
        Degree of the polynomial.
    monic : bool, optional
        If `True`, scale the leading coefficient to be 1. Default is
        `False`.

    Returns
    -------
    T : orthopoly1d
        Shifted Chebyshev polynomial of the first kind.

    Notes
    -----
    The polynomials :math:`T^*_n` are orthogonal over :math:`[0, 1]`
    with weight function :math:`(x - x^2)^{-1/2}`.

    r   r   r   r   r   r   rB   r    rk   r   rC   rC   rI   r   u  s    
r   c                 C   sN   t | d\}}}|d d }tdd}||| 9 }|rB|||fS ||fS dS )a4  Gauss-Chebyshev (second kind, shifted) quadrature.

    Computes the sample points and weights for Gauss-Chebyshev
    quadrature. The sample points are the roots of the nth degree
    shifted Chebyshev polynomial of the second kind, :math:`U_n(x)`.
    These sample points and weights correctly integrate polynomials of
    degree :math:`2n - 1` or less over the interval :math:`[0, 1]`
    with weight function :math:`w(x) = \sqrt{x - x^2}`. See 22.2.9 in
    [AS]_ for more details.

    Parameters
    ----------
    n : int
        quadrature order
    mu : bool, optional
        If True, return the sum of the weights, optional.

    Returns
    -------
    x : ndarray
        Sample points
    w : ndarray
        Weights
    mu : float
        Sum of the weights

    See Also
    --------
    scipy.integrate.quadrature
    scipy.integrate.fixed_quad

    References
    ----------
    .. [AS] Milton Abramowitz and Irene A. Stegun, eds.
        Handbook of Mathematical Functions with Formulas,
        Graphs, and Mathematical Tables. New York: Dover, 1972.

    Tr   rp   r   N)r#   r   r   )rt   re   rM   r|   r   Zm_usrC   rC   rI   r.     s    '
r.   c                 C   s.   t | dd|d}|r|S d|  }|| |S )aZ  Shifted Chebyshev polynomial of the second kind.

    Defined as :math:`U^*_n(x) = U_n(2x - 1)` for :math:`U_n` the nth
    Chebyshev polynomial of the second kind.

    Parameters
    ----------
    n : int
        Degree of the polynomial.
    monic : bool, optional
        If `True`, scale the leading coefficient to be 1. Default is
        `False`.

    Returns
    -------
    U : orthopoly1d
        Shifted Chebyshev polynomial of the second kind.

    Notes
    -----
    The polynomials :math:`U^*_n` are orthogonal over :math:`[0, 1]`
    with weight function :math:`(x - x^2)^{1/2}`.

    r   r   r   r   r   r   rC   rC   rI   r     s    
r   c              	   C   sX   t | }| dk s| |kr tdd}dd }dd }tj}dd }t||||||d|S )	a  Gauss-Legendre quadrature.

    Compute the sample points and weights for Gauss-Legendre
    quadrature. The sample points are the roots of the nth degree
    Legendre polynomial :math:`P_n(x)`. These sample points and
    weights correctly integrate polynomials of degree :math:`2n - 1`
    or less over the interval :math:`[-1, 1]` with weight function
    :math:`w(x) = 1.0`. See 2.2.10 in [AS]_ for more details.

    Parameters
    ----------
    n : int
        quadrature order
    mu : bool, optional
        If True, return the sum of the weights, optional.

    Returns
    -------
    x : ndarray
        Sample points
    w : ndarray
        Weights
    mu : float
        Sum of the weights

    See Also
    --------
    scipy.integrate.quadrature
    scipy.integrate.fixed_quad
    numpy.polynomial.legendre.leggauss

    References
    ----------
    .. [AS] Milton Abramowitz and Irene A. Stegun, eds.
        Handbook of Mathematical Functions with Formulas,
        Graphs, and Mathematical Tables. New York: Dover, 1972.

    r   r~   r   c                 S   s   d|  S r   rC   r   rC   rC   rI   rP     rQ   z roots_legendre.<locals>.<lambda>c                 S   s   | t dd|  |  d   S )NrB   r   r   r   r   rC   rC   rI   rP     rQ   c                 S   s6   |  | t | | | t | d |  d|d   S )Nr   rp   )r   r0   r   rC   rC   rI   rP     s   
T)r   r   r   r0   r}   )rt   re   r   ru   rv   rw   rx   ry   rC   rC   rI   r!     s    'r!   c                    s    dk rt d dkr" d }n }t|\}} dkrDg g  }}dd  d  }td  d t d d  d   }t||||dd d| fd	dd
}|S )a*  Legendre polynomial.

    Defined to be the solution of

    .. math::
        \frac{d}{dx}\left[(1 - x^2)\frac{d}{dx}P_n(x)\right]
          + n(n + 1)P_n(x) = 0;

    :math:`P_n(x)` is a polynomial of degree :math:`n`.

    Parameters
    ----------
    n : int
        Degree of the polynomial.
    monic : bool, optional
        If `True`, scale the leading coefficient to be 1. Default is
        `False`.

    Returns
    -------
    P : orthopoly1d
        Legendre polynomial.

    Notes
    -----
    The polynomials :math:`P_n` are orthogonal over :math:`[-1, 1]`
    with weight function 1.

    Examples
    --------
    Generate the 3rd-order Legendre polynomial 1/2*(5x^3 + 0x^2 - 3x + 0):

    >>> from scipy.special import legendre
    >>> legendre(3)
    poly1d([ 2.5,  0. , -1.5,  0. ])

    r   r   r   r   rp   c                 S   s   dS r   rC   rL   rC   rC   rI   rP   T  rQ   zlegendre.<locals>.<lambda>r   c                    s
   t  | S rK   )r0   rL   r   rC   rI   rP   U  rQ   r   )r   r!   r   rA   r   rC   r   rI   r   "  s    &

(r   c                 C   s:   t | \}}|d d }|d }|r.||dfS ||fS dS )a  Gauss-Legendre (shifted) quadrature.

    Compute the sample points and weights for Gauss-Legendre
    quadrature. The sample points are the roots of the nth degree
    shifted Legendre polynomial :math:`P^*_n(x)`. These sample points
    and weights correctly integrate polynomials of degree :math:`2n -
    1` or less over the interval :math:`[0, 1]` with weight function
    :math:`w(x) = 1.0`. See 2.2.11 in [AS]_ for details.

    Parameters
    ----------
    n : int
        quadrature order
    mu : bool, optional
        If True, return the sum of the weights, optional.

    Returns
    -------
    x : ndarray
        Sample points
    w : ndarray
        Weights
    mu : float
        Sum of the weights

    See Also
    --------
    scipy.integrate.quadrature
    scipy.integrate.fixed_quad

    References
    ----------
    .. [AS] Milton Abramowitz and Irene A. Stegun, eds.
        Handbook of Mathematical Functions with Formulas,
        Graphs, and Mathematical Tables. New York: Dover, 1972.

    r   rp   rB   N)r!   )rt   re   rM   r|   rC   rC   rI   r,   [  s    &
r,   c                    s    dk rt ddd } dkr>tg g dd|d| fddS t \}}dd  d  }td  d	 t d	 d  }t|||||d| fd
dd}|S )a  Shifted Legendre polynomial.

    Defined as :math:`P^*_n(x) = P_n(2x - 1)` for :math:`P_n` the nth
    Legendre polynomial.

    Parameters
    ----------
    n : int
        Degree of the polynomial.
    monic : bool, optional
        If `True`, scale the leading coefficient to be 1. Default is
        `False`.

    Returns
    -------
    P : orthopoly1d
        Shifted Legendre polynomial.

    Notes
    -----
    The polynomials :math:`P^*_n` are orthogonal over :math:`[0, 1]`
    with weight function 1.

    r   r   c                 S   s   d|  d S )Nr   rB   rC   rL   rC   rC   rI   rP     rQ   zsh_legendre.<locals>.<lambda>rB   r   c                    s
   t  | S rK   r;   rL   r   rC   rI   rP     rQ   rp   r   c                    s
   t  | S rK   r   rL   r   rC   rI   rP     rQ   )r]   rc   rd   )r   rA   r,   r   )rt   rc   rH   rM   r|   ra   rb   rj   rC   r   rI   r     s    
 
r   )r@   r5   r>   r:   r1   r2   r4   r3   r<   r=   r0   r;   r7   r6   r8   r9   )F)F)F)F)F)F)F)F)F)r   )r   )F)F)F)F)F)F)F)F)F)F)F)F)F)F)F)F)F)F)F)F)F)]__doc__ZnumpyrV   r   r   r   r   r   r   r   r	   r
   r   r   Zscipyr   Zscipy.specialr    r   r   r   r   r   Z	_polyfunsZ_rootfuns_mapZ	_evalfunsr[   keys__all__rW   rA   r}   r&   r   r/   r    r(   r   r'   r   r)   r   r   r   r   r   r   r   r   r*   r   r+   r   r"   r   r#   r   r$   r   r%   r   r-   r   r.   r   r!   r   r,   r   r?   r@   r5   r>   r:   r1   r2   r4   r3   r<   r=   r0   r;   r7   r6   r8   r9   globalsZ	_modattrsitemsZnewfunZoldfunappendrC   rC   rC   rI   <module>   s   M4,-
H
:
7
8
A
@
*
0
R
$#('n
/9
E
J
5
?
O
3
4
3
,
1
9
1
:
+
%
1
#
4
9
.
.H