[英]Element-wise Cross Product of 2D arrays of Coordinates
I'm working with a dataset that stores an array of unit-vectors as arrays of the vectors' components.我正在使用一个数据集,该数据集将单位向量数组存储为向量组件的 arrays。
How would I use vectorised code / broadcasting to write clean and compact code to give the cross product of the vectors element-wise?我将如何使用矢量化代码/广播来编写干净紧凑的代码以逐元素地给出向量的叉积?
For example, here's a brute force method for looping through the length of the arrays, picking out the coordinates, re-composing the two vectors, then calculating the cross product.例如,这里有一个蛮力方法,循环遍历 arrays 的长度,取出坐标,重新组合两个向量,然后计算叉积。
x = [0,0,1,1]
y = [0,1,0,1]
z = [1,0,0,1]
v1 = np.array([x,y,z])
x = [1,1,0,1]
y = [1,0,1,1]
z = [0,1,1,1]
v2 = np.array([x,y,z])
result = []
for i in range(0, len(x)):
a = [v1[0][i], v1[1][i], v1[2][i]]
b = [v2[0][i], v2[1][i], v2[2][i]]
result.append(np.cross(a,b))
result
>>>
[
array([-1, 1, 0]),
array([ 1, 0, -1]),
array([ 0, -1, 1]),
array([ 0, 0, 0])
]
I've tried to understand this question and answer to generalise it, but failed:我试图理解这个问题和答案来概括它,但失败了:
- Element wise cross product of vectors contained in 2 arrays with Python - 2 arrays 与 Python 中包含的向量的元素交叉乘积
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.