RcppArmadillo中的稀疏矩阵求幂

时间:2015-06-30 21:01:57

标签: r sparse-matrix rcpp exponentiation

我正在尝试使用Rcpp和RcppArmadillo在C ++中复制以下R函数:

function (M, t) {
  I = diag(nrow(M)) 
  return(I %*% expm(t*M))
}

其中M是Matrix包中类"dgCMatrix"的稀疏平方矩阵,t只是一个正整数。

我是C ++和Rcpp / RcppArmadillo的新手,但这是我到目前为止所做的:

// [[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);
}

当我尝试使用sourceCpp进行编译时,我得到以下内容:

 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

我成功使用arma::expmat之前; arma::sp_mat类型的这个函数可能有问题吗?

感谢任何帮助

0 个答案:

没有答案