2
2
title : Coercion of matrix to sparse matrix (dgCMatrix) and maintaining dimnames.
3
3
author : Søren Højsgaard
4
4
license : GPL (>= 2)
5
- tags : eigen matrix
5
+ tags : eigen matrix sparse
6
6
summary : We illustrate 1) a fast way of coercing a dense matrix to a sparse matrix and 2) how to copy the dimnames from the dense to the sparse matrix.
7
7
layout : post
8
8
src : 2013-01-20-sparse-matrix-coercion.Rmd
@@ -59,12 +59,6 @@ Loading required package: methods
59
59
60
60
61
61
62
- <pre class =" output " >
63
- Loading required package: lattice
64
- </pre >
65
-
66
-
67
-
68
62
{% highlight r %}
69
63
M1 <- as(m, "dgCMatrix")
70
64
M1
73
67
74
68
75
69
<pre class =" output " >
76
- 6 x 6 sparse Matrix of class " dgCMatrix"
70
+ 6 x 6 sparse Matrix of class & quot ; dgCMatrix& quot ;
77
71
a b c d e f
78
72
a . . . . . 1
79
73
b . . . 1 . 1
@@ -92,13 +86,13 @@ str(M1)
92
86
93
87
94
88
<pre class =" output " >
95
- Formal class 'dgCMatrix' [package " Matrix" ] with 6 slots
89
+ Formal class 'dgCMatrix' [package & quot ; Matrix& quot ; ] with 6 slots
96
90
..@ i : int [1:6] 4 4 1 5 0 1
97
91
..@ p : int [1:7] 0 1 2 2 4 4 6
98
92
..@ Dim : int [1:2] 6 6
99
93
..@ Dimnames:List of 2
100
- .. ..$ : chr [1:6] "a" "b" "c" "d" ...
101
- .. ..$ : chr [1:6] "a" "b" "c" "d" ...
94
+ .. ..$ : chr [1:6] & quot ; a & quot ; & quot ; b & quot ; & quot ; c & quot ; & quot ; d & quot ; ...
95
+ .. ..$ : chr [1:6] & quot ; a & quot ; & quot ; b & quot ; & quot ; c & quot ; & quot ; d & quot ; ...
102
96
..@ x : num [1:6] 1 1 1 1 1 1
103
97
..@ factors : list()
104
98
</pre >
@@ -139,7 +133,7 @@ SEXP asdgCMatrix_( SEXP XX_ ){
139
133
140
134
141
135
<pre class =" output " >
142
- 6 x 6 sparse Matrix of class " dgCMatrix"
136
+ 6 x 6 sparse Matrix of class & quot ; dgCMatrix& quot ;
143
137
a b c d e f
144
138
a . . . . . 1
145
139
b . . . 1 . 1
@@ -158,13 +152,13 @@ str(M2)
158
152
159
153
160
154
<pre class =" output " >
161
- Formal class 'dgCMatrix' [package " Matrix" ] with 6 slots
155
+ Formal class 'dgCMatrix' [package & quot ; Matrix& quot ; ] with 6 slots
162
156
..@ i : int [1:6] 4 4 1 5 0 1
163
157
..@ p : int [1:7] 0 1 2 2 4 4 6
164
158
..@ Dim : int [1:2] 6 6
165
159
..@ Dimnames:List of 2
166
- .. ..$ : chr [1:6] "a" "b" "c" "d" ...
167
- .. ..$ : chr [1:6] "a" "b" "c" "d" ...
160
+ .. ..$ : chr [1:6] & quot ; a & quot ; & quot ; b & quot ; & quot ; c & quot ; & quot ; d & quot ; ...
161
+ .. ..$ : chr [1:6] & quot ; a & quot ; & quot ; b & quot ; & quot ; c & quot ; & quot ; d & quot ; ...
168
162
..@ x : num [1:6] 1 1 1 1 1 1
169
163
..@ factors : list()
170
164
</pre >
@@ -197,8 +191,8 @@ rbenchmark::benchmark(asdgCMatrix_(m * 1.0), as(m, "dgCMatrix"),
197
191
198
192
<pre class =" output " >
199
193
test replications elapsed relative user.self sys.self
200
- 1 asdgCMatrix_(m * 1) 1000 0.025 1.00 0.024 0.000
201
- 2 as(m, " dgCMatrix" ) 1000 0.246 9.84 0.240 0.004
194
+ 1 asdgCMatrix_(m * 1) 1000 0.028 1.00 0.028 0.000
195
+ 2 as(m, & quot ; dgCMatrix& quot ; ) 1000 0.287 10.25 0.284 0.004
202
196
</pre >
203
197
204
198
@@ -218,8 +212,8 @@ rbenchmark::benchmark(asdgCMatrix_(m * 1.0), as(m, "dgCMatrix"),
218
212
219
213
<pre class =" output " >
220
214
test replications elapsed relative user.self sys.self
221
- 1 asdgCMatrix_(m * 1) 1000 0.137 1.000 0.136 0.000
222
- 2 as(m, " dgCMatrix" ) 1000 0.443 3.234 0.432 0.008
215
+ 1 asdgCMatrix_(m * 1) 1000 0.133 1.000 0.132 0.000
216
+ 2 as(m, & quot ; dgCMatrix& quot ; ) 1000 0.359 2.699 0.356 0.004
223
217
</pre >
224
218
225
219
@@ -238,8 +232,8 @@ rbenchmark::benchmark(asdgCMatrix_(m * 1.0), as(m, "dgCMatrix"),
238
232
239
233
<pre class =" output " >
240
234
test replications elapsed relative user.self sys.self
241
- 1 asdgCMatrix_(m * 1) 100 1.193 1.000 1.180 0.008
242
- 2 as(m, " dgCMatrix" ) 100 2.201 1.845 2.064 0.128
235
+ 1 asdgCMatrix_(m * 1) 100 1.193 1.00 1.184 0.004
236
+ 2 as(m, & quot ; dgCMatrix& quot ; ) 100 2.303 1.93 2.092 0.204
243
237
</pre >
244
238
245
239
@@ -258,8 +252,8 @@ rbenchmark::benchmark(asdgCMatrix_(m * 1.0), as(m, "dgCMatrix"),
258
252
259
253
<pre class =" output " >
260
254
test replications elapsed relative user.self sys.self
261
- 1 asdgCMatrix_(m * 1) 100 8.911 1.000 6.024 2.828
262
- 2 as(m, " dgCMatrix" ) 100 21.557 2.419 16.930 4.500
255
+ 1 asdgCMatrix_(m * 1) 100 8.868 1.000 5.82 3.004
256
+ 2 as(m, & quot ; dgCMatrix& quot ; ) 100 23.441 2.643 18.70 4.636
263
257
</pre >
264
258
265
259
0 commit comments