我有一个numpy一维数组c
,它应该填充
a + b
。我首先使用PyOpenCL
在设备上执行a + b
。
我想使用numpy
切片快速确定python中结果数组c
的正确性。
这就是我现在拥有的
def python_kernel(a, b, c):
temp = a + b
if temp[:] != c[:]:
print "Error"
else:
print "Success!"
但我得到了错误:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
但似乎a.any
或a.all
将决定这些值是否不是0。
如果我想测试numpy
数组temp
中的所有标量是否都等于numpy
数组c
中的每个值,该怎么办?
如果
np.array
数据类型是floats,那么np.allclose是一个不错的选择。np.array_equal
并不总是正常工作。例如:结果:
您可以根据比较的结果调用
any
:if np.any(a+b != c):
或等效的if np.all(a+b == c):
。a+b != c
创建一个由dtype=bool
组成的数组,然后any
查看该数组中是否有成员是True
。尽管如此,尽管如此,Amber's solution可能更快,因为它不必创建整个布尔结果数组。
为什么不直接使用NumPy函数中的^{}[docs] ?
相关问题 更多 >
编程相关推荐