
    ^Mh!                       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g d	Zd
dddddddddddddddZe ee                                          z   Z G d dej                  Zd ZdDdZdDdZ dDd Z!dDd!Z"dDd"Z#dDd#Z$dDd$Z%dDd%Z&dDd&Z'dEd(Z(d) Z)d* Z*d+ Z+d, Z,dEd-Z-d. Z.dDd/Z/dDd0Z0dDd1Z1dDd2Z2dDd3Z3dDd4Z4dDd5Z5dDd6Z6dDd7Z7dDd8Z8dDd9Z9dDd:Z:dDd;Z;dDd<Z<dDd=Z=dDd>Z>dDd?Z?dDd@Z@dDdAZAdDdBZBdDdCZC eD            ZEeF                                D ]%\  ZGZHeEeG         eEeH<   eI                    eH           &dS )FaI  
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   )_specfun)_ufuncs)legendrechebytchebyuchebycchebysjacobilaguerregenlaguerrehermitehermitenorm
gegenbauersh_legendre	sh_chebyt	sh_chebyu	sh_jacobip_rootst_rootsu_rootsc_rootss_rootsj_rootsl_rootsla_rootsh_rootshe_rootscg_rootsps_rootsts_rootsus_roots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_jacobic                   &    e Zd Z	 	 ddZd Zd ZdS )orthopoly1dN      ?Fc	           	         fdt          t                              D             }	t          |          }
|r |r|fd}|
t          |          z  }
d}t	          j        d          }t          j                            | |j        t          |          z             t	          j	        t          t          |	                              | _        | _        || _        |
| _        || _        d S )Nc                 D    g | ]}|          |                   z  S  rE   ).0krootsweightswfuncs     Y/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/scipy/special/_orthogonal.py
<listcomp>z(orthopoly1d.__init__.<locals>.<listcomp>w   s>     1 1 1 !eeE!Hoo5 1 1 1    c                       |           z  S NrE   )xevfknns    rK   	eval_funcz'orthopoly1d.__init__.<locals>.eval_func~   s    3q66C<'rM   rB   T)r)rangelenr   absnppoly1d__init__coeffsfloatarraylistziprI   weight_funclimitsnormcoef
_eval_func)selfrH   rI   hnknrJ   ra   monicrS   equiv_weightsmupolyrQ   rR   s    ``  `      @@rK   rZ   zorthopoly1d.__init__u   s!   1 1 1 1 1 1#CJJ//1 1 1"XX 	C (( ( ( ( ( (c"ggBB y$'''
	4uRyy!8999xS%G%G H HII  $rM   c                     | j         r/t          |t          j                  s|                      |          S t          j                            | |          S rO   )rc   
isinstancerX   rY   __call__)rd   vs     rK   rm   zorthopoly1d.__call__   sI    ? 	/:a#;#; 	/??1%%%9%%dA...rM   c                     dk    rd S | xj         z  c_         | j        rfd| _        | xj        z  c_        d S )NrB   c                       |           z  S rO   rE   )rP   rQ   ps    rK   <lambda>z$orthopoly1d._scale.<locals>.<lambda>   s    A
 rM   )_coeffsrc   rb   )rd   rq   rQ   s    `@rK   _scalezorthopoly1d._scale   sX    88Fo 	322222DOrM   )NrB   rB   NNFN)__name__
__module____qualname__rZ   rm   rt   rE   rM   rK   rA   rA   s   sL        BF59$ $ $ $4/ / /    rM   rA   c                 z   t          j        | d          }t          j        d| f          }	 ||dd                   |	dddf<    ||          |	dddf<   t          j        |	d          }
 || |
          } || |
          }|
||z  z  }
 || dz
  |
          }t          j        t          j        |                    }t          j        t          j        |                    }|t          j        |                                |	                                z   d	z            z  }|t          j        |                                |	                                z   d	z            z  }d
||z  z  }|r"||ddd         z   dz  }|
|
ddd         z
  dz  }
|||
                                z  z  }|r|
||fS |
|f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)dtype   r   Nr   T)overwrite_a_band       @rB   )rX   r   zerosr   eigvals_bandedlogrW   r   maxminsum)nmu0an_funcbn_funcfdf
symmetrizeri   rG   crP   ydyfmlog_fmlog_dyws                    rK   _gen_roots_and_weightsr      s    		!3A
!QAgaennAadGWQZZAacFa$777A 	
!QA	AqB2IA 
1Q3BVBF2JJFVBF2JJF"&&**,,-3
4
44B"&&**,,-3
4
44BrBwA 44R4[A44R4[AquuwwA	 !Sy!trM   Fc           
      x  
 t          |           }| dk     s| |k    rt          d          |dk    s|dk    rt          d          |dk    r|dk    rt          ||          S ||k    rt          ||dz   |          S ||z   dk    r(d||z   dz   z  t	          j        |dz   |dz             z  }nKt          j        ||z   dz   t          j        d          z  t	          j	        |dz   |dz             z             }|
|
z   dk    r
fd	}n
fd
}
fd}
fd}
fd}	t          ||||||	d|          S )aO  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.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.r~   z'alpha and beta must be greater than -1.              ?i  r}   c                 P    t          j        | dk    z
  dz   z   z  d          S )Nr   r{   r   rX   whererG   abs    rK   r   zroots_jacobi.<locals>.an_func  s,    8AFQUq1uqy$93???rM   c                     t          j        | dk    z
  dz   z   z  z  z  z
  d| z  z   z   d| z  z   z   dz   z  z            S )Nr   r{   r}   r   r   s    rK   r   zroots_jacobi.<locals>.an_func  sh    8QQ1q519%QQC!GaK!Oa!a!8K#LM  rM   c           
         dd| z  z   z   z  t          j        | z   | z   z  d| z  z   z   dz   z            z  t          j        | dk    dt          j        | | z   z   z  d| z  z   z   dz
  z                      z  S )Nr}   r{   r   rB   )rX   r   r   r   s    rK   r   zroots_jacobi.<locals>.bn_func  s    37Q;?#gq1uQ'1q519q=1+<=>>?hqAvsBGAQOsQw{QQR?R,S$T$TUUV	
rM   c                 2    t          j        | |          S rO   r   eval_jacobir   rP   r   r   s     rK   r   zroots_jacobi.<locals>.f   s    "1aA...rM   c                 b    d| z   z   dz   z  t          j        | dz
  dz   dz   |          z  S )Nr   r   r   r   s     rK   r   zroots_jacobi.<locals>.df"  s=    a!eai!m$w':1q5!a%QPQ'R'RRRrM   F)int
ValueErrorr1   r;   r   betarX   r   r   betalnr   )r   alphar   ri   mr   r   r   r   r   r   r   s             @@rK   r6   r6      s   T 	AA1uuQ8999{{dbjjBCCC||a$$$}}59b111E$JqL!GLq$q&$A$AA fedlQ&"&++5~eAgtAv667 8 8AA1u||	@ 	@ 	@ 	@ 	@ 	@ 	@	 	 	 	 	 	
 
 
 
 
 
/ / / / / /S S S S S S!!S'7Ar5"MMMrM   c                 J     dk     rt          d          fd} dk    r!t          g g dd|d|t          j                  S t	           d          \  }}}z   dz   }d	|z  d	 z  |z   z  t           z   d
z             z  }	|	t           z   dz             t           d
z             z  t           |z             z  z  }	t          d	 z  |z             d z  z  t           d
z             z  t           |z             z  }
t          |||	|
|d| f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`.

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

    Examples
    --------
    The Jacobi polynomials satisfy the recurrence relation:

    .. math::
        P_n^{(\alpha, \beta-1)}(x) - P_n^{(\alpha-1, \beta)}(x)
          = P_{n-1}^{(\alpha, \beta)}(x)

    This can be verified, for example, for :math:`\alpha = \beta = 2`
    and :math:`n = 1` over the interval :math:`[-1, 1]`:

    >>> import numpy as np
    >>> from scipy.special import jacobi
    >>> x = np.arange(-1.0, 1.0, 0.01)
    >>> np.allclose(jacobi(0, 2, 2)(x),
    ...             jacobi(1, 2, 1)(x) - jacobi(1, 1, 2)(x))
    True

    Plot of the Jacobi polynomial :math:`P_5^{(\alpha, -0.5)}` for
    different values of :math:`\alpha`:

    >>> import matplotlib.pyplot as plt
    >>> x = np.arange(-1.0, 1.0, 0.01)
    >>> fig, ax = plt.subplots()
    >>> ax.set_ylim(-2.0, 2.0)
    >>> ax.set_title(r'Jacobi polynomials $P_5^{(\alpha, -0.5)}$')
    >>> for alpha in np.arange(0, 4, 1):
    ...     ax.plot(x, jacobi(5, alpha, -0.5)(x), label=rf'$\alpha={alpha}$')
    >>> plt.legend(loc='best')
    >>> plt.show()

    r   n must be nonnegative.c                 &    d| z
  z  d| z   z  z  S Nr   rE   )rP   r   r   s    rK   rJ   zjacobi.<locals>.wfuncu  s    A%1q5T/11rM   rB   r~   r   rS   Tri   r{   r   r}   c                 2    t          j        |           S rO   r   )rP   r   r   r   s    rK   rr   zjacobi.<locals>.<lambda>  s    g1!UD!DD rM   )r   rA   rX   	ones_liker6   _gam)r   r   r   rg   rJ   rP   r   ri   ab1re   rf   rq   s   ```         rK   r   r   '  sp   V 	1uu12222 2 2 2 2 2Avv2r3UGU%'\3 3 3 	3Audt444HAq"
$,
C	
C1q53;	$q5y1}"5"5	5B$q4x#~

a!e
,tAG}}
<<B	a!eck		S!V	#d1q5kk	1DSMM	ABAq"b%%DDDDDD	F 	FAHrM   c                     ||z
  dk    s|dk    rd}t          |          t          | ||z
  |dz
  d          \  }}}|dz   dz  }d|z  }||z  }||z  }|r|||fS ||fS )aQ  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.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   z>(p - q) must be greater than -1, and q must be greater than 0.r   Tr{   r}   )r   r6   )	r   p1q1ri   messagerP   r   r   scales	            rK   r?   r?     s    T 	2"}}aR!!!1beRT400GAq!	
Q!AGEJAJA	 !Qw!trM   c                      dk     rt          d          fd} dk    r!t          g g dd|d|t          j                  S  }t	          |          \  }}t           dz             t           z             z  t           z             z  t           z   z
  dz             z  }|d z  z   t          d z  z             dz  z  z  }d}	t          ||||	|d	| f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                 ,    d| z
  z
  z  | dz
  z  z  S NrB   rE   )rP   rq   qs    rK   rJ   zsh_jacobi.<locals>.wfunc  s#    aQU#aAGn44rM   rB   r   r   r   r{   r   r   c                 2    t          j        |           S rO   )r   eval_sh_jacobi)rP   r   rq   r   s    rK   rr   zsh_jacobi.<locals>.<lambda>  s    )?1a)K)K rM   rJ   ra   rg   rS   )r   rA   rX   r   r?   r   )r   rq   r   rg   rJ   n1rP   r   re   rf   pps   ```        rK   r!   r!     s<   H 	1uu12225 5 5 5 5 5Avv2r3UGU%'\3 3 3 	3	
B2q!$$DAq	a!etAE{{	"T!a%[[	04A	A3F3F	FB1q519a!eai!+
,,B	B	Q2rvUKKKKKK
M 
M 
MBIrM   c           
         t          |           }| dk     s| |k    rt          d          dk     rt          d          t          j        dz             }|dk    r:t	          j        dz   gd          }t	          j        |gd          }|r|||fS ||fS fd}fd}fd	}	f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.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~   zalpha must be greater than -1.rB   ry   c                     d| z  z   dz   S Nr{   r   rE   rG   r   s    rK   r   z"roots_genlaguerre.<locals>.an_func0  s    1uu}q  rM   c                 :    t          j        | | z   z             S rO   rX   r   r   s    rK   r   z"roots_genlaguerre.<locals>.bn_func2  s    QY((((rM   c                 0    t          j        | |          S rO   r   eval_genlaguerrer   rP   r   s     rK   r   zroots_genlaguerre.<locals>.f4  s    '5!444rM   c                 |    | t          j        | |          z  | z   t          j        | dz
  |          z  z
  |z  S r   r   r   s     rK   r   zroots_genlaguerre.<locals>.df6  sN    G,Qq999u9 8Qq I IIJMNO 	OrM   F)r   r   r   gammarX   r]   r   )r   r   ri   r   r   rP   r   r   r   r   r   s    `         rK   r8   r8     s3   P 	AA1uuQ8999rzz9:::
-	
"
"CAvvHeCi[#&&HcUC   	a9a4K! ! ! ! !) ) ) ) )5 5 5 5 5O O O O O "!S'7Ar5"MMMrM   c                     dk    rt          d           dk     rt          d           dk    r dz   }n }t          |          \  }}fd} dk    rg g }}t           z   dz             t           dz             z  }d z  t           dz             z  }t          |||||dt          f| fd          }	|	S )aO	  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.

    See Also
    --------
    laguerre : Laguerre polynomial.
    hyp1f1 : confluent hypergeometric function

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

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

    Examples
    --------
    The generalized Laguerre polynomials are closely related to the confluent
    hypergeometric function :math:`{}_1F_1`:

        .. math::
            L_n^{(\alpha)} = \binom{n + \alpha}{n} {}_1F_1(-n, \alpha +1, x)

    This can be verified, for example,  for :math:`n = \alpha = 3` over the
    interval :math:`[-1, 1]`:

    >>> import numpy as np
    >>> from scipy.special import binom
    >>> from scipy.special import genlaguerre
    >>> from scipy.special import hyp1f1
    >>> x = np.arange(-1.0, 1.0, 0.01)
    >>> np.allclose(genlaguerre(3, 3)(x), binom(6, 3) * hyp1f1(-3, 4, x))
    True

    This is the plot of the generalized Laguerre polynomials
    :math:`L_3^{(\alpha)}` for some values of :math:`\alpha`:

    >>> import matplotlib.pyplot as plt
    >>> x = np.arange(-4.0, 12.0, 0.01)
    >>> fig, ax = plt.subplots()
    >>> ax.set_ylim(-5.0, 10.0)
    >>> ax.set_title(r'Generalized Laguerre polynomials $L_3^{\alpha}$')
    >>> for alpha in np.arange(0, 5):
    ...     ax.plot(x, genlaguerre(3, alpha)(x), label=rf'$L_3^{(alpha)}$')
    >>> plt.legend(loc='best')
    >>> plt.show()

    r~   zalpha must be > -1r   r   r   c                 0    t          |            | z  z  S rO   r   )rP   r   s    rK   rJ   zgenlaguerre.<locals>.wfunc  s    A2wwe##rM   c                 0    t          j        |           S rO   r   rP   r   r   s    rK   rr   zgenlaguerre.<locals>.<lambda>  s    g6q%CC rM   )r   r8   r   rA   r   )
r   r   rg   r   rP   r   rJ   re   rf   rq   s
   ``        rK   r   r   <  s   b {{-...1uu1222AvvUR''DAq$ $ $ $ $Avv21	a%i!m		tAE{{	*B
q4A;;	BAq"b%!S5CCCCC	E 	EAHrM   c                 &    t          | 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.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   )r8   )r   ri   s     rK   r7   r7     s    L Q++++rM   c                       dk     rt          d           dk    r dz   }n }t          |          \  }} dk    rg g }}d}d z  t           dz             z  }t          ||||d dt          f| f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.

    See Also
    --------
    genlaguerre : Generalized (associated) Laguerre polynomial.

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

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

    Examples
    --------
    The Laguerre polynomials :math:`L_n` are the special case
    :math:`\alpha = 0` of the generalized Laguerre polynomials
    :math:`L_n^{(\alpha)}`.
    Let's verify it on the interval :math:`[-1, 1]`:

    >>> import numpy as np
    >>> from scipy.special import genlaguerre
    >>> from scipy.special import laguerre
    >>> x = np.arange(-1.0, 1.0, 0.01)
    >>> np.allclose(genlaguerre(3, 0)(x), laguerre(3)(x))
    True

    The polynomials :math:`L_n` also satisfy the recurrence relation:

    .. math::
        (n + 1)L_{n+1}(x) = (2n +1 -x)L_n(x) - nL_{n-1}(x)

    This can be easily checked on :math:`[0, 1]` for :math:`n = 3`:

    >>> x = np.arange(0.0, 1.0, 0.01)
    >>> np.allclose(4 * laguerre(4)(x),
    ...             (7 - x) * laguerre(3)(x) - 3 * laguerre(2)(x))
    True

    This is the plot of the first few Laguerre polynomials :math:`L_n`:

    >>> import matplotlib.pyplot as plt
    >>> x = np.arange(-1.0, 5.0, 0.01)
    >>> fig, ax = plt.subplots()
    >>> ax.set_ylim(-5.0, 5.0)
    >>> ax.set_title(r'Laguerre polynomials $L_n$')
    >>> for n in np.arange(0, 5):
    ...     ax.plot(x, laguerre(n)(x), label=rf'$L_{n}$')
    >>> plt.legend(loc='best')
    >>> plt.show()

    r   r   r   rB   r~   c                 "    t          |            S rO   r   rP   s    rK   rr   zlaguerre.<locals>.<lambda>&  s    CGG rM   c                 .    t          j        |           S rO   )r   eval_laguerrerP   r   s    rK   rr   zlaguerre.<locals>.<lambda>'  s    g3Aq99 rM   )r   r7   r   rA   r   r   rg   r   rP   r   re   rf   rq   s   `       rK   r   r     s    Z 	1uu1222AvvU"DAqAvv21	B
q4A;;	BAq"b"3"3aXu9999	; 	;AHrM   c           
      .   t          |           }| dk     s| |k    rt          d          t          j        t          j                  }| dk    r+d }d }t
          j        }d }t          ||||||d|          S t          |          \  }}	|r||	|fS ||	f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

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

    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.

    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                     d| z  S Nr   rE   rG   s    rK   r   zroots_hermite.<locals>.an_funcq      7NrM   c                 0    t          j        | dz            S Nr}   r   r   s    rK   r   zroots_hermite.<locals>.bn_funcs  s    71s7###rM   c                 >    d| z  t          j        | dz
  |          z  S )Nr}   r   r   eval_hermiter   rP   s     rK   r   zroots_hermite.<locals>.dfv  s"    7W1!a%;;;;rM   T)	r   r   rX   r   r   r   r   r   _roots_hermite_asy
r   ri   r   r   r   r   r   r   nodesrI   s
             rK   r9   r9   -  s    | 	AA1uuQ8999
'"%..CCxx	 	 		$ 	$ 	$ 	< 	< 	<%agw2tRPPP+A..w 	"'3&&'>!rM      c                    | dz  dz
  }dt          | dz            z  d|z  z
  dz   t          z  dt          | dz            z  d|z  z   dz   z  fd}d }dt          z  }t          |          D ]}| ||           ||          z  z
  }|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
    r{   r         @r}         @c                 .    | t          |           z
  z
  S rO   )r   )rP   r   s    rK   r   z_compute_tauk.<locals>.f  s    3q66zA~rM   c                 &    dt          |           z
  S r   )r	   r   s    rK   r   z_compute_tauk.<locals>.df  s    SVV|rM   )r   r   rU   )	r   rG   maxitr   r   r   xiir   s	           @rK   _compute_taukr     s    4 	
AA	U1S5\\	CE	!C	'+s53<</?#a%/G#/MNA      	RB5\\  !!B%%2,IrM   c                     t          | |          }t          d|z            dz  }| dz  dz
  }dt          | dz            z  d|z  z   dz   }||z  dd|z  z  ddd|z
  dz  z  z  dd|z
  z  z
  dz
  z  z
  }|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   r{   r   r}   rB   r   g      @      ?)r   r	   r   )r   rG   tauksigkr   nuxksqs          rK   _initial_nodes_ar     s    4 ADs4x==!D	AA	U1S5\\	CE	!C	'Bd7S#b&\S#s4x!m*;%<sCH~%MPT%TUUDKrM   c                    | dz  dz
  }dt          | dz            z  d|z  z   dz   }t          j        |                                d          d         ddd         }|d	|z  |d
z  z  z   d|dz  z  |dz  z  z   dd|dz  z  z
  |dz  z  z   d|z  d|dz  z  z   d	z  |dz  z  z   d|dz  z  d|dz  z  z   dz  |dz  z  z
  }|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
    r{   r   r   r}   r   r   Nr~   g<n=e?UUUUUU?g<=a ?gUUUUUUտgPuPu?gE'危?   g      gHg΄?g`A?   gg^PTxt?r   gJ_?gr(?g)r   r   airyzor   )r   rG   r   r   akr   s         rK   _initial_nodes_br    s   4 	
AA	U1S5\\	CE	!C	'B	!	$	$Q	'"	-Br!BM12&Q.h?@ :A--d;< R+A"55G"x.X	Y
 !2q5(?RU+BB!#h00D KrM   c           	         d| z  dz
  }t          |                              t                    }t          dt          t	          | dz            dz                       }|ddd         }t          | |d|dz                      }t          | ||dz   d                   }t          t          ||g                    }| dz  dk    rt          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   Nr~   r{   r   )	r
   astyper   r   r   r   r  r   r   )r   fitturnoveriaibxasqxbsqivs           rK   _initial_nodesr    s    0 Q,
#Cc{{!!#&&H	3uQsU||A~&&	'	'B	DDbDBAr+8A:+//DAr(1*++//D	fdD\""	#	#B1uzzS"IIrM   c                    t          |          }t          |          }d| z  dz   }d|z  d|z  |z  z
  }d|z  dz  dz   }| |dz  z  dz  }d}d}	d	}
d
}d}d}d}d}d}d}d}d}|t          d                              d          z  }d}d|dddf         z  d|z  z
  dz  }d|dddf         z  d|dddf         z  z   dz   dz  }d|dddf         z  d|d ddf         z  z   d!|d"ddf         z  z
  d#|dddf         z  z
  d$|z  z
  d%z  }d&|d'ddf         z  d(|d)ddf         z  z
  d*|d+ddf         z  z
  d,|dddf         z  z   d-|dddf         z  z   d.z   d/z  }d0|d1ddf         z  d2|d3ddf         z  z
  d4|d5ddf         z  z   d6|dddf         z  z
  d7|d ddf         z  z   d8|d"ddf         z  z
  d9|dddf         z  z
  d:|z  z
  d;z  }d}d|dddf         z  d|z  z   dz  }d<|dddf         z  d=|dddf         z  z
  d>z
  dz  }d|dddf         z  d|d ddf         z  z   d?|d"ddf         z  z
  d@|dddf         z  z   d$|z  z   d%z  }dA|d'ddf         z  dB|d)ddf         z  z   dC|d+ddf         z  z
  dD|dddf         z  z
  dE|dddf         z  z
  dFz
  d/z  }d0|d1ddf         z  d2|d3ddf         z  z
  dG|d5ddf         z  z   dH|dddf         z  z
  dI|d ddf         z  z   dJ|d"ddf         z  z   dK|dddf         z  z   dL|z  z   d;z  } t	          |dz  |z            \  }!}"}#}$dt          t                    z  |dMz  z  |z  }%|t          d+dNd+                              d          z  }&||z  }'||z  |&dOddf         |z  |z  z   |&dPddf         |z  |z  z   |dz  z  }(||z  |&dOddf         |z  |z  z   |&dPddf         |z  |z  z   |&dddf         |z  |z  z   |&dddf         |z  |z  z   |d+z  z  })|	|z  |&dOddf         |z  |z  z    |dz  z  }*||z  |&dOddf         |
z  |z  z   |&dPddf         |	z  |z  z   |&dddf         |z  |z  z    |d"z  z  }+||z  |&dOddf         |z  |z  z   |&dPddf         |z  |z  z   |&dddf         |
z  |z  z   |&dddf         |	z  |z  z   |&dddf         |z  |z  z    |d)z  z  },|%|!|'|(|dz  z  z   |)|dQz  z  z   z  |"|*|+|dz  z  z   |,|dQz  z  z   z  |dRz  z  z   z  }-t          dt          z            |dSz  z  |z  }.||z  |&dOddf         |z  |z  z    |z  }/||z  |&dOddf         |z  |z  z   |&dPddf         |z  |z  z   |&dddf         |z  |z  z    |dz  z  }0||z  |&dOddf         |z  |z  z   |&dPddf         |z  |z  z   |&dddf         |z  |z  z   |&dddf         |z  |z  z   |&dddf         |z  | z  z    |d z  z  }1||z  }2|
|z  |&dOddf         |	z  |z  z   |&dPddf         |z  |z  z   |dz  z  }3||z  |&dOddf         |z  |z  z   |&dPddf         |
z  |z  z   |&dddf         |	z  |z  z   |&dddf         |z  |z  z   |d+z  z  }4|.|!|/|0|dz  z  z   |1|dQz  z  z   z  |dz  z  |"|2|3|dz  z  z   |4|dQz  z  z   z  z   z  }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?r{   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   reshaper   r   r   )6r   thetastctri   etazetaphia0a1a2a3a4a5b0b1b2b3b4b5ctpu0u1u2u3u4u5v0v1v2v3v4v5AiAipBiBipPphipA0A1A2B0B1B2UPdC0C1C2D0D1D2Uds6                                                         rK   _pbcfrO    s
   D 
UB	UB	QB
e)c"fRi
CWS[g&&D52q5=d
#C 
B	B	B	B	B	B	B	 B	 B	 B	 B	 B r

""6**
*C	B
c!AAA#h,R
4	'B
s1QQQ3x-%AaaaC.
(5
0F	:B
#ac(
WS111X-
-AaaaC0@
@S111X (,/7
8B
#bd)
hs1QQQ3x/
/(3qs82C
Cs1QQQ3x "-c!AAA#h"679CDGQ
RB
SAAAY
SAAAY!6
6c"QQQ$i9O
OQqqqS!"$0QqqqS$9:<I#aPQPQPQc(<RS3qs8#$&3B&67:F
GB 
B
c!AAA#h,R
4	'B
s1QQQ3x-%AaaaC.
(5
0F	:B
#ac(
WS111X-
-AaaaC0@
@S111X (,/7
8B
C111I
QqqqS 1
1HS111X4E
Es1QQQ3x "-c!AAA#h"679ABEO
PB
SAAAY
SAAAY!6
6c"QQQ$i9O
OQqqqS!"$0QqqqS$9:<I#aPQPQPQc(<RS3qs8#$&1"n58D
EB BMD011BRDHHrG}$s*A &B""**6222D	BB
R%$qs)B,r/
!D111IbLO
3tQw	>B
R%$qs)B,r/
!D111IbLO
3d1QQQ3il2o
E111IbLO#Qw
'Bb54!!!9R<?"	#dAg	-Bb54!!!9R<?"T!AAA#Yr\"_4tAaaaCy|BF	G$PQ'	QBb54!!!9R<?"T!AAA#Yr\"_4tAaaaCy|BF!AAA#Yr\"_#AaaaCy|B/ 
026'
:B 	
R22s7
?RCZ/0BBGObSj01BMAB 	CA 
c"fW	%	+B b54!!!9R<?"	#d	*Bb54!!!9R<?"T!AAA#Yr\"_4tAaaaCy|BF	G$PQ'	QBb54!!!9R<?"T!AAA#Yr\"_4tAaaaCy|BF!AAA#Yr\"_#AaaaCy|B/ 
026'
:B	BB
R%$qs)B,r/
!D111IbLO
3tQw	>B
R%$qs)B,r/
!D111IbLO
3d1QQQ3il2o
E111IbLO#Qw
'B 
rR"RW*_r"c'z12R']Bb2b#g:o2s7
234 
5Bb5LrM   c                    t          d| z  dz             }||z  }t          |          }t          |          D ]d}t          | |          \  }}|t          d          |z  t	          |          z  |z  z  }	||	z   }t          t          |	                    dk     r ne|t          |          z  }
| dz  dk    rd|
d<   t          |
dz             d|dz  z  z  }|
|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+=r{   r   r   r   )	r   r   rU   rO  r   r   rW   r	   r   )r   	x_initialr   ri   tr  r   uuddthetarP   r   s               rK   _newtonrV    s    6 
c!eck		BBA1IIE5\\  a2d3ii"ns5zz1B67s6{{e##E $ 	SZZA1uzz!QTE

c"a%i Aa4KrM   c                    t          |           }t          | |          \  }}| dz  dk    r6t          |ddd          |g          }t          |ddd         |g          }n5t          |ddd          |g          }t          |ddd         |g          }|t          t                    t          |          z  z  }||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`.
    r{   r   Nr~   )r  rV  r   r   r   r   )r   r  r   rI   s       rK   r   r     s    V 
		BQ^^NE71uzzttte,--'$$B$-122r!Bw/00'"Qr'*G455tBxx#g,,&&G'>rM   c                 <     dk     rt          d           dk    r dz   }n }t          |          \  }}d } dk    rg g }}d z  t           dz             z  t          t                    z  }d z  }t          |||||t           t          f| f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                 (    t          |  | z            S rO   r   r   s    rK   rJ   zhermite.<locals>.wfunc3  s    A26{{rM   r{   c                 .    t          j        |           S rO   r   r   s    rK   rr   zhermite.<locals>.<lambda>:  s    g21a88 rM   )r   r9   r   r   r   rA   r   	r   rg   r   rP   r   rJ   re   rf   rq   s	   `        rK   r   r     s    b 	1uu1222AvvUDAq  Avv21	
AQU	d2hh	&B	
ABAq"b%3$e8888	: 	:AHrM   c           
      |   t          |           }| dk     s| |k    rt          d          t          j        dt          j        z            }| dk    r+d }d }t
          j        }d }t          ||||||d|          S t          |          \  }}	|t          d	          z  }|	t          d	          z  }	|r||	|fS ||	fS )
a-  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

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

    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.

    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                     d| z  S r   rE   r   s    rK   r   z"roots_hermitenorm.<locals>.an_funcy  r   rM   c                 *    t          j        |           S rO   r   r   s    rK   r   z"roots_hermitenorm.<locals>.bn_func{  s    71::rM   c                 8    | t          j        | dz
  |          z  S r   r   eval_hermitenormr   s     rK   r   zroots_hermitenorm.<locals>.df~  s    w/Aq9999rM   Tr{   )	r   r   rX   r   r   r   ra  r   r   r   s
             rK   r:   r:   @  s    f 	AA1uuQ8999
'#be)

CCxx	 	 		 	 	$	: 	: 	:%agw2tRPPP+A..wa477 	"'3&&'>!rM   c                 2     dk     rt          d           dk    r dz   }n }t          |          \  }}d } dk    rg g }}t          dt          z            t	           dz             z  }d}t          |||||t           t          f| f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                 .    t          |  | z  dz            S r   r   r   s    rK   rJ   zhermitenorm.<locals>.wfunc  s    A26C<   rM   r{   rB   c                 .    t          j        |           S rO   r`  r   s    rK   rr   zhermitenorm.<locals>.<lambda>      (@A(F(F rM   r   )r   r:   r   r   r   rA   r   r[  s	   `        rK   r   r     s    > 	1uu1222AvvUR  DAq! ! !Avv21	a"fQU	#B	BAq"btSkFFFF	H 	H 	HAHrM   c           
         t          |           }| dk     s| |k    rt          d          dk     rt          d          dk    rt          | |          S dk    rOt          j        t          j                  t          j        dz             z  t          j        dz             z  }nudz  }t          j        g d	          }|d
         }t          dt          |                    D ]}||z  ||         z   }|t          j        t          j        z            z  }d }fd}	fd}
fd}t          ||||	|
|d|          S )a(  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.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         z alpha must be greater than -0.5.r      r   rB   )gF90(+?g&{XgkʰDg3Ts?g      ?g      rB   r   c                     d| z  S r   rE   r   s    rK   r   z!roots_gegenbauer.<locals>.an_func      QwrM   c                 h    t          j        | | dz  z   dz
  z  d| z   z  | z   dz
  z  z            S )Nr{   r   r  r   r   s    rK   r   z!roots_gegenbauer.<locals>.bn_func  s@    wqAE	MA-.!q5y/QYQR]2STUUUrM   c                 0    t          j        | |          S rO   r   eval_gegenbauerr   s     rK   r   zroots_gegenbauer.<locals>.f  s    &q%333rM   c                     |  |z  t          j        | |          z  | dz  z   dz
  t          j        | dz
  |          z  z   d|dz  z
  z  S r   rm  r   s     rK   r   zroots_gegenbauer.<locals>.df
  sc    BFW,Qq9991u9}q G$;AE5!$L$LLMaZ 	rM   T)r   r   r2   rX   r   r   r   r   r]   rU   rV   r   )r   r   ri   r   r   	inv_alphar[   termr   r   r   r   s    `          rK   r;   r;     s   P 	AA1uuQ8999t||;<<<	#
 Ar"""||wru~~eck : ::eai(() J	 > > > ? ?Qi!S[[)) 	1 	1D	/F4L0CCBGBEEM***  V V V V V4 4 4 4 4    
 "!S'7Ar4LLLrM   c                     t          j                  rdk    rt          d          t           dz
  dz
  |          }|s dk    r|S t	          dz   z             t	          dz             z  t	          dz            z  t	          dz    z             z  }|                    |            fd|j        d<   |S )	a  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
    --------
    >>> import numpy as np
    >>> 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()

    rg  z1`alpha` must be a finite number greater than -1/2r   rg   r   r{   c                 J    t          j        t                    |           S rO   )r   rn  r\   r   s    rK   rr   zgegenbauer.<locals>.<lambda>\  s#    G,CE!HHDI1-N -N rM   rc   )rX   isfiniter   r   r   rt   __dict__)r   r   rg   basefactors   ``   rK   r   r     s    B ;u N$LMMM!US[%#+U;;;D Q1U7Q;$us{"3"331U7mm"53;?334FKK#N #N #N #N #NDM,KrM   c                    t          |           }| dk     s| |k    rt          d          t          j        t	          j        | dz   |d          d|z  z            }t	          j        |t          |z            }|r
||t          fS ||fS )a.  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.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   r{   )r   r   r   _sinpirX   r   	full_liker   )r   ri   r   rP   r   s        rK   r2   r2   e  s    N 	AA1uuQ8999ry!aA..!A#677A
Q1A	 !Rx!trM   c                      dk     rt          d          d } dk    rt          g g t          d|d| fd          S  }t          |d          \  }}}t          d	z  }d	 d
z
  z  }t          |||||d| fd          }	|	S )ao
  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.

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

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

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

    Examples
    --------
    Chebyshev polynomials of the first kind of order :math:`n` can
    be obtained as the determinant of specific :math:`n \times n`
    matrices. As an example we can check how the points obtained from
    the determinant of the following :math:`3 \times 3` matrix
    lay exactly on :math:`T_3`:

    >>> import numpy as np
    >>> import matplotlib.pyplot as plt
    >>> from scipy.linalg import det
    >>> from scipy.special import chebyt
    >>> x = np.arange(-1.0, 1.0, 0.01)
    >>> fig, ax = plt.subplots()
    >>> ax.set_ylim(-2.0, 2.0)
    >>> ax.set_title(r'Chebyshev polynomial $T_3$')
    >>> ax.plot(x, chebyt(3)(x), label=rf'$T_3$')
    >>> for p in np.arange(-1.0, 1.0, 0.1):
    ...     ax.plot(p,
    ...             det(np.array([[p, 1, 0], [1, 2*p, 1], [0, 1, 2*p]])),
    ...             'rx')
    >>> plt.legend(loc='best')
    >>> plt.show()

    They are also related to the Jacobi Polynomials
    :math:`P_n^{(-0.5, -0.5)}` through the relation:

    .. math::
        P_n^{(-0.5, -0.5)}(x) = \frac{1}{4^n} \binom{2n}{n} T_n(x)

    Let's verify it for :math:`n = 3`:

    >>> from scipy.special import binom
    >>> from scipy.special import jacobi
    >>> x = np.arange(-1.0, 1.0, 0.01)
    >>> np.allclose(jacobi(3, -0.5, -0.5)(x),
    ...             1/64 * binom(6, 3) * chebyt(3)(x))
    True

    We can plot the Chebyshev polynomials :math:`T_n` for some values
    of :math:`n`:

    >>> x = np.arange(-1.5, 1.5, 0.01)
    >>> fig, ax = plt.subplots()
    >>> ax.set_ylim(-4.0, 4.0)
    >>> ax.set_title(r'Chebyshev polynomials $T_n$')
    >>> for n in np.arange(2,5):
    ...     ax.plot(x, chebyt(n)(x), label=rf'$T_n={n}$')
    >>> plt.legend(loc='best')
    >>> plt.show()

    r   r   c                 2    dt          d| | z  z
            z  S )NrB   r   r   r   s    rK   rJ   zchebyt.<locals>.wfunc  s    T!a!e)__$$rM   rB   r   c                 .    t          j        |           S rO   r   eval_chebytr   s    rK   rr   zchebyt.<locals>.<lambda>  s    W%8A%>%> rM   Tr   r{   r   c                 .    t          j        |           S rO   r  r   s    rK   rr   zchebyt.<locals>.<lambda>  s    g1!Q77 rM   )r   rA   r   r2   )
r   rg   rJ   r   rP   r   ri   re   rf   rq   s
   `         rK   r   r     s    t 	1uu1222% % %Avv2r2sE7E>>>>@ @ 	@	
BB4(((HAq"	aB	
QUBAq"b%%7777	9 	9AHrM   c                 6   t          |           }| dk     s| |k    rt          d          t          j        |dd          t          z  |dz   z  }t          j        |          }t          t          j        |          dz  z  |dz   z  }|r||t          dz  fS ||f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.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   r~   r{   )r   r   rX   r   r   r	   r   )r   ri   r   rR  rP   r   s         rK   r3   r3     s    L 	AA1uuQ8999
	!Qb AE*A
q		A
RVAYY\QU#A	 !R!V|!trM   c                     t          | dd|          }|r|S t          t                    dz  t          | dz             z  t          | dz             z  }|                    |           |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.

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

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

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

    Examples
    --------
    Chebyshev polynomials of the second kind of order :math:`n` can
    be obtained as the determinant of specific :math:`n \times n`
    matrices. As an example we can check how the points obtained from
    the determinant of the following :math:`3 \times 3` matrix
    lay exactly on :math:`U_3`:

    >>> import numpy as np
    >>> import matplotlib.pyplot as plt
    >>> from scipy.linalg import det
    >>> from scipy.special import chebyu
    >>> x = np.arange(-1.0, 1.0, 0.01)
    >>> fig, ax = plt.subplots()
    >>> ax.set_ylim(-2.0, 2.0)
    >>> ax.set_title(r'Chebyshev polynomial $U_3$')
    >>> ax.plot(x, chebyu(3)(x), label=rf'$U_3$')
    >>> for p in np.arange(-1.0, 1.0, 0.1):
    ...     ax.plot(p,
    ...             det(np.array([[2*p, 1, 0], [1, 2*p, 1], [0, 1, 2*p]])),
    ...             'rx')
    >>> plt.legend(loc='best')
    >>> plt.show()

    They satisfy the recurrence relation:

    .. math::
        U_{2n-1}(x) = 2 T_n(x)U_{n-1}(x)

    where the :math:`T_n` are the Chebyshev polynomial of the first kind.
    Let's verify it for :math:`n = 2`:

    >>> from scipy.special import chebyt
    >>> x = np.arange(-1.0, 1.0, 0.01)
    >>> np.allclose(chebyu(3)(x), 2 * chebyt(2)(x) * chebyu(1)(x))
    True

    We can plot the Chebyshev polynomials :math:`U_n` for some values
    of :math:`n`:

    >>> x = np.arange(-1.0, 1.0, 0.01)
    >>> fig, ax = plt.subplots()
    >>> ax.set_ylim(-1.5, 1.5)
    >>> ax.set_title(r'Chebyshev polynomials $U_n$')
    >>> for n in np.arange(1,5):
    ...     ax.plot(x, chebyu(n)(x), label=rf'$U_n={n}$')
    >>> plt.legend(loc='best')
    >>> plt.show()

    r   rs  r}   r{         ?)r   r   r   r   rt   r   rg   rw  rx  s       rK   r   r   7  sl    r !S#U+++D "XX^d1q5kk)DSMM9FKKKrM   c                 ^    t          | d          \  }}}|dz  }|dz  }|dz  }|r|||fS ||f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.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{   r2   r   ri   rP   r   r   s        rK   r4   r4     Q    L 1d##GAq!FAFAFA	 !Qw!trM   c           	      @     dk     rt          d           dk    r dz   }n }t          |          \  }} dk    rg g }}dt          z   dk    dz   z  }d}t          ||||d d|          }|s.|                    d	 |d
          z              f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]`.

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

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

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

    r   r   r   r  rB   c                 8    dt          d| | z  dz  z
            z  S )NrB   r   r   r~  r   s    rK   rr   zchebyc.<locals>.<lambda>  s    C$q1q53;*?*?$? rM   r{   rJ   ra   rg   r}   r{   c                 .    t          j        |           S rO   )r   eval_chebycr   s    rK   rr   zchebyc.<locals>.<lambda>      W-@A-F-F rM   rc   )r   r4   r   rA   rt   rv  r   s   `       rK   r   r     s    D 	1uu1222AvvUDAqAvv21	
RAFa<	 B	BAq"b??"%	1 	1 	1A  G	qqtt#F#F#F#F
< HrM   c                 ^    t          | d          \  }}}|dz  }|dz  }|dz  }|r|||fS ||f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.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{   )r3   r  s        rK   r5   r5     r  rM   c           	      0     dk     rt          d           dk    r dz   }n }t          |          \  }} dk    rg g }}t          }d}t          ||||d d|          }|s3 dz    |d          z  }|                    |            f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]`.

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

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

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

    r   r   r   rB   c                 2    t          d| | z  dz  z
            S )Nr   r   r~  r   s    rK   rr   zchebys.<locals>.<lambda>b  s    DQUS[$9$9 rM   r  r  r{   c                 .    t          j        |           S rO   )r   eval_chebysr   s    rK   rr   zchebys.<locals>.<lambda>g  r  rM   rc   )r   r5   r   rA   rt   rv  )	r   rg   r   rP   r   re   rf   rq   rx  s	   `        rK   r   r   3  s    D 	1uu1222AvvUDAqAvv21	B	BAq"b99"%	1 	1 	1A  Gc'QQqTT!	#F#F#F#F
< HrM   c                 V    t          | |          }|d         dz   dz  f|dd         z   S )a  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.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{   Nr  )r   ri   xws      rK   r=   r=   m  s6    L 
a		BUQY!O122&&rM   c                     t          | dd|          }|r|S | dk    r	d| z  dz  }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   rs  r   r  r}   rB   r!   rt   r  s       rK   r   r     s[    2 QS...D 1uuAKKKrM   c                     t          | d          \  }}}|dz   dz  }t          j        dd          }|||z  z  }|r|||fS ||fS )a  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.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   r{   r  )r3   r   r   )r   ri   rP   r   r   m_uss         rK   r>   r>     sb    L 1d##GAq!	
Q!A<S!!DMA	 !Tz!trM   c                 h    t          | dd|          }|r|S d| z  }|                    |           |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  rs  r  r  r  s       rK   r    r      sG    2 QS...D TFKKKrM   c           
          t          |           }| dk     s| |k    rt          d          d}d }d }t          j        }d }t	          ||||||d|          S )a  Gauss-Legendre quadrature.

    Compute the sample points and weights for Gauss-Legendre
    quadrature [GL]_. 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`. 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.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.
    .. [GL] Gauss-Legendre quadrature, Wikipedia,
        https://en.wikipedia.org/wiki/Gauss%E2%80%93Legendre_quadrature

    Examples
    --------
    >>> import numpy as np
    >>> from scipy.special import roots_legendre, eval_legendre
    >>> roots, weights = roots_legendre(9)

    ``roots`` holds the roots, and ``weights`` holds the weights for
    Gauss-Legendre quadrature.

    >>> roots
    array([-0.96816024, -0.83603111, -0.61337143, -0.32425342,  0.        ,
            0.32425342,  0.61337143,  0.83603111,  0.96816024])
    >>> weights
    array([0.08127439, 0.18064816, 0.2606107 , 0.31234708, 0.33023936,
           0.31234708, 0.2606107 , 0.18064816, 0.08127439])

    Verify that we have the roots by evaluating the degree 9 Legendre
    polynomial at ``roots``.  All the values are approximately zero:

    >>> eval_legendre(9, roots)
    array([-8.88178420e-16, -2.22044605e-16,  1.11022302e-16,  1.11022302e-16,
            0.00000000e+00, -5.55111512e-17, -1.94289029e-16,  1.38777878e-16,
           -8.32667268e-17])

    Here we'll show how the above values can be used to estimate the
    integral from 1 to 2 of f(t) = t + 1/t with Gauss-Legendre
    quadrature [GL]_.  First define the function and the integration
    limits.

    >>> def f(t):
    ...    return t + 1/t
    ...
    >>> a = 1
    >>> b = 2

    We'll use ``integral(f(t), t=a, t=b)`` to denote the definite integral
    of f from t=a to t=b.  The sample points in ``roots`` are from the
    interval [-1, 1], so we'll rewrite the integral with the simple change
    of variable::

        x = 2/(b - a) * t - (a + b)/(b - a)

    with inverse::

        t = (b - a)/2 * x + (a + b)/2

    Then::

        integral(f(t), a, b) =
            (b - a)/2 * integral(f((b-a)/2*x + (a+b)/2), x=-1, x=1)

    We can approximate the latter integral with the values returned
    by `roots_legendre`.

    Map the roots computed above from [-1, 1] to [a, b].

    >>> t = (b - a)/2 * roots + (a + b)/2

    Approximate the integral as the weighted sum of the function values.

    >>> (b - a)/2 * f(t).dot(weights)
    2.1931471805599276

    Compare that to the exact result, which is 3/2 + log(2):

    >>> 1.5 + np.log(2)
    2.1931471805599454

    r   r   r}   c                     d| z  S r   rE   r   s    rK   r   zroots_legendre.<locals>.an_func	  rj  rM   c                 H    | t          j        dd| z  | z  dz
  z            z  S )NrB   r  r   r   r   s    rK   r   zroots_legendre.<locals>.bn_func	  s(    273!a%!)a-01111rM   c                     |  |z  t          j        | |          z  | t          j        | dz
  |          z  z   d|dz  z
  z  S )Nr   r{   r   eval_legendrer   s     rK   r   zroots_legendre.<locals>.df	  sR    Q.q!444g+AE15556:;a1f*F 	FrM   T)r   r   r   r  r   )r   ri   r   r   r   r   r   r   s           rK   r1   r1   	  s    X 	AA1uuQ8999
C  2 2 2AF F F "!S'7Ar4LLLrM   c                 4     dk     rt          d           dk    r dz   }n }t          |          \  }} dk    rg g }}dd z  dz   z  }t          d z  dz             t           dz             dz  z  d z  z  }t          ||||d d| f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}   r{   c                     dS r   rE   r   s    rK   rr   zlegendre.<locals>.<lambda>	  s    # rM   r   c                 .    t          j        |           S rO   r  r   s    rK   rr   zlegendre.<locals>.<lambda>	  s    (=a(C(C rM   r   )r   r1   r   rA   r   s   `       rK   r   r   	  s    L 	1uu1222AvvU"DAqAvv21	A		B	a!eai4A;;>	)CF	2BAq"bgCCCC	E 	E 	EA HrM   c                 V    t          |           \  }}|dz   dz  }|dz  }|r||dfS ||f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.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{   rB   )r1   )r   ri   rP   r   s       rK   r<   r<   	  sI    J !DAq	
Q!AFA	 !Sy!trM   c                 :     dk     rt          d          d } dk    rt          g g dd|d| fd          S t                     \  }}dd z  dz   z  }t          d z  dz             t           dz             dz  z  }t          |||||d| f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                     d| z  dz   S )Nr   rB   rE   r   s    rK   rJ   zsh_legendre.<locals>.wfunc
  s    Qw}rM   rB   r   c                 .    t          j        |           S rO   r   eval_sh_legendrer   s    rK   rr   zsh_legendre.<locals>.<lambda>
  s    W%=a%C%C rM   r{   r   c                 .    t          j        |           S rO   r  r   s    rK   rr   zsh_legendre.<locals>.<lambda>
  re  rM   )ra   rg   rS   )r   rA   r<   r   )r   rg   rJ   rP   r   re   rf   rq   s   `       rK   r   r   	  s    2 	1uu1222  Avv2r3UFECCCCE E 	EQDAq	A	B	a!eai4A;;>	)BAq"b%eFFFF	H 	H 	HAHrM   )F)r   )J__doc__numpyrX   r   r   r   r   r   r   r	   r
   r   r   r   scipyr   scipy.specialr    r   r   r   r   	_polyfuns_rootfuns_mapr^   keys__all__rY   rA   r   r6   r   r?   r!   r8   r   r7   r   r9   r   r   r  r  rO  rV  r   r   r:   r   r;   r   r2   r   r3   r   r4   r   r5   r   r=   r   r>   r    r1   r   r<   r   globals	_modattrsitemsnewfunoldfunappendrE   rM   rK   <module>r     s/  G GZ    + + + + + + + + + + + + + + + + + + + + + + + + + +                        }' ' '	 $-!*!*!*!*!*#,&0"+&0%/&0$.$.$.0 0  dd=--//00
0* * * * *") * * *Z, , ,bSN SN SN SNl[ [ [ [@5 5 5 5p4 4 4 4rAN AN AN ANHc c c cP&, &, &, &,R[ [ [ [@Q" Q" Q" Q"h# # # #L     F& & &R$ $ $Nu u up, , , ,^6 6 6rA A A ALI" I" I" I"X/ / / /lMM MM MM MM`L L L Lf/ / / /dh h h h\/ / / /d^ ^ ^ ^F- - - -`4 4 4 4r- - - -`5 5 5 5t'' '' '' ''T! ! ! !J- - - -`   FyM yM yM yMx5 5 5 5t+ + + +\& & & &T GII	#))++  NFF!&)IfNN6 rM   