首页 > 解决方案 > RcppArmadillo:for循环中的负索引

问题描述

for()我是 Rcpp 的新手,我正在尝试使用 RcppArmadillo在循环中基于负索引执行计算。我已经发现 RcppArmadillo 中的负索引并不是那么简单,但是可以通过应该保留的元素向量来完成(正如我在此处找到的那样)。当要删除的元素是循环索引时,这对我来说似乎有点困难。我试图在这个答案中实施最后一种方法,但没有成功。有没有一种简单的方法来指定元素的向量,不包括具有循环索引的元素?

因此,我试图y[-i]在以下 MWE 中找到 RcppArmadillo 中的等价物:

# In R:
# Input 
n <- 10
y <- 1:20

# Computation
x <- rep(NA, n)
for(i in 1:n){
x[i] <- sum(y[-i])
}

到目前为止我的代码 Rcpp 代码:

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

// [[Rcpp::export]]
arma::vec rcpp_sum (arma::vec y, int n){

arma::vec x(n);
arma::vec ind(n);

for (i=0; i<n; i++){
ind[i] = /*no idea...*/
x[i] = sum(y[ind]);
}

return x;
}

任何帮助是极大的赞赏!

标签: loopsindexingrcpprcpparmadillo

解决方案


对于这样的任务,最好跳过有问题的索引。在标准C++中,我们会检查索引并跳过它。像这样的东西:

// [[Rcpp::export]]
arma::vec rcpp_sum (arma::vec y, int n){
    
    arma::vec x(n);
    
    for (int i = 0; i < n; i++) {
        x[i] = 0; // Initialize value
        
        for (int j = 0; j < y.size(); ++j) {
            if (i != j) {
                x[i] += y[j];
            }
        }
    }
    
    return x;
}

在上面,我们正在远离糖语法。IMO 在这种情况下没关系,因为替代方案并不过分复杂。虽然我们正在简化,但依赖于RcppArmadillo不是必需的,因为我们可以使用 pureRcpp

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
NumericVector pure_rcpp_sum (NumericVector y, int n){
    
    NumericVector x(n);
    
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < y.size(); ++j) {
            if (i != j) {
                x[i] += y[j];
            }
        }
    }
    
    return x;
}

验证输出:

all.equal(as.vector(rcpp_sum(y, n)), x)
[1] TRUE

all.equal(pure_rcpp_sum(y, n), x)
[1] TRUE

更新

根据 OP 的要求,我们R为此特定目的提供了一种优化的方法。上面演示了如何解决一个非常具体的问题,即只对向量中的值求和,而在C++. 这本质上是教学性的,不一定是完成这项特定任务的最佳方式(如下所示)。

在我们展示简单的R代码之前,我想指出,OP 担心在内部循环中有一个简单的条件语句是C++不应该担心的(就像在 base 中的情况一样R)。据我所知,在 OP 的链接中展示的子集是 O(n) 并且具有额外逻辑向量的额外开销。我们上面提出的解决方案应该更有效,因为它基本上可以在没有额外对象的情况下做同样的事情。

现在,对于更新的代码:

baseR <- function(y, n) {
    mySum <- sum(y)
    vapply(1:n, function(x) mySum - y[x], FUN.VALUE = 1)    
}

## Here is the OP code for reference
OP <- function(y, n) {
    x <- rep(NA, n)
    for(i in 1:n) {x[i] <- sum(y[-i])}
    x
}

而已。它也快如闪电:

huge_y <- rnorm(1e6)
huge_n <- 1e3

system.time(t1 <- baseR(huge_y, huge_n))
 user  system elapsed 
 0.003   0.000   0.003

system.time(t2 <- pure_rcpp_sum(huge_y, huge_n))
 user  system elapsed 
2.776   0.003   2.779 

system.time(t3 <- OP(huge_y, huge_n))
 user  system elapsed 
9.555   1.248  10.805

all.equal(t1, t2)
[1] TRUE

all.equal(t1, t3)
[1] TRUE

推荐阅读