1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
## METHODS FOR CLASS: sparseMatrix, [CRT]sparseMatrix (virtual)
## sparse matrices, in some cases restricted to CSC, CSR, triplet
## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.sparse.band <- function(x, k1, k2, ...)
.Call(R_sparse_band, x, k1, k2)
.sparse.triu <- function(x, k = 0L, ...)
.Call(R_sparse_band, x, k, NULL)
.sparse.tril <- function(x, k = 0L, ...)
.Call(R_sparse_band, x, NULL, k)
.sparse.diag.get <- function(x = 1, nrow, ncol, names = TRUE)
.Call(R_sparse_diag_get, x, names)
.sparse.diag.set <- function(x, value)
.Call(R_sparse_diag_set, x, value)
.sparse.t <- function(x)
.Call(R_sparse_transpose, x, FALSE)
.sparse.fS1 <- function(x, uplo)
.Call(R_sparse_force_symmetric, x, NULL)
.sparse.fS2 <- function(x, uplo)
.Call(R_sparse_force_symmetric, x, uplo)
.sparse.symmpart <- function(x)
.Call(R_sparse_symmpart, x)
.sparse.skewpart <- function(x)
.Call(R_sparse_skewpart, x)
.sparse.is.di <- function(object)
.Call(R_sparse_is_diagonal, object)
.sparse.is.tr <- function(object, upper = NA, ...)
.Call(R_sparse_is_triangular, object, upper)
.sparse.is.sy <- function(object, checkDN = TRUE, ...) {
if(checkDN) {
ca <- function(check.attributes = TRUE, ...) check.attributes
checkDN <- ca(...)
}
.Call(R_sparse_is_symmetric, object, checkDN)
}
.sparse.is.sy.dz <- function(object, checkDN = TRUE,
tol = 100 * .Machine$double.eps, ...) {
## backwards compatibility: don't check DN if check.attributes=FALSE
if(checkDN) {
ca <- function(check.attributes = TRUE, ...) check.attributes
checkDN <- ca(...)
}
## be very fast when requiring exact symmetry
if(tol <= 0)
return(.Call(R_sparse_is_symmetric, object, checkDN))
## pretest: is it square?
d <- object@Dim
if((n <- d[2L]) != d[1L])
return(FALSE)
## pretest: are DN symmetric in the sense of validObject(<symmetricMatrix>)?
if(checkDN && !isSymmetricDN(object@Dimnames))
return(FALSE)
if(n == 0L)
return(TRUE)
## now handling an n-by-n [dz]g[CRT]Matrix, n >= 1:
Cj <- if(is.complex(object@x)) Conj else identity
ae <- function(check.attributes, ...) {
## discarding possible user-supplied check.attributes
all.equal(..., check.attributes = FALSE)
}
isTRUE(ae(target = .M2V( object ),
current = .M2V(Cj(t(object))),
tolerance = tol, ...))
}
setMethod("diff", c(x = "sparseMatrix"),
## Mostly cut and paste of base::diff.default :
function(x, lag = 1L, differences = 1L, ...) {
if(length(lag) != 1L || length(differences) != 1L ||
lag < 1L || differences < 1L)
stop(gettextf("'%s' and '%s' must be positive integers",
"lag", "differences"),
domain = NA)
if(lag * differences >= x@Dim[1L])
return(x[0L])
i1 <- -seq_len(lag)
for(i in seq_len(differences)) {
m <- x@Dim[1L]
x <- x[i1, , drop = FALSE] -
x[-m:-(m - lag + 1L), , drop = FALSE]
}
x
})
setMethod("mean", c(x = "sparseMatrix"),
function(x, ...) mean(as(x, "sparseVector"), ...))
setMethod("rep", c(x = "sparseMatrix"),
function(x, ...) rep(as(x, "sparseVector"), ...))
for(.cl in paste0(c("C", "R", "T"), "sparseMatrix")) {
setMethod("band" , c(x = .cl), .sparse.band)
setMethod("triu" , c(x = .cl), .sparse.triu)
setMethod("tril" , c(x = .cl), .sparse.tril)
setMethod("diag" , c(x = .cl), .sparse.diag.get)
setMethod("diag<-", c(x = .cl), .sparse.diag.set)
setMethod("t" , c(x = .cl), .sparse.t)
setMethod("forceSymmetric", c(x = .cl, uplo = "missing"), .sparse.fS1)
setMethod("forceSymmetric", c(x = .cl, uplo = "character"), .sparse.fS2)
setMethod("symmpart", c(x = .cl), .sparse.symmpart)
setMethod("skewpart", c(x = .cl), .sparse.skewpart)
setMethod("isSymmetric" , c(object = .cl), .sparse.is.sy)
setMethod("isTriangular", c(object = .cl), .sparse.is.tr)
setMethod("isDiagonal" , c(object = .cl), .sparse.is.di)
}
.sparse.subclasses <- names(getClassDef("sparseMatrix")@subclasses)
for(.cl in grep("^[dz][gt][CRT]Matrix$", .sparse.subclasses, value = TRUE))
setMethod("isSymmetric" , c(object = .cl), .sparse.is.sy.dz)
rm(.cl, .sparse.subclasses)
rm(list = c(grep("^[.]sparse[.](band|tri[ul]|diag[.](get|set)|t|fS[12]|symmpart|skewpart|is[.](sy|tr|di)([.]dz)?)$",
ls(all.names = TRUE), value = TRUE)))
if(FALSE) ### FIXME: This would *NOT* be needed, if as.matrix(<sparseMatrix>) was a no-op ;
### ----- and then, base::scale() -> base::scale.default() would work "magically" already..
scale.sparseMatrix <- function(x, center = FALSE, scale = TRUE) {
if(center) warning("a sparseMatrix should rarely be centered: will not be sparse anymore")
## x <- as.matrix(x)
## This rest is *identically* == base :: scale.default :
nc <- ncol(x)
if (is.logical(center)) {
if (center) {
center <- colMeans(x, na.rm=TRUE)
x <- sweep(x, 2L, center, check.margin=FALSE)
}
}
else if (is.numeric(center) && (length(center) == nc))
x <- sweep(x, 2L, center, check.margin=FALSE)
else
stop("length of 'center' must equal the number of columns of 'x'")
if (is.logical(scale)) {
if (scale) {
f <- function(v) {
v <- v[!is.na(v)]
sqrt(sum(v^2) / max(1, length(v) - 1L))
}
scale <- apply(x, 2L, f)
x <- sweep(x, 2L, scale, "/", check.margin=FALSE)
}
}
else if (is.numeric(scale) && length(scale) == nc)
x <- sweep(x, 2L, scale, "/", check.margin=FALSE)
else
stop("length of 'scale' must equal the number of columns of 'x'")
if(is.numeric(center)) attr(x, "scaled:center") <- center
if(is.numeric(scale)) attr(x, "scaled:scale") <- scale
x
}
|