@@ -18,18 +18,18 @@ Let's start with creating a sparse matrix.
18
18
19
19
20
20
{% highlight r %}
21
- suppressMessages(library(Matrix))
22
- i <- c(1,3:8)
23
- j <- c(2,9,6:10)
24
- x <- 7 * (1:7)
25
- A <- sparseMatrix(i, j, x = x)
26
- print(A)
21
+ suppressMessages(library(Matrix))
22
+ i <- c(1,3:8)
23
+ j <- c(2,9,6:10)
24
+ x <- 7 * (1:7)
25
+ A <- sparseMatrix(i, j, x = x)
26
+ print(A)
27
27
{% endhighlight %}
28
28
29
29
30
30
31
31
<pre class =" output " >
32
- 8 x 10 sparse Matrix of class " dgCMatrix"
32
+ 8 x 10 sparse Matrix of class & quot ; dgCMatrix& quot ;
33
33
34
34
[1,] . 7 . . . . . . . .
35
35
[2,] . . . . . . . . . .
@@ -53,35 +53,29 @@ using namespace Rcpp ;
53
53
54
54
// [[ Rcpp::export]]
55
55
void convertSparse(S4 mat) { // slight improvement with two non-nested loops
56
+
56
57
IntegerVector dims = mat.slot("Dim");
57
- IntegerVector i = mat.slot("i");
58
- IntegerVector p = mat.slot("p");
59
- NumericVector x = mat.slot("x");
60
-
58
+ arma::urowvec i = Rcpp::as<arma::urowvec>( mat.slot("i") );
59
+ arma::urowvec p = Rcpp::as<arma::urowvec>( mat.slot("p"));
60
+ arma::vec x = Rcpp::as<arma::vec>( mat.slot("x") );
61
+
61
62
int nrow = dims[0], ncol = dims[1];
62
63
arma::sp_mat res(nrow, ncol);
63
64
64
65
// create space for values, and copy
65
- arma::access::rw(res.values) =
66
- arma::memory::acquire_chunked<double>(x.size() + 1);
67
- arma::arrayops::copy(arma::access::rwp(res.values),
68
- x.begin(), x.size() + 1);
69
-
70
- // create space for row_indices, and copy -- so far in a lame loop
71
- arma::access::rw(res.row_indices) =
72
- arma::memory::acquire_chunked<arma::uword>(x.size() + 1);
73
- for (int j=0; j<i.size(); j++)
74
- arma::access::rwp(res.row_indices)[j] = i[j];
66
+ arma::access::rw(res.values) = arma::memory::acquire_chunked<double>(x.size() + 1);
67
+ arma::arrayops::copy(arma::access::rwp(res.values), x.begin(), x.size() + 1);
68
+
69
+ // create space for row_indices, and copy
70
+ arma::access::rw(res.row_indices) = arma::memory::acquire_chunked<arma::uword>(i.size() + 1);
71
+ arma::arrayops::copy(arma::access::rwp(res.row_indices), i.begin(), i.size() + 1);
75
72
76
- // create space for col_ptrs, and copy -- so far in a lame loop
77
- arma::access::rw(res.col_ptrs) =
78
- arma::memory::acquire<arma::uword>(p.size() + 2);
79
- for (int j=0; j<p.size(); j++)
80
- arma::access::rwp(res.col_ptrs)[j] = p[j];
73
+ // create space for col_ptrs, and copy
74
+ arma::access::rw(res.col_ptrs) = arma::memory::acquire<arma::uword>(p.size() + 2);
75
+ arma::arrayops::copy(arma::access::rwp(res.col_ptrs), p.begin(), p.size() + 1);
81
76
82
77
// important: set the sentinel as well
83
- arma::access::rwp(res.col_ptrs)[p.size()+1] =
84
- std::numeric_limits<arma::uword>::max();
78
+ arma::access::rwp(res.col_ptrs)[p.size()+1] = std::numeric_limits<arma::uword>::max();
85
79
86
80
// set the number of non-zero elements
87
81
arma::access::rw(res.n_nonzero) = x.size();
@@ -95,7 +89,7 @@ Running this example shows the same matrix printed to `stdout` by
95
89
Armadillo.
96
90
97
91
{% highlight r %}
98
- convertSparse(A)
92
+ convertSparse(A)
99
93
{% endhighlight %}
100
94
101
95
@@ -116,5 +110,7 @@ SpMat res:
116
110
117
111
Support for sparse matrix is currently still limited in Armadillo,
118
112
but expected to grow. Likewise, RcppArmadillo does not yet have
119
- ` as<>() ` and ` wrap() ` converters but we expect to add these
113
+ converters such as ` as<>() ` (though the example above does
114
+ essentially everything that is needed) and ` wrap() ` converters.
115
+ But we expect to add these
120
116
eventually --- at which point this example will be much simpler.
0 commit comments