algebra

Problem statement

In an n-dimensional space, given 2 vectors and with angle , how to compute the rotation matrix that rotates to along the 2d subspace generated by and ?

Idea

In high dimensional space, the rotation happens:

  • along the 2D plane generated by and
  • around the complemented (n-2)-dimensional subspace , which means this stays the same

Thus, the idea is

  1. Project onto
  2. Rotate in
  3. Project onto Q
  4. Sum the projections onto Q with the rotated vector in

Here are the steps:

  • In the 2D plane generated by and :
    • find 2 orthonormal vectors from and to represent that space
    • the projection in to this space is given by
    • map to and do the rotation there
      • and are 2 bases of a plane and is the mapping to that plane. ()
      • do the rotation with angle in that space () where
    • then map back to n-dimensional ()
  • In complemented (n - 2)-dimensional subspace:
    • Which is the complemented (n-2)-dimensional subspace of the 2D plane mentioned above
    • The projection on to this can be computed by subtracting the projection matrix onto the space generated by and from the identity matrix
  • The final rotation matrix is ^e160cf

Implementation

import numpy as np
 
# number of dimensions
n = 17
 
# 2 random vectors
x = np.random.rand(17)
y = np.random.rand(17)
 
# compute 2 orthonormal vectors to describe the plane
u = x / np.linalg.norm(x)
v = y - (y @ u) * u
v = v / np.linalg.norm(v)
 
# compute the rotation matrix (counter-clockwise)
cos_theta = x @ y / np.linalg.norm(x) / np.linalg.norm(y)
sin_theta = np.sqrt(1 -  cos_theta ** 2)
R_theta = [
	 [cos_theta, -sin_theta], 
	 [sin_theta, cos_theta]
]
 
# the final transformation
uv = np.column_stack([u, v])
u = u.reshape(-1, u.shape[-1])
v = v.reshape(-1, v.shape[-1])
R = np.eye(n) - (np.outer(u, u) + np.outer(v, v)) + uv @ R_theta @ uv.T

References