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 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.”
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.r-project.org/web/packages/xtensor/index.html Dirk | 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 -- http://dirk.eddelbuettel.com | @eddelbuettel | e...@debian.org _______________________________________________ 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