scipy.special.

roots_legendre#

scipy.special.roots_legendre(n, mu=False)[源代码]#

高斯-勒让德求积。

计算高斯-勒让德求积的样本点和权重[GL]。样本点是n次勒让德多项式的根\(P_n(x)\)。这些样本点和权重可以正确地积分次数小于等于\(2n - 1\)的多项式,积分区间为\([-1, 1]\),权重函数为\(w(x) = 1\)。 详情请参见[AS]的2.2.10节。

参数:
nint

求积阶数

mubool, 可选

如果为 True,则返回权重的总和,可选。

返回值:
xndarray

样本点

wndarray

权重

mufloat

权重之和

参考文献

[AS]

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

[GL] (1,2)

Gauss-Legendre quadrature, Wikipedia, https://en.wikipedia.org/wiki/Gauss%E2%80%93Legendre_quadrature

示例

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

roots保存根,weights保存高斯-勒让德求积的权重。

>>> 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])

通过在roots处评估9次勒让德多项式来验证我们是否具有根。 所有值都近似为零

>>> 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])

这里我们将展示如何使用上述值来估计f(t) = t + 1/t从1到2的积分,使用高斯-勒让德求积[GL]。 首先定义函数和积分限。

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

我们将使用integral(f(t), t=a, t=b)来表示f从t=a到t=b的定积分。 roots中的样本点来自区间[-1, 1],因此我们将通过简单的变量变换来重写积分

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

逆变换为

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

然后

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

我们可以用roots_legendre返回的值来近似后一个积分。

将上面计算的根从[-1, 1]映射到[a, b]。

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

将积分近似为函数值的加权和。

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

将其与精确结果进行比较,精确结果为3/2 + log(2)

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