RcppArmadillo的样本是否需要预先实例化参数?

时间:2016-05-23 16:34:07

标签: c++ r rcpp

我在我的Rcpp代码中使用RcppArmadillo::sample,这在下面有这种奇怪的行为。 fun_good按预期工作,从x向量中抽取1个元素。但是,fun_bad不起作用,即使唯一的区别是我之前没有创建源向量x

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

using namespace Rcpp;

// [[Rcpp::export]]
IntegerVector fun_good() {
  IntegerVector x = seq_len(5);
  IntegerVector newOffer = RcppArmadillo::sample(x, 1, true);
  return newOffer;
}

// [[Rcpp::export]]
IntegerVector fun_bad() {
  IntegerVector newOffer = RcppArmadillo::sample(seq_len(5), 1, true);
  return newOffer;
}

错误消息为lvalue required as left operand of assignment,并指向以下来源。为什么ret[ii]无法分配fun_bad

 // copy the results into the return vector
        for (ii=0; ii<size; ii++) {
            jj = index[ii];
            ret[ii] = x[jj];
        }
        return(ret);

2 个答案:

答案 0 :(得分:4)

<强> TL; DR

进行显式转换(如cdeterman所做)或显式构造函数调用:

// [[Rcpp::export]]
Rcpp::IntegerVector fun_bad() {
    Rcpp::IntegerVector newOffer = 
        RcppArmadillo::sample(Rcpp::IntegerVector(seq_len(5)), 1, true);
    return newOffer;
}

不要引用我的具体细节,但我很确定你遇到了一个表达模板的边缘情况,不能很好地使用模板类型扣除规则。首先,我的编译器发出的错误消息的相关部分:

  

...在实例化'T Rcpp :: RcppArmadillo :: sample(const T&amp;,int,   bool,Rcpp :: NumericVector)[与T = Rcpp :: sugar :: SeqLen; ...

因此,在templated sample function中,T被推断为具有Rcpp::sugar::SeqLen类型。

SeqLen是一个表达式模板类 - defined here - 在大多数情况下,会(隐式)转换为Rcpp::IntegerVector(由于它从{{1}继承而来}})。例如,

Rcpp::VectorBase<INTSXP, ...>

但是,由于隐式转换是重载解析过程的一部分,而不是模板类型推导过程,因此// [[Rcpp::export]] Rcpp::IntegerVector test(int n = 5) { return Rcpp::seq_len(5); // Ok } 完全推导为T - 意味着此表达式

Rcpp::sugar::SeqLen

正在调用ret[ii] = x[jj]; (而 Rcpp::sugar::SeqLen::operator[],通常就是这种情况),这会产生一个右值(见下文†)。

您可能已经注意到,与某些ET Rcpp::Vector::operator[]类不同,sugar更像是一个“真正的”表达模板,因为它仅提供SeqLen进行延迟评估。它不存储常量引用数据成员/提供向量转换运算符(例如cumprod和许多其他运算符);如果我没有弄错的话,它实际上用于构造一个向量 - this constructor

operator[]

使用template <bool NA, typename VEC> Vector( const VectorBase<RTYPE,NA,VEC>& other ) { RCPP_DEBUG_2( "Vector<%d>( const VectorBase<RTYPE,NA,VEC>& ) [VEC = %s]", RTYPE, DEMANGLE(VEC) ) import_sugar_expression( other, typename traits::same_type<Vector,VEC>::type() ) ; } 类中定义的以下辅助方法:

Vector

无论如何,在Rcpp从// we are importing a real sugar expression, i.e. not a vector template <bool NA, typename VEC> inline void import_sugar_expression( const Rcpp::VectorBase<RTYPE,NA,VEC>& other, traits::false_type ) { RCPP_DEBUG_4( "Vector<%d>::import_sugar_expression( VectorBase<%d,%d,%s>, false_type )", RTYPE, NA, RTYPE, DEMANGLE(VEC) ) ; R_xlen_t n = other.size() ; Storage::set__( Rf_allocVector( RTYPE, n ) ) ; import_expression<VEC>( other.get_ref() , n ) ; } template <typename T> inline void import_expression( const T& other, int n ) { iterator start = begin() ; RCPP_LOOP_UNROLL(start,other) } 表达式自动生成实际向量对象之前,它不可用(至少以此特定表达式中所需的方式:sugar::SeqLen) 。

†作为一个完整性检查,我们可以使用一些C ++ 11元编程构造来检查ret[ii] = x[jj];SeqLen::operator[]的返回值之间的差异:

Vector::operator[]

答案 1 :(得分:2)

虽然我无法提供明确的原因,但如果您明确声明seq_len输出为IntegerVector,则函数将按预期编译并运行。

// [[Rcpp::export]]
IntegerVector fun_bad() {
  IntegerVector newOffer = RcppArmadillo::sample((IntegerVector)seq_len(5), 1, true);
  return newOffer;
}

我认为这是因为seq_len内的sample调用是临时对象,因此是rvalue。将结果显式地转换为IntegerVector似乎使其成为lvalue,因此它的工作原理。再次,为什么我不知道这种情况。

我确信如果你等了足够长的话,Dirk或者比我更有C ++知识的人会来最后给出一个更明确的答案。