在_NQuad的integrate函数中,args是如何在没有赋值的情况下被更新的?有人能解释一下这段代码是怎么回事吗?
from scipy.integrate import quad
from functools import partial
class _RangeFunc:
def __init__(self, range_):
self.range_ = range_
def __call__(self, *args):
"""Return stored value.
*args needed because range_ can be float or func, and is called with
variable number of parameters.
"""
return self.range_
class _OptFunc:
def __init__(self, opt):
self.opt = opt
def __call__(self, *args):
"""Return stored dict."""
return self.opt
class _NQuad:
def __init__(self, func, ranges, opts, full_output):
self.loop = 0
self.abserr = 0
self.func = func
self.ranges = ranges
self.opts = opts
self.maxdepth = len(ranges)
self.full_output = full_output
if self.full_output:
self.out_dict = {'neval': 0}
def integrate(self, *args, **kwargs):
depth = kwargs.pop('depth', 0)
if kwargs:
raise ValueError('unexpected kwargs')
# Get the integration range and options for this depth.
ind = -(depth + 1)
fn_range = self.ranges[ind]
low, high = fn_range(*args)
fn_opt = self.opts[ind]
opt = dict(fn_opt(*args))
if 'points' in opt:
opt['points'] = [x for x in opt['points'] if low <= x <= high]
if depth + 1 == self.maxdepth:
f = self.func
else:
f = partial(self.integrate, depth=depth+1)
quad_r = quad(f, low, high, args=args, full_output=self.full_output,**opt)
print(f'loop: {self.loop}; args: {args}; quad_r: {quad_r}')
self.loop += 1
value = quad_r[0]
abserr = quad_r[1]
if self.full_output:
infodict = quad_r[2]
# The 'neval' parameter in full_output returns the total
# number of times the integrand function was evaluated.
# Therefore, only the innermost integration loop counts.
if depth + 1 == self.maxdepth:
self.out_dict['neval'] += infodict['neval']
self.abserr = max(self.abserr, abserr)
if depth > 0:
return value
else:
# Final result of N-D integration with error
if self.full_output:
return value, self.abserr, self.out_dict
else:
return value, self.abserr
def nquad(func, ranges, args=None, opts=None, full_output=False):
depth = len(ranges)
ranges = [rng if callable(rng) else _RangeFunc(rng) for rng in ranges]
if args is None:
args = ()
if opts is None:
opts = [dict([])] * depth
if isinstance(opts, dict):
opts = [_OptFunc(opts)] * depth
else:
opts = [opt if callable(opt) else _OptFunc(opt) for opt in opts]
return _NQuad(func, ranges, opts, full_output).integrate(*args)
def func(x, y):
return x*y**2
ranges = [[0, 2], [0, 1]]
opts = {"epsabs": 1.49e-8, "epsrel": 1.49e-8}
args = ()
full_output = False
result = nquad(func=func, ranges=ranges, args=args, opts=opts, full_output=False)
result
_NQuad的integrate方法中的args参数是一个可变长度的参数列表,作为args=args传递给quad函数。在_NQuad的每个循环中,*args语法用于将args元组解压缩为单独的参数,然后在各种计算中使用。
因此,虽然integrate方法中没有明确的args赋值,但args的内容实际上随着循环的每次迭代而更新,因为解包参数的值会发生变化。
总之,args参数在需要时被传递和解包,其内容通过对解包参数的操作被隐式更新。
1条答案
按热度按时间fykwrbwg1#
下面是一个修改
args
参数的函数的简单例子。它没有做任何重要的事情,但显示了如何更改该参数。每次调用函数时都会更改,但除此之外quad
不会对它做任何事情。