Dear list

I have written the following function to produce multidimensional arrays using 
Rcpp:

NumericVector arrayFromVector(NumericVector input, IntegerVector dim) { 
  
  // check for zero-length vectors
  if(dim.length() == 0 || input.length() == 0) {
    return NULL;
  }
  
  long p = 1;
  for(int i = 0; i < dim.length(); i++) {
    p *= dim[i];
  }
  
  if((input.length() == 1) & (p > 1)) {
         input = Rcpp::rep_each(input, p);
  } else if (input.length() != p) {
    return NULL;
  } 
  
  input.attr("dim") = dim;
  return input;
}


This function usually produces the desired result, but in some cases (it seems 
with relatively large vector lengths) I get the error “negative length vectors 
are not allowed.” The error occurs on the line:

    input = Rcpp::rep_each(input, p);

I changed p from an int to a long, thinking I was encountering an overflow 
issue. But this did not resolve the problem. The test vector contains 45,000 
values.

Any suggestions?

Thanks in advance

Barth


Sent from Mail for Windows 10

_______________________________________________
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

Reply via email to