Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sparse matrix conversion from Matrix to armadillo with RcppArmadilloExtensions/sp_mat.h

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:

  1. I created an empty package with RcppArmadillo.package.skeleton()
  2. I defined 2 .cpp files with their corresponding headers .h to perform sum and product of sparse matrices imported from R, as follows :

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.

like image 369
Julien Chiquet Avatar asked Aug 20 '13 13:08

Julien Chiquet


1 Answers

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)
*/
like image 173
Romain Francois Avatar answered Nov 13 '22 20:11

Romain Francois