
    (ph!                       S r SSKrSSKJrJrJrJrJrJrJ	r	J
r
JrJrJr  SSKJr  SSKJr  SSKJr  SSKJr  \R*                  r/ S	QrS
SSSSSSSSSSSSSSS.r\\" \R5                  5       5      -   r " S S\R8                  5      rS rSBS jrSBS jr SBS jr!SBS  jr"SBS! jr#SBS" jr$SBS# jr%SBS$ jr&SBS% jr'SCS& jr(S' r)S( r*S) r+S* r,SCS+ jr-S, r.SBS- jr/SBS. jr0SBS/ jr1SBS0 jr2SBS1 jr3SBS2 jr4SBS3 jr5SBS4 jr6SBS5 jr7SBS6 jr8SBS7 jr9SBS8 jr:SBS9 jr;SBS: jr<SBS; jr=SBS< jr>SBS= jr?SBS> jr@SBS? jrASBS@ jrBSBSA jrC\D" 5       rE\R                  5        H  u  rGrH\E\G   \E\H'   \R                  \H5        M!     g)DaI  
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                   .    \ rS rSr  SS jrS rS rSrg)orthopoly1ds   Nc	           	        ^^ [        [        U5      5       V	s/ s H  n	X)   U" X   5      -  PM     n
n	[        U5      nU(       a"  UmT(       a	  UmUU4S jnU[        U5      -  nSn[        R
                  " USS9n[        R
                  R                  XR                  [        U5      -  5        [        R                  " [        [        XU
5      5      5      U l        XPl        X`l        Xl        Xl        g s  sn	f )Nc                    > T" U 5      T-  $ N )xevfknns    L/var/www/html/venv/lib/python3.13/site-packages/scipy/special/_orthogonal.py	eval_func'orthopoly1d.__init__.<locals>.eval_func~   s    q6C<'          ?T)r)rangelenr   absnppoly1d__init__coeffsfloatarraylistzipweightsweight_funclimitsnormcoef
_eval_func)selfrootsr[   hnknwfuncr]   monicrK   kequiv_weightsmupolyrH   rI   s                @@rJ   rU   orthopoly1d.__init__u   s     $CJ/1/ !eEHo5/ 	 1"XC(c"gBB yy$'
		4uRy!89xxS%G HI  $-1s   C9c                     U R                   (       a0  [        U[        R                  5      (       d  U R                  U5      $ [        R                  R	                  X5      $ rE   )r_   
isinstancerS   rT   __call__)r`   vs     rJ   rm   orthopoly1d.__call__   s=    ??:a#;#;??1%%99%%d..rM   c                    ^^ TS:X  a  g U =R                   T-  sl         U R                  mT(       a  UU4S jU l        U =R                  T-  sl        g )NrN   c                    > T" U 5      T-  $ rE   rF   )rG   rH   ps    rJ   <lambda>$orthopoly1d._scale.<locals>.<lambda>   s    A
rM   )_coeffsr_   r^   )r`   rr   rH   s    `@rJ   _scaleorthopoly1d._scale   s<    8oo2DOrM   )r_   r]   r^   r\   r[   )NrN   rN   NNFN)__name__
__module____qualname____firstlineno__rU   rm   rv   __static_attributes__rF   rM   rJ   rA   rA   s   s    BF59$4/rM   rA   c                 @   [         R                  " U SS9n[         R                  " SU 45      n	U" USS 5      U	SSS24'   U" U5      U	SSS24'   [        R                  " U	SS9n
U" X
5      nU" X
5      nXU-  -  n
U" U S-
  U
5      n[         R
                  " [         R                  " U5      5      n[         R
                  " [         R                  " U5      5      nU[         R                  " UR                  5       UR                  5       -   S	-  5      -  nU[         R                  " UR                  5       UR                  5       -   S	-  5      -  nS
X-  -  nU(       a  UUSSS2   -   S-  nXSSS2   -
  S-  n
UUUR                  5       -  -  nU(       a  U
UU4$ U
U4$ )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       @rN   )rS   r   zerosr   eigvals_bandedlogrR   r   maxminsum)nmu0an_funcbn_funcfdf
symmetrizerh   rf   crG   ydyfmlog_fmlog_dyws                    rJ   _gen_roots_and_weightsr      st    			!3A
!QAaenAadGQZAacFa$7A 	
!A	AB2IA 
1Q3BVVBFF2JFVVBFF2JF"&&&**,-3
44B"&&&**,-3
44BrwA4R4[A4R4[AquuwA	!Sy!trM   c           
      N  ^
^ [        U 5      nU S:  d  X:w  a  [        S5      eUS::  d  US::  a  [        S5      eUS:X  a  US:X  a  [        XC5      $ X:X  a  [        XAS-   U5      $ X-   S::  a)  SX-   S-   -  [        R
                  " US-   US-   5      -  nOP[        R                  " X-   S-   [        R                  " S5      -  [        R                  " US-   US-   5      -   5      nUm
UmT
T-   S:X  a  U
U4S	 jnOU
U4S
 jnU
U4S jnU
U4S jnU
U4S jn	[        XEXgXSU5      $ )a  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                 R   > [         R                  " U S:H  TT-
  ST-   T-   -  S5      $ )Nr   r   r   rS   whererf   abs    rJ   r   roots_jacobi.<locals>.an_func  s+    88AFQUq1uqy$93??rM   c                    > [         R                  " U S:H  TT-
  ST-   T-   -  TT-  TT-  -
  SU -  T-   T-   SU -  T-   T-   S-   -  -  5      $ )Nr   r   r   r   r   s    rJ   r   r     se    88QQ1q519%QQC!GaK!Oa!a!8K#LM rM   c           
        > SSU -  T-   T-   -  [         R                  " U T-   U T-   -  SU -  T-   T-   S-   -  5      -  [         R                  " U S:H  S[         R                  " X T-   T-   -  SU -  T-   T-   S-
  -  5      5      -  $ )Nr   r   r   rN   )rS   r   r   r   s    rJ   r   roots_jacobi.<locals>.bn_func  s    37Q;?#ggq1uQ'1q519q=1+<=>?hhqAvsBGGAQOsQw{QQR?R,S$TUV	
rM   c                 6   > [         R                  " U TTU5      $ rE   r   eval_jacobir   rG   r   r   s     rJ   r   roots_jacobi.<locals>.f   s    ""1aA..rM   c                 f   > SU T-   T-   S-   -  [         R                  " U S-
  TS-   TS-   U5      -  $ )Nr   r   r   r   s     rJ   r   roots_jacobi.<locals>.df"  s=    a!eai!m$w':':1q5!a%QPQ'RRRrM   F)int
ValueErrorr1   r;   r   betarS   r   r   betalnr   )r   alphar   rh   mr   r   r   r   r   r   r   s             @@rJ   r6   r6      s#   T 	AA1u899{dbjBCC|a$$}9b11EJqL!GLLq$q&$AA ffelQ&"&&+5~~eAgtAv67 8AA1u|	@	
/S!!'A5"MMrM   c                   ^ ^^ T S:  a  [        S5      eUU4S jnT S:X  a  [        / / SSUSU[        R                  S9$ [	        T TTSS9u  pVnTT-   S-   nS	U-  S	T -  U-   -  [        T T-   S
-   5      -  n	U	[        T T-   S-   5      [        T S
-   5      -  [        T U-   5      -  -  n	[        S	T -  U-   5      ST -  -  [        T S
-   5      -  [        T U-   5      -  n
[        XVXUSUUUU 4S j5      nU$ )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                 &   > SU -
  T-  SU -   T-  -  $ Nr   rF   )rG   r   r   s    rJ   rd   jacobi.<locals>.wfuncu  s    A%1q5T/11rM   rN   r   r   rK   Trh   r   r   r   c                 6   > [         R                  " TTTU 5      $ rE   r   )rG   r   r   r   s    rJ   rs   jacobi.<locals>.<lambda>  s    g11!UD!DrM   )r   rA   rS   	ones_liker6   _gam)r   r   r   re   rd   rG   r   rh   ab1rb   rc   rr   s   ```         rJ   r   r   '  s   V 	1u1222Av2r3UGU%'\\3 	3Audt4HA"
$,
C	
C1q53;	$q5y1}"5	5B$q4x#~
a!e
,tAG}
<<B	a!eck	S!V	#d1q5k	1DSM	ABA"%%D	FAHrM   c                     X-
  S::  d  US::  a  Sn[        U5      e[        XU-
  US-
  S5      u  pVnUS-   S-  nSU-  nXh-  nXx-  nU(       a  XVU4$ XV4$ )a  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q1rh   messagerG   r   r   scales	            rJ   r?   r?     su    T 	"}aR!!1eRT40GA!	
Q!AGEJAJA	QwtrM   c                   ^ ^^ T S:  a  [        S5      eUU4S jnT S:X  a  [        / / SSUSU[        R                  S9$ T n[	        UTT5      u  pg[        T S-   5      [        T T-   5      -  [        T T-   5      -  [        T T-   T-
  S-   5      -  nUST -  T-   [        ST -  T-   5      S-  -  -  nSn	[        XgXUS	UU UU4S
 jS9n
U
$ )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U -
  TT-
  -  U TS-
  -  -  $ NrN   rF   )rG   rr   qs    rJ   rd   sh_jacobi.<locals>.wfunc  s#    aQU#aAGn44rM   rN   r   r   r   r   r   r   c                 6   > [         R                  " TTTU 5      $ rE   )r   eval_sh_jacobi)rG   r   rr   r   s    rJ   rs   sh_jacobi.<locals>.<lambda>  s    )?)?1a)KrM   rd   r]   re   rK   )r   rA   rS   r   r?   r   )r   rr   r   re   rd   n1rG   r   rb   rc   pps   ```        rJ   r!   r!     s    H 	1u1225Av2r3UGU%'\\3 	3	
B2q!$DA	a!etAE{	"T!a%[	04A	A3F	FB1q519a!eai!+
,,B	B	Q2vUK
MBIrM   c           
      v  ^ [        U 5      nU S:  d  X:w  a  [        S5      eTS:  a  [        S5      e[        R                  " TS-   5      nUS:X  aA  [        R
                  " TS-   /S5      n[        R
                  " U/S5      nU(       a  XVU4$ XV4$ U4S jnU4S jnU4S	 jn	U4S
 jn
[        X4XxXSU5      $ )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.rN   r~   c                    > SU -  T-   S-   $ Nr   r   rF   rf   r   s    rJ   r   "roots_genlaguerre.<locals>.an_func0  s    1uu}q  rM   c                 <   > [         R                  " X T-   -  5      * $ rE   rS   r   r   s    rJ   r   "roots_genlaguerre.<locals>.bn_func2  s    Y(((rM   c                 4   > [         R                  " U TU5      $ rE   r   eval_genlaguerrer   rG   r   s     rJ   r   roots_genlaguerre.<locals>.f4  s    ''5!44rM   c                    > U [         R                  " U TU5      -  U T-   [         R                  " U S-
  TU5      -  -
  U-  $ r   r   r   s     rJ   r   roots_genlaguerre.<locals>.df6  sN    G,,Qq99u9 8 8Qq IIJMNO 	OrM   F)r   r   r   gammarS   rX   r   )r   r   rh   r   r   rG   r   r   r   r   r   s    `         rJ   r8   r8     s    P 	AA1u899rz9::
--	
"CAvHHeCi[#&HHcUC 94K!)5O "!'A5"MMrM   c                 B  ^ ^ TS::  a  [        S5      eT S:  a  [        S5      eT S:X  a  T S-   nOT n[        UT5      u  pEU4S jnT S:X  a  / / pT[        T T-   S-   5      [        T S-   5      -  nST -  [        T S-   5      -  n[        XEXxUS[        4UUU 4S j5      n	U	$ )aS  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                 (   > [        U * 5      U T-  -  $ rE   r   )rG   r   s    rJ   rd   genlaguerre.<locals>.wfunc  s    A2we##rM   c                 4   > [         R                  " TTU 5      $ rE   r   rG   r   r   s    rJ   rs   genlaguerre.<locals>.<lambda>  s    g66q%CrM   )r   r8   r   rA   r   )
r   r   re   r   rG   r   rd   rb   rc   rr   s
   ``        rJ   r   r   <  s    b {-..1u122AvUR'DA$Av21	a%i!m	tAE{	*B
q4A;	BA"%!S5C	EAHrM   c                     [        U SUS9$ )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   rh   s     rJ   r7   r7     s    L Q++rM   c                    ^  T S:  a  [        S5      eT S:X  a  T S-   nOT n[        U5      u  p4T S:X  a  / / pCSnST -  [        T S-   5      -  n[        X4XVS S[        4UU 4S j5      nU$ )aj  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   rN   r   c                     [        U * 5      $ rE   r   rG   s    rJ   rs   laguerre.<locals>.<lambda>&  s
    CGrM   c                 2   > [         R                  " TU 5      $ rE   )r   eval_laguerrerG   r   s    rJ   rs   r   '  s    g33Aq9rM   )r   r7   r   rA   r   r   re   r   rG   r   rb   rc   rr   s   `       rJ   r   r     s    Z 	1u122AvU"DAAv21	B
q4A;	BA""3aXu9	;AHrM   c           
         [        U 5      nU S:  d  X:w  a  [        S5      e[        R                  " [        R                  5      nU S::  a(  S nS n[
        R                  nS n[        X#XEXgSU5      $ [        U5      u  pU(       a  XU4$ X4$ )aN  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                     SU -  $ Nr   rF   rf   s    rJ   r   roots_hermite.<locals>.an_funcq      7NrM   c                 4    [         R                  " U S-  5      $ Nr   r   r   s    rJ   r   roots_hermite.<locals>.bn_funcs  s    771s7##rM   c                 B    SU -  [         R                  " U S-
  U5      -  $ )Nr   r   r   eval_hermiter   rG   s     rJ   r   roots_hermite.<locals>.dfv  s"    7W11!a%;;;rM   T)	r   r   rS   r   r   r   r   r   _roots_hermite_asy
r   rh   r   r   r   r   r   r   nodesr[   s
             rJ   r9   r9   -  s    | 	AA1u899
''"%%.CCx		$  	<%agtRPP+A.3&&>!rM   c                    ^ U S-  S-
  nS[        U S-  5      -  SU-  -
  S-   [        -  S[        U S-  5      -  SU-  -   S-   -  mU4S jnS nS[        -  n[        U5       H  nXd" U5      U" U5      -  -
  nM     U$ )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                 &   > U [        U 5      -
  T-
  $ rE   )r   )rG   r   s    rJ   r   _compute_tauk.<locals>.f  s    3q6zA~rM   c                     S[        U 5      -
  $ r   )r	   r   s    rJ   r   _compute_tauk.<locals>.df  s    SV|rM   )r   r   rP   )	r   rf   maxitr   r   r   xiir   s	           @rJ   _compute_taukr    s    4 	
AA	U1S5\	CE	!C	'+s53</?#a%/G#/MNA	RB5\!B%2, IrM   c                     [        X5      n[        SU-  5      S-  nU S-  S-
  nS[        U S-  5      -  SU-  -   S-   nXS-  SSU-  -  SSSU-
  S-  -  -  SSU-
  -  -
  S-
  -  -
  nU$ )	a  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   rN   r  g      @      ?)r  r	   r   )r   rf   tauksigkr   nuxksqs          rJ   _initial_nodes_ar    s    4 Ds4x=!D	AA	U1S5\	CE	!C	'B7S#b&\S#s4x!m*;%<sCH~%MPT%TUUDKrM   c                 j   U S-  S-
  nS[        U S-  5      -  SU-  -   S-   n[        R                  " UR                  5       S5      S   SSS2   nUS	U-  US
-  -  -   SUS-  -  US-  -  -   SSUS-  -  -
  US-  -  -   SU-  SUS-  -  -   S	-  US-  -  -   SUS-  -  SUS-  -  -   S-  US-  -  -
  nU$ )a  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?   gJ_?gr(?g)r   r   airyzor   )r   rf   r   r  akr  s         rJ   _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   SU -  S-
  n[        U5      R                  [        5      n[        S[        [	        U S-  5      S-   5      5      nUSSS2   n[        XSUS-    5      n[        XUS-   S 5      n[        [        XV/5      5      nU S-  S:X  a  [        SU/5      nU$ )	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           rJ   _initial_nodesr'    s    0 Q,
#Cc{!!#&H	3uQsU|A~&	'B	DbDBA+8A:/DA(1*+/D	fd\"	#B1uzS"IIrM   c                    [        U5      n[        U5      nSU -  S-   nSU-  SU-  U-  -
  nSU-  S-  S-  * nU* US-  -  S-  nSnSn	S	n
S
nSnSnSnSnSnSnSnSnU[        S5      R                  S5      -  nSnSUSSS24   -  SU-  -
  S-  nSUSSS24   -  SUSSS24   -  -   S-   S-  nSUSSS24   -  SUS SS24   -  -   S!US"SS24   -  -
  S#USSS24   -  -
  S$U-  -
  S%-  nS&US'SS24   -  S(US)SS24   -  -
  S*US+SS24   -  -
  S,USSS24   -  -   S-USSS24   -  -   S.-   S/-  nS0US1SS24   -  S2US3SS24   -  -
  S4US5SS24   -  -   S6USSS24   -  -
  S7US SS24   -  -   S8US"SS24   -  -
  S9USSS24   -  -
  S:U-  -
  S;-  nSnSUSSS24   -  SU-  -   S-  nS<USSS24   -  S=USSS24   -  -
  S>-
  S-  nSUSSS24   -  SUS SS24   -  -   S?US"SS24   -  -
  S@USSS24   -  -   S$U-  -   S%-  nSAUS'SS24   -  SBUS)SS24   -  -   SCUS+SS24   -  -
  SDUSSS24   -  -
  SEUSSS24   -  -
  SF-
  S/-  nS0US1SS24   -  S2US3SS24   -  -
  SGUS5SS24   -  -   SHUSSS24   -  -
  SIUS SS24   -  -   SJUS"SS24   -  -   SKUSSS24   -  -   SLU-  -   S;-  n [	        US-  U-  5      u  n!n"n#n$S[        [        5      -  USM-  -  U-  n%U[        S+SNS+5      R                  S5      -  n&UU-  n'UU-  U&SOSS24   U-  U-  -   U&SPSS24   U-  U-  -   US-  -  n(UU-  U&SOSS24   U-  U-  -   U&SPSS24   U-  U-  -   U&SSS24   U-  U-  -   U&SSS24   U-  U-  -   US+-  -  n)U	U-  U&SOSS24   U-  U-  -   * US-  -  n*UU-  U&SOSS24   U
-  U-  -   U&SPSS24   U	-  U-  -   U&SSS24   U-  U-  -   * US"-  -  n+UU-  U&SOSS24   U-  U-  -   U&SPSS24   U-  U-  -   U&SSS24   U
-  U-  -   U&SSS24   U	-  U-  -   U&SSS24   U-  U-  -   * US)-  -  n,U%U!U'U(US-  -  -   U)USQ-  -  -   -  U"U*U+US-  -  -   U,USQ-  -  -   -  USR-  -  -   -  n-[        S[        -  5      USS-  -  U-  n.UU-  U&SOSS24   U-  U-  -   * U-  n/UU-  U&SOSS24   U-  U-  -   U&SPSS24   U-  U-  -   U&SSS24   U-  U-  -   * US-  -  n0UU-  U&SOSS24   U-  U-  -   U&SPSS24   U-  U-  -   U&SSS24   U-  U-  -   U&SSS24   U-  U-  -   U&SSS24   U-  U -  -   * US -  -  n1UU-  n2U
U-  U&SOSS24   U	-  U-  -   U&SPSS24   U-  U-  -   US-  -  n3UU-  U&SOSS24   U-  U-  -   U&SPSS24   U
-  U-  -   U&SSS24   U	-  U-  -   U&SSS24   U-  U-  -   US+-  -  n4U.U!U/U0US-  -  -   U1USQ-  -  -   -  US-  -  U"U2U3US-  -  -   U4USQ-  -  -   -  -   -  n5U-U54$ )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   rN   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ctrh   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                                                         rJ   _pbcfrh    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!A#h,R
4	'B
s1Q3x-%AaC.
(5
0F	:B
#ac(
WS1X-
-AaC0@
@S1X (,/7
8B
#bd)
hs1Q3x/
/(3qs82C
Cs1Q3x "-c!A#h"679CDGQ
RB
SAY
SAY!6
6c"Q$i9O
OQqS!"$0QqS$9:<I#aPQc(<RS3qs8#$&3B&67:F
GB 
B
c!A#h,R
4	'B
s1Q3x-%AaC.
(5
0F	:B
#ac(
WS1X-
-AaC0@
@S1X (,/7
8B
C1I
QqS 1
1HS1X4E
Es1Q3x "-c!A#h"679ABEO
PB
SAY
SAY!6
6c"Q$i9O
OQqS!"$0QqS$9:<I#aPQc(<RS3qs8#$&1"n58D
EB BMD01BRDHrG}$s*A &B"**622D	BB
R%$qs)B,r/
!D1IbLO
3tQw	>B
R%$qs)B,r/
!D1IbLO
3d1Q3il2o
E1IbLO#Qw
'Bb54!9R<?"	#dAg	-Bb54!9R<?"T!A#Yr\"_4tAaCy|BF	G$PQ'	QBb54!9R<?"T!A#Yr\"_4tAaCy|BF!A#Yr\"_#AaCy|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!A#Yr\"_4tAaCy|BF	G$PQ'	QBb54!9R<?"T!A#Yr\"_4tAaCy|BF!A#Yr\"_#AaCy|B/ 
026'
:B	BB
R%$qs)B,r/
!D1IbLO
3tQw	>B
R%$qs)B,r/
!D1IbLO
3d1Q3il2o
E1IbLO#Qw
'B 
rR"RW*_r"c'z12R']Bb2b#g:o2s7
234 
5Bb5LrM   c                 j   [        SU -  S-   5      nX-  n[        U5      n[        U5       HN  n[        X5      u  pxU[        S5      U-  [	        U5      -  U-  -  n	XY-   n[        [        U	5      5      S:  d  MN    O   U[        U5      -  n
U S-  S:X  a  SU
S'   [        U
S-  * 5      SWS-  -  -  nX4$ )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   rN   g+=r   r   r   r   )	r   r   rP   rh  r   r   rR   r	   r   )r   	x_initialr  rh   tr4  r  uuddthetarG   r   s               rJ   _newtonro    s    6 
c!eck	BA1IE5\ad3i"ns5z1B67s6{e#  	SZA1uz!QTE
c"a%i A4KrM   c                 $   [        U 5      n[        X5      u  p#U S-  S:X  a(  [        USSS2   * U/5      n[        USSS2   U/5      nO'[        USSS2   * U/5      n[        USSS2   U/5      nU[        [        5      [        U5      -  -  nX#4$ )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'  ro  r   r   r   r   )r   r&  r  r[   s       rJ   r  r    s    V 
	BQ^NE1uztte,-'$B$-12r!Bw/0'"Qr'*G45tBx#g,&&G>rM   c                   ^  T S:  a  [        S5      eT S:X  a  T S-   nOT n[        U5      u  p4S nT S:X  a  / / pCST -  [        T S-   5      -  [        [        5      -  nST -  n[        X4XgU[        * [        4UU 4S j5      nU$ )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                      [        U * U -  5      $ rE   r   r   s    rJ   rd   hermite.<locals>.wfunc3  s    A26{rM   r   c                 2   > [         R                  " TU 5      $ rE   r   r   s    rJ   rs   hermite.<locals>.<lambda>:  s    g221a8rM   )r   r9   r   r   r   rA   r   	r   re   r   rG   r   rd   rb   rc   rr   s	   `        rJ   r   r     s    b 	1u122AvUDAAv21	
AQU	d2h	&B	
ABA"%3$e8	:AHrM   c           
      \   [        U 5      nU S:  d  X:w  a  [        S5      e[        R                  " S[        R                  -  5      nU S::  a(  S nS n[
        R                  nS n[        X#XEXgSU5      $ [        U5      u  pU[        S	5      -  nU	[        S	5      -  n	U(       a  XU4$ X4$ )
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                     SU -  $ r   rF   r   s    rJ   r   "roots_hermitenorm.<locals>.an_funcy  r   rM   c                 .    [         R                  " U 5      $ rE   r   r   s    rJ   r   "roots_hermitenorm.<locals>.bn_func{  s    771:rM   c                 <    U [         R                  " U S-
  U5      -  $ r   r   eval_hermitenormr   s     rJ   r   roots_hermitenorm.<locals>.df~  s    w//Aq999rM   Tr   )	r   r   rS   r   r   r   r~  r   r  r  s
             rJ   r:   r:   @  s    f 	AA1u899
''#bee)
CCx		$$	:%agtRPP+A.a473&&>!rM   c                    ^  T S:  a  [        S5      eT S:X  a  T S-   nOT n[        U5      u  p4S nT S:X  a  / / pC[        S[        -  5      [	        T S-   5      -  nSn[        X4XgU[        * [        4UU 4S jS9nU$ )	a  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                 &    [        U * U -  S-  5      $ r   r   r   s    rJ   rd   hermitenorm.<locals>.wfunc  s    A26C<  rM   r   rN   c                 2   > [         R                  " TU 5      $ rE   r}  r   s    rJ   rs   hermitenorm.<locals>.<lambda>      (@(@A(FrM   r   )r   r:   r   r   r   rA   r   rv  s	   `        rJ   r   r     s    > 	1u122AvUR DA!Av21	a"fQU	#B	BA"tSkF	HAHrM   c           
      r  ^ [        U 5      nU S:  d  X:w  a  [        S5      eTS:  a  [        S5      eTS:X  a  [        X5      $ TS::  aY  [        R                  " [        R
                  5      [        R                  " TS-   5      -  [        R                  " TS-   5      -  nOqST-  n[        R                  " / S	Q5      nUS
   n[        S[        U5      5       H  nXE-  Xg   -   nM     U[        R                  " [        R
                  T-  5      -  nS nU4S jn	U4S jn
U4S jn[        X4XXSU5      $ )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   rN   )gF90(+?g&{XgkʰDg3Ts?g      ?g      rN   r   c                     SU -  $ r   rF   r   s    rJ   r   !roots_gegenbauer.<locals>.an_func      QwrM   c                 j   > [         R                  " X ST-  -   S-
  -  SU T-   -  U T-   S-
  -  -  5      $ )Nr   r   r  r   r   s    rJ   r   !roots_gegenbauer.<locals>.bn_func  s=    wwqE	MA-.!q5y/QYQR]2STUUrM   c                 4   > [         R                  " U TU5      $ rE   r   eval_gegenbauerr   s     rJ   r   roots_gegenbauer.<locals>.f  s    &&q%33rM   c                    > U * U-  [         R                  " U TU5      -  U ST-  -   S-
  [         R                  " U S-
  TU5      -  -   SUS-  -
  -  $ r   r  r   s     rJ   r   roots_gegenbauer.<locals>.df
  sc    BFW,,Qq991u9}q G$;$;AE5!$LLMaZ 	rM   T)r   r   r2   rS   r   r   r   r   rX   rP   rQ   r   )r   r   rh   r   r   	inv_alpharV   termr   r   r   r   s    `          rJ   r;   r;     s   P 	AA1u899t|;<<	#
 A""|wwruu~eck ::eai() J	 > ?Qi!S[)D/FL0C *BGGBEEEM**V4
 "!'A4LLrM   c                 p  ^ ^ [         R                  " T5      (       a  TS::  a  [        S5      e[        T TS-
  TS-
  US9nU(       d  T S:X  a  U$ [	        ST-  T -   5      [	        TS-   5      -  [	        ST-  5      -  [	        TS-   T -   5      -  nUR                  U5        UU 4S jUR                  S'   U$ )	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()

r  z1`alpha` must be a finite number greater than -1/2r   re   r   r   c                 F   > [         R                  " [        T5      TU 5      $ rE   )r   r  rW   r   s    rJ   rs   gegenbauer.<locals>.<lambda>\  s    G,C,CE!HDI1-NrM   r_   )rS   isfiniter   r   r   rv   __dict__)r   r   re   basefactors   ``   rJ   r   r     s    B ;;u$LMM!US[%#+U;DQ1U7Q;$us{"331U7m"53;?34FKK#NDMM,KrM   c                    [        U 5      nU S:  d  X:w  a  [        S5      e[        R                  " [        R
                  " U* S-   US5      SU-  -  5      n[        R                  " U[        U-  5      nU(       a  X4[        4$ X44$ )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   _sinpirS   r   	full_liker   )r   rh   r   rG   r   s        rJ   r2   r2   e  sv    N 	AA1u899ryy!aA.!A#67A
Q1A	RxtrM   c                    ^  T S:  a  [        S5      eS nT S:X  a  [        / / [        SUSUU 4S j5      $ T n[        USS9u  pEn[        S	-  nS	T S
-
  -  n[        XEXxUSUU 4S j5      n	U	$ )aS	  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                 (    S[        SX -  -
  5      -  $ )NrN   r   r   r   s    rJ   rd   chebyt.<locals>.wfunc  s    T!ae)_$$rM   rN   r   c                 2   > [         R                  " TU 5      $ rE   r   eval_chebytr   s    rJ   rs   chebyt.<locals>.<lambda>  s    W%8%8A%>rM   Tr   r   r   c                 2   > [         R                  " TU 5      $ rE   r  r   s    rJ   rs   r    s    g11!Q7rM   )r   rA   r   r2   )
r   re   rd   r   rG   r   rh   rb   rc   rr   s
   `         rJ   r   r     s    t 	1u122%Av2r2sE7E>@ 	@	
BB4(HA"	aB	
QUBA"%%7	9AHrM   c                 0   [        U 5      nU S:  d  X:w  a  [        S5      e[        R                  " USS5      [        -  US-   -  n[        R
                  " U5      n[        [        R                  " U5      S-  -  US-   -  nU(       a  XE[        S-  4$ XE4$ )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   rS   r   r   r	   r   )r   rh   r   rk  rG   r   s         rJ   r3   r3     s    L 	AA1u899
		!Qb AE*A
q	A
RVVAY\QU#A	R!V|trM   c                     [        U SSUS9nU(       a  U$ [        [        5      S-  [        U S-   5      -  [        U S-   5      -  nUR	                  U5        U$ )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   r  r   r         ?)r   r   r   r   rv   r   re   r  r  s       rJ   r   r   7  sU    r !S#U+D"X^d1q5k)DSM9FKKKrM   c                 Z    [        U S5      u  p#nUS-  nUS-  nUS-  nU(       a  X#U4$ X#4$ )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   rh   rG   r   r   s        rJ   r4   r4     C    L 1d#GA!FAFAFA	QwtrM   c           
        ^  T S:  a  [        S5      eT S:X  a  T S-   nOT n[        U5      u  p4T S:X  a  / / pCS[        -  T S:H  S-   -  nSn[        X4XVS SUS9nU(       d-  UR	                  S	U" S
5      -  5        U 4S jUR
                  S'   U$ )a  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  rN   c                 .    S[        SX -  S-  -
  5      -  $ )NrN   r   r  r  r   s    rJ   rs   chebyc.<locals>.<lambda>  s    C$q153;*?$?rM   r   rd   r]   re   r   r   c                 2   > [         R                  " TU 5      $ rE   )r   eval_chebycr   s    rJ   rs   r        W-@-@A-FrM   r_   )r   r4   r   rA   rv   r  r   s   `       rJ   r   r     s    D 	1u122AvUDAAv21	
RAFa<	 B	BA"?"%	1A 	qt#F

< HrM   c                 Z    [        U S5      u  p#nUS-  nUS-  nUS-  nU(       a  X#U4$ X#4$ )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        rJ   r5   r5     r  rM   c           
        ^  T S:  a  [        S5      eT S:X  a  T S-   nOT n[        U5      u  p4T S:X  a  / / pC[        nSn[        X4XVS SUS9nU(       d2  T S-   U" S5      -  nUR	                  U5        U 4S	 jUR
                  S
'   U$ )a  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   rN   c                 (    [        SX -  S-  -
  5      $ )Nr   r  r  r   s    rJ   rs   chebys.<locals>.<lambda>b  s    DQUS[$9rM   r  r  r   c                 2   > [         R                  " TU 5      $ rE   )r   eval_chebysr   s    rJ   rs   r  g  r  rM   r_   )r   r5   r   rA   rv   r  )	r   re   r   rG   r   rb   rc   rr   r  s	   `        rJ   r   r   3  s    D 	1u122AvUDAAv21	B	BA"9"%	1A c'QqT!	#F

< HrM   c                 <    [        X5      nUS   S-   S-  4US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   rh   xws      rJ   r=   r=   m  s/    L 
a	BUQY!O12&&rM   c                 t    [        U SSUS9nU(       a  U$ U S:  a	  SU -  S-  nOSnUR                  U5        U$ )a  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   rN   r!   rv   r  s       rJ   r   r     sF    2 QS.D1uAKKKrM   c                     [        U S5      u  p#nUS-   S-  n[        R                  " SS5      nX5U-  -  nU(       a  X#U4$ X#4$ )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   rh   rG   r   r   m_uss         rJ   r>   r>     sQ    L 1d#GA!	
Q!A<<S!DMA	TztrM   c                 \    [        U SSUS9nU(       a  U$ SU -  nUR                  U5        U$ )a  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  s       rJ   r    r      s6    2 QS.DTFKKKrM   c           
          [        U 5      nU S:  d  X:w  a  [        S5      eSnS nS n[        R                  nS n[	        X#XEXgSU5      $ )ay  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                     SU -  $ r   rF   r   s    rJ   r   roots_legendre.<locals>.an_func	  r  rM   c                 L    U [         R                  " SSU -  U -  S-
  -  5      -  $ )NrN   r  r   r   r   s    rJ   r   roots_legendre.<locals>.bn_func	  s'    2773!a%!)a-0111rM   c                     U * U-  [         R                  " X5      -  U [         R                  " U S-
  U5      -  -   SUS-  -
  -  $ )Nr   r   r   eval_legendrer   s     rJ   r   roots_legendre.<locals>.df	  sP    Q..q44g++AE1556:;a1f*F 	FrM   T)r   r   r   r  r   )r   rh   r   r   r   r   r   r   s           rJ   r1   r1   	  sZ    X 	AA1u899
C2AF "!'A4LLrM   c                    ^  T S:  a  [        S5      eT S:X  a  T S-   nOT n[        U5      u  p4T S:X  a  / / pCSST -  S-   -  n[        ST -  S-   5      [        T S-   5      S-  -  ST -  -  n[        X4XVS SUU 4S jS	9nU$ )
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                     gr   rF   r   s    rJ   rs   legendre.<locals>.<lambda>	  s    #rM   r   c                 2   > [         R                  " TU 5      $ rE   r  r   s    rJ   rs   r  	  s    (=(=a(CrM   r   )r   r1   r   rA   r   s   `       rJ   r   r   	  s    L 	1u122AvU"DAAv21	A		B	a!eai4A;>	)CF	2BA"gC	EA HrM   c                 R    [        U 5      u  p#US-   S-  nUS-  nU(       a  X#S4$ X#4$ )aj  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   rN   )r1   )r   rh   rG   r   s       rJ   r<   r<   	  s;    J !DA	
Q!AFA	SytrM   c                   ^  T S:  a  [        S5      eS nT S:X  a  [        / / SSUSUU 4S j5      $ [        T 5      u  p4SST -  S-   -  n[        ST -  S-   5      [        T S-   5      S-  -  n[        X4XVUSUU 4S	 jS
9nU$ )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U -  S-   $ )Nr   rN   rF   r   s    rJ   rd   sh_legendre.<locals>.wfunc
  s    Qw}rM   rN   r   c                 2   > [         R                  " TU 5      $ rE   r   eval_sh_legendrer   s    rJ   rs   sh_legendre.<locals>.<lambda>
  s    W%=%=a%CrM   r   r   c                 2   > [         R                  " TU 5      $ rE   r  r   s    rJ   rs   r  
  r  rM   )r]   re   rK   )r   rA   r<   r   )r   re   rd   rG   r   rb   rc   rr   s   `       rJ   r   r   	  s    2 	1u122Av2r3UFECE 	EQDA	A	B	a!eai4A;>	)BA"%eF	HAHrM   )F)r  )J__doc__numpyrS   r   r   r   r   r   r   r	   r
   r   r   r   scipyr   scipy.specialr    r   r   r   r   	_polyfuns_rootfuns_maprY   keys__all__rT   rA   r   r6   r   r?   r!   r8   r   r7   r   r9   r  r  r  r'  rh  ro  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appendrF   rM   rJ   <module>r     s  GZ + + + +    }}'	 $-!*!*!*!*!*#,&0"+&0%/&0$.$.$.0  d=--/0
0*")) *Z,bSNl[@5p4rANHcP&,R[@Q"h#L F&R$Nup,^6rALI"X/lMM`Lf/dh\/d^F-`4r-`5t''T!J-`FyMx5t+\&T I	#))+NFF!&)IfNN6 ,rM   