L i!dZddlZddlmZmZmZmZmZmZm Z m Z m Z m Z m Z ddlmZddlmZddlmZddlmZej*Zgd Zd d d d ddddddddddddZeeej5zZGddej8ZdZdBdZdBdZ dBdZ!dBd Z"dBd!Z#dBd"Z$dBd#Z%dBd$Z&dBd%Z'dCd&Z(d'Z)d(Z*d)Z+d*Z,dCd+Z-d,Z.dBd-Z/dBd.Z0dBd/Z1dBd0Z2dBd1Z3dBd2Z4dBd3Z5dBd4Z6dBd5Z7dBd6Z8dBd7Z9dBd8Z:dBd9Z;dBd:ZdBd=Z?dBd>Z@dBd?ZAdBd@ZBdBdAZCeDZEejD]\ZGZHeEeGeEeH<ejeH y)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) expinfpisqrtfloorsincosaroundhstackarccosarange)linalg)airy)_specfun)_ufuncs)legendrechebytchebyuchebycchebysjacobilaguerre genlaguerrehermite hermitenorm gegenbauer sh_legendre sh_chebyt sh_chebyu sh_jacobip_rootst_rootsu_rootsc_rootss_rootsj_rootsl_rootsla_rootsh_rootshe_rootscg_rootsps_rootsts_rootsus_rootsjs_roots)roots_legendre roots_chebyt roots_chebyu roots_chebyc roots_chebys roots_jacobiroots_laguerreroots_genlaguerre roots_hermiteroots_hermitenormroots_gegenbauerroots_sh_legendreroots_sh_chebytroots_sh_chebyuroots_sh_jacobic$eZdZ ddZdZdZy) orthopoly1dNc  tt|D cgc]} || ||| z } } t|} |r| r| fd}| t|z } d}t j |d} tj j || jt|zt jtt||| |_ ||_ ||_| |_||_ycc} w)Nc|z SN)xevfknns _/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/scipy/special/_orthogonal.py eval_funcz'orthopoly1d.__init__..eval_func~sq6C<'?T)r)rangelenrabsnppoly1d__init__coeffsfloatarraylistzipweights weight_funclimitsnormcoef _eval_func)selfrootsrYhnknwfuncr[monicrJk equiv_weightsmupolyrGrHs @@rIrSzorthopoly1d.__init__us$CJ/1!eE!Ho51 1 "X C(c"gBByy$' 4uRy!89xxS%G HI    $-1sC4c|jr+t|tjs|j|Stjj ||SrD)r] isinstancerQrR__call__)r^vs rIrjzorthopoly1d.__call__s< ??:a#;??1% %99%%dA. .rKcdk(ry|xjzc_|jr fd|_|xjzc_y)NrLc|zSrDrE)rFrGps rIz$orthopoly1d._scale..sA rK)_coeffsr]r\)r^rnrGs `@rI_scalezorthopoly1d._scales< 8   oo 2DO  rK)NrLrLNNFN)__name__ __module__ __qualname__rSrjrqrErKrIrArAssBF59$4/ rKrAc0tj|d}tjd|f} ||dd| dddf<||| dddf<tj| d} ||| } ||| } | | | z z} ||dz | } tj tj | }tj tj | }| tj|j|jzd z z} | tj|j|jzd z z} d | | zz }|r||ddd zdz }| | ddd z dz } |||jz z}|r| ||fS| |fS) 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)dtyperNrT)overwrite_a_band@rL) rQr zerosreigvals_bandedlogrPrmaxminsum)nmu0an_funcbn_funcfdf symmetrizerfrdcrFydyfmlog_fmlog_dyws rI_gen_roots_and_weightsrs~ !3A !QAaenAadG QZAacFa$7A !QA AqB2IA 1Q3B VVBFF2J F VVBFF2J F"&&&**,-3 44B"&&&**,-3 44B rBwA 4R4[A  4R4[A quuwA !Sy!t rKc > t|}|dks||k7r td|dks|dkr td|dk(r|dk(r t||S||k(rt||dz|S||zdkr)d||zdzzt j |dz|dzz}nNt j||zdzt jdzt j|dz|dzz}| | zdk(r fd }n fd } fd } fd } fd } t|||||| d|S)aOGauss-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. rn must be a positive integer.r{z'alpha and beta must be greater than -1.?irzcPtj|dk(z dzzz dS)NrrxrrQwhererdabs rIrzroots_jacobi..an_funcs+88AFQUq1uqy$93? ?rKctj|dk(z dzzz zzz d|zzzd|zzzdzzz S)Nrrxrzrrs rIrzroots_jacobi..an_funcse88QQ1q519%QQC!GaK!Oa! a!8K#LM rKc dd|zzzz tj|z|zzd|zzzdzz ztj|dk(dtj||zzzd|zzzdz z zS)NrzrxrrL)rQrrrs rIrzroots_jacobi..bn_funcs 37Q;? #ggq1uQ'1q519q=1+<=> ?hhqAvsBGGAQOsQw{QQR?R,S$TU V rKc4tj||SrDr eval_jacobirrFrrs rIrzroots_jacobi..f s""1aA..rKcdd|zzdzztj|dz dzdz|zS)Nrrrrs rIrzroots_jacobi..df"s=a!eai!m$w':':1q5!a%QPQ'RRRrKF) int ValueErrorr1r;rbetarQrr~betalnr) ralpharrfmrrrrrrrs @@rIr6r6s7T AA1uQ899 {dbjBCC | a$$ }59b11  E$JqL!GLLq$q&$AAffedlQ&"&&+5~~eAgtAv678 A A1u| @  /S !!S'7Ar5" MMrKc dkr tdfd}dk(r!tggdd|d|tjSt d\}}}zdz}d |zd z|zz t zd zz} | t zdzt d zz t |zz z} t d z|zd zz t d zz t |zz } t||| | |d|fd } | S) aJacobi 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() rn must be nonnegative.c&d|z zd|zzzSNrrE)rFrrs rIrbzjacobi..wfuncusA%1q5T/11rKrLr{rrJTrfrxrrzc4tj|SrDr)rFrrrs rIrozjacobi..sg11!UD!DrK)rrArQ ones_liker6_gam) rrrrcrbrFrrfab1r`rarns ``` rIrr'sV 1u1222Av2r3UGU%'\\3 3Audt4HAq" $, C C1q53; $q5y1}"5 5B$q4x#~ a!e ,tAG} < -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{rz>(p - q) must be greater than -1, and q must be greater than 0.rTrxrz)rr6) rp1q1rfmessagerFrrscales rIr?r?sT 2"}aR!!1beRT40GAq! Q! A GEJAJA !Qw!t rKc dkr tdfd}dk(r!tggdd|d|tjS}t |\}}t dzt zzt zzt zz dzz}|dzzt dzzdzzz}d} t|||| |d |fd  } | S) aShifted 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}`. rrc,d|z z z|dz zzSNrLrE)rFrnqs rIrbzsh_jacobi..wfuncs#aQU#aAGn44rKrLrrrrxrrc4tj|SrD)reval_sh_jacobi)rFrrnrs rIrozsh_jacobi..s)?)?1a)KrKrbr[rcrJ)rrArQrr?r) rrnrrcrbn1rFrr`rapps ``` rIr!r!sH 1u1225Av2r3UGU%'\\3 3 B 2q! $DAq a!etAE{ "T!a%[ 04A A 3F FB1q519a!eai!+ ,,B B Q2rvUK MB IrKc bt|}|dks||k7r tddkr tdtjdz}|dk(r -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. rrr{zalpha must be greater than -1.rLrvcd|zzdzSNrxrrErdrs rIrz"roots_genlaguerre..an_func0s1uu}q  rKc<tj||zz SrDrQrrs rIrz"roots_genlaguerre..bn_func2sQY(((rKc2tj||SrDreval_genlaguerrerrFrs rIrzroots_genlaguerre..f4s''5!44rKc|tj||z|ztj|dz |zz |z Srrrs rIrzroots_genlaguerre..df6sNG,,Qq99u9 8 8Qq IIJMNO OrKF)rrrgammarQrVr) rrrfrrrFrrrrrs ` rIr8r8sP AA1uQ899 rz9:: -- "CAv HHeCi[# & HHcUC  a9 a4K!)5O "!S'7Ar5" MMrKc >dkr tddkr tddk(rdz}n}t|\}}fd}dk(rgg}}tzdztdzz }dztdzz }t|||||dtf|fd} | 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{zalpha must be > -1rrrc(t| |zzSrDr)rFrs rIrbzgenlaguerre..wfuncsA2we##rKc2tj|SrDrrFrrs rIrozgenlaguerre..sg66q%CrK)rr8rrAr) rrrcrrFrrbr`rarns `` rIrr<sb {-..1u122Av U  R 'DAq$Av21 a%i!m tAE{ *B q4A; BAq"b%!S5C EA HrKct|d|S)aGauss-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. rr)r8)rrfs rIr7r7sL Q ++rKc dkr tddk(rdz}n}t|\}}dk(rgg}}d}dztdzz }t||||ddtf|fd}|S)aRLaguerre 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() rrrrLr{ct| SrDrrFs rIrozlaguerre..&s CGrKc0tj|SrD)r eval_laguerrerFrs rIrozlaguerre..'sg33Aq9rK)rr7rrArrrcrrFrr`rarns` rIrrsZ 1u122Av U  " DAqAv21 B q4A; BAq"b"3aXu9 ;A HrKc t|}|dks||k7r tdtjtj}|dkr+d}d}t j }d}t||||||d|St|\}} |r|| |fS|| fS)aGauss-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. rrc d|zSNrrErds rIrzroots_hermite..an_funcq 7NrKc2tj|dz SNrzrrs rIrzroots_hermite..bn_funcss771s7# #rKc@d|ztj|dz |zS)Nrzrr eval_hermiterrFs rIrzroots_hermite..dfvs"7W11!a%;; ;rKT) rrrQrrrrr_roots_hermite_asy rrfrrrrrrnodesrYs rIr9r9-s| AA1uQ899 ''"%%.CCx  $   <%agw2tRPP+A.w '3& &'> !rKc|dzdz }dt|dz zd|zz dztzdt|dz zd|zzdzz fd}d}dtz}t|D]}|||||z z }|S)aHelper 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 rxr@rz@c&|t|z z SrD)r)rFrs rIrz_compute_tauk..fs3q6zA~rKcdt|z Sr)r rs rIrz_compute_tauk..dfsSV|rK)rrrN) rrdmaxitrrrxiirs @rI _compute_taukrs4 A A U1S5\ CE !C '+s53z dz }d|dddfzd|d ddfzzd?|d"ddfzz d@|dddfzzd$|zzd%z }dA|d'ddfzdB|d)ddfzzdC|d+ddfzz dD|dddfzz dE|dddfzz dFz d/z }d0|d1ddfzd2|d3ddfzz dG|d5ddfzzdH|dddfzz dI|d ddfzzdJ|d"ddfzzdK|dddfzzdL|zzd;z } t |dz|z\}!}"}#}$dt t z|dMzz|z}%|td+dNd+jdz}&||z}'||z|&dOddf|z|zz|&dPddf|z|zz|dzz }(||z|&dOddf|z|zz|&dPddf|z|zz|&dddf|z|zz|&dddf|z|zz|d+zz })| |z|&dOddf|z|zz |dzz }*| |z|&dOddf| z|zz|&dPddf| z|zz|&dddf|z|zz |d"zz }+| |z|&dOddf| z|zz|&dPddf| z|zz|&dddf| z|zz|&dddf| z|zz|&dddf|z|zz |d)zz },|%|!|'|(|dzz z|)|dQzz zz|"|*|+|dzz z|,|dQzz zz|dRzz zz}-t dt z|dSzz|z }.||z|&dOddf|z|zz |z }/||z|&dOddf|z|zz|&dPddf|z|zz|&dddf|z|zz |dzz }0||z|&dOddf|z|zz|&dPddf|z|zz|&dddf|z|zz|&dddf|z|zz|&dddf|z| zz |d zz }1||z}2| |z|&dOddf| z|zz|&dPddf|z|zz|dzz }3| |z|&dOddf| z|zz|&dPddf| z|zz|&dddf| z|zz|&dddf|z|zz|d+zz }4|.|!|/|0|dzz z|1|dQzz zz|dzz |"|2|3|dzz z|4|dQzz zzzz}5|-|5fS)TaAsymptotic 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 rzrLrrgUUUUUU?rxrg?g98c?gHxi?gd?g_cJ6?g¿grqGgk~X X¿g@9SsMԿg:(,a(rrNg@g8@g"rg o@g b@g@g g@@g@rg؍AgЦAgPAg@@ gAg0Ag8 JAgH;Ag \hAgAg夓AgdjA gm6A g AgP/Agf!Bgݱ;BghdBgAg.@gpt@ga@g`@g Aggj AgAgH$iAgAig.Ag(AgӍAg5e Bgd{>Bl+2VgUUUUUU?rrrgUUUUUU?r)rr r reshaperrr)6rthetastctrfetazetaphia0a1a2a3a4a5b0b1b2b3b4b5ctpu0u1u2u3u4u5v0v1v2v3v4v5AiAipBiBipPphipA0A1A2B0B1B2UPdC0C1C2D0D1D2Uds6 rI_pbcfrLsD UB UB QB 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 #ac( WS1X- -AaC0@ @ S1X   (  ,/7 8B #bd) hs1Q3x/ /(3qs82C C s1Q3x  "-c!A#h"6 79C DGQ RB SAY SAY!6 6c"Q$i9O O QqS ! "$0QqS$9 :B R%$qs)B,r/ !D1IbLO 3d1Q3il2o E 1IbLO #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 R22s7 ?RCZ/ 0 BBGObSj0 1BM AB CA c"fW % +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 BB R%$qs)B,r/ !D1IbLO 3tQw >B R%$qs)B,r/ !D1IbLO 3d1Q3il2o E 1IbLO #Qw 'B rR"RW*_r"c'z12R']Bb2b#g:o2s7 234 5B b5LrKcltd|zdz}||z }t|}t|D]O}t||\}}|td|zt |z|zz } || z}t t | dksOn|t|z} |dzdk(rd| d<t| dz ddzzz } | | fS)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 rzrLg+=rxrrr) rr rNrLrrrPr r) r x_initialrrftrruuddthetarFrs rI_newtonrSs6 c!eck BBA 1IE 5\a2d3i"ns5z1B67 s6{ e #   SZA1uz! QTE c"a%i A a4KrKc(t|}t||\}}|dzdk(r(t|ddd |g}t|ddd|g}n't|ddd |g}t|ddd|g}|ttt |z z}||fS)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`. rxrNr{)r rSr rrr)rr rrYs rIrrsV  BQ^NE71uztt e,-'$B$-12r!Bw/0'"Qr'*G45 tBx#g,&&G '>rKc dkr tddk(rdz}n}t|\}}d}dk(rgg}}dztdzzttz}dz}t |||||t t f|fd}|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() rrrc t| |zSrDrrs rIrbzhermite..wfunc3sA26{rKrxc0tj|SrDrrs rIrozhermite..:sg221a8rK)rr9rrrrAr rrcrrFrrbr`rarns ` rIrrsb 1u122Av U   DAqAv21 AQU d2h &B ABAq"b%3$e8 :A HrKc Xt|}|dks||k7r tdtjdtjz}|dkr+d}d}t j }d}t||||||d|St|\}} |td z}| td z} |r|| |fS|| fS) 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. rrrzrc d|zSrrErs rIrz"roots_hermitenorm..an_funcyrrKc,tj|SrDrrs rIrz"roots_hermitenorm..bn_func{s771: rKc:|tj|dz |zSrreval_hermitenormrs rIrzroots_hermitenorm..df~sw//Aq99 9rKTrx) rrrQrrrr^rrrs rIr:r:@sf AA1uQ899 ''#bee) CCx    $ $ :%agw2tRPP+A.w a47 '3& &'> !rKc dkr tddk(rdz}n}t|\}}d}dk(rgg}}tdtzt dzz}d}t |||||t t f|fd}|S) abNormalized (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}`. rrrc&t| |zdz Srrrs rIrbzhermitenorm..wfuncsA26C<  rKrxrLc0tj|SrDr]rs rIrozhermitenorm..(@(@A(FrKr)rr:rrrrArrXs ` rIrrs> 1u122Av U  R DAq!Av21 a"fQU #B BAq"btSkF HA HrKc bt|}|dks||k7r tddkr tddk(r t||SdkrVtjtj t jdzzt jdzz }nodz }tjgd }|d }tdt|D] }||z||z}|tjtj z z}d }fd } fd } fd} 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. rrz alpha must be greater than -0.5.rrrL)gF90(+?g& {XgkʰDg3Ts?g?grLrc d|zSrrErs rIrz!roots_gegenbauer..an_func QwrKcjtj||dzzdz zd|zz|zdz zz S)Nrxrrrrs rIrz!roots_gegenbauer..bn_funcs?wwqAE MA-.!q5y/QYQR]2STUUrKc2tj||SrDreval_gegenbauerrs rIrzroots_gegenbauer..fs&&q%33rKc| |ztj||z|dzzdz tj|dz |zzd|dzz z Srrjrs rIrzroots_gegenbauer..df sc BFW,,Qq9 91u9}q G$;$;AE5!$LL M aZ rKT) rrr2rQrrrrrVrNrOr) rrrfrr inv_alpharTtermrrrrs ` rIr;r;s.P AA1uQ899 t|;<< # Ar"" |wwruu~ eck :: eai() J >?Qi!S[) 1D /F4L0C 1BGGBEEEM**V4 "!S'7Ar4 LLrKcZtjrdkr tdtdz dz |}|sdk(r|St dzzt dzzt dzz t dzzz }|j |fd|j d<|S) aGegenbauer (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() rdz1`alpha` must be a finite number greater than -1/2rrcrrxcDtjt|SrD)rrkrUrs rIrozgegenbauer..\sG,C,CE!HDI1-NrKr])rQisfiniterrrrq__dict__)rrrcbasefactors`` rIrrsB ;;u $LMM !US[%#+U ;D Q 1U7Q;$us{"331U7m"53;?34FKK#NDMM, KrKct|}|dks||k7r tdtjt j | dz|dd|zz }t j |t|z }|r ||tfS||fS)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. rrrx)rrr_sinpirQr full_liker)rrfrrFrs rIr2r2es|N AA1uQ899ryy!aA.!A#67A Q1A !Rx!t rKc dkr tdd}dk(rtggtd|d|fdS}t|d\}}}td z }d d z z}t|||||d|fd } | 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() rrc*dtd||zz z S)NrLrrrs rIrbzchebyt..wfuncsT!a!e)_$$rKrLrc0tj|SrDr eval_chebytrs rIrozchebyt..sW%8%8A%>rKTrrxrc0tj|SrDr}rs rIrozchebyt..sg11!Q7rK)rrArr2) rrcrbrrFrrfr`rarns ` rIrrst 1u122%Av2r2sE7E>@ @ BB4(HAq" aB QUBAq"b%%7 9A HrKc"t|}|dks||k7r tdtj|ddtz|dzz }tj |}ttj |dzz|dzz }|r ||tdz fS||fS)aGauss-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. rrrr{rx)rrrQr rr r)rrfrrOrFrs rIr3r3sL AA1uQ899 !Qb AE*A q A RVVAY\QU#A !R!V|!t rKct|dd|}|r|Sttdz t|dzzt|dzz }|j ||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() rrprzrx?)rrrrrqrrcrtrus rIrr7sUr !S#U +D  "X^d1q5k )DSM 9FKK KrKcVt|d\}}}|dz}|dz}|dz}|r|||fS||fS)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. Trxr2rrfrFrrs rIr4r4IL1d#GAq!FAFAFA !Qw!t rKc dkr tddk(rdz}n}t|\}}dk(rgg}}dtzdk(dzz}d}t||||dd|}|s,|j d |d z fd |j d <|S) agChebyshev 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. rrrrrLc0dtd||zdz z z S)NrLrrr{rs rIrozchebyc..sC$q1q53;*?$?rKrxrbr[rcrzrxc0tj|SrD)r eval_chebycrs rIrozchebyc..W-@-@A-FrKr])rr4rrArqrsrs` rIrrsD 1u122Av U   DAqAv21 RAFa< B BAq"b?"% 1A  qt#F < HrKcVt|d\}}}|dz}|dz}|dz}|r|||fS||fS)aGauss-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. Trx)r3rs rIr5r5rrKc  dkr tddk(rdz}n}t|\}}dk(rgg}}t}d}t||||dd|}|s1dz|dz }|j |fd |j d <|S) afChebyshev 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. rrrrLc*td||zdz z S)Nrrr{rs rIrozchebys..bsDQUS[$9rKrrrxc0tj|SrD)r eval_chebysrs rIrozchebys..grrKr])rr5rrArqrs) rrcrrFrr`rarnrus ` rIrr3sD 1u122Av U   DAqAv21 B BAq"b9"% 1A c'QqT! #F < HrKc>t||}|ddzdz f|ddzS)aGauss-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. rrrxNr)rrfxws rIr=r=ms1L a B UQY!O 12 &&rKcnt|dd|}|r|S|dkDr d|zdz }nd}|j||S)aXShifted 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}`. rrrprrrzrLr!rqrs rIrrsF2 QS .D  1uAKK KrKct|d\}}}|dzdz }tjdd}|||z z}|r|||fS||fS)aGauss-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. Trrxr)r3rr)rrfrFrrm_uss rIr>r>sYL1d#GAq! Q! A <<S !DMA !Tz!t rKcXt|dd|}|r|Sd|z}|j||S)aZShifted 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}`. rzrrprrrs rIr r s62 QS .D  TFKK KrKc t|}|dks||k7r tdd}d}d}tj}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 rrrzc d|zSrrErs rIrzroots_legendre..an_func rgrKcJ|tjdd|z|zdz z zS)NrLrrrrs rIrzroots_legendre..bn_func s'2773!a%!)a-0111rKc| |ztj||z|tj|dz |zzd|dzz z S)Nrrxr eval_legendrers rIrzroots_legendre..df sRQ..q!44g++AE1556:;a1f*F FrKT)rrrrr)rrfrrrrrrs rIr1r1 sbX AA1uQ899 C2AF "!S'7Ar4 LLrKc dkr tddk(rdz}n}t|\}}dk(rgg}}ddzdzz }tdzdztdzdzz dzz }t||||dd|fd }|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. ]) rrrrzrxcyrrErs rIrozlegendre.. srKrc0tj|SrDrrs rIrozlegendre.. s(=(=a(CrKr)rr1rrArs` rIrr sL 1u122Av U  " DAqAv21 A B a!eai4A;> )CF 2BAq"b gC EA HrKcNt|\}}|dzdz }|dz}|r||dfS||fS)aGauss-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. rrxrL)r1)rrfrFrs rIr<r< sAJ ! DAq Q! AFA !Sy!t rKc dkr tdd}dk(rtggdd|d|fdSt\}}ddzdzz }tdzdztdzdzz }t|||||d|fd  }|S) aShifted 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. rrcd|zdzS)NrrLrErs rIrbzsh_legendre..wfunc sQw}rKrLrc0tj|SrDreval_sh_legendrers rIrozsh_legendre.. sW%=%=a%CrKrxrc0tj|SrDrrs rIrozsh_legendre.. rbrK)r[rcrJ)rrAr<r)rrcrbrFrr`rarns` rIrr s2 1u122Av2r3UFECE E Q DAq A B a!eai4A;> )BAq"b%eF HA HrK)F)r)J__doc__numpyrQrrrrrrr r r r r scipyr scipy.specialrrrrr _polyfuns _rootfuns_maprWkeys__all__rRrArr6rr?r!r8rr7rr9rrrr rLrSrrr:rr;rr2rr3rr4rr5rr=rr>r r1rr<rglobals _modattrsitemsnewfunoldfunappendrErKrIrsGZ++++}} ' $-!*!*!*!*!*#,&0"+&0%/&0$.$.$.0 d=--/0 0*"))*Z,bSNl[ @5p4rANHc P&,R[ @Q"h#L F&R$Nup,^6rA LI"X/ lMM`Lf/dh \/d^F-`4 r-`5 t''T!J-`FyMx5 t+\& T I #))+NFF!&)If NN6rK