overwrite all values of a sparse matrix in rcpp armadillo

Viewed 60

I'm getting started with rcpp and am not sure how to convert a portion of my code to c++

My process uses sparse matrices in which the elements are all operated on as vectors then inserted back into a sparse matrix. In R I can use

sparse_mat@x <- new_entries

Where sparse_mat is a dsCMatrix (columns based symmetric matrix) and new_entries is a vector of values ordered so as to match the matrix they are being inserted into.

Is such an overwriting process possible using rcpp Armadillo or Eigen? I want to be able to do it this way as then the rest of the operations are simply vector manipulation which is much faster.

What I have tried to do so far is the following. The function is supposed to replace all the values in the sparse matrix with a vector (this is just for testing not the real function)

// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
using namespace Rcpp;

// [[Rcpp::export]]
arma::sp_mat sqrt2_(arma::sp_mat X, Rcpp::NumericVector a) {

// In order to access the internal arrays of the SpMat class
//X.sync();

std::copy(a.begin(), a.end(), arma::access::rwp(X.values));

return sqrt(X);
}

But it produces and error "elf_dynamic_array_reader" and "tag not found".

I am using the .sync method becuase it is suggested here as a way to access the matrix internals.

0 Answers
Related