Home > other >  Looping Through Large, Multidimensional Array Using Rcpp
Looping Through Large, Multidimensional Array Using Rcpp

Time:12-14

I am trying to create models that involve looping through large multidimensional arrays (ex: dimensions = 20 x 1000 x 60), which run very slow the way I code them in R. I downloaded Rcpp and have been trying to implement such a model, since C handles loops very well. Normally, I would write such a function in R as:

fun <- function(x,y,z){
  f <- array(0, dim = c(18,50,10));
  for (i in 1:18){
    for (j in 1:50){
      for (l in 1:10){
        f[i,j,l] <- (i*j/10)   l;
      }
    }
  }
  return(f[x,y,z])
}

and as expected the function yields:

> fun(10,20,5)
[1] 25

This is what I thought the equivalent code in Rcpp should look like:

cppFunction('
  double fun(int x, int y, int z){
  int f[18][50][10] = {0};
  for (int i = 1; i > 18; i  ){
    for (int j = 1; j > 50; j  ){
      for (int l = 1; l > 10; l  ){
        f[i][j][l] = (i * j/10)   l;
      }
    }
  }
  return f[x][y][z];
  }          
')

but I am getting 0's anytime I go to use the function.

> fun(10,20,5)
[1] 0 

The actual models I'll be implementing use backward iteration, so I do need the arrays as part of the function. Alternatively, returning the array itself would also work for my purposes, but I haven't had luck with that either.

Any help would be sincerely appreciated.

Thanks

CodePudding user response:

Remember that C is 0 indexed. You need to start your indexing at 0 rather than 1 as in R. You also need to make sure that your loops only continue while the value of i, j, and l are less than the dimensions of the array (so switch > for <. And your array needs to be an array of double, not int:

Rcpp::cppFunction('
  double fun(int x, int y, int z){
  double f[18][50][10] = {0};
  for (int i = 0; i < 18; i  ){
    for (int j = 0; j < 50; j  ){
      for (int l = 0; l < 10; l  ){
        f[i][j][l] = (i * j/10)   l;
      }
    }
  }
  return f[x][y][z];
  }          
')

Testing gives:

fun(10, 20, 5)
#> [1] 25
  • Related