fastMisc {Matrix} | R Documentation |
“Semi-API” functions used internally by Matrix,
often to bypass S4 dispatch and avoid the associated overhead.
These are exported to provide this capability to expert users.
Typical users should continue to rely on S4 generic functions
to dispatch suitable methods, by calling,
e.g., as(., <class>)
for coercions.
.M2tri(from, ...)
.M2sym(from, ...)
.M2diag(from)
.m2dense(from, code, uplo = "U", diag = "N")
.m2sparse(from, code, uplo = "U", diag = "N")
.dense2m(from)
.sparse2m(from)
.dense2v(from)
.sparse2v(from)
.dense2kind(from, kind)
.sparse2kind(from, kind, drop0 = FALSE)
.dense2g(from, kind = ".")
.sparse2g(from)
.dense2sparse(from, repr = "C")
.sparse2dense(from, packed = FALSE)
.diag2dense(from, code, uplo = "U")
.diag2sparse(from, code, uplo = "U", drop0 = TRUE)
.CR2T(from)
.T2CR(from, Csparse = TRUE)
.CR2RC(from)
.tCR2RC(from)
.diag.dsC(x, Chx = Cholesky(x, LDL = TRUE), res.kind = "diag")
.solve.dgC.chol(a, b, check = TRUE)
.solve.dgC.lu (a, b, tol = .Machine$double.eps, check = TRUE)
.solve.dgC.qr (a, b, order = 3L, check = TRUE)
from |
a |
code |
a string whose first three characters specify the class of
the result. It should match the pattern
|
kind |
a string ( |
uplo |
a string ( |
diag |
a string ( |
drop0 |
a logical. If |
repr |
a string ( |
packed |
a logical. If |
Csparse |
a logical. If |
... |
optional arguments passed to |
x |
a numeric sparse column-compressed |
Chx |
optionally the |
res.kind |
a string, one of |
a |
a numeric symmetric sparse column-compressed
|
b |
a vector or matrix, the “right hand side” |
check |
a logical indicating if the first argument possibly first needs to be coerced to a dgCMatrix; should be set to false for speedup only if it is known to be already of the correct class. |
tol |
non-negative number, the tolerance for singularity checking in the LU decomposition. |
order |
only used for |
Functions with names of the form .<A>2<B>
implement coercions
from virtual class A to the “nearest” non-virtual subclass of
virtual class B, where the virtual classes are abbreviated as follows:
Matrix
, matrix, or vector
matrix
vector
Abbreviations should be seen as guides, rather than as an exact
description of behaviour. For example, .m2dense
and
.m2sparse
accept vectors in addition to matrices.
.CR2T
and .T2CR
coerce between
TsparseMatrix
and the union of
CsparseMatrix
and
RsparseMatrix
.
.CR2RC
and .tCR2RC
coerce between
CsparseMatrix
and
RsparseMatrix
. Conceptually, the latter
performs the coercion on the transpose of its argument.
That is, .tCR2RC(from)
is equivalent to but much more
efficient than .CR2RC(t(from))
and t(.CR2RC(from))
.
.M2tri
, .M2sym
, and .M2diag
can be seen as
drop-in replacements for as(., "*Matrix")
, but allowing users
to pass optional arguments to the structure-checking functions.
.diag.dsC(x)
computes (or uses if Chx
is
specified) the LDL'
Cholesky decomposition of x
, returning
diverse diagonal / determinant related statistics, for different result kinds,
see res.kind
in ‘Arguments’ above.
.solve.dgC.*()
Note that .solve.dgC.lu(a, ..)
needs a square matrix
a
and it and .solve.dgC.qr(a, ..)
solve sparse n \times n
matrix systems directly.
.solve.dgC.qr()
and .solve.dgC.chol()
may both be used to solve
sparse least squares problems.
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))
})