Givens rotation from canonical axis to an arbitrary direction
Source:R/cone_sampling.R
rotate_from_canonical.RdRotates a vector \(\hat{x}\) aligned with the \(n\)-th canonical basis vector \(\hat{e}_n\) so that its image is aligned with an arbitrary unit direction \(\hat{\mu}\), in \(O(n)\) operations using a Givens rotation in the plane spanned by \(\hat{e}_n\) and \(\hat{\mu}\).
Details
This function performs a Givens rotation in the plane containing \(\hat{e}_n\) and \(\hat{\mu}\). The rotation is: $$\hat{y} = \hat{x} + P(G - I_2)P^T\hat{x}$$ where P is an orthonormal basis for the plane of rotation, G is the 2D Givens rotation matrix, and \(I_2\) is the 2×2 identity matrix.
The matrices are defined as: $$P = [\hat{e}_n, \frac{\hat{\mu} - \mu_n\hat{e}_n} {\|\hat{\mu} - \mu_n\hat{e}_n\|}]$$ $$G = \begin{bmatrix} \mu_n & -\sqrt{1-\mu_n^2} \\ \sqrt{1-\mu_n^2} & \mu_n \end{bmatrix}$$ where \(\mu_n = \hat{e}_n^T\hat{\mu}\).
This rotation costs only O(n) operations, unlike general n-dimensional rotations which cost O(n²).
References
Arun, I., & Venkatapathi, M. (2025). An O(n) algorithm for generating uniform random vectors in n-dimensional cones. Sankhya A, 87(2), 327-348. doi:10.1007/s13171-025-00387-9
See also
generate_cone_sample, generate_cone_samples
for the cone samplers that compose this rotation with a sampled
canonical-axis vector; generate_point_on_sphere for the
uniform-sphere primitive.