# HG changeset patch # User Jordi GutiƩrrez Hermoso # Date 1312300211 18000 # Node ID f7a8d1dafda3842df5cea4ae0b1e733983f862e7 # Parent 7cdf3934887979838f09f03fb80cf09679aee421 Let rand accept negative dimensions (bug #33301) * rand.cc (do_rand): Make it so that ranges, matrices, and individual arguments treat negative dimensions as zero. * data.cc (eye): Document that negative dimensions are treated as zero (rand's docstring references eye) diff -r 7cdf39348879 -r f7a8d1dafda3 src/DLD-FUNCTIONS/rand.cc --- a/src/DLD-FUNCTIONS/rand.cc Mon Aug 01 01:18:51 2011 +0300 +++ b/src/DLD-FUNCTIONS/rand.cc Tue Aug 02 10:50:11 2011 -0500 @@ -179,18 +179,16 @@ octave_idx_type incr = NINTbig (r.inc ()); octave_idx_type lim = NINTbig (r.limit ()); - if (base < 0 || lim < 0) - error ("%s: all dimensions must be positive", fcn); - else + for (octave_idx_type i = 0; i < n; i++) { - for (octave_idx_type i = 0; i < n; i++) - { - dims(i) = base; - base += incr; - } + //Negative dimensions are treated as zero for Matlab + //compatibility + dims(i) = base >= 0 ? base : 0; + base += incr; + } - goto gen_matrix; - } + goto gen_matrix; + } else error ("%s: all elements of range must be integers", @@ -208,15 +206,10 @@ for (octave_idx_type i = 0; i < len; i++) { + //Negative dimensions are treated as zero for Matlab + //compatibility octave_idx_type elt = iv(i); - - if (elt < 0) - { - error ("%s: all dimensions must be positive", fcn); - goto done; - } - - dims(i) = iv(i); + dims(i) = elt >=0 ? elt : 0; } goto gen_matrix; @@ -278,13 +271,14 @@ for (int i = 0; i < nargin; i++) { - dims(i) = args(idx+i).int_value (); - + octave_idx_type elt = args(idx+i).int_value (); if (error_state) { error ("%s: expecting integer arguments", fcn); goto done; } + //Negative is zero for Matlab compatibility + dims(i) = elt >= 0 ? elt : 0; } goto gen_matrix; diff -r 7cdf39348879 -r f7a8d1dafda3 src/data.cc --- a/src/data.cc Mon Aug 01 01:18:51 2011 +0300 +++ b/src/data.cc Tue Aug 02 10:50:11 2011 -0500 @@ -4187,8 +4187,8 @@ @end example\n\ \n\ Calling @code{eye} with no arguments is equivalent to calling it\n\ -with an argument of 1. This odd definition is for compatibility\n\ -with @sc{matlab}.\n\ +with an argument of 1. Any negative dimensions are treated as zero. \n\ +These odd definitions are for compatibility with @sc{matlab}.\n\ @seealso{speye}\n\ @end deftypefn") {