r语言 - Rcpp 编译器使用返回值标记错误 - 无能为力



我试图用Rcpp函数加速一些R代码。 一个函数给了我适合编译的机会,我不知道为什么编译器抱怨返回参数。我声明了返回 NumericVector 的函数,结果是 NumericVector,但编译器抱怨返回参数无效。

RCPP 是版本 0.12.18, R 是Microsoft打开 R 3.5.3

cppFunction('NumericVector NNE(IntegerVector X, IntegerVector Y, IntegerVector XY, IntegerVector xy, NumericVector P, int radius ) {
int n = X.size();
NumericVector vN[n];
NumericVector vSum[n];
NumericVector vAvg[n];

// for each xy determine neighborhood Sum and count (N)
for(int i=0; i<n; i++) {
vN[i] = 0.0;
vSum[i] = 0.0;
// traverse neighborhood, if the xy exists in the input
// vector then accumulate the values, otherwise ignore
for(int dx=-1*radius; dx<=radius; dx++) {
for(int dy=-1*radius; dy<=radius; dy++) {
// construct an xy index for the neighborhood die
xy[0] = ( (X[i]+dx) * 10000 ) + (Y[i]+dy);
// check to see if index above exists in input set
IntegerVector m = Rcpp::match(xy, XY);
// if valid then accumulate and count
if(m[0] != NA_INTEGER) {
vN[i] = vN[i] + 1.0;
vSum[i] = vSum[i] + P[ m[0] ];
}
}
}
vAvg[i] = vSum[i] / vN[i];
}
return vAvg;
}')

令人困惑的编译器消息如下:

C:/RBuildTools/3.5/mingw_64/bin/g++ -m64 -I"C:/PROGRA~1/MICROS~3/ROPEN~1/R-35~1.3/include" -DNDEBUG   -I"D:/Users/ka/Documents/R/win-library/3.5/Rcpp/include" -I"D:/Users/ka/AppData/Local/Temp/4/RtmpeGKfUg/sourceCpp-x86_64-w64-mingw32-0.12.18"   -I"C:/a/w/1/s/vendor/extsoft/include"     -O2 -Wall  -mtune=core2 -c filefcc651c7fa9.cpp -o filefcc651c7fa9.o
filefcc651c7fa9.cpp: In function 'Rcpp::NumericVector NNE(Rcpp::IntegerVector, Rcpp::IntegerVector, Rcpp::IntegerVector, Rcpp::IntegerVector, Rcpp::NumericVector, int)':
filefcc651c7fa9.cpp:42:10: error: invalid conversion from 'Rcpp::NumericVector* {aka Rcpp::Vector<14, Rcpp::PreserveStorage>*}' to 'const int&' [-fpermissive]
return vAvg;
^
In file included from D:/Users/ka/Documents/R/win-library/3.5/Rcpp/include/Rcpp/Vector.h:52:0,
from D:/Users/ka/Documents/R/win-library/3.5/Rcpp/include/Rcpp.h:40,
from filefcc651c7fa9.cpp:1:
D:/Users/ka/Documents/R/win-library/3.5/Rcpp/include/Rcpp/vector/Vector.h:128:5: note: initializing argument 1 of 'Rcpp::Vector<RTYPE, StoragePolicy>::Vector(const int&) [with int RTYPE = 14; StoragePolicy = Rcpp::PreserveStorage]'
Vector( const int& size ) {
^
make: *** [C:/PROGRA~1/MICROS~3/ROPEN~1/R-35~1.3/etc/x64/Makeconf:215: filefcc651c7fa9.o] Error 1
Error in sourceCpp(code = code, env = env, rebuild = rebuild, cacheDir = cacheDir,  : 
Error 1 occurred building shared library.

就编译器而言您有一个微小的错误,使变量"坏",然后你误解了"坏"变量的拒绝返回是一个不同的问题。

它发生了。我们都去过那里。

这是修复的代码。简而言之,您需要使用圆形而不是方形括号NumeriVector x(n);(因为后者表示 C 中的数组,然后是 C++)。

法典

我还将其转换为sourceCpp()输入,考虑到函数的长度,这更容易。

#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector NNE(IntegerVector X, IntegerVector Y, IntegerVector XY,
IntegerVector xy, NumericVector P, int radius ) {
int n = X.size();
NumericVector vN(n);
NumericVector vSum(n);
NumericVector vAvg(n);
// for each xy determine neighborhood Sum and count (N)
for(int i=0; i<n; i++) {
vN[i] = 0.0;
vSum[i] = 0.0;
// traverse neighborhood, if the xy exists in the input
// vector then accumulate the values, otherwise ignore
for(int dx=-1*radius; dx<=radius; dx++) {
for(int dy=-1*radius; dy<=radius; dy++) {
// construct an xy index for the neighborhood die
xy[0] = ( (X[i]+dx) * 10000 ) + (Y[i]+dy);
// check to see if index above exists in input set
IntegerVector m = Rcpp::match(xy, XY);
// if valid then accumulate and count
if(m[0] != NA_INTEGER) {
vN[i] = vN[i] + 1.0;
vSum[i] = vSum[i] + P[ m[0] ];
}
}
}
vAvg[i] = vSum[i] / vN[i];
}
return vAvg;
}
/*** R
cat("Builtn")
*/

输出

由于我们没有参考数据,我只能证明它构建了:

R> sourceCpp("~/git/stackoverflow/61377960/answer.cpp")
R> cat("Builtn")
Built
R> 

最新更新