A vector of unit norm is an element on the hypersphere. When two unit-norm vectors \(x\) and \(y\) in 3-dimensional space are given, this function computes a rotation matrix \(Q\) on the 2-dimensional sphere such that $$y=Qx$$.

rotationS2(x, y)

Arguments

x

a length-\(3\) vector. If \(\|x\|\neq 1\), normalization is applied.

y

a length-\(3\) vector. If \(\|y\|\neq 1\), normalization is applied.

Value

a \((3\times 3)\) rotation matrix.

Examples

# \donttest{
## generate two data points
#  one randomly and another on the north pole
x = stats::rnorm(3)
x = x/sqrt(sum(x^2))
y = c(0,0,1)

## compute the rotation
Q = rotationS2(x,y)

## compare 
Qx = as.vector(Q%*%x)

## print
printmat = rbind(Qx, y)
rownames(printmat) = c("rotated:", "target:")
print(printmat)
#>                  [,1]         [,2] [,3]
#> rotated: 1.314822e-16 5.979751e-17    1
#> target:  0.000000e+00 0.000000e+00    1
# }