Examples for 'Matrix::dgTMatrix-class'


Sparse matrices in triplet form

Aliases: dgTMatrix-class +,dgTMatrix,dgTMatrix-method coerce,dgTMatrix,dgCMatrix-method coerce,dgTMatrix,dgRMatrix-method coerce,dgTMatrix,dgeMatrix-method coerce,dgTMatrix,dsCMatrix-method coerce,dgTMatrix,dsTMatrix-method coerce,dgTMatrix,dtCMatrix-method coerce,dgTMatrix,dtTMatrix-method coerce,dgTMatrix,lgTMatrix-method coerce,dgTMatrix,ngTMatrix-method coerce,matrix,dgTMatrix-method coerce,numLike,dgTMatrix-method determinant,dgTMatrix,logical-method

Keywords: classes algebra

### ** Examples

m <- Matrix(0+1:28, nrow = 4)
m[-3,c(2,4:5,7)] <- m[ 3, 1:4] <- m[1:3, 6] <- 0
(mT <- as(m, "TsparseMatrix"))
4 x 7 sparse Matrix of class "dgTMatrix"
                      
[1,] 1 .  9 .  .  .  .
[2,] 2 . 10 .  .  .  .
[3,] . .  . . 19  . 27
[4,] 4 . 12 .  . 24  .
str(mT)
Formal class 'dgTMatrix' [package "Matrix"] with 6 slots
  ..@ i       : int [1:9] 0 1 3 0 1 3 2 3 2
  ..@ j       : int [1:9] 0 0 0 2 2 2 4 5 6
  ..@ Dim     : int [1:2] 4 7
  ..@ Dimnames:List of 2
  .. ..$ : NULL
  .. ..$ : NULL
  ..@ x       : num [1:9] 1 2 4 9 10 12 19 24 27
  ..@ factors : list()
mT[1,]
[1] 1 0 9 0 0 0 0
mT[4, drop = FALSE]
[1] 4
stopifnot(identical(mT[lower.tri(mT)],
                    m [lower.tri(m) ]))
<sparse>[ <logic> ]: .M.sub.i.logical() maybe inefficient
mT[lower.tri(mT,diag=TRUE)] <- 0
mT
4 x 7 sparse Matrix of class "dgTMatrix"
                      
[1,] . .  9 .  .  .  .
[2,] . . 10 .  .  .  .
[3,] . .  . . 19  . 27
[4,] . .  . .  . 24  .
## Triplet representation with repeated (i,j) entries
## *adds* the corresponding x's:
T2 <- new("dgTMatrix",
          i = as.integer(c(1,1,0,3,3)),
          j = as.integer(c(2,2,4,0,0)), x=10*1:5, Dim=4:5)
str(T2) # contains (i,j,x) slots exactly as above, but
Formal class 'dgTMatrix' [package "Matrix"] with 6 slots
  ..@ i       : int [1:5] 1 1 0 3 3
  ..@ j       : int [1:5] 2 2 4 0 0
  ..@ Dim     : int [1:2] 4 5
  ..@ Dimnames:List of 2
  .. ..$ : NULL
  .. ..$ : NULL
  ..@ x       : num [1:5] 10 20 30 40 50
  ..@ factors : list()
T2 ## has only three non-zero entries, as for repeated (i,j)'s,
4 x 5 sparse Matrix of class "dgTMatrix"
                 
[1,]  . .  . . 30
[2,]  . . 30 .  .
[3,]  . .  . .  .
[4,] 90 .  . .  .
   ## the corresponding x's are "implicitly" added
stopifnot(nnzero(T2) == 3)

[Package Matrix version 1.5-3 Index]