Dynamic dispatch for sparse matrices
Fabian Scheipl —
written Mar 20, 2014 —
source
We want to do matrix multiplication for 3 cases:
- dense times dense
- sparse times dense for sparse matrices of class
dgCMatrix
- sparse times dense for sparse matrices of class
indMatrix
,
using R’s Matrix
package for sparse matrices in R and
RcppArmadillo
for C++ linear algebra:
Set up test cases:
Loading required package: methods
Check exception handling:
<Rcpp::exception: unknown class of Xr>
Dense times dense:
[1] TRUE
test replications elapsed relative
2 matmult_cpp(X, Y) 100 0.086 1.00
1 X %*% Y 100 0.098 1.14
dgCMatrix
times dense:
[1] TRUE
test replications elapsed relative
2 matmult_cpp(X_sp, Y) 100 0.006 1.000
1 X_sp %*% Y 100 0.013 2.167
indMatrix
times dense:
[1] TRUE
test replications elapsed relative
2 matmult_cpp(X_ind, Y) 100 0.008 1.0
1 X_ind %*% Y 100 0.012 1.5
Based on this Q&A on StackOverflow,
thanks again to Kevin Ushey for his helpful comment.
tags:
sparse
Related Articles