如何在简单的python函数中使用numba njit?

2024-04-20 05:59:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我创建了一个简单的函数,意思是取一个向量,将结果沿着几个矩阵进行乘以,其中k就是B和C的长度。不断发生的是,函数给出了错误:

Invalid use of Function(<built-in function dot>) with argument(s) of type(s):
(reflected list(reflected list(float64)), array(float64, 2d, C))
 * parameterized
In definition 0:
    All templates rejected with literals.
In definition 1:
    All templates rejected without literals.

...

def calc(A,k, B, C):
<source elided>
x2 = C[i]
c = np.dot(b,x1)
^

我确保将函数拆分为一种更易于遵循的方法:

@njit(fastmath = True)
def calc(A,k,B,C):
    b = A
    for i in range(k):
        x1 = B[i]
        x2 = C[i]
        c = np.dot(b,x1)
        D = c + x2
        c = np.tanh(D)
        b = c
    return b

有没有解决这个问题的好方法?如果是,正确的方法是什么?你知道吗


Tags: of方法函数inwithnpalldot