I am trying to write a function that runs a loop in C ++ from R using Rcpp.
I have a Z matrix that is one row shorter than the OUT matrix that the function should return, because each position of the first OUT row will be given by scalar sigma_0.
It is assumed that the function implements a differential equation. Each iteration depends on the value of the matrix Z, as well as the previously generated value of the matrix OUT.
What I have:
cppFunction(' NumericMatrix sim(NumericMatrix Z, long double sigma_0, long double delta, long double omega, long double gamma) { int nrow = Z.nrow() + 1, 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); out(j, i) = pow(abs(z * sigma) - gamma * z * sigma, delta); } } return out; } ')
Unfortunately, I am sure this will not work. The function is executed, but the calculated values ββare incorrect - I checked with simple examples in Excel and simple R-coding. I divided the main differential equation, trying to gradually increase it to see when the implementation of Excel and R using C ++ starts to differ. It seems that when I start using the abs () function and the power () function, but I just can't narrow down the problem. Any help would be greatly appreciated - I can also mention that this is the first time I use C ++ and C ++ with R.
source share