Numpy 如何知道scipy函数是否使用C代码?
有人知道scipy.signal.argrelmax和scipy.integrate.simps是在源代码中使用C代码,还是在纯python中?我想加快使用Numba的速度,所以问一下Numpy 如何知道scipy函数是否使用C代码?,numpy,scipy,Numpy,Scipy,有人知道scipy.signal.argrelmax和scipy.integrate.simps是在源代码中使用C代码,还是在纯python中?我想加快使用Numba的速度,所以问一下 通过几个级别的调用进行跟踪,看起来argrelmax最终使用以下循环: def _boolrelextrema(data, comparator...) # comparator - a function .... results = np.ones(data.shape, dtype=
通过几个级别的调用进行跟踪,看起来
argrelmax
最终使用以下循环:
def _boolrelextrema(data, comparator...)
# comparator - a function
....
results = np.ones(data.shape, dtype=bool)
main = data.take(locs, axis=axis, mode=mode)
for shift in xrange(1, order + 1):
plus = data.take(locs + shift, axis=axis, mode=mode)
minus = data.take(locs - shift, axis=axis, mode=mode)
results &= comparator(main, plus)
results &= comparator(main, minus)
if(~results.any()):
return results
order : How many points on each side to use for the comparison
因此,如果顺序不是很大,则迭代量很小,并且不会对速度产生太大影响
simps
安装程序使用后
def _basic_simps(y,start,stop,x,dx,axis):
nd = len(y.shape)
if start is None:
start = 0
step = 2
all = (slice(None),)*nd
slice0 = tupleset(all, axis, slice(start, stop, step))
slice1 = tupleset(all, axis, slice(start+1, stop+1, step))
slice2 = tupleset(all, axis, slice(start+2, stop+2, step))
if x is None: # Even spaced Simpson's rule.
result = add.reduce(dx/3.0 * (y[slice0]+4*y[slice1]+y[slice2]),
axis)
else:
# Account for possibly different spacings.
...
return result
通过使用带有预定义切片集的add.reduce
,我想这是最快的了
因此,这些不是在C
中专门编码的,但是它们有效地利用了numpy
矢量化操作。我的猜测是,使用numpy
和/或cython
来加速它们将需要大量的工作,除非你关注一些特殊情况。我认为大多数scipy实际上是包装的Fortran库(而不是C)。argrelmax
是在中定义的<“代码>simps
是在中定义的。在您提供的链接中,实际上有指向源代码的链接。查看源代码,我很难确定它们是使用C还是Fortran,有什么想法吗。@WarrenWeckesser查看源代码,我很难弄清楚他们是使用C语言还是Fortran语言,有什么想法吗