mat2triplet.Rd
From an R object coercible to "TsparseMatrix"
,
typically a (sparse) matrix, produce its triplet representation which may
collapse to a “Duplet” in the case of binary aka pattern, such as
"nMatrix"
objects.
mat2triplet(x, uniqT = FALSE)
any R object for which as(x, "TsparseMatrix")
works; typically a matrix
of one of the Matrix
package matrices.
logical
indicating if the triplet
representation should be ‘unique’ in the sense of
asUniqueT(byrow=FALSE)
.
A list
, typically with three components,
vector of row indices for all non-zero entries of x
vector of columns indices for all non-zero entries of x
vector of all non-zero entries of x
; exists only
when as(x, "TsparseMatrix")
is not a
"nsparseMatrix"
.
Note that the order
of the entries is determined by the
coercion to "TsparseMatrix"
and hence typically
with increasing j
(and increasing i
within ties of j
).
The mat2triplet()
utility was created to be a more efficient and
more predictable substitute for summary(<sparseMatrix>)
.
UseRs have wrongly expected the latter to return a data frame with
columns i
and j
which however is wrong for a
"diagonalMatrix"
.
The summary()
method for "sparseMatrix"
,
summary,sparseMatrix-method
.
mat2triplet()
is conceptually the inverse function of
spMatrix
and (one case of) sparseMatrix
.
mat2triplet # simple definition
#> function (x, uniqT = FALSE)
#> {
#> T <- as(x, "TsparseMatrix")
#> if (uniqT)
#> T <- asUniqueT(T, isT = TRUE)
#> if (is(T, "nsparseMatrix"))
#> list(i = T@i + 1L, j = T@j + 1L)
#> else list(i = T@i + 1L, j = T@j + 1L, x = T@x)
#> }
#> <bytecode: 0x557a02e8bd88>
#> <environment: namespace:Matrix>
i <- c(1,3:8); j <- c(2,9,6:10); x <- 7 * (1:7)
(Ax <- sparseMatrix(i, j, x = x)) ## 8 x 10 "dgCMatrix"
#> 8 x 10 sparse Matrix of class "dgCMatrix"
#>
#> [1,] . 7 . . . . . . . .
#> [2,] . . . . . . . . . .
#> [3,] . . . . . . . . 14 .
#> [4,] . . . . . 21 . . . .
#> [5,] . . . . . . 28 . . .
#> [6,] . . . . . . . 35 . .
#> [7,] . . . . . . . . 42 .
#> [8,] . . . . . . . . . 49
str(trA <- mat2triplet(Ax))
#> List of 3
#> $ i: int [1:7] 1 4 5 6 3 7 8
#> $ j: int [1:7] 2 6 7 8 9 9 10
#> $ x: num [1:7] 7 21 28 35 14 42 49
stopifnot(i == sort(trA$i), sort(j) == trA$j, x == sort(trA$x))
D <- Diagonal(x=4:2)
summary(D)
#> 3 x 3 diagonal Matrix of class "ddiMatrix"
#> Min. 1st Qu. Median Mean 3rd Qu. Max.
#> 2.0 2.5 3.0 3.0 3.5 4.0
str(mat2triplet(D))
#> List of 3
#> $ i: int [1:3] 1 2 3
#> $ j: int [1:3] 1 2 3
#> $ x: num [1:3] 4 3 2