我想學習使用numba。不幸的是,我發現從文檔中學習numba有點困難。所以我必須試着問你們。我想將一個函數f
作爲參數傳遞給構造函數。不過,我嘗試,我得到各種錯誤。我該怎麼辦?Python:numba,構造函數如何將函數作爲參數?
這裏是我的代碼:
def f(x):
# return some mathematical expression
f_numba = jit(double(double))(f)
@autojit
class name:
def __init__(self, f)
self.f = f
@double(double)
def __call__(self, x)
return self.f(x)
funct = name(f_numba)
a = funct(5)
這裏有一些我正在錯誤的(我很抱歉的是,縮進和換行不保留我嘗試了一些不同的東西,但在所有情況下,格式化丟失)。我張貼這個,因爲我被要求在評論中。但那種我得到的錯誤與具體實現有所不同:
Traceback (most recent call last): File "/home/marius/dev/python/inf1100/test_ODE.py", line 7, in from DE import * File "/home/marius/dev/python/inf1100/DE.py", line 3, in @autojit File "/home/marius/anaconda/lib/python2.7/site-packages/numba/decorators.py", line 183, in autojit nopython=nopython, locals=locals, **kwargs)(func) File "/home/marius/anaconda/lib/python2.7/site-packages/numba/decorators.py", line 165, in _autojit_decorator numba_func = wrapper(f, compilerimpl, cache) File "/home/marius/anaconda/lib/python2.7/site-packages/numba/exttypes/autojitclass.py", line 360, in autojit_class_wrapper py_class = autojitmeta.create_unspecialized_cls(py_class, class_specializer) File "/home/marius/anaconda/lib/python2.7/site-packages/numba/exttypes/autojitmeta.py", line 22, in create_unspecialized_cls class AutojitMeta(type(py_class)): TypeError: Error when calling the metaclass bases type 'classobj' is not an acceptable base type
「我得到各種各樣的錯誤」不是很具體。一般來說,如果您遇到錯誤,最好在此處發佈,以便我們知道我們正在處理的是什麼。請參閱[SSCCE.org](http://sscce.org/)瞭解如何提出重要問題的精彩教程。 – mgilson
你能發佈實際的錯誤嗎? – Dan