Given a hyperplane $\{x\in\mathbb R^n | a^T x=0\}$ where $a\in\mathbb R^n$, and I want to find some orthogonal basis to this hyperplane.

I found many solutions for special cases, but non of which considers the general case.

Thanks in advance!


Adapted from the answer under the link in the comment by jibounet, but without the messy formulae.

Without loss of generality assume $\|a\|=1$ (in other words replace $a$ by its normalisation, if necessary). Find one of the standard basis vectors, or its negative, that is different from $a$, say $e=\pm e_k\neq a$, which is clearly always possible (you can even make a choice such that $\|a-e\|>1$, for the sake of numeric stability in what follows). Then $v=a-e$ is a nonzero vector, and the orthogonal reflection $R$ with respect to the hyperplane perpendicular to $v$ will send $e_k\mapsto\pm a$. This means column $k$ of the matrix of $R$ equals$~\pm a$, and the remaining columns give you a basis of your hyperplane (remember, the columns of any orthogonal matrix, like that of a reflection, form an orthonormal basis of the space)

Concretely, the matrix of $R$ is given by $I-\frac2{\|v\|^2}(v\cdot v^T)$.