D. <- diag(x = c(1, 1, 2, 3, 5, 8))
D.0 <- Diagonal(x = c(0, 0, 0, 3, 5, 8))
S. <- toeplitz(as.double(1:6))
C. <- new("dgCMatrix", Dim = c(3L, 4L),
p = c(0L, 1L, 1L, 1L, 3L), i = c(1L, 0L, 2L), x = c(-8, 2, 3))
stopifnot(identical(.M2tri( D.), as(D., "triangularMatrix")),
identical(.M2sym( D.), as(D., "symmetricMatrix")),
identical(.M2diag(D.), as(D., "diagonalMatrix")),
identical(.sparse2kind(C., "l"),
as(C., "lMatrix")),
identical(.dense2kind(.sparse2dense(C.), "l"),
as(as(C., "denseMatrix"), "lMatrix")),
identical(.diag2sparse(D.0, "ntC"),
.dense2sparse(.diag2dense(D.0, "ntp"), "C")),
identical(.dense2g(.diag2dense(D.0, "dsy")),
.sparse2dense(.sparse2g(.diag2sparse(D.0, "dsT")))),
identical(S.,
.sparse2m(.m2sparse(S., ".sR"))),
identical(S. * lower.tri(S.) + diag(1, 6L),
.dense2m(.m2dense(S., ".tr", "L", "U"))),
identical(.CR2RC(C.), .T2CR(.CR2T(C.), FALSE)),
identical(.tCR2RC(C.), .CR2RC(t(C.))))
A <- tcrossprod(C.)/6 + Diagonal(3, 1/3); A[1,2] <- 3; A
stopifnot(exprs = {
is.numeric( x. <- c(2.2, 0, -1.2) )
all.equal(.solve.dgC.lu(A, c(1,0,0), check=FALSE),
Matrix(x.))
all.equal(x., .solve.dgC.qr(A, c(1,0,0), check=FALSE))
})
## Solving sparse least squares:
X <- rbind(A, Diagonal(3)) # design matrix X (for L.S.)
Xt <- t(X) # *transposed* X (for L.S.)
(y <- drop(crossprod(Xt, 1:3)) + c(-1,1)/1000) # small rand.err.
str(solveCh <- .solve.dgC.chol(Xt, y, check=FALSE)) # Xt *is* dgC..
stopifnot(exprs = {
all.equal(solveCh$coef, 1:3, tol = 1e-3)# rel.err ~ 1e-4
all.equal(solveCh$coef, drop(solve(tcrossprod(Xt), Xt %*% y)))
all.equal(solveCh$coef, .solve.dgC.qr(X, y, check=FALSE))
})
Run the code above in your browser using DataLab