In several previous blogs (here and here), I’ve written about the orthonormal Householder matrix and how it can constructed such that it contains a particular unit-vector. A recent posted a link to that was originally put together in 1999 by Tomas Akenine-Möller and John Hughes to find an orthogonal matrix which maps one unit-vector to another. This is a slightly more general problem which can be still solved in the same way using the Householder matrix. The vectors and matrices in this post are assumed to be real but the method can be modified to work with complex numbers.

In the previously linked posts, we described the method to find the Householder matrix given a unit-vector such that . Another way of wording this is: find the orthogonal matrix that contains as the first row. Now, we will generalise this to find an orthogonal matrix given a unit-vector such that where is another unit-vector. We start by defining to be the Householder reflector and attempt to solve for :

As the quantity is a scalar, the vector must be a scaled version of . Define and make the substitution into the previous equation:

Observe that has a singularity as approaches one. This can be addressed (as has been done in the previous posts) by negating the definition of the reflector (i.e. set ) and solving for again:

This time, will be defined as and the solution for becomes:

This shifts the singularity to . If the quantity is positive, this latter solution will be more numerically robust; if it is negative, the former will be more numerically robust. We will build an algorithm that performs the flip based on the sign of 云南快乐十分前三直选 which results in a discontinuity of the resulting matrix when the sign changes. This might be important to consider in some use-cases.

We can avoid computing the square root because always ends up being used squared. We define where is defined to be either or leading to being defined as either or respectively. We choose which quantity to use based on the sign of . The following C listing is not efficient but provides a reference in very little code for *arbitrary dimension vectors*.

/* Find the orthonormal symmetric reflection matrix which * maps the given unit-vector x to the given unit-vector y. * As the matrix is symmetric, it can be interpreted as * either row or column major. * * This isn't efficient. */ void make_reflector(const float *y, const float *x, unsigned N, float *mat) { unsigned i, j; float proj, sgn, beta; for (i = 0, proj = 0.0f; i < N; i++) proj += y[i] * x[i]; sgn = (proj >= 0.0f) ? 1.0 : -1.0; beta = 1.0f / (proj + sgn); for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { float w_i = x[i] + y[i] * sgn; float w_j = x[j] + y[j] * sgn; mat[i*N+j] = w_i * beta * w_j; } mat[i*N+i] -= sgn; } } |

Because the resulting matrix is symmetric only elements need to be computed. The w vector could also be precomputed to avoid excessive repeated computation in the inner loop. The completely unrolled and branch-less 3-dimension case can be written as:

void make_reflector(float x[3], float y[3], float mat[3][3]) { float proj = x[0]*y[0] + x[1]*y[1] + x[2]*y[2]; float sgn = copysignf(1.0f, proj); float beta = 1.0f / (proj + sgn); float w0 = x[0] + y[0] * sgn; float w1 = x[1] + y[1] * sgn; float w2 = x[2] + y[2] * sgn; float w0a = w0 * beta; float w1a = w1 * beta; float w2a = w2 * beta; mat[0][0] = w0a * w0 - sgn; mat[0][1] = w0a * w1; mat[0][2] = w0a * w2; mat[1][0] = mat[0][1]; mat[1][1] = w1a * w1 - sgn; mat[1][2] = w1a * w2; mat[2][0] = mat[0][2]; mat[2][1] = mat[1][2]; mat[2][2] = w2a * w2 - sgn; } |

This looks very similar to the listings used to reflect to 云南快乐十分前三直选. None of this is new. The Householder matrix has been used for a very long time to solve this exact problem in the context of finding orthogonal matrices for performing the QR decomposition (google “QR decomposition using reflectors” to find less-verbose descriptions of what I’ve written on this page).