Drop Non-Structural Zeros from a Sparse Matrix
drop0.RdDeletes “non-structural” zeros (i.e., zeros stored explicitly, in memory) from a sparse matrix and returns the result.
Arguments
- x
a
Matrix, typically inheriting from virtual classsparseMatrix.denseMatrixand traditional vectors and matrices are coerced toCsparseMatrix, with zeros dropped automatically, hence users passing suchxshould consideras(x, "CsparseMatrix")instead, notably in thetol = 0case.- tol
a non-negative number. If
xis numeric, then entries less than or equal totolin absolute value are deleted.- is.Csparse
a logical used only if
give.CsparseisTRUE, indicating ifxalready inherits from virtual classCsparseMatrix, in which case coercion is not attempted, permitting some (typically small) speed-up.- give.Csparse
a logical indicating if the result must inherit from virtual class
CsparseMatrix. IfFALSEandxinherits fromRsparseMatrix,TsparseMatrix, orindMatrix, then the result preserves the class ofx. The default value isTRUEonly for backwards compatibility.
Value
A sparseMatrix, the result of deleting
non-structural zeros from x, possibly after coercion.
Note
drop0 is sometimes called in conjunction with
zapsmall, e.g., when dealing with sparse
matrix products; see the example.
See also
Function sparseMatrix, for constructing objects
inheriting from virtual class sparseMatrix;
nnzero.
Examples
(m <- sparseMatrix(i = 1:8, j = 2:9, x = c(0:2, 3:-1),
dims = c(10L, 20L)))
#> 10 x 20 sparse Matrix of class "dgCMatrix"
#>
#> [1,] . 0 . . . . . . . . . . . . . . . . . .
#> [2,] . . 1 . . . . . . . . . . . . . . . . .
#> [3,] . . . 2 . . . . . . . . . . . . . . . .
#> [4,] . . . . 3 . . . . . . . . . . . . . . .
#> [5,] . . . . . 2 . . . . . . . . . . . . . .
#> [6,] . . . . . . 1 . . . . . . . . . . . . .
#> [7,] . . . . . . . 0 . . . . . . . . . . . .
#> [8,] . . . . . . . . -1 . . . . . . . . . . .
#> [9,] . . . . . . . . . . . . . . . . . . . .
#> [10,] . . . . . . . . . . . . . . . . . . . .
drop0(m)
#> 10 x 20 sparse Matrix of class "dgCMatrix"
#>
#> [1,] . . . . . . . . . . . . . . . . . . . .
#> [2,] . . 1 . . . . . . . . . . . . . . . . .
#> [3,] . . . 2 . . . . . . . . . . . . . . . .
#> [4,] . . . . 3 . . . . . . . . . . . . . . .
#> [5,] . . . . . 2 . . . . . . . . . . . . . .
#> [6,] . . . . . . 1 . . . . . . . . . . . . .
#> [7,] . . . . . . . . . . . . . . . . . . . .
#> [8,] . . . . . . . . -1 . . . . . . . . . . .
#> [9,] . . . . . . . . . . . . . . . . . . . .
#> [10,] . . . . . . . . . . . . . . . . . . . .
## A larger example:
t5 <- new("dtCMatrix", Dim = c(5L, 5L), uplo = "L",
x = c(10, 1, 3, 10, 1, 10, 1, 10, 10),
i = c(0L,2L,4L, 1L, 3L,2L,4L, 3L, 4L),
p = c(0L, 3L, 5L, 7:9))
TT <- kronecker(t5, kronecker(kronecker(t5, t5), t5))
IT <- solve(TT)
I. <- TT %*% IT ; nnzero(I.) # 697 ( == 625 + 72 )
#> [1] 697
I.0 <- drop0(zapsmall(I.))
## which actually can be more efficiently achieved by
I.. <- drop0(I., tol = 1e-15)
stopifnot(all(I.0 == Diagonal(625)), nnzero(I..) == 625)