rRotationMatrix function

Random rotation matrix

Random rotation matrix

Generate a random rotation matrix, i.e., a matrix c("%", "\n\n", "P=(p[i,j]),i=1,ldots,p,j=1,ldots,p, P = (p[i,j]), i=1,\\ldots,p, j=1,\\ldots,p,")

which satisfies

a) PP=IP * P' = I,

b) PP=IP' * P = I,

c) det(P)=1det(P) = 1.

Details

For dim = 2, p[2,1]p[2,1]

(sin(theta)sin(theta)) is generated from Unif(0, 1) and the rest computed as follows: c("%", "\n\n", "p[1,1]=p[2,2]=sqrt(1p[2,1]2) p[1,1] = p[2,2] = sqrt(1 - p[2,1]^2)")

(cos(theta)cos(theta)) and p[1,2]=p[2,1]p[1,2] = p[2,1]

(sin(theta)-sin(theta)).

For dim >> 2, the matrix PP is generated in the following steps:

  1. Generate a pxpp x p matrix AA with independent Unif(0, 1) elements and check whether AA

is of full rank pp.

  1. Computes a QR decomposition of AA, i.e., A=QRA = QR where QQ satisfies QQ=IQ * Q' = I, QQ=IQ' * Q = I, det(Q)=(1)p+1det(Q) = (-1)^{p+1}, and columns of QQ spans the linear space generated by the columns of AA.

  2. For odd dim, return matrix QQ. For even dim, return corrected matrix QQ to satisfy the determinant condition.

rRotationMatrix(n, dim)

Arguments

  • n: number of matrices to generate.
  • dim: dimension of a generated matrix/matrices.

Returns

For n=1, a matrix is returned.

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

References

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

Author(s)

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

Examples

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]])) }