I am trying to use the very recent capability of the RcppArmadillo package (version 0.3.910.0 with R 3.0.1 and evrerything up to date) for conversion of a sparse matrix from the Matrix package (class "dgCMatrix") to the sp_mat type of armadillo. I am using the "as" and "wrap" functions from the file "RcppArmadilloExtensions/spmat.h". Unfortunately, I am experiencing a compilation error while trying to create the shared library .so when invoking "R CMD INSTALL myRpackage".
Here is a minimal example to reproduce the error:
file "arma_sp_sum.h"
#ifndef _anRpackage_ARMA_SP_SUM_H
#define _anRpackage_ARMA_SP_SUM_H
#include <RcppArmadilloExtensions/spmat.h>
RcppExport SEXP arma_sp_prod(SEXP SPMAT) ;
#endif
file "arma_sp_sum.cpp"
#include "arma_sp_sum.h"
using namespace Rcpp ;
SEXP arma_sp_sum(SEXP SPMAT){
arma::sp_mat m1 = Rcpp::as<arma::sp_mat>(SPMAT) ;
arma::sp_mat m2 = Rcpp::as<arma::sp_mat>(SPMAT) ;
arma::sp_mat res = m1 + m2;
return Rcpp::wrap(res) ;
}
file "arma_sp_prod.h"
#ifndef _anRpackage_ARMA_SP_PROD_H
#define _anRpackage_ARMA_SP_PROD_H
#include <RcppArmadilloExtensions/spmat.h>
RcppExport SEXP arma_sp_prod(SEXP SPMAT) ;
#endif
file "arma_sp_prod.cpp"
#include "arma_sp_prod.h"
using namespace Rcpp ;
SEXP arma_sp_prod(SEXP SPMAT){
arma::sp_mat m1 = Rcpp::as<arma::sp_mat>(SPMAT) ;
arma::sp_mat m2 = Rcpp::as<arma::sp_mat>(SPMAT) ;
arma::sp_mat res = m1 * m2;
return Rcpp::wrap(res) ;
}
Then, when running $ R CMD INSTALL anRpackage $, the compiler successively creates the ".o" files but I get the following ld error :
ld: duplicate symbol arma::SpMat<double> Rcpp::as<arma::SpMat<double> >(SEXPREC*)in arma_sp_sum.o and arma_sp_prod.o for architecture x86_64
collect2: ld returned 1 exit status
make: *** [anRpackage.so] Error 1
ERROR: compilation failed for package ‘anRpackage’
So what am I doing wrong? Sorry if it is a silly question... Anyway, thanks to all the guys doing such a good job with armadilllo/RcppArmadillo, and for your help.
J.
I have made a few changes to RcppArmadillo
to clean this. Now as
and wrap
are correctly templated for sparse matrix types from armadillo
(arma::SpMat<T>
).
Can you try again using the RcppArmadillo
from svn ?
Also, now, you should only need
#include <RcppArmadillo.h>
With the updated code, I'm able to compile your package as well as something like this :
#include <RcppArmadillo.h>
// [[Rcpp::depends("RcppArmadillo")]]
using namespace Rcpp ;
// [[Rcpp::export]]
arma::sp_mat sparse( arma::sp_mat A ){
A(0,0) = 1;
A(1,0) = 2;
return A ;
}
/*** R
require(Matrix)
m <- Matrix(c(0,0,2:0), 3,5)
sparse(m)
*/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With