scipy.integrate.nquad¶
- scipy.integrate.nquad(func, ranges, args=None, opts=None, full_output=False)[源代码]¶
多变量集成。
包装
quad
以实现多个变量的集成。各种选项允许改进不连续函数的积分,以及使用加权积分,并且通常可以更精细地控制积分过程。- 参数
- func{Callable,scipy.LowLevelCallable}
要集成的功能。有以下论点
x0, ... xn
,t0, ... tm
,在其中执行集成x0, ... xn
,它必须是浮点数。哪里`t0, ... tm
是在参数中传递的额外参数。函数签名应为 ``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
是变量和参数的数量。这个xx
数组包含坐标和额外参数。user_data
是包含在scipy.LowLevelCallable
。- ranges可迭代对象
范围的每个元素可以是2个数字的序列,或者返回这样的序列的可调用对象。
ranges[0]
对应于X0上的积分,依此类推。如果范围的元素是可调用的,则将使用所有可用的集成参数以及任何参数参数来调用它。例如,如果func = f(x0, x1, x2, t0, t1)
,那么ranges[0]
可以定义为(a, b)
否则作为(a, b) = range0(x1, x2, t0, t1)
。- args可迭代对象,可选
其他参数
t0, ..., tn
,要求 func , ranges ,以及opts
。- opts可迭代对象或字典,可选
要传递给的选项
quad
。可以是空的,可以是字典,也可以是返回字典的字典或函数序列。如果为空,则使用scipy.Integrate.quad中的默认选项。如果是DICT,则所有级别的积分都使用相同的选项。如果是序列,则序列的每个元素对应于特定的整合。例如,OPTS [0] 对应于X0上的积分,依此类推。如果是可调用的,则签名必须与ranges
。可用的选项及其默认值包括:EPSABS=1.49e-08
EPSR=1.49e-08
限制=50
点=无
权重=无
wvar=无
wopts=无
有关这些选项的详细信息,请参见
quad
和quad_explain
。- full_output布尔值,可选
部分实施
full_output
来自scipy.Integrate.quad。被积函数求值次数neval
可以通过设置full_output=True
当调用nquad时。
- 退货
- result浮动
整合的结果。
- abserr浮动
各种积分结果的绝对误差估计值的最大值。
- out_dictDICT,可选
包含有关集成的附加信息的字典。
参见
quad
一维数值积分
dblquad
,tplquad
二重积分和三重积分
fixed_quad
定阶高斯求积
quadrature
自适应高斯求积
示例
>>> 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})
>>> scale = .1 >>> def func2(x0, x1, x2, x3, t0, t1): ... return x0*x1*x3**2 + np.sin(x2) + 1 + (1 if x0+t1*x1-t0>0 else 0) >>> def lim0(x1, x2, x3, t0, t1): ... return [scale * (x1**2 + x2 + np.cos(x3)*t0*t1 + 1) - 1, ... scale * (x1**2 + x2 + np.cos(x3)*t0*t1 + 1) + 1] >>> def lim1(x2, x3, t0, t1): ... return [scale * (t0*x2 + t1*x3) - 1, ... scale * (t0*x2 + t1*x3) + 1] >>> def lim2(x3, t0, t1): ... return [scale * (x3 + t0**2*t1**3) - 1, ... scale * (x3 + t0**2*t1**3) + 1] >>> def lim3(t0, t1): ... return [scale * (t0+t1) - 1, scale * (t0+t1) + 1] >>> def opts0(x1, x2, x3, t0, t1): ... return {'points' : [t0 - t1*x1]} >>> def opts1(x2, x3, t0, t1): ... return {} >>> def opts2(x3, t0, t1): ... return {} >>> def opts3(t0, t1): ... return {} >>> integrate.nquad(func2, [lim0, lim1, lim2, lim3], args=(0,0), ... opts=[opts0, opts1, opts2, opts3]) (25.066666666666666, 2.7829590483937256e-13)