How to explicitly represent a basis for the subspace perpendicular to $v ∈ ℝ^3$ I'm looking for an analytic way to explicitly represent two vectors which form a basis
for the space in $\mathbb{R}^{3}$ perpendicular to some vector $v$.
For example, my first thought was to do this:
\begin{align}
u &= v\times u', &
w &= v\times u.
\end{align}
The idea is that $u$ will be perpendicular to $v$ (and $u'$, but that choice is arbitrary and doesn't matter) and that $w$ will be perpendicular to $v$ and $u$.
The fact that both $u$ and $w$ are perpendicular to $v$ puts them in the correct subspace.
The fact that $u$ and $w$ are perpendicular to each other means they form an orthogonal basis.
The problem, of course, is that this fails when $v$ is parallel to $u'$. I'm looking for a fully general way to express $u$ and $w$.
There are many algorithms for doing this, but those are not useful to me at the moment. I'm looking for an explicit representation of the form
\begin{align}
u &= f(v), &
w &= g(v,u).
\end{align}
Any ideas?
 A: What you’re really after is a method of producing a vector $u = f(v)$ orthogonal to $v$ which always works and which doesn’t involve any sort of ‘choice’.
With that, getting the last vector is as easy as $w = f(v) × u$.
You could take
$$
f(v) = \begin{cases}
v × (1,0,0) & \text{if $v ≠ (1,0,0)$} \\
(0,1,0) & \text{otherwise}
\end{cases}
$$
but you’re probably wanting to exclude this kind of solution on the basis of the choice of $(1,0,0)$ as special.
But suppose we have a magic formula $f : ℝ^3 → ℝ^3$ which produces an orthogonal vector $f(v) \perp v$ as advertised.
There are infinitely many possible answers, arranged with rotational symmetry in the plane $v^\perp$.
The formula must have a way of picking out one of those vectors as distinct from the rest, so the formula itself cannot be symmetric — it must involve some choice of vector.
So you can’t get away with not making a choice.
But if we take $v × u'$ for an initial guess $u'$, the formula will fail for $u' \parallel v$.
As a way of “disguising” the choice, you could generate two vectors, one of which may be zero, and then choosing the one that isn’t. For example,
$$
f(v) ∈ \{v × (1,0,0), v × (0,1,0)\}
.$$
Both of these are $\perp v$, and they can’t both vanish for the same $v ≠ 0$.

Arguably, the better way to handle the geometry of vectors and planes is to use a language with less redundancy:
Instead of representing $v^\perp$ with an orthogonal basis, you can represent it as a bivector.
For example, if $v = v_x ̂ + v_y ̂ + v_z ̂$, then the normal plane is described by the bivector
$$
v_x ̂ ∧ ̂ + v_y ̂ ∧ ̂ + v_z ̂ ∧ ̂
.$$
If this kind of ‘multilinear algebra’ interests you, I recommend reading about multivectors and geometric algebra.
A: If we approach the problem, finding "a basis for the space in $\mathbb R^3$ perpendicular to some vector" $\vec v$, using an orthogonal transformation, then numerical stability will be better than Gram-Schmidt or the cross-product (which only exists in $\mathbb R^3$).
An orthogonal transformation preserves the angles between and lengths of vectors.  This helps with numerical stability, because inevitable rounding errors will not grow geometrically under an orthogonal transformation.
Especially convenient for your purpose is a Householder reflection $Q$ which is both orthogonal ($Q^T = Q^{-1}$) and symmetric ($Q^T = Q$). Hence such $Q$ is its own inverse.  The approach works for Euclidean space $\mathbb R^n$ quite generally.
Notice that if column vector $\vec v$ is nonzero, then the vectors perpendicular to it are the same as those perpendicular to the unit length vector $\vec v/||\vec v||$.  So we consider without loss of generality only the case $||\vec v|| = 1$.
If $\vec v$ were a standard basis vector, e.g. $\mathbf e_1 = (1\;0\;\ldots\;0)$, then we would know immediately a basis for those vectors perpendicular to $\vec v$ is formed by all the other standard basis vectors, $\mathbf e_2, \mathbf e_3, \ldots, \mathbf e_n$. So let's assume $\vec v \neq\mathbf e_1$.
The idea is to construct a Householder reflection $Q$ such that $Q\vec v = \mathbf e_1$.  It follows that $Q\mathbf e_1 = \vec v$, and that a basis for the subspace of $\mathbb R^n$ perpendicular to $\vec v$ will be given by $Q\mathbf e_2,Q\mathbf e_3,\ldots,Q\mathbf e_n$.  In other words $\vec v$ is the first column of $Q$, and the remaining columns of $Q$ form a basis for the orthogonal complement.
The formula for $Q$ is $I - 2\vec x\,\vec x^T$ where:
$$ \vec x = \frac{\mathbf e_1 - \vec v}{||\mathbf e_1 - \vec v||} $$
It is routine to show that Householder reflection $Q$ is orthogonal and symmetric (exercise left for the Reader?), so the essential point we need to detail is that the first column of $Q$ is $\vec v$.
Writing column vector $\vec v$ by its components:
$$ \vec v = (v_1 \; v_2 \; \ldots \; v_n)^T $$
permits the following substitution and simplification:
$$ \begin{align*} Q\mathbf e_1 &= (I - 2\vec x\,\vec x^T)\mathbf e_1 \\
&= \left( I - \frac{2(\mathbf e_1 - \vec v)(\mathbf e_1 - \vec v)^T}{||\mathbf e_1 - \vec v||^2} \right) \mathbf e_1 \\
&= \mathbf e_1 - \frac{2(\mathbf e_1 - \vec v)(1 - v_1)}{||\mathbf e_1 - \vec v||^2} \\
&= \mathbf e_1 - \frac{2(1 - v_1)}{(1-v_1)^2 + \sum_{i=2}^n v_i^2} (\mathbf e_1 - \vec v) \\
&= \mathbf e_1 - \frac{2(1 - v_1)}{1-2v_1 + \sum_{i=1}^n v_i^2} (\mathbf e_1 - \vec v) \\
&= \mathbf e_1 - \frac{2 - 2v_1}{2 - 2v_1} (\mathbf e_1 - \vec v) \\
&= \mathbf e_1 - (\mathbf e_1 - \vec v) \\
&= \vec v \end{align*} $$
The diligent Reader will spot where the assumption $\vec v \neq \mathbf e_1$ allows us to avoid (multiplying and) dividing by zero.  In fact the computation should be numerically accurate unless $\vec v$ is so close to $\mathbf e_1$ as to cause a subtractive cancellation problem with $(\mathbf e_1 - \vec v)$.  If this were to happen, it can be fixed by replacing $\vec v$ with $-\vec v$. The same vectors perpendicular to $\vec v$ are perpendicular to $-\vec v$, and $(\mathbf e_1 + \vec v)$ would then avoid subtractive cancellation.
