spMatrix.Rd
User friendly construction of a sparse matrix (inheriting from class
TsparseMatrix
) from the triplet representation.
This is much less flexible than sparseMatrix()
and hence
somewhat deprecated.
A sparse matrix in triplet form, as an R object inheriting from both
TsparseMatrix
and
generalMatrix
.
The matrix \(M\) will have
M[i[k], j[k]] == x[k]
, for \(k = 1,2,\ldots, n\), where
n = length(i)
and
M[ i', j' ] == 0
for all other pairs \((i',j')\).
Matrix(*, sparse=TRUE)
for the more usual
constructor of such matrices. Then, sparseMatrix
is more general and flexible than spMatrix()
and by default
returns a CsparseMatrix
which is often slightly
more desirable. Further, bdiag
and
Diagonal
for (block-)diagonal matrix constructors.
Consider TsparseMatrix
and similar class
definition help files.
## simple example
A <- spMatrix(10,20, i = c(1,3:8),
j = c(2,9,6:10),
x = 7 * (1:7))
A # a "dgTMatrix"
#> 10 x 20 sparse Matrix of class "dgTMatrix"
#>
#> [1,] . 7 . . . . . . . . . . . . . . . . . .
#> [2,] . . . . . . . . . . . . . . . . . . . .
#> [3,] . . . . . . . . 14 . . . . . . . . . . .
#> [4,] . . . . . 21 . . . . . . . . . . . . . .
#> [5,] . . . . . . 28 . . . . . . . . . . . . .
#> [6,] . . . . . . . 35 . . . . . . . . . . . .
#> [7,] . . . . . . . . 42 . . . . . . . . . . .
#> [8,] . . . . . . . . . 49 . . . . . . . . . .
#> [9,] . . . . . . . . . . . . . . . . . . . .
#> [10,] . . . . . . . . . . . . . . . . . . . .
summary(A)
#> 10 x 20 sparse Matrix of class "dgTMatrix", with 7 entries
#> i j x
#> 1 1 2 7
#> 2 3 9 14
#> 3 4 6 21
#> 4 5 7 28
#> 5 6 8 35
#> 6 7 9 42
#> 7 8 10 49
str(A) # note that *internally* 0-based indices (i,j) are used
#> Formal class 'dgTMatrix' [package "Matrix"] with 6 slots
#> ..@ i : int [1:7] 0 2 3 4 5 6 7
#> ..@ j : int [1:7] 1 8 5 6 7 8 9
#> ..@ Dim : int [1:2] 10 20
#> ..@ Dimnames:List of 2
#> .. ..$ : NULL
#> .. ..$ : NULL
#> ..@ x : num [1:7] 7 14 21 28 35 42 49
#> ..@ factors : list()
L <- spMatrix(9, 30, i = rep(1:9, 3), 1:27,
(1:27) %% 4 != 1)
L # an "lgTMatrix"
#> 9 x 30 sparse Matrix of class "lgTMatrix"
#>
#> [1,] : . . . . . . . . | . . . . . . . . | . . . . . . . . . . .
#> [2,] . | . . . . . . . . | . . . . . . . . | . . . . . . . . . .
#> [3,] . . | . . . . . . . . | . . . . . . . . : . . . . . . . . .
#> [4,] . . . | . . . . . . . . : . . . . . . . . | . . . . . . . .
#> [5,] . . . . : . . . . . . . . | . . . . . . . . | . . . . . . .
#> [6,] . . . . . | . . . . . . . . | . . . . . . . . | . . . . . .
#> [7,] . . . . . . | . . . . . . . . | . . . . . . . . : . . . . .
#> [8,] . . . . . . . | . . . . . . . . : . . . . . . . . | . . . .
#> [9,] . . . . . . . . : . . . . . . . . | . . . . . . . . | . . .
## A simplified predecessor of Matrix' rsparsematrix() function :
rSpMatrix <- function(nrow, ncol, nnz,
rand.x = function(n) round(rnorm(nnz), 2))
{
## Purpose: random sparse matrix
## --------------------------------------------------------------
## Arguments: (nrow,ncol): dimension
## nnz : number of non-zero entries
## rand.x: random number generator for 'x' slot
## --------------------------------------------------------------
## Author: Martin Maechler, Date: 14.-16. May 2007
stopifnot((nnz <- as.integer(nnz)) >= 0,
nrow >= 0, ncol >= 0, nnz <= nrow * ncol)
spMatrix(nrow, ncol,
i = sample(nrow, nnz, replace = TRUE),
j = sample(ncol, nnz, replace = TRUE),
x = rand.x(nnz))
}
M1 <- rSpMatrix(100000, 20, nnz = 200)
summary(M1)
#> 100000 x 20 sparse Matrix of class "dgTMatrix", with 200 entries
#> i j x
#> 1 66401 20 -1.69
#> 2 73286 11 -1.52
#> 3 56307 3 -1.38
#> 4 17474 20 1.38
#> 5 74722 3 -1.64
#> 6 81403 11 0.83
#> 7 1732 17 -0.08
#> 8 55227 2 -0.78
#> 9 87006 16 0.34
#> 10 31204 9 0.70
#> 11 484 4 0.24
#> 12 13842 6 0.18
#> 13 95779 2 -1.90
#> 14 75891 4 1.59
#> 15 57053 20 1.55
#> 16 10659 20 -2.34
#> 17 38599 10 -1.14
#> 18 55312 20 -0.91
#> 19 51936 19 -1.59
#> 20 98825 15 -0.80
#> 21 82446 14 -2.55
#> 22 76710 20 1.14
#> 23 43721 7 0.55
#> 24 7184 4 3.33
#> 25 80374 9 0.74
#> 26 48296 19 0.77
#> 27 56293 10 -0.47
#> 28 48970 10 0.06
#> 29 34698 10 -0.84
#> 30 95920 16 -0.45
#> 31 47633 4 -0.52
#> 32 5705 11 -0.52
#> 33 46752 13 -0.87
#> 34 94041 15 -1.54
#> 35 14333 14 -0.57
#> 36 22170 9 -1.61
#> 37 13842 12 1.44
#> 38 64691 9 -0.99
#> 39 69066 20 -0.66
#> 40 61421 11 -0.39
#> 41 87540 8 -0.03
#> 42 64557 17 -0.57
#> 43 45329 2 0.48
#> 44 37300 1 -0.72
#> 45 24731 11 -0.92
#> 46 63561 20 -1.08
#> 47 43865 19 1.02
#> 48 92551 7 -0.35
#> 49 50348 9 0.05
#> 50 28596 2 0.20
#> 51 52372 18 0.09
#> 52 70595 4 0.23
#> 53 73098 7 -0.01
#> 54 66538 10 0.89
#> 55 6106 6 0.47
#> 56 51900 15 -0.98
#> 57 28463 12 0.63
#> 58 7717 2 -0.51
#> 59 78123 3 0.23
#> 60 24688 7 -1.64
#> 61 44410 14 1.24
#> 62 98086 5 1.84
#> 63 18443 1 2.24
#> 64 69854 15 -1.54
#> 65 26336 5 -1.40
#> 66 23442 17 0.74
#> 67 67412 7 0.53
#> 68 45061 15 0.17
#> 69 26565 16 0.91
#> 70 84678 3 -1.79
#> 71 15575 13 -2.02
#> 72 90916 7 -1.91
#> 73 31882 8 -1.77
#> 74 50504 10 0.18
#> 75 11932 5 0.92
#> 76 85490 6 1.12
#> 77 33216 9 -0.80
#> 78 36299 7 -0.30
#> 79 62067 16 0.55
#> 80 48393 19 0.41
#> 81 14940 6 -3.49
#> 82 21789 17 1.52
#> 83 96787 2 -1.50
#> 84 84841 10 0.72
#> 85 55960 17 1.28
#> 86 48550 1 0.15
#> 87 42093 5 -0.64
#> 88 44455 12 0.11
#> 89 16173 9 0.30
#> 90 30553 15 1.37
#> 91 38815 5 -0.61
#> 92 22620 19 -0.54
#> 93 48177 15 1.07
#> 94 69250 20 0.01
#> 95 38925 6 -1.00
#> 96 65047 18 -1.72
#> 97 27060 20 -1.23
#> 98 46340 10 -0.89
#> 99 12114 16 1.00
#> 100 52749 12 -0.34
#> 101 565 20 0.59
#> 102 42878 19 -0.56
#> 103 74945 7 1.11
#> 104 53474 4 -0.42
#> 105 6693 13 -0.17
#> 106 69103 14 0.45
#> 107 65514 5 0.57
#> 108 40777 5 0.23
#> 109 50826 1 1.32
#> 110 98977 6 -0.53
#> 111 57023 1 1.95
#> 112 44212 11 -0.24
#> 113 41595 2 -0.21
#> 114 81950 5 1.63
#> 115 27076 16 0.62
#> 116 59896 16 0.75
#> 117 2245 3 3.94
#> 118 15661 1 -0.17
#> 119 52357 19 1.12
#> 120 81379 5 0.50
#> 121 49370 13 1.03
#> 122 44319 2 -0.07
#> 123 10597 1 -0.72
#> 124 14634 8 -0.63
#> 125 33315 19 -0.29
#> 126 93140 20 -0.63
#> 127 90911 9 0.41
#> 128 68921 11 0.97
#> 129 87778 4 1.34
#> 130 22972 1 -1.09
#> 131 88787 16 0.43
#> 132 25256 3 0.09
#> 133 99742 14 2.36
#> 134 42461 3 0.39
#> 135 30544 4 -0.97
#> 136 77652 9 2.03
#> 137 10659 6 -0.98
#> 138 51644 11 -0.77
#> 139 93929 6 -1.61
#> 140 92490 1 -0.24
#> 141 22962 2 -1.67
#> 142 95346 15 0.90
#> 143 6 7 0.92
#> 144 38258 12 -1.02
#> 145 82166 9 -0.41
#> 146 20574 17 0.51
#> 147 20290 12 -0.36
#> 148 93005 14 2.65
#> 149 29564 19 0.74
#> 150 57121 10 -1.27
#> 151 48549 3 -0.79
#> 152 17358 6 -0.65
#> 153 15644 20 -0.64
#> 154 84674 2 -0.11
#> 155 46378 3 -0.20
#> 156 14376 15 0.74
#> 157 78306 20 0.83
#> 158 99426 11 0.79
#> 159 71698 1 -0.61
#> 160 40846 11 -0.12
#> 161 9712 4 1.16
#> 162 49030 20 -1.05
#> 163 19226 7 0.12
#> 164 94872 13 1.80
#> 165 7760 12 0.37
#> 166 98387 15 0.36
#> 167 30113 20 -0.69
#> 168 47281 18 2.40
#> 169 2710 11 0.04
#> 170 97056 6 -0.93
#> 171 21689 8 -0.80
#> 172 67544 3 -1.74
#> 173 76459 20 -0.44
#> 174 41386 12 -1.50
#> 175 72789 9 -1.50
#> 176 17401 1 -0.74
#> 177 91364 14 -1.05
#> 178 12213 20 0.11
#> 179 26805 19 0.03
#> 180 47909 13 0.27
#> 181 11316 12 0.70
#> 182 29991 2 2.02
#> 183 96370 11 0.60
#> 184 88280 15 -0.07
#> 185 79393 19 0.50
#> 186 38237 6 0.91
#> 187 35374 5 0.72
#> 188 3352 5 -0.53
#> 189 5169 9 -0.90
#> 190 2500 1 -0.06
#> 191 50336 7 -1.82
#> 192 32294 6 -0.66
#> 193 4670 3 -0.02
#> 194 70286 15 1.28
#> 195 84397 8 1.78
#> 196 34583 14 -1.37
#> 197 36292 10 -0.48
#> 198 92450 8 -0.10
#> 199 25322 3 0.40
#> 200 53761 8 -1.47