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.


  • The Gram-Schmidt procedure will do exactly this. For example:

    >>> 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 -= * 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])
    >>>, y)            # and they are orthogonal
    >>>, k)
    >>>, k)