R/rsop.R

Defines functions rsop

Documented in rsop

################################
#### Simulation of random rotation matrices
#### Matrices in SO(p)
################################
rsop <- function(n, p) {

  a <- c(1, numeric(p - 1) )
  A <- array( dim = c(p, p, n) )
  Ip <- diag(p)

  for ( i in 1:n ) {
    b <- rnorm(p)
    b <- b / sqrt( sum(b^2) )
    ca <- a - b * b[1]
    ca <- ca / sqrt( sum(ca^2) )
    B <- tcrossprod(b, ca)
    B <- B - t(B)
    theta <- acos( b[1] )
    A[, , i] <- Ip + sin(theta) * B + ( cos(theta) - 1 ) * ( tcrossprod(b) + tcrossprod(ca) )
  }

  if (n == 1)   A <- as.matrix( A[, , 1] )
  A
}

Try the Directional package in your browser

Any scripts or data that you put into this service are public.

Directional documentation built on Oct. 12, 2023, 1:07 a.m.