[Rcpp-devel] Operating on sparse matrices in a list.
Dirk Eddelbuettel
edd at debian.org
Wed Dec 11 17:21:13 CET 2024
On 11 December 2024 at 10:40, Kumar MS wrote:
| # -- The function in question --
| cppFunction(
| "arma::mat accessEntries(Rcpp::List x){
| for(int i=0; i<x.size(); i++){
| arma::sp_mat ar=Rcpp::as<arma::sp_mat>(x[i]); //similar to : https://gallery.rcpp.org/articles/dynamic-dispatch-for-sparse-matrices/
| }
| }",
You say you return an arma::mat but you have no return value. That can lead
to the type of run-time surprises you saw.
Below are both a minimal C++ file (I prefer to embed the R code in the C++
file), and its output. I hope you find it helpful.
Now, if I may, you also posted and deleted a series of questions at
StackOverflow which, and please take this is as constructive criticism,
exhibited a bit of confusion about how R and Rcpp work with extensions,
extending to RcppArmadillo.
Briefly, *EVERYTHING* goes in and out as SEXP so you have no chance to detect
'a dense matrix'. You see a REALSXP as that is all there is -- every REAL aka
'double' is a vector, if it happens to have a two element dimension it is a
matrix. For R, there is no difference between Rcpp::NumericMatrix and
arma::mat as they all come in in the one form possible: REALSXP. Ditto for a
sp_mat: It is S4, and you get to test the class attribute. The Rcpp Gallery
examples show you how to do this. (And my favourite trick is to also study
the unit tests in RcppArmadillo which also have small, self-contained *and
working* examples.)
So please do a bit more reading, and if I were you, work on small examples to
convince yourself you have it right. If you don't it is not necessarily the
fault of RcppArmadillo though bugs on our side are always possible.
Regards, Dirk
#### Code
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo/Light>
// [[Rcpp::export]]
void accessEntries(Rcpp::List x){
for(int i=0; i<x.size(); i++){
arma::sp_mat ar = Rcpp::as<arma::sp_mat>(x[i]); //similar to : https://gallery.rcpp.org/articles/dynamic-dispatch-for-sparse-matrices/
}
}
/*** R
library(Matrix)
X <- as(cbind( c(1,2,3), c(3,4,5), c(5, 6, 7) ), "dgCMatrix")
spList <- list( X, X )
accessEntries(spList)
cat("All good\n")
*/
#### Output (with the extra lines we always get)
> Rcpp::sourceCpp("sparse_list_question.cpp")
> library(Matrix)
> X <- as(cbind( c(1,2,3), c(3,4,5), c(5, 6, 7) ), "dgCMatrix")
> spList <- list( X, X )
> accessEntries(spList)
> cat("All good\n")
All good
>
--
dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org
More information about the Rcpp-devel
mailing list