覆盖RCPP Armadillo中稀疏矩阵的所有值

发布于 2025-02-11 02:26:04 字数 974 浏览 1 评论 0原文

我开始使用RCPP,不确定如何将代码的一部分转换为C ++

我的过程使用稀疏的矩阵,其中元素全部作为向量操作,然后将其插入稀疏的矩阵中。在RI中可以使用

sparse_mat@x <- new_entries

sparess_mat是DSCMATRIX(基于列的对称矩阵),而new_entries是订购的值的向量,以匹配它们正在插入的矩阵。

使用RCPP Armadillo或eigen可能会覆盖这样的过程吗?我希望能够这样做,因为其余的操作只是矢量操作,这要快得多。

到目前为止,我试图做的就是以下内容。该函数应该用向量替换稀疏矩阵中的所有值(这仅是用于测试实际函数),

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

但它会产生和错误“ ELF_DYNAMIC_ARRAY_READER”和“找不到标签”。

我正在使用.sync方法,因为建议在这里作为访问矩阵内部的一种方式。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文