Discussion:
[Rcpp-devel] Negative vector lengths from rep_each()
Barth Riley
2018-10-29 14:12:10 UTC
Permalink
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
Dirk Eddelbuettel
2018-10-29 16:23:17 UTC
Permalink
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-***@lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
--
http://dirk.eddelbuettel.com | @eddelbuettel | ***@debian.org
Continue reading on narkive:
Search results for '[Rcpp-devel] Negative vector lengths from rep_each()' (Questions and Answers)
20
replies
which is the best way to achieve a flat tummy without losing weight overall?
started 2007-04-27 07:21:17 UTC
diet & fitness
Loading...