[Rcppdevel] Negative vector lengths from rep_each()
Dirk Eddelbuettel
edd at debian.org
Mon Oct 29 17:23:17 CET 2018
On 29 October 2018 at 09:12, Barth Riley wrote:
 Dear list

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

 NumericVector arrayFromVector(NumericVector input, IntegerVector dim) {

 // check for zerolength 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.”
That probably means integer overflow. Integers are "just" 32 bit so this may
happen. R itself uses an "extended" type for this, which is really a double
with larger range.
I would recommend you test for that value.
 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.
It's not the number of values, but their values.
 Any suggestions?
If you need multidimensional arrays and can use C++14 you may find the
xtensor package useful:
https://cran.rproject.org/web/packages/xtensor/index.html
Dirk
 Thanks in advance

 Barth


 Sent from Mail for Windows 10

 _______________________________________________
 Rcppdevel mailing list
 Rcppdevel at lists.rforge.rproject.org
 https://lists.rforge.rproject.org/cgibin/mailman/listinfo/rcppdevel

http://dirk.eddelbuettel.com  @eddelbuettel  edd at debian.org
More information about the Rcppdevel
mailing list