pythonvectorscipy

Scipy: calculate orthogonal vector


I'm trying to use Scipy for orthogonal vector calculation:

import numpy as np
from scipy import linalg

e1 = np.float16([-0.913,  -0.4072]).reshape(2,1)

e2 = linalg.orth(e1)

print(f'e_1 {e1} ,'
      f' ortogonal e2 is {e2}')

I expected output to be:

   e2 is [[-0.4072] [0.913]]

I checked it, by: 0.913 * -0.4072 + (-0.4072)*0.913 = 0

But receive:

   e2 is [[-0.913] [-0.4072 ]]

What am I doing wrong?


Solution

  • Read the documentation. The function you're calling doesn't compute an orthogonal vector to a given vector, but an orthonormal basis of a given set of vectors. You're giving it one normalised vector, so you're just getting the same vector back.

    If it's just in a plane from a single non-zero vector, you don't need scipy since (b, -a) is always orthogonal to (a, b). Otherwise there might be one solution, no solution or multiple solutions. The function linalg.null_space(e1.T) gives you the orthonormal basis of all possible "orthogonal vectors" here, i.e. null space. Note the transpose.