11

What is the simplest and most efficient ways in numpy to generate two orthonormal vectors a and b such that the cross product of the two vectors equals another unit vector k, which is already known?

I know there are infinitely many such pairs, and it doesn't matter to me which pairs I get as long as the conditions axb=k and a.b=0 are satisfied.

4

3 回答 3

15

这将做:

>>> k  # an arbitrary unit vector k is not array. k is must be numpy class. np.array
np.array([ 0.59500984,  0.09655469, -0.79789754])

要获得第一个:

>>> x = np.random.randn(3)  # take a random vector
>>> x -= x.dot(k) * k       # make it orthogonal to k
>>> x /= np.linalg.norm(x)  # normalize it

要获得第二个:

>>> y = np.cross(k, x)      # cross product with k

并验证:

>>> np.linalg.norm(x), np.linalg.norm(y)
(1.0, 1.0)
>>> np.cross(x, y)          # same as k
array([ 0.59500984,  0.09655469, -0.79789754])
>>> np.dot(x, y)            # and they are orthogonal
-1.3877787807814457e-17
>>> np.dot(x, k)
-1.1102230246251565e-16
>>> np.dot(y, k)
0.0
于 2015-11-11T19:58:10.377 回答
10

抱歉,由于缺乏声誉,我无法将其作为评论。

关于@behzad.nouri 的回答,请注意,如果k不是单位向量,则代码将不再给出正交向量!

这样做的正确和通用方法是减去随机向量的纵向部分。通用公式在 这里

因此,您只需在原始代码中替换它:

>>> x -= x.dot(k) * k / np.linalg.norm(k)**2
于 2017-06-02T08:32:34.140 回答
1

假设支持正交基的向量是u。

b1 = np.cross(u, [1, 0, 0])   # [1, 0, 0] can be replaced by other vectors, just get a vector orthogonal to u
b2 = np.cross(u, b1)
b1, b2 = b1 / np.linalg.norm(b1), b2 / np.linalg.norm(b2)

如果您愿意,可以使用更短的答案。

获取变换矩阵

B = np.array([b1, b2])
TransB = np.dot(B.T, B)
u2b = TransB.dot(u) # should be like [0, 0, 0]
于 2018-05-27T01:32:47.413 回答