Rcpp - 用二进制函数转换 NumericVector?
Rcpp - transform NumericVector with binary function?
我正在考虑通过用 C++ 重写并通过 Rcpp 集成来加速一些 R 代码。至少可以说,我的 Cpp 生锈了:所以我很感激任何建议。特别是,我正在寻找有关将函数映射到 Rcpp NumericVector
的所有元素的指针。这是一个例子。
我需要生成一个新的向量如下:
- 对现有
NumericVector
进行 tail
切片;
- 用除数除以新切片的每个元素
到目前为止我有这个:
// [[Rcpp::export]]
NumericVector cppAdjustProbabilities( NumericVector& currentProbs,
const int index,
const double divisor ) {
//Note index <=0, e.g. -1 means remove first element
if(index == 0) {
return(currentProbs);
} else {
NumericVector newProbs = no_init(currentProbs.size()+index);
NumericVector::iterator i = currentProbs.begin() - index;
NumericVector::iterator j = newProbs.begin();
for(; i != currentProbs.end(); ++i, ++j) {
*j=*i/divisor;
}
return(newProbs);
}
}
这可行,但我更愿意使用 "map" 方法。我查看了 std::transform
,但它只支持对向量元素的一元运算 - 所以我看不到如何传递除数。例如,这是无效的:
std::transform(currentProbs.begin()-index, currentProbs.end(),
newProbs.begin(), [](double val) { return (val / divisor);} );
有没有办法将 divisor
带入 lambda 的作用域?或者另一种方法?
谢谢
使用 c++ lambda functions 您可以捕获这样的值:
src1 <- 'NumericVector cppAdjustProbabilities( NumericVector& currentProbs,
const int index,
const double divisor ) {
//Note index <=0, e.g. -1 means remove first element
if(index == 0) {
return(currentProbs);
} else {
NumericVector newProbs = no_init(currentProbs.size()+index);
std::transform(currentProbs.begin()-index, currentProbs.end(),
newProbs.begin(), [&divisor](double val) { return (val / divisor);} );
// ^^^^^^^^
return(newProbs);
}
}'
Rcpp::cppFunction(src1)
currentProbs <- c(0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9)
index <- -5L
divisor <- 2.0
cppAdjustProbabilities(currentProbs, index, divisor)
#> [1] 0.30 0.35 0.40 0.45
我正在考虑通过用 C++ 重写并通过 Rcpp 集成来加速一些 R 代码。至少可以说,我的 Cpp 生锈了:所以我很感激任何建议。特别是,我正在寻找有关将函数映射到 Rcpp NumericVector
的所有元素的指针。这是一个例子。
我需要生成一个新的向量如下:
- 对现有
NumericVector
进行tail
切片; - 用除数除以新切片的每个元素
到目前为止我有这个:
// [[Rcpp::export]]
NumericVector cppAdjustProbabilities( NumericVector& currentProbs,
const int index,
const double divisor ) {
//Note index <=0, e.g. -1 means remove first element
if(index == 0) {
return(currentProbs);
} else {
NumericVector newProbs = no_init(currentProbs.size()+index);
NumericVector::iterator i = currentProbs.begin() - index;
NumericVector::iterator j = newProbs.begin();
for(; i != currentProbs.end(); ++i, ++j) {
*j=*i/divisor;
}
return(newProbs);
}
}
这可行,但我更愿意使用 "map" 方法。我查看了 std::transform
,但它只支持对向量元素的一元运算 - 所以我看不到如何传递除数。例如,这是无效的:
std::transform(currentProbs.begin()-index, currentProbs.end(),
newProbs.begin(), [](double val) { return (val / divisor);} );
有没有办法将 divisor
带入 lambda 的作用域?或者另一种方法?
谢谢
使用 c++ lambda functions 您可以捕获这样的值:
src1 <- 'NumericVector cppAdjustProbabilities( NumericVector& currentProbs,
const int index,
const double divisor ) {
//Note index <=0, e.g. -1 means remove first element
if(index == 0) {
return(currentProbs);
} else {
NumericVector newProbs = no_init(currentProbs.size()+index);
std::transform(currentProbs.begin()-index, currentProbs.end(),
newProbs.begin(), [&divisor](double val) { return (val / divisor);} );
// ^^^^^^^^
return(newProbs);
}
}'
Rcpp::cppFunction(src1)
currentProbs <- c(0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9)
index <- -5L
divisor <- 2.0
cppAdjustProbabilities(currentProbs, index, divisor)
#> [1] 0.30 0.35 0.40 0.45