Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从修改后的arma::vec对象高效采样_R_Rcpp_Armadillo - Fatal编程技术网

从修改后的arma::vec对象高效采样

从修改后的arma::vec对象高效采样,r,rcpp,armadillo,R,Rcpp,Armadillo,我正在使用Rcpp来加速一些R代码。然而,我真的很难处理类型-因为这些类型在R中是外来的。下面是我尝试做的一个简化版本: #include <RcppArmadillo.h> #include <algorithm> //[[Rcpp::depends(RcppArmadillo)]] using namespace Rcpp; using namespace arma; // [[Rcpp::export]] NumericVector fun(SEXP Pk, in

我正在使用Rcpp来加速一些R代码。然而,我真的很难处理类型-因为这些类型在R中是外来的。下面是我尝试做的一个简化版本:

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

// [[Rcpp::export]]
NumericVector fun(SEXP Pk, int k, int i, const vec& a, const mat& D) {
  // this is dummy version of my actual function - with actual arguments.;
  // I'm guessing SEXP is going to need to be replaced with something else when it's called from C++ not R.;
  return D.col(i);
}

// [[Rcpp::export]]
NumericVector f(const arma::vec& assignment, char k, int B, const mat& D) {
    uvec k_ind = find(assignment == k);
    NumericVector output(assignment.size());  // for dummy output.

    uvec::iterator k_itr = k_ind.begin();

    for(; k_itr != k_ind.end(); ++k_itr) {
        // this is R code, as I don't know the best way to do this in C++;
        k_rep = sample(c(assignment[assignment != k], -1), size = B, replace = TRUE);

        output = fun(k_rep, k, *k_itr, assignment, D);
        // do something with output;
    }

    // compile result, ultimately return a List (after I figure out how to do that.  For right now, I'll cheat and return the last output);
    return output;
}
#包括
#包括
//[[Rcpp::depends(RcppArmadillo)]]
使用名称空间Rcpp;
使用arma;
//[[Rcpp::导出]]
数字向量乐趣(SEXP Pk、int k、int i、const vec&a、const mat&D){
//这是我的实际函数的虚拟版本-带有实际参数。;
我猜,当从C++调用而不是R时,需要用别的东西代替。
返回D.col(i);
}
//[[Rcpp::导出]]
数值向量f(常数arma::向量与赋值、字符k、整数B、常数mat&D){
uvec k_ind=find(赋值==k);
NumericVector输出(assignment.size());//用于虚拟输出。
uvec::迭代器k_itr=k_ind.begin();
对于(;k_itr!=k_ind.end();++k_itr){
这是R代码,因为我不知道C++中最好的方法。
k_rep=sample(c(赋值[assignment!=k],-1),size=B,replace=TRUE);
输出=乐趣(k_rep,k,*k_itr,赋值,D);
//对输出做一些事情;
}
//编译结果,最终返回一个列表(在我弄明白怎么做之后。现在,我将作弊并返回最后的输出);
返回输出;
}
我正在努力解决的部分是
作业的随机抽样问题
。我知道
sample
已在
Rarmadillo
中实现。然而,我可以看到两种方法,我不确定哪一种更有效/可行

方法1:
  • 制作一个
    赋值表
    值。用-1替换
    assignment==k
    ,并将其“计数”设置为1
  • 从“名称”表中抽取样本,概率与计数成比例
方法2:
  • 赋值
    向量的相关子集复制到一个新向量中,并为-1添加一个额外的点
  • 以相同的概率从复制的向量中采样
我想说的是,方法1将更有效,除了
赋值
目前属于
arma::vec
类型,我不确定如何从该类型生成表格,或者将其转换为更兼容的格式需要多少成本。我想我可以实现方法2,但我希望避免昂贵的拷贝


感谢您提供的任何见解。

许多变量声明与您所做的赋值不一致,比如赋值=k是不可能比较的,因为赋值有实际值,而k是字符。由于queston写得不好,我可以随意更改变量类型。这是我的汇编

// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
#include <RcppArmadilloExtensions/sample.h>

// [[Rcpp::export]]
arma::vec fun(const Rcpp::NumericVector& Pk, int k, unsigned int i, const arma::ivec& a, const arma::mat& D)
{
  return D.col(i);
}

// [[Rcpp::export]]
Rcpp::NumericMatrix f(const arma::ivec& assignment, int k, unsigned int B, const arma::mat& D) 
{
  arma::uvec k_ind = find(assignment == k);
  arma::ivec KK = assignment(find(assignment != k));
  //these 2 row are for KK = c(assignment[assignment != k], -1)  
  //I dont know what is this -1 is for, why -1 ? maybe you dont need it.
     KK.insert_rows(KK.n_rows, 1);
     KK(KK.n_rows - 1) = -1;

  arma::uvec k_ind_not = find(assignment != k);
  Rcpp::NumericVector k_rep(B);
  arma::mat output(D.n_rows,k_ind.n_rows);  // for dummy output.

  for(unsigned int i =0; i < k_ind.n_rows ; i++) 
  {
    k_rep = Rcpp::RcppArmadillo::sample(KK, B, true);

    output(arma::span::all, i) = fun(k_rep, k, i, assignment, D);
    // do something with output;
  }

  // compile result, ultimately return a List (after I figure out how to do that.  For right now, I'll cheat and return the last output);
  return Rcpp::wrap(output);
}
/[[Rcpp::depends(RcppArmadillo)]]
#包括
#包括
//[[Rcpp::导出]]
arma::vec fun(常数Rcpp::数值向量与主键,整数k,无符号整数i,常数arma::ivec&a,常数arma::mat&D)
{
返回D.col(i);
}
//[[Rcpp::导出]]
Rcpp::NumericMatrix f(常数arma::ivec和赋值,整数k,无符号整数B,常数arma::mat和D)
{
arma::uvec k_ind=find(赋值==k);
arma::ivec KK=赋值(find(assignment!=k));
//这两行用于KK=c(赋值[assignment!=k],-1)
//我不知道这是什么-1,为什么-1?也许你不需要它。
KK.插入_行(KK.n_行,1);
KK(KK.n_行-1)=-1;
arma::uvec k_ind_not=find(赋值!=k);
Rcpp::数值向量k_rep(B);
arma::mat输出(D.n_行,k_ind.n_行);//用于虚拟输出。
for(无符号整数i=0;i
这不是优化的(因为问题是假的),这写得很糟糕,因为我认为R在搜索向量的索引时会足够快(所以在R中这样做,在Rcpp中实现只是有趣的)…在这里浪费时间是没有用的,还有其他问题需要在Rcpp中实现解算器,而不是这个搜索东西。。。
但这并不是一个有用的问题,因为您对算法的要求比函数签名更高,您可以直接在Rcpp中使用
sample
。请参阅本页底部:我发现R-C++混合语言相当混乱。你能提供一个只包含示例输入和预期输出的R解决方案吗?@MelissaKey:我建议你重新阅读RcppArmadillo
sample
函数上的帖子,然后重新编写你的问题。现在我们无法帮助你。谢谢你的努力。我正在努力解决这个问题——当我提出更具体的问题时,我无法通过你们提供的大量资源找到答案,我会问他们。