perpendicular_vector

odl.tomo.util.utility.perpendicular_vector(vec)[source]

Return a vector perpendicular to vec.

Parameters
vecarray-like

Vector(s) of arbitrary length. The axis along the vector components must come last.

Returns
perp_vecnumpy.ndarray

Array of same shape as vec such that dot(vec, perp_vec) == 0 (along the last axis if there are multiple vectors).

Examples

Works in 2d:

>>> perpendicular_vector([0, 1])
array([-1.,  0.])
>>> np.allclose(perpendicular_vector([1, 0]), [0, 1])  # would print -0
True

And in 3d:

>>> perpendicular_vector([0, 1, 0])
array([-1.,  0.,  0.])
>>> perpendicular_vector([0, 0, 1])
array([ 1.,  0.,  0.])
>>> np.allclose(perpendicular_vector([1, 0, 0]), [0, 1, 0])
True

The function is vectorized, i.e., it can be called with multiple vectors at once (additional axes being added to the left):

>>> perpendicular_vector([[0, 1, 0],
...                       [0, 0, 1]])  # 2 vectors
array([[-1.,  0.,  0.],
       [ 1.,  0.,  0.]])
>>> vecs = np.zeros((2, 3, 3))
>>> vecs[..., 1] = 1  # (2, 3) array of vectors (0, 1, 0)
>>> perpendicular_vector(vecs)
array([[[-1.,  0.,  0.],
        [-1.,  0.,  0.],
        [-1.,  0.,  0.]],

       [[-1.,  0.,  0.],
        [-1.,  0.,  0.],
        [-1.,  0.,  0.]]])