Learn R Programming

mixAK (version 5.8)

rRotationMatrix: Random rotation matrix

Description

Generate a random rotation matrix, i.e., a matrix \(\boldsymbol{P} = (p_{i,j})_{i=1,\dots,p, j=1,\dots,p},\) which satisfies

a) \(\boldsymbol{P}\boldsymbol{P}' = \boldsymbol{I}\),

b) \(\boldsymbol{P}'\boldsymbol{P} = \boldsymbol{I}\),

c) \(\mbox{det}(\boldsymbol{P}) = 1\).

Usage

rRotationMatrix(n, dim)

Value

For n=1, a matrix is returned.

For n>1, a list of matrices is returned.

Arguments

n

number of matrices to generate.

dim

dimension of a generated matrix/matrices.

Author

Arnošt Komárek arnost.komarek@mff.cuni.cz

Details

For dim = 2, \(p_{2,1}\) (\(\sin(\theta)\)) is generated from Unif(0, 1) and the rest computed as follows: \(p_{1,1} = p_{2,2} = \sqrt{1 - p_{2,1}^2}\) (\(\cos(\theta)\)) and \(p_{1,2} = -p_{2,1}\) (\(-\sin(\theta)\)).

For dim \(>\) 2, the matrix \(\boldsymbol{P}\) is generated in the following steps:

1) Generate a \(p\times p\) matrix \(\boldsymbol{A}\) with independent Unif(0, 1) elements and check whether \(\boldsymbol{A}\) is of full rank \(p\).

2) Computes a QR decomposition of \(\boldsymbol{A}\), i.e., \(\boldsymbol{A} = \boldsymbol{Q}\boldsymbol{R}\) where \(\boldsymbol{Q}\) satisfies \(\boldsymbol{Q}\boldsymbol{Q}' = \boldsymbol{I}\), \(\boldsymbol{Q}'\boldsymbol{Q} = \boldsymbol{I}\), \(\mbox{det}(\boldsymbol{Q}) = (-1)^{p+1}\), and columns of \(\boldsymbol{Q}\) spans the linear space generated by the columns of \(\boldsymbol{A}\).

3) For odd dim, return matrix \(\boldsymbol{Q}\). For even dim, return corrected matrix \(\boldsymbol{Q}\) to satisfy the determinant condition.

References

Golub, G. H. and Van Loan, C. F. (1996, Sec. 5.1). Matrix Computations. Third Edition. Baltimore: The Johns Hopkins University Press.

Examples

Run this code
P <- rRotationMatrix(n=1, dim=5)
print(P)
round(P %*% t(P), 10)
round(t(P) %*% P, 10)
det(P)

n <- 10
P <- rRotationMatrix(n=n, dim=5)
for (i in 1:3){
  cat(paste("*** i=", i, "\n", sep=""))
  print(P[[i]])
  print(round(P[[i]] %*% t(P[[i]]), 10))
  print(round(t(P[[i]]) %*% P[[i]], 10))
  print(det(P[[i]]))
}

Run the code above in your browser using DataLab