我正在开发一些功能Rcpp上进行操作big.matrix,从对象bigmemory包.这些对象Rcpp作为SEXP对象传递给对象,然后我必须将其转换为对象XPtr<BigMatrix>,然后转换为MatrixAccessor对象以访问矩阵的元素.
例如,如果我想实现一个得到对角线的函数:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::depends(BH, bigmemory)
#include <bigmemory/MatrixAccessor.hpp>
#include <numeric>
// [[Rcpp::export]]
NumericVector GetDiag(SEXP pmat) {
XPtr<BigMatrix> xpMat(pMat); // allows you to access attributes
MatrixAccessor<double> mat(*xpMat); // allows you to access matrix elements
NumericVector diag(xpMat->nrow()); // Assume matrix is square
for (int i = 0; i < xpMat->nrow(); i++) {
diag[i] = mat[i][i];
}
return diag;
}
Run Code Online (Sandbox Code Playgroud)
如果big.matrixR中的对象充满双打,则此功能可以很好地工作.
但是,如果在整数矩阵(例如diag(as.big.matrix(matrix(1:9, …
rcpp ×1