Vector perpendicular to 3d vector?

Given a unit vector $$v_1 \in \mathbb{R}^3 : |v_1| = 1$$, let $$v_1 = (x_1,y_1,z_1)$$

What's the simplest way to find another unit vector $$v_2 = (x_2, y_2, z_2)$$ such that they are perpendicular (ie $$v_1 \cdotp v_2 = 0$$) ?

Notice the constraints are:

\begin{align} x_1x_2 + y_1y_2 + z_1z_2 &= 0,\\ {x_1}^2 + {y_1}^2 + {z_1}^2 &= 1,\\ {x_2}^2 + {y_2}^2 + {z_2}^2 &= 1. \end{align}

What is a solution for $$x_2$$, $$y_2$$, $$z_2$$ in terms of $$x_1$$, $$y_1$$, $$z_1$$ that is easy to express and calculate?

(Yes there is an infinite family of such vectors $$v_2$$ for a given $$v_1$$, I just want one, any one)

• "the constraints are $x_1^2+y_1^2+z_1^2=0$..." and these are real numbers? The only such real numbers satisfying this are for them all to be zero. Surely you mean to have some different constraints here Feb 23 at 2:23
• @JMoravitz: Sorry, typo corrected. Meant 1 not 0. Feb 23 at 2:24
• Now... as for finding a vector perpendicular to another... consider using the Gram-Schmidt process. Alternatively, take the cross product of your vector by an arbitrary other vector and normalize it after the fact. Feb 23 at 2:28
• @JMoravitz: That won't work if $v_1$ is collinear with the arbitrary vector, it will result in the zero vector. Feb 23 at 2:34
• "arbitrary" is not "random." When picking an arbitrary vector you can arbitrarily pick it such that it is not colinear. Feb 23 at 2:35

It looks like OP is looking for an algorithm to find a perpendicular vector.

Let $$\mathbf{p}_1 = (x, y, z)$$ be the original unit vector, $$x^2 + y^2 + z^2 = 1$$. I shall use notation $$\mathbf{p}_1 = (x, y, z) = \left[\begin{matrix}x\\y\\z\\ \end{matrix}\right]$$ where the parenthesised form $$(x, y, z)$$ is just shorthand for the proper vector/matrix form.

Construct two helper vectors by rotating $$\mathbf{p}$$ 90° around two different axes (the axes being perpendicular to each other), say around the $$z$$ axis and the $$y$$ axis, \begin{aligned} \mathbf{q}_1 &= (y ,\, -x ,\, z) \\ \mathbf{q}_2 &= (z ,\, y ,\, -x) \\ \end{aligned} and calculate their vector cross products wrt. the original vector: \begin{aligned} \mathbf{d}_1 = \mathbf{p} \times \mathbf{q}_1 &= ( y z + x z ,\, y z - x z ,\, - y^2 - x^2 ) \\ \mathbf{d}_2 = \mathbf{p} \times \mathbf{q}_2 &= ( - y z - x y ,\, z^2 + x^2 ,\, x y - y z ) \\ \end{aligned} One of these may be a zero vector (or very small), depending on how close the original $$\mathbf{p}$$ was to the respective rotation axis, but if nonzero, they are perpendicular to the original vector. So, pick the larger one in magnitude: $$\mathbf{p}_2 = \begin{cases} \displaystyle \frac{\mathbf{d}_1}{\left\lVert \mathbf{d}_1 \right\rVert}, & \left\lVert \mathbf{d}_1 \right\rVert \ge \left\lVert \mathbf{d}_2 \right\rVert \\ \displaystyle \frac{\mathbf{d}_2}{\left\lVert \mathbf{d}_2 \right\rVert}, & \left\lVert \mathbf{d}_1 \right\rVert \lt \left\lVert \mathbf{d}_2 \right\rVert \\ \end{cases}$$

• It would be nice if there was a solution without a branch, but it looks like there isn't one. I find this somehow suprising. Feb 24 at 3:11
• @AndrewTomazos: The fact that a branch/selection among candidates is needed here, has to do with avoiding singularities (and numerically, the unstable regions near singularities). In 2D, a single unit vector already defines the orientation, so there is nothing to choose, and no singularities. In 3D, we have three degrees of freedom, and any permutation (including partial negation) of the original vector leaves at least one singularity. For example, $(y, -z, x)$ is parallel to $(x, y, z)$ if $x = y z / (y + z)$; and $(z, -x, y)$ is parallel to $(x, y, z)$ if $x = y z / (y - z)$. Feb 24 at 7:32
• As to exactly why those singularities do exist, I do not know. Feb 24 at 7:32

Pick any vector $${\bf v}_2 \neq {\bf v}_1$$, then compute $${\bf v}_1 \times {\bf v}_2$$, and normalize the result.

(Oh... I just saw @JMoravitz stated this in a comment. Upvote his comment, not my "answer.")

• It seems strange but its actually difficult to pick a vector that is not collinear (or close to collinear) to $v_1$ in a generic and efficient fashion. I suppose I could find the maximum of $x_1, y_1, z_1$ and if x is the largest use $(0,1,1)$, if y is the largest use $(1,0,1)$ and if z is the largest use $(1,1,0)$ Feb 23 at 2:59
• Who needs "generic"? Try this: ${\bf v}_2 = {\bf v}_1 + (1,0,0)^t$ (so long as ${\bf v}_1 \neq (1,0,0)^t$) and be done with it. ANY vector other than ${\bf v}_1$ will work. What could possibly be more "efficient" than that?! What does "close to collinear" have to do with anything? Feb 23 at 3:16
• When two vectors are close to collinear it can cause numeric problems with the cross product and/or normalization. Feb 23 at 8:49