简体   繁体   English

Python:numba,构造函数如何将函数作为参数?

[英]Python: numba, how can constructor take a function as an argument?

I want to learn to use numba. 我想学习使用numba。 Unfortunatly I am finding it a bit difficult to learn numba from the documentation. 不幸的是,我发现从文档中学习numba有点困难。 So I have to try to ask you guys. 所以我必须试着问你们。 I want to pass a function f as an argument to a constructor. 我想将函数f作为参数传递给构造函数。 However I try, I get all sorts of errors. 但是我试试,我得到各种各样的错误。 What should I do? 我该怎么办?

Here's my code: 这是我的代码:

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)

Here are some of the error I am getting ( I am sorry that the indentation and line breaks are not preserved. I tried a few different things, but in all cases the formating is lost). 以下是我得到的一些错误(我很抱歉没有保留缩进和换行符。我尝试了一些不同的东西,但在所有情况下,格式化都会丢失)。 I am posting this because I was asked to in the comments. 我发帖是因为我在评论中被要求。 But the kind of errors I am getting varies with the exact implementation: 但是我得到的那种错误因确切的实现而异:

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 回溯(最近一次调用最后一次):从DE import *文件“/home/marius/dev/python/inf1100/DE.py “,第3行,在@autojit文件中”/home/marius/anaconda/lib/python2.7/site-packages/numba/decorators.py“,第183行,在autojit中nopython = nopython,locals = locals,** kwargs )(func)文件“/home/marius/anaconda/lib/python2.7/site-packages/numba/decorators.py”,第165行,在_autojit_decorator中numba_func = wrapper(f,compilerimpl,cache)文件“/ home / marius / anaconda / lib / python2.7 / site-packages / numba / exttypes / autojitclass.py“,第360行,在autojit_class_wrapper py_class = autojitmeta.create_unspecialized_cls(py_class,class_specializer)文件”/ home / marius / anaconda / lib / python2 .7 / site-packages / numba / exttypes / autojitmeta.py“,第22行,在create_unspecialized_cls类AutojitMeta(type(py_class)):TypeError:调用元类基类型'classobj'时的错误不是可接受的基类型

Sounds like you should be using a new-style class instead. 听起来你应该使用新式的类

@autojit
class name(object):

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM