Rcpp编译器使用返回值标记错误-不确定
我试图用Rcpp函数来加速一些R代码。一个函数让我适合编译,我不知道为什么编译器会抱怨返回参数。我声明函数返回NumericVector,结果是NumericVector,但编译器抱怨返回参数无效 Rcpp版本为0.12.18, R是Microsoft Open R 3.5.3Rcpp编译器使用返回值标记错误-不确定,r,rcpp,R,Rcpp,我试图用Rcpp函数来加速一些R代码。一个函数让我适合编译,我不知道为什么编译器会抱怨返回参数。我声明函数返回NumericVector,结果是NumericVector,但编译器抱怨返回参数无效 Rcpp版本为0.12.18, R是Microsoft Open R 3.5.3 cppFunction('NumericVector NNE(IntegerVector X, IntegerVector Y, IntegerVector XY, IntegerVector xy, NumericVe
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;
}')
cppFunction('NumericVector NNE(整数向量X、整数向量Y、整数向量XY、整数向量XY、整数向量P、整数半径){
int n=X.size();
数值向量vN[n];
数值向量vSum[n];
数值向量vAvg[n];
//对于每个xy,确定邻域和和计数(N)
对于(int i=0;i就编译器而言,您在呈现变量“bad”时出现了一个微小的错误,然后您将“bad”变量的拒绝返回误解为另一个问题
是的,我们都去过
这是修复后的代码。简而言之,您需要numerivectorx(n);
使用round而不是squared paren(后者表示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("Built\n")
*/
就编译器而言,您在呈现变量“bad”时出现了一个很小的错误,然后您将“bad”变量的拒绝返回误解为另一个问题
是的,我们都去过
这是修复后的代码。简而言之,您需要numerivectorx(n);
使用round而不是squared paren(后者表示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("Built\n")
*/
你能添加一些样本输入和预期结果吗?你错过了一个早期的错误。你能添加一些样本输入和预期结果吗?你错过了一个早期的错误。哈利路亚!Dirk。你的帮助是最受赞赏的。我几周前买了你的书‘无缝R和C++集成WRCPP’。我对性能提升感到兴奋。Rcpp是PROVIi。丁。感谢您对R社区的及时帮助和无休止的贡献。非常欢迎您!如果我有更多的时间更新第二版……此外,根据StackOverflow惯例(顺便说一句,欢迎使用StackOverflow),请随意“向上投票”答案(单击向上三角形)并“接受”(点击原始海报所示的记号)这样,荣誉就流过系统,你也得到了一些点,接受一个答案。哈利路亚!Dirk,你的帮助是最受赞赏的。我几周前买了你的书‘无缝R和C++集成WRCPP’,我很激动Rcpp的提供。谢谢你的及时帮助和无尽的控制。但是对于R社区,你是非常受欢迎的!如果我有更多的时间更新第二版就好了……而且,根据StackOverflow惯例(顺便说一句,欢迎来到StackOverflow),请随意“向上投票”答案(点击向上三角形)并“接受它”(点击原始海报上的勾号).通过这种方式,荣誉在整个系统中流动,你接受答案的行为也会得到一些分数。
R> sourceCpp("~/git/stackoverflow/61377960/answer.cpp")
R> cat("Built\n")
Built
R>