2015-01-02 40 views
0

有誰知道scipy.signal.argrelmax和scipy.integrate.simps是否在源代碼中使用C代碼,還是純Python?我想加快使用Numba,這樣問。如何知道scipy函數是否使用C代碼?

http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.signal.argrelmax.html http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.integrate.simps.html

+0

我認爲大多數scipy實際上是包裝* Fortran *庫(不是C)。 – DaoWen

+2

'argrelmax'在https://github.com/scipy/scipy/blob/master/scipy/signal/_peak_finding.py中定義;在https://github.com/scipy/scipy/blob/master/scipy/integrate/quadrature.py –

+1

中定義了'simps'。在你提供的鏈接中,實際上有源代碼*的鏈接。 – jonrsharpe

回答

2

通過幾個電話級別的跟蹤,它看起來像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 

所以,如果order不是非常大,迭代量很小,不應該影響速度太快。

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的速度將會花費很多工作量 - 除非您專注於一些特殊情況。

相關問題