我有两组各包含2000个3D向量,需要计算每个可能的向量对之间的叉积。我当前的处理方法如下:
for tx in tangents_x:
for ty in tangents_y:
cross = np.cross(tx, ty)
(... do something with the cross variable...)
这个方法可以工作,但速度比较慢。有没有办法让它更快?
如果我想要逐元素相乘,我可以使用以下方法:
# Define initial vectors
tx = np.array([np.random.randn(3) for i in range(2000)])
ty = np.array([np.random.randn(3) for i in range(2000)])
# Store them into matrices
X = np.array([tx for i in range(2000)])
Y = np.array([ty for i in range(2000)]).T
# Compute the element-wise product
ew = X * Y
# Use the element_wise product as usual
for i,tx in enumerate(tangents_x):
for j,ty in enumerate(tangents_y):
(... use the element wise product of tx and ty as ew[i,j])
我该如何将这个应用到向量的叉积上而不是逐元素相乘?或者,您有其他替代方案吗?
非常感谢 :)
np.cross
实现的叉积
吗?还是np.outer
或np.dot
的某个版本? - hpauljnp.cross(tangents_x[:,None,:], tangents_y)
? - Paul Panzernp.cross
完全支持广播... - Brenlla