I'm trying to replicate the following R function in C++ using Rcpp and RcppArmadillo:
function (M, t) {
I = diag(nrow(M))
return(I %*% expm(t*M))
}
where M
is a Sparse squared Matrix of class "dgCMatrix"
in the Matrix package and t
is just a -positive- integer.
I'm new to C++ and Rcpp/RcppArmadillo, but this is what I have so far:
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
using namespace Rcpp;
arma::mat tp(const arma::sp_mat X, const int t) {
int n = X.n_rows, k = X.n_cols;
arma::mat I = arma::eye(n, k);
arma::mat out = I * arma::expmat(t*X);
return out;
}
// [[Rcpp::export]]
arma::mat tpf(SEXP Xr, const int t) {
return tp(as<arma::sp_mat>(Xr), t);
}
When I try to compile using sourceCpp
, I get the following:
clang++ -arch x86_64 -ftemplate-depth-256 -stdlib=libstdc++ -I/Library/Frameworks/R.framework/Resources/include -I/usr/local/include -I/usr/local/include/freetype2 -I/opt/X11/include -I"/Library/Frameworks/R.framework/Versions/3.2/Resources/library/Rcpp/include" -I"/Library/Frameworks/R.framework/Versions/3.2/Resources/library/RcppArmadillo/include" -I"/Users/ignacioquintero/repos/env_tr" -fPIC "-mtune=native -O3 -Wall -pedantic -Wconversion" -c tp.cpp -o tp.o
Error in sourceCpp(file = "~/repos/env_tr/tp.cpp") :
Error 1 occurred building shared library.
tp.cpp:8:22: error: no matching function for call to 'expmat'
arma::mat out = I * arma::expmat(t*X);
^~~~~~~~~~~~
/Library/Frameworks/R.framework/Versions/3.2/Resources/library/RcppArmadillo/include/armadillo_bits/fn_expmat.hpp:18:1: note: candidate template ignored: could not match 'Base' against 'SpOp'
expmat(const Base<typename T1::elem_type,T1>& A)
^
1 error generated.
make: *** [tp.o] Error 1
I have used arma::expmat
before successfully; is there maybe some problem with this function for arma::sp_mat
type?
Any help is appreciated