Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将R代码更改为Rcpp代码_C++_R_Performance_Rcpp - Fatal编程技术网

C++ 将R代码更改为Rcpp代码

C++ 将R代码更改为Rcpp代码,c++,r,performance,rcpp,C++,R,Performance,Rcpp,因此,我编写了一些非常慢的R代码,我想使用Rcpp库将其转换为C++代码。然而,当我试图使用它时,我收到了一条错误消息,我似乎无法找到错误发生的地方。非常感谢您的帮助 以下是原始代码: #Necessary packages to run library(Rcpp) library(mvtnorm) #Here are the variables I will be working with x = c(0.53137100,0.75357474,0.87904120,0.29727488,

因此,我编写了一些非常慢的
R
代码,我想使用
Rcpp
库将其转换为
C++
代码。然而,当我试图使用它时,我收到了一条错误消息,我似乎无法找到错误发生的地方。非常感谢您的帮助

以下是原始代码:

#Necessary packages to run
library(Rcpp)
library(mvtnorm)

#Here are the variables I will be working with 
x = c(0.53137100,0.75357474,0.87904120,0.29727488,0.00000000,0.00000000,
      0.00000000,0.00000000,0.00000000,0.04059217)
y = c(4.873500,3.896917,1.258215,5.776484,12.475491,5.273784,13.803158,
    4.472204,2.629839,6.689242)
front = c(NA,NA,3,NA,NA,NA,NA,NA,9,NA)
all.preds = c(0.596905183,0.027696850,1.005666896,0.007688514,3.900000000)


x = x[!is.na(front)]
y = y[!is.na(front)]

mu = c(all.preds[1],all.preds[3])
sigma = matrix(c(all.preds[2],0,0,all.preds[4]),nrow=2)

z = rmvnorm(10000,mu,sigma)
z[,1] = sapply(z[,1],function(x){max(x,0)})

temp = 1:nrow(z)

for(i in 1:length(temp)){
    cond1 = z[i,2]!=min(z[which(z[,1]==z[i,1]),2])
    cond2 = z[i,1]!=min(z[which(z[,2]==z[i,2]),1])
        for(n in 1:length(x)){
                if((z[i,1]>x[n]  &  z[i,2]>y[n]) | (z[i,1]==x[n] & cond1) | (z[i,2]==y[n] & cond2)){
                    temp[i] = NA
                    break
                }
            }
        }
这是我写的新的
Rcpp
代码:

#Necessary packages to run
library(Rcpp)
library(mvtnorm)

#Here are the variables I will be working with 
x = c(0.53137100,0.75357474,0.87904120,0.29727488,0.00000000,0.00000000,
      0.00000000,0.00000000,0.00000000,0.04059217)
y = c(4.873500,3.896917,1.258215,5.776484,12.475491,5.273784,13.803158,
    4.472204,2.629839,6.689242)
front = c(NA,NA,3,NA,NA,NA,NA,NA,9,NA)
all.preds = c(0.596905183,0.027696850,1.005666896,0.007688514,3.900000000)


x = x[!is.na(front)]
y = y[!is.na(front)]

mu = c(all.preds[1],all.preds[3])
sigma = matrix(c(all.preds[2],0,0,all.preds[4]),nrow=2)

z = rmvnorm(10000,mu,sigma)
z[,1] = sapply(z[,1],function(x){max(x,0)})

cppFunction('
  int prop(NumericMatrix z, NumericVector x, NumericVector y) {
  int nrow = z.nrow();
  int n = x.size();
  int temp;  

  for (int i = 0; i < nrow; i++) {
    bool cond1 = z(i,2)!=min(z(which(z(,1)==z[i,1]),2));
    bool cond2 = z(i,1)!=min(z(which(z(,2)==z[i,2]),1));

    for (int j; j < n; j++) {
      if((z(i,1)>x[n]  &&  z(i,2)>y[n]) || (z(i,1)==x[n] && cond1) || (z(i,2)==y[n] && cond2)) {
        temp[i] = 0;
        break;
      }
    }
  }
   return temp;
  }
  ')

错误消息,您可以格式化或缩进,告诉您在C++中没有这样的东西<代码>()<代码>。哪个是正确的。除了
Which()
命令之外,其他一切都正确吗?另外,是否有一个与
which()
命令等效的函数或我可以绕过它的方法?此外,在
中,您使用了
z(,1)
z(,2)
语句,其中您指的是
z[,1]
z[,2]
,这是其他编译器错误的来源。
g++ -m64 -I"C:/PROGRA~1/R/R-30~1.1/include" -DNDEBUG     -I"C:/Users/BabakP/Documents/R/win-library/3.0/Rcpp/include"  -I"d:/RCompile/CRANpkg/extralibs64/local/include"     -O2 -Wall  -mtune=core2 -c file7444c995de6.cpp -o file7444c995de6.o file7444c995de6.cpp: In function 'int prop(Rcpp::NumericMatrix, Rcpp::NumericVector, Rcpp::NumericVector)': file7444c995de6.cpp:13:40: error: expected primary-expression before ',' token file7444c995de6.cpp:13:49: warning: left operand of comma operator has no effect [-Wunused-value] file7444c995de6.cpp:13:51: error: 'which' was not declared in this scope file7444c995de6.cpp:14:40: error: expected primary-expression before ',' token file7444c995de6.cpp:14:49: warning: left operand of comma operator has no effect [-Wunused-value] file7444c995de6.cpp:18:15: error: invalid types 'int[int]' for array subscript make: *** [file7444c995de6.o] Error 1 
Error in sourceCpp(code = code, env = env, rebuild = rebuild, showOutput = showOutput,  : 
  Error 1 occurred building shared library.