code <- readLines(textConnection("SEXP res;
int nprotect = 0, nx, ny, nz, x, y;
PROTECT(res = Rf_duplicate(a)); nprotect++;
nx = INTEGER(GET_DIM(a))[0];
ny = INTEGER(GET_DIM(a))[1];
nz = INTEGER(GET_DIM(a))[2];
double sigma2 = REAL(s)[0] * REAL(s)[0], d2 ;
double cx = REAL(centre)[0], cy = REAL(centre)[1], *data, *rdata;
for (int im = 0; im < nz; im++) {
data = &(REAL(a)[im*nx*ny]); rdata = &(REAL(res)[im*nx*ny]);
for (x = 0; x < nx; x++)
for (y = 0; y < ny; y++) {
d2 = (x-cx)*(x-cx) + (y-cy)*(y-cy);
rdata[x + y*nx] = data[x + y*nx] * exp(-d2/sigma2);
}
}
UNPROTECT(nprotect);
return res;
"))
x <- array(runif(50*50), c(50,50,1))
funx <- cfunction(signature(a="array", s="numeric", centre="numeric"), code)
res <- funx(a=x, s=10, centre=c(25,15))
image(res[,,1])
setCMethod("funy", signature(a="array", s="numeric", centre="numeric"), code, verbose=TRUE)
res <- funy(x, 10, c(35,35))
x11()
image(res[,,1])Run the code above in your browser using DataLab