0

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

iq447
  • 11
  • 4
  • 2
    The documentation for `arma::expmat` says "If X is not square, a std::logic_error exception is thrown". – nrussell Jun 30 '15 at 21:16
  • Yes, I forgot to mention it is a Square matrix (so the n, k might be redundant). – iq447 Jul 01 '15 at 14:45
  • Okay, and on second thought I'm not sure my comment is actually relevant since the `std::logic_error` would be a *run time* issue, rather than a *compile time* issue, as you experienced. However, based on the compiler error I would guess that `expmat` does not have a function signature compatible with `sp_mat` objects. I'm trying to track down the source code for this at the moment. – nrussell Jul 01 '15 at 15:10
  • It seems that `sp_mat` is not one of the several `arma::` types that can be passed into `expmat` directly, but a quick fix would be to use `arma::expmat(arma::conv_to::from(t*X))`. – nrussell Jul 01 '15 at 15:59
  • 1
    This works great, thank you! – iq447 Jul 01 '15 at 16:11

0 Answers0