Rev 4902 | Rev 4994 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
#### Matrix Factorizations --- of all kindslibrary(Matrix)source(system.file("test-tools.R", package = "Matrix"))# identical3() etc### "sparseQR" : Check consistency of methods## --------data(KNex); mm <- KNex$mm; y <- KNex$ystopifnot(is((Y <- Matrix(y)), "dgeMatrix"))md <- as(mm, "matrix") # densesystem.time(mmq <- qr(mm))system.time(mdq <- qr(md))# much (~ 150 x) slower## qr.qy and qr.qty should be inversesstopifnot(all.equal(qr.qy (mmq, qr.qty(mmq, y))@x, y),all.equal(qr.qty(mmq, qr.qy (mmq, y))@x, y),all.equal(qr.qty(mmq, y), qr.qty(mmq, Y)) )## consistency of results dense and sparsestopifnot(is.all.equal3(qr.coef (mdq, y), qr.coef (mmq,y)@x, qr.coef (mmq,Y)@x) ,is.all.equal3(qr.resid (mdq, y), qr.resid (mmq,y)@x, qr.resid (mmq,Y)@x) ,is.all.equal3(qr.fitted(mdq, y), qr.fitted(mmq,y)@x, qr.fitted(mmq,Y)@x) )### "denseLU"## Testing expansions of factorizations {was ./expand.R, then in simple.R }set.seed(1)(m1 <- round(Matrix(rnorm(25), 5), 2))str(lu1 <- lu(m1))(luX <- expand(lu1))stopifnot(all.equal(as(m1, "matrix"),as(luX$P %*% (luX$L %*% luX$U), "matrix")))### "sparseLU"por1 <- readMM(system.file("external/pores_1.mtx", package = "Matrix"))lu1 <- lu(por1)pm <- as(por1, "CsparseMatrix")(pmLU <- lu(pm)) # -> show(<MatrixFactorization>)## identical only as long as we don't keep the original class info:stopifnot(identical(lu1, pmLU))## permute rows and columns of original matrixppm <- pm[pmLU@p + 1:1, pmLU@q + 1:1]Ppm <- pmLU@L %*% pmLU@U## these two should be the same, and `are' in some ways:assert.EQ.mat(ppm, as(Ppm, "matrix"), tol = 1e-14)## *however*length(ppm@x)# 180length(Ppm@x)# 317 !table(Ppm@x == 0)# (194, 123) - has 123 "zero" and 14 ``almost zero" entries## FIXME: expand(pmLU)## Cholesky()data(KNex)mtm <- with(KNex, crossprod(mm))c1 <- Cholesky(mtm)c2 <- Cholesky(mtm, super = TRUE)bv <- 1:nrow(mtm) # even integerb <- matrix(bv)## solve(c2, b) by default solves Ax = b, where A = c2'c2 !x <- solve(c2,b)stopifnot(identical3(x, solve(c2, bv), solve(c2, b, system = "A")),all.equal(x, solve(mtm, b)))for(sys in c("A", "LDLt", "LD", "DLt", "L", "Lt", "D", "P", "Pt")) {x <- solve(c2, b, system = sys)cat(sys,":\n"); print(head(x))stopifnot(dim(x) == c(712, 1),identical(x, solve(c2, bv, system = sys)))}## Schur() ----------------------checkSchur <- function(A, SchurA = Schur(A), tol = 1e-14) {stopifnot(is(SchurA, "Schur"),isOrthogonal(Q <- SchurA@Q),all.equal(as.mat(A),as.mat(Q %*% SchurA@T %*% t(Q)), tol = tol))}SH <- Schur(H5 <- Hilbert(5))checkSchur(H5, SH)checkSchur(Diagonal(x = 9:3))p <- 4LuTp <- new("dtpMatrix", x=c(2, 3, -1, 4:6, -2:1), Dim = c(p,p))(uT <- as(uTp, "dtrMatrix"))## Schur ( <general> ) <--> Schur( <triangular> )Su <- Schur(uT) ; checkSchur(uT, Su)gT <- as(uT,"generalMatrix")Sg <- Schur(gT) ; checkSchur(gT, Sg)Stg <- Schur(t(gT));checkSchur(t(gT), Stg)Stu <- Schur(t(uT));checkSchur(t(uT), Stu)stopifnot(identical3(Sg@T, uT, Su@T),identical(Sg@Q, as(diag(p), "dgeMatrix")),identical(Stg@T, as(t(gT[,p:1])[,p:1], "triangularMatrix")),identical(Stg@Q, as(diag(p)[,p:1], "dgeMatrix")),identical(Stu@T, Stg@T))assert.EQ.mat(Stu@Q, as(Stg@Q,"matrix"), tol=0)