swh:1:snp:81eadaa089e8253d8469bcef66aa332632c6c669
Tip revision: c1688b374d201c13fb40b4dda2d2a89e34b94ec6 authored by Hans W. Borchers on 23 January 2021, 09:10:02 UTC
version 2.3.3
version 2.3.3
Tip revision: c1688b3
randortho.R
##
## r a n d o r t h o . R Generate Random Orthogonal Matrix
##
randortho <- function(n, type = c("orthonormal", "unitary")) {
stopifnot(is.numeric(n), length(n) == 1,
floor(n) == ceiling(n), n >= 1)
if (n == 1)
return(matrix(1, 1, 1))
type <- match.arg(type)
if (type == "orthonormal") {
z <- randn(n, n) / sqrt(2.0)
} else {
z <- (randn(n, n) + 1i * randn(n, n)) / sqrt(2.0)
}
# QR decomposition for real or complex matrices
Z <- qr(z)
q <- qr.Q(Z); r <- qr.R(Z)
d <- diag(r)
ph <- d/abs(d)
q %*% diag(ph) %*% q
}
# rortho <- function(n) {
# warning("Function 'rortho' is deprecated; use 'randortho' instead.",
# call. = FALSE, immediate. = TRUE)
# randortho(n, type = "orthonormal")
# }