library(stats, pos = "package:base", verbose = FALSE)
library(utils, pos = "package:base", verbose = FALSE)
showClass("denseLU")
set.seed(1)
n <- 3L
(A <- Matrix(round(rnorm(n * n), 2L), n, n))
## With dimnames, to see that they are propagated :
dimnames(A) <- dn <- list(paste0("r", seq_len(n)),
paste0("c", seq_len(n)))
(lu.A <- lu(A))
str(e.lu.A <- expand2(lu.A), max.level = 2L)
## Underlying LAPACK representation
(m.lu.A <- as(lu.A, "dgeMatrix")) # which is L and U interlaced
stopifnot(identical(as(m.lu.A, "matrix"), `dim<-`(lu.A@x, lu.A@Dim)))
ae1 <- function(a, b, ...) all.equal(as(a, "matrix"), as(b, "matrix"), ...)
ae2 <- function(a, b, ...) ae1(unname(a), unname(b), ...)
## A ~ P1' L U in floating point
stopifnot(exprs = {
identical(names(e.lu.A), c("P1.", "L", "U"))
identical(e.lu.A[["P1."]],
new( "pMatrix", Dim = c(n, n), Dimnames = c(dn[1L], list(NULL)),
margin = 1L, perm = invertPerm(asPerm(lu.A@perm))))
identical(e.lu.A[["L"]],
new("dtrMatrix", Dim = c(n, n), Dimnames = list(NULL, NULL),
uplo = "L", diag = "U", x = lu.A@x))
identical(e.lu.A[["U"]],
new("dtrMatrix", Dim = c(n, n), Dimnames = c(list(NULL), dn[2L]),
uplo = "U", diag = "N", x = lu.A@x))
ae1(A, with(e.lu.A, P1. %*% L %*% U))
ae2(A[asPerm(lu.A@perm), ], with(e.lu.A, L %*% U))
})
## Factorization handled as factorized matrix
b <- rnorm(n)
stopifnot(identical(det(A), det(lu.A)),
identical(solve(A, b), solve(lu.A, b)))
Run the code above in your browser using DataLab