Problem making simple loop
Hello
I'm completely new to C++ and only use it through R. Anyway I'm trying to make a loop - since looping in R is rather inefficient:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
cppFunction('
NumericMatrix sim(NumericMatrix Z, double sigma_0,long double delta,long double omega,long double gamma){
int nrow=Z.nrow(), ncol=Z.ncol();
NumericMatrix out(nrow,ncol);
for (int q=0; q<ncol; q++) {out(0,q)=sigma_0;
}
for (int i=0; i<ncol; i++){
for (int j=1; j<nrow; j++){
long double z=Z(j-1,i);
long double sigma=out(j-1,i);
long double e=z*sigma;
long double w=abs(e)-gamma*e;
out(j,i)=w;
}
}
return out;
}
')
|
this should give me a function in R that is executed in C++ and then I give it a matrix Z and some arguments - so this is what I type in R:
1 2
|
Z<-matrix(1:12,nrow=4,byrow=TRUE)
sim(Z, sigma_0=9,delta=0.5,omega=4,gamma=0.5)
|
everything works out fine except for the last spot in the returned matrix giving me a result of 181.75 which should have been
abs(40.5*9)-0.5*9*40.5=182.25
Last edited on
Topic archived. No new replies allowed.