algebra

Problem statement

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 :
    • project to the 2D plane generated by and
      • find 2 orthonormal vectors from and to represent that space then and are 2 bases of a plane and is the projection matrix to that plane. ()
    • do the rotation with angle in that space () where
      • then projected back to n-dimensional ()
    • The projection in to this space is given by
  • 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

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
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) - (u @ u.T + v @ v.T) + uv @ R_theta @ uv.T

References