Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

generating two orthogonal vectors that are orthogonal to a particular direction

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.

like image 847
Physicist Avatar asked Nov 11 '15 19:11

Physicist


People also ask

How do you make two vectors orthogonal?

Two vectors x , y in R n are orthogonal or perpendicular if x · y = 0. Notation: x ⊥ y means x · y = 0. Since 0 · x = 0 for any vector x , the zero vector is orthogonal to every vector in R n .

Can two orthogonal vectors be linearly dependent?

Orthogonal vectors are linearly independent. A set of n orthogonal vectors in Rn automatically form a basis. Proof: The dot product of a linear relation a1v1 + ...

How do you show two vectors are orthonormal?

Answer: if length of vector product is same as scalar product the 2 vectors are orthonormal.


2 Answers

This will do:

>>> 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])

To obtain the 1st one:

>>> 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

To obtain the 2nd one:

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

and to verify:

>>> 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
like image 22
behzad.nouri Avatar answered Oct 05 '22 16:10

behzad.nouri


Sorry, I can't put it as a comment because of a lack of reputation.

Regarding @behzad.nouri's answer, note that if k is not a unit vector the code will not give an orthogonal vector anymore!

The correct and general way to do so is to subtract the longitudinal part of the random vector. The general formula for this is here

So you simply have to replace this in the original code:

>>> x -= x.dot(k) * k / np.linalg.norm(k)**2
like image 172
René Wirnata Avatar answered Oct 05 '22 16:10

René Wirnata