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.

spMatrix(nrow, ncol, i = integer(0L), j = integer(0L), x = double(0L))

Arguments

nrow, ncol

integers specifying the desired number of rows and columns.

i,j

integer vectors of the same length specifying the locations of the non-zero (or non-TRUE) entries of the matrix.

x

atomic vector of the same length as i and j, specifying the values of the non-zero entries.

Value

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')\).

See also

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.

Examples

## 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   56307 11 -1.38
#> 2   17474 17  1.38
#> 3   74722  2 -1.64
#> 4   81403 16  0.83
#> 5    1732  9 -0.08
#> 6   55227  4 -0.78
#> 7   87006  6  0.34
#> 8   31204  2  0.70
#> 9     484  4  0.24
#> 10  13842 20  0.18
#> 11  95779 20 -1.90
#> 12  75891 10  1.59
#> 13  57053 20  1.55
#> 14  10659 19 -2.34
#> 15  38599 15 -1.14
#> 16  55312 14 -0.91
#> 17  51936 20 -1.59
#> 18  98825  7 -0.80
#> 19  82446  4 -2.55
#> 20  76710  9  1.14
#> 21  43721 19  0.55
#> 22   7184 10  3.33
#> 23  80374 10  0.74
#> 24  48296 10  0.77
#> 25  56293 16 -0.47
#> 26  48970  4  0.06
#> 27  34698 11 -0.84
#> 28  95920 13 -0.45
#> 29  47633 15 -0.52
#> 30   5705 14 -0.52
#> 31  46752  9 -0.87
#> 32  94041 12 -1.54
#> 33  14333  9 -0.57
#> 34  22170 20 -1.61
#> 35  13842 11  1.44
#> 36  64691  8 -0.99
#> 37  69066 17 -0.66
#> 38  61421  2 -0.39
#> 39  87540  1 -0.03
#> 40  64557 11 -0.57
#> 41  45329 20  0.48
#> 42  37300 19 -0.72
#> 43  24731  7 -0.92
#> 44  63561  9 -1.08
#> 45  43865  2  1.02
#> 46  92551 18 -0.35
#> 47  50348  4  0.05
#> 48  28596  7  0.20
#> 49  52372 10  0.09
#> 50  70595  6  0.23
#> 51  73098 15 -0.01
#> 52  66538 12  0.89
#> 53   6106  2  0.47
#> 54  51900  3 -0.98
#> 55  28463  7  0.63
#> 56   7717 14 -0.51
#> 57  78123  5  0.23
#> 58  24688  1 -1.64
#> 59  44410 15  1.24
#> 60  98086  5  1.84
#> 61  18443 17  2.24
#> 62  69854  7 -1.54
#> 63  26336 15 -1.40
#> 64  23442 16  0.74
#> 65  67412  3  0.53
#> 66  45061 13  0.17
#> 67  26565  7  0.91
#> 68  84678  8 -1.79
#> 69  15575 10 -2.02
#> 70  90916  5 -1.91
#> 71  31882  6 -1.77
#> 72  50504  9  0.18
#> 73  11932  7  0.92
#> 74  85490 16  1.12
#> 75  33216 19 -0.80
#> 76  36299  6 -0.30
#> 77  62067 17  0.55
#> 78  48393  2  0.41
#> 79  14940 10 -3.49
#> 80  21789 17  1.52
#> 81  96787  1 -1.50
#> 82  84841  5  0.72
#> 83  55960 12  1.28
#> 84  48550  9  0.15
#> 85  42093 15 -0.64
#> 86  44455  5  0.11
#> 87  16173 19  0.30
#> 88  30553 15  1.37
#> 89  38815 20 -0.61
#> 90  22620  6 -0.54
#> 91  48177 18  1.07
#> 92  69250 20  0.01
#> 93  38925 10 -1.00
#> 94  65047 16 -1.72
#> 95  27060 12 -1.23
#> 96  46340 20 -0.89
#> 97  12114 19  1.00
#> 98  52749  7 -0.34
#> 99    565  4  0.59
#> 100 42878 13 -0.56
#> 101 74945 14  1.11
#> 102 53474  5 -0.42
#> 103  6693  5 -0.17
#> 104 69103  1  0.45
#> 105 65514  6  0.57
#> 106 40777  1  0.23
#> 107 50826 11  1.32
#> 108 98977  2 -0.53
#> 109 57023  5  1.95
#> 110 44212 16 -0.24
#> 111 41595 16 -0.21
#> 112 81950  3  1.63
#> 113 27076  1  0.62
#> 114 59896 19  0.75
#> 115  2245  5  3.94
#> 116 15661 13 -0.17
#> 117 52357  2  1.12
#> 118 81379  1  0.50
#> 119 49370  8  1.03
#> 120 44319 19 -0.07
#> 121 10597 20 -0.72
#> 122 14634  9 -0.63
#> 123 33315 11 -0.29
#> 124 93140  4 -0.63
#> 125 90911  1  0.41
#> 126 68921 16  0.97
#> 127 87778  3  1.34
#> 128 22972 14 -1.09
#> 129 88787  3  0.43
#> 130 25256  4  0.09
#> 131 99742  9  2.36
#> 132 42461  6  0.39
#> 133 30544 11 -0.97
#> 134 77652  6  2.03
#> 135 10659  1 -0.98
#> 136 51644  2 -0.77
#> 137 93929 15 -1.61
#> 138 92490  7 -0.24
#> 139 22962 12 -1.67
#> 140 95346  9  0.90
#> 141     6 17  0.92
#> 142 38258 12 -1.02
#> 143 82166 14 -0.41
#> 144 20574 19  0.51
#> 145 20290 10 -0.36
#> 146 93005  3  2.65
#> 147 29564  6  0.74
#> 148 57121 20 -1.27
#> 149 48549  2 -0.79
#> 150 17358  3 -0.65
#> 151 15644 15 -0.64
#> 152 84674 20 -0.11
#> 153 46378 11 -0.20
#> 154 14376  1  0.74
#> 155 78306 11  0.83
#> 156 99426  4  0.79
#> 157 71698 20 -0.61
#> 158 40846  7 -0.12
#> 159  9712 13  1.16
#> 160 49030 12 -1.05
#> 161 19226 15  0.12
#> 162 94872 20  1.80
#> 163  7760 18  0.37
#> 164 98387 11  0.36
#> 165 30113  6 -0.69
#> 166 47281  8  2.40
#> 167  2710  3  0.04
#> 168 97056 20 -0.93
#> 169 21689 12 -0.80
#> 170 67544  9 -1.74
#> 171 76459  1 -0.44
#> 172 41386 14 -1.50
#> 173 72789 20 -1.50
#> 174 17401 19 -0.74
#> 175 91364 13 -1.05
#> 176 12213 12  0.11
#> 177 26805  2  0.03
#> 178 47909 11  0.27
#> 179 11316 15  0.70
#> 180 29991 19  2.02
#> 181 96370  6  0.60
#> 182 88280  5 -0.07
#> 183 79393  5  0.50
#> 184 38237  9  0.91
#> 185 35374  1  0.72
#> 186  3352  7 -0.53
#> 187  5169  6 -0.90
#> 188  2500  3 -0.06
#> 189 50336 15 -1.82
#> 190 32294  8 -0.66
#> 191  4670 14 -0.02
#> 192 70286 10  1.28
#> 193 84397  8  1.78
#> 194 34583  3 -1.37
#> 195 36292  8 -0.48
#> 196 92450 11 -0.10
#> 197 25322  8  0.40
#> 198 53761 10 -1.47
#> 199 19252 10  0.71
#> 200 74243  9 -0.21