无法解析Numba键入错误



有一个过程涉及搜索(最终(大量参数组合,我正试图使用numba来加快搜索速度。我试图更改我的初始代码以使其兼容,但仍然收到一个我不知道如何解决的TypeingError。我在使用"np.array(("时遇到了很多问题,所以我删除了它,现在主要处理标准列表,但它仍然不起作用。我只会提到代码中与错误消息相关的部分,如果需要更多,我也会提供(只是为了保持简短(。

错误消息中看似关键的部分(据我所知,其余部分只是由于该函数在另一个函数中被调用,而在另一个中又被调用(写道:

在nopyson模式管道中失败(步骤:nopyson前端(未为签名找到函数function(<0x0000021490EA2400>处的函数点(的实现:

点(list(list(float64(<iv=无>(<iv=无>,反射列表(float64(<iv=无>(

有4种候选实现:-其中4个不匹配,原因是:函数'_OverloadWrapper中的重载_建筑ol_generated':文件:numba\core\overload_glue.py:第131行。带参数:'(list(list(float64(<iv=无>(<iv=无>,反射列表(float64(<iv=无>(':由于实施引发特定错误而被拒绝:键入错误:在nopyson模式管道中失败(步骤:nopyson前端(未找到用于签名的函数function((的实现:

>>> stub(list(list(float64)<iv=None>)<iv=None>, reflected list(float64)<iv=None>)

它似乎源于如下定义的函数"nvaxes_lab":

# reference position of NV axis vectors, each row holds the orientation (vector) of one NV axis    
nvaxes_ref = [[1.0, 1.0, 1.0],
[-1.0, 1.0, -1.0], [1.0, -1.0, -1.0], [-1.0, -1.0, 1.0]]
@njit()
def rot_euler(a, b, g):
"""
:param a: float, angle of third rotation (around z-axis) in radians
:param b: float, angle of second rotation (around x-axis) in radians
:param g: float, angle of first rotation (around z-axis) in radians
:return: rotation matrix for a general rotation around the Euler angles a (=alpha), b (=beta), g (=gamma) defined
as follows: rot_euler(a,b,g) = rotZ(a).rotX(b).rotZ(g)
"""
return [[np.cos(a) * np.cos(g) - np.cos(b) * np.sin(a) * np.sin(g),
-np.cos(b) * np.cos(g) * np.sin(a) - np.cos(a) * np.sin(g), np.sin(a) * np.sin(b)],
[np.cos(g) * np.sin(a) + np.cos(a) * np.cos(b) * np.sin(g),
np.cos(a) * np.cos(b) * np.cos(g) - np.sin(a) * np.sin(g), -np.cos(a) * np.sin(b)],
[np.sin(b) * np.sin(g), np.cos(g) * np.sin(b), np.cos(b)]]
@njit()    
def nvaxes_lab(a, b, g):
"""
:param a: float, angle of third Euler rotation (around z-axis) in radians
:param b: float, angle of second Euler rotation (around x-axis) in radians
:param g: float, angle of first Euler rotation (around z-axis) in radians
:return: numpy array of dimensions 3x4, each row holds the orientation (vector) of the corresponding NV-axis
after rotation by the Euler angles a,b,g
"""
return [np.dot(rot_euler(a, b, g), nvaxes_ref[0]).astype(float), np.dot(rot_euler(a, b, g), nvaxes_ref[1]).astype(float),
np.dot(rot_euler(a, b, g).astype(float), nvaxes_ref[2]), np.dot(rot_euler(a, b, g), nvaxes_ref[3]).astype(float)]

问题似乎是在"nvaxes_lab"中返回的数组的每个组件中的点积。。。不幸的是,我不知道如何定义数组(我尝试了多个版本(,也不知道我还能做些什么来消除这个错误,因为这是我第一次使用numba。

我将感谢你的帮助!

您有几个问题。首先,您看到的实际错误来自于numba编译函数时,它使用了numpy函数的C实现。这些实现需要numpy数组,而不是列表。所以错误:

点(list(list(float64(<iv=无>(<iv=无>,反射列表(float64(<iv=无>(

基本上是想告诉您np.dot函数不知道如何处理列表。在python版本中,它会将列表强制为numpy数组,但我猜在C/numba版本中不会。您可以将np.array封装在每个列表中,或者更好的选择是输出一个数组rot_euler

接下来,最好不要引用在您尝试JIT的函数范围之外定义的可变变量。在nvaxes_lab中,可以引用nvaxes_ref的每个元素。您最好将它们作为函数的参数传入,为了保持一致,还可以将它们作为一个数组。

最后,您的docstring与nvaxes_lab的返回类型不匹配。您可以将整个列表进行vstack,使其成为一个数组。

在将stuff转换为float方面,因为nvaxes_ref数组已经是一个float数组,所以所有的输出也将是float,所以您可以放弃astype方法。

import numpy as np
from numba import njit

nvaxes_ref = np.array([
[1.0, 1.0, 1.0],
[-1.0, 1.0, -1.0], 
[1.0, -1.0, -1.0], 
[-1.0, -1.0, 1.0]
])
@njit
def rot_euler(a, b, g):
"""
:param a: float, angle of third rotation (around z-axis) in radians
:param b: float, angle of second rotation (around x-axis) in radians
:param g: float, angle of first rotation (around z-axis) in radians
:return: rotation matrix for a general rotation around the Euler angles
a (=alpha), b (=beta), g (=gamma) defined as follows: 
rot_euler(a,b,g) = rotZ(a).rotX(b).rotZ(g)
"""
return np.array([
[
np.cos(a) * np.cos(g) - np.cos(b) * np.sin(a) * np.sin(g), 
-np.cos(b) * np.cos(g) * np.sin(a) - np.cos(a) * np.sin(g), 
np.sin(a) * np.sin(b)
],
[
np.cos(g) * np.sin(a) + np.cos(a) * np.cos(b) * np.sin(g),
np.cos(a) * np.cos(b) * np.cos(g) - np.sin(a) * np.sin(g), 
-np.cos(a) * np.sin(b)
],
[
np.sin(b) * np.sin(g), 
np.cos(g) * np.sin(b), 
np.cos(b)
]
])
@njit
def nvaxes_lab(a, b, g, refs):
"""
:param a: float, angle of third Euler rotation (around z-axis) in radians
:param b: float, angle of second Euler rotation (around x-axis) in radians
:param g: float, angle of first Euler rotation (around z-axis) in radians
:return: numpy array of dimensions 3x4, each row holds the orientation (vector) 
of the corresponding NV-axis after rotation by the Euler angles a,b,g
"""
return [
np.dot(rot_euler(a, b, g), refs[0]),
np.dot(rot_euler(a, b, g), refs[1]),
np.dot(rot_euler(a, b, g), refs[2]),
np.dot(rot_euler(a, b, g), refs[3])
]

最新更新