scipy.integrate.

nquad#

scipy.integrate.nquad(func, ranges, args=None, opts=None, full_output=False)[source]#

多變數積分。

包裝 quad 以啟用多變數積分。各種選項允許改進不連續函數的積分,以及使用加權積分,並通常能更精細地控制積分過程。

參數:
func{callable, scipy.LowLevelCallable}

要積分的函數。具有 x0, ... xn, t0, ... tm 等參數,其中積分是對 x0, ... xn 進行的,這些必須是浮點數。其中 t0, ... tm 是 args 中傳遞的額外參數。函數簽名應為 func(x0, x1, ..., xn, t0, t1, ..., tm)。積分按順序進行。也就是說,對 x0 的積分是最內層積分,而 xn 是最外層積分。

如果使用者希望提高積分效能,則 f 可以是 scipy.LowLevelCallable,並具有以下簽名之一

double func(int n, double *xx)
double func(int n, double *xx, void *user_data)

其中 n 是變數和 args 的數量。xx 陣列包含座標和額外參數。user_datascipy.LowLevelCallable 中包含的資料。

rangesiterable object

ranges 的每個元素可以是 2 個數字的序列,也可以是返回此類序列的可呼叫物件。ranges[0] 對應於對 x0 的積分,依此類推。如果 ranges 的元素是可呼叫物件,則將使用所有可用的積分參數以及任何參數化參數來呼叫它。例如,如果 func = f(x0, x1, x2, t0, t1),則 ranges[0] 可以定義為 (a, b)(a, b) = range0(x1, x2, t0, t1)

argsiterable object, optional

func、ranges 和 opts 所需的額外參數 t0, ... tn

optsiterable object or dict, optional

要傳遞給 quad 的選項。可以是空的、一個 dict,或是一系列 dict 或返回 dict 的函數。如果為空,則使用 scipy.integrate.quad 的預設選項。如果是 dict,則所有積分層級都使用相同的選項。如果是序列,則序列的每個元素對應於特定的積分。例如,opts[0] 對應於對 x0 的積分,依此類推。如果是可呼叫物件,則簽名必須與 ranges 的簽名相同。可用的選項及其預設值為

  • epsabs = 1.49e-08

  • epsrel = 1.49e-08

  • limit = 50

  • points = None

  • weight = None

  • wvar = None

  • wopts = None

有關這些選項的更多資訊,請參閱 quad

full_outputbool, optional

full_output 從 scipy.integrate.quad 的部分實作。積分函數評估的次數 neval 可以透過在呼叫 nquad 時設定 full_output=True 來獲得。

回傳:
resultfloat

積分的結果。

abserrfloat

各種積分結果中絕對誤差估計值的最大值。

out_dictdict, optional

包含有關積分的額外資訊的 dict。

另請參閱

quad

一維數值積分

dblquad, tplquad

二重和三重積分

fixed_quad

固定階高斯求積

註解

為了獲得有效結果,積分必須收斂;不保證發散積分的行為。

QUADPACK 層級常式詳情

nquad 呼叫來自 FORTRAN 函式庫 QUADPACK 的常式。本節提供有關呼叫每個常式的條件以及每個常式的簡短描述。呼叫的常式取決於 weightpoints 和積分極限 ab

QUADPACK 常式

weight

points

無限邊界

qagse

qagie

qagpe

qawoe

‘sin’, ‘cos’

qawfe

‘sin’, ‘cos’

a 或 b

qawse

‘alg*’

qawce

‘cauchy’

以下提供每個常式的簡短描述,出自 [1]

qagse

是一種基於全域自適應區間細分的積分器,結合外插法,可以消除多種類型被積函數奇異點的影響。

qagie

處理無限區間上的積分。無限範圍被映射到有限區間,然後應用與 QAGS 相同的策略。

qagpe

與 QAGS 的用途相同,但也允許使用者提供有關問題點的位置和類型的明確資訊,即被積函數的內部奇異點、不連續點和其他困難的橫座標。

qawoe

是一種積分器,用於評估有限區間 [a,b] 上的 \(\int^b_a \cos(\omega x)f(x)dx\)\(\int^b_a \sin(\omega x)f(x)dx\),其中 \(\omega\)\(f\) 由使用者指定。規則評估組件基於修改後的 Clenshaw-Curtis 技術

自適應細分方案與外插程序結合使用,外插程序是 QAGS 中的修改版本,允許演算法處理 \(f(x)\) 中的奇異點。

qawfe

計算使用者提供的 \(\omega\)\(f\) 的傅立葉變換 \(\int^\infty_a \cos(\omega x)f(x)dx\)\(\int^\infty_a \sin(\omega x)f(x)dx\)QAWO 的程序應用於連續的有限區間,並透過 \(\varepsilon\)-演算法將收斂加速應用於積分近似值序列。

qawse

近似 \(\int^b_a w(x)f(x)dx\),其中 \(a < b\)\(w(x) = (x-a)^{\alpha}(b-x)^{\beta}v(x)\),其中 \(\alpha,\beta > -1\),其中 \(v(x)\) 可能是以下函數之一: \(1\), \(\log(x-a)\), \(\log(b-x)\), \(\log(x-a)\log(b-x)\)

使用者指定 \(\alpha\)\(\beta\) 和函數 \(v\) 的類型。應用全域自適應細分策略,並在包含 ab 的子區間上進行修改後的 Clenshaw-Curtis 積分。

qawce

計算 \(\int^b_a f(x) / (x-c)dx\),其中積分必須解釋為柯西主值積分,對於使用者指定的 \(c\)\(f\)。該策略是全域自適應的。修改後的 Clenshaw-Curtis 積分用於包含點 \(x = c\) 的區間。

參考文獻

[1]

Piessens, Robert; de Doncker-Kapenga, Elise; Überhuber, Christoph W.; Kahaner, David (1983). QUADPACK: A subroutine package for automatic integration. Springer-Verlag. ISBN 978-3-540-12553-2.

範例

計算

\[\int^{1}_{-0.15} \int^{0.8}_{0.13} \int^{1}_{-1} \int^{1}_{0} f(x_0, x_1, x_2, x_3) \,dx_0 \,dx_1 \,dx_2 \,dx_3 ,\]

其中

\[\begin{split}f(x_0, x_1, x_2, x_3) = \begin{cases} x_0^2+x_1 x_2-x_3^3+ \sin{x_0}+1 & (x_0-0.2 x_3-0.5-0.25 x_1 > 0) \\ x_0^2+x_1 x_2-x_3^3+ \sin{x_0}+0 & (x_0-0.2 x_3-0.5-0.25 x_1 \leq 0) \end{cases} .\end{split}\]
>>> import numpy as np
>>> from scipy import integrate
>>> func = lambda x0,x1,x2,x3 : x0**2 + x1*x2 - x3**3 + np.sin(x0) + (
...                                 1 if (x0-.2*x3-.5-.25*x1>0) else 0)
>>> def opts0(*args, **kwargs):
...     return {'points':[0.2*args[2] + 0.5 + 0.25*args[0]]}
>>> integrate.nquad(func, [[0,1], [-1,1], [.13,.8], [-.15,1]],
...                 opts=[opts0,{},{},{}], full_output=True)
(1.5267454070738633, 2.9437360001402324e-14, {'neval': 388962})

計算

\[\int^{t_0+t_1+1}_{t_0+t_1-1} \int^{x_2+t_0^2 t_1^3+1}_{x_2+t_0^2 t_1^3-1} \int^{t_0 x_1+t_1 x_2+1}_{t_0 x_1+t_1 x_2-1} f(x_0,x_1, x_2,t_0,t_1) \,dx_0 \,dx_1 \,dx_2,\]

其中

\[\begin{split}f(x_0, x_1, x_2, t_0, t_1) = \begin{cases} x_0 x_2^2 + \sin{x_1}+2 & (x_0+t_1 x_1-t_0 > 0) \\ x_0 x_2^2 +\sin{x_1}+1 & (x_0+t_1 x_1-t_0 \leq 0) \end{cases}\end{split}\]

\((t_0, t_1) = (0, 1)\)

>>> def func2(x0, x1, x2, t0, t1):
...     return x0*x2**2 + np.sin(x1) + 1 + (1 if x0+t1*x1-t0>0 else 0)
>>> def lim0(x1, x2, t0, t1):
...     return [t0*x1 + t1*x2 - 1, t0*x1 + t1*x2 + 1]
>>> def lim1(x2, t0, t1):
...     return [x2 + t0**2*t1**3 - 1, x2 + t0**2*t1**3 + 1]
>>> def lim2(t0, t1):
...     return [t0 + t1 - 1, t0 + t1 + 1]
>>> def opts0(x1, x2, t0, t1):
...     return {'points' : [t0 - t1*x1]}
>>> def opts1(x2, t0, t1):
...     return {}
>>> def opts2(t0, t1):
...     return {}
>>> integrate.nquad(func2, [lim0, lim1, lim2], args=(0,1),
...                 opts=[opts0, opts1, opts2])
(36.099919226771625, 1.8546948553373528e-07)