# HG changeset patch # User John W. Eaton # Date 1581528086 18000 # Node ID e9d57f6d6353e73ca38e794df660495b3eb1de90 # Parent cf59b503db2ad0486872acb54421aa52408a22a4 allow sprand and sprandsym to create empty sparse matrices * sprandsym.m, __sprand__.m: Allow dimensions to be 0. * sprandsym.m, sprand.m, sprandn.m: Adjust tests. diff -r cf59b503db2a -r e9d57f6d6353 scripts/sparse/private/__sprand__.m --- a/scripts/sparse/private/__sprand__.m Wed Feb 12 12:20:49 2020 -0500 +++ b/scripts/sparse/private/__sprand__.m Wed Feb 12 12:21:26 2020 -0500 @@ -49,16 +49,21 @@ [m, n, d, rc, fcnname, randfun] = deal (varargin{:}); endif - if (! (isscalar (m) && m == fix (m) && m > 0)) - error ("%s: M must be an integer greater than 0", fcnname); + if (! (isscalar (m) && m == fix (m) && m >= 0)) + error ("%s: M must be a non-negative integer", fcnname); endif - if (! (isscalar (n) && n == fix (n) && n > 0)) - error ("%s: N must be an integer greater than 0", fcnname); + if (! (isscalar (n) && n == fix (n) && n >= 0)) + error ("%s: N must be a non-negative integer", fcnname); endif if (d < 0 || d > 1) error ("%s: density D must be between 0 and 1", fcnname); endif + if (m == 0 || n == 0) + S = sparse (m, n); + return; + endif + if (nargin == 5) mn = m*n; k = round (d*mn); diff -r cf59b503db2a -r e9d57f6d6353 scripts/sparse/sprand.m --- a/scripts/sparse/sprand.m Wed Feb 12 12:20:49 2020 -0500 +++ b/scripts/sparse/sprand.m Wed Feb 12 12:21:26 2020 -0500 @@ -93,16 +93,22 @@ %!test %! s = sprand (1e6, 1e6, 1e-7); +## Test empty array creation +%!assert (size (sprand (0, 0, 0.5)), [0, 0]) +%!assert (size (sprand (0, 3, 0.5)), [0, 3]) +%!assert (size (sprand (3, 0, 0.5)), [3, 0]) + ## Test input validation %!error sprand () %!error sprand (1, 2) %!error sprand (1, 2, 3, 4) -%!error sprand (ones (3), 3, 0.5) -%!error sprand (3.5, 3, 0.5) -%!error sprand (0, 3, 0.5) -%!error sprand (3, ones (3), 0.5) -%!error sprand (3, 3.5, 0.5) -%!error sprand (3, 0, 0.5) +%!error sprand (-1, -1, 0.5) +%!error sprand (ones (3), 3, 0.5) +%!error sprand (3.5, 3, 0.5) +%!error sprand (-1, 3, 0.5) +%!error sprand (3, ones (3), 0.5) +%!error sprand (3, 3.5, 0.5) +%!error sprand (3, -1, 0.5) %!error sprand (3, 3, -1) %!error sprand (3, 3, 2) %!error sprand (2, 2, 0.2, ones (3,3)) diff -r cf59b503db2a -r e9d57f6d6353 scripts/sparse/sprandn.m --- a/scripts/sparse/sprandn.m Wed Feb 12 12:20:49 2020 -0500 +++ b/scripts/sparse/sprandn.m Wed Feb 12 12:21:26 2020 -0500 @@ -92,16 +92,22 @@ %!test %! s = sprandn (1e6,1e6,1e-7); +## Test empty array creation +%!assert (size (sprandn (0, 0, 0.5)), [0, 0]) +%!assert (size (sprandn (0, 3, 0.5)), [0, 3]) +%!assert (size (sprandn (3, 0, 0.5)), [3, 0]) + ## Test input validation %!error sprandn () %!error sprandn (1, 2) %!error sprandn (1, 2, 3, 4) -%!error sprandn (ones (3), 3, 0.5) -%!error sprandn (3.5, 3, 0.5) -%!error sprandn (0, 3, 0.5) -%!error sprandn (3, ones (3), 0.5) -%!error sprandn (3, 3.5, 0.5) -%!error sprandn (3, 0, 0.5) +%!error sprand (-1, -1, 0.5) +%!error sprandn (ones (3), 3, 0.5) +%!error sprandn (3.5, 3, 0.5) +%!error sprandn (-1, 3, 0.5) +%!error sprandn (3, ones (3), 0.5) +%!error sprandn (3, 3.5, 0.5) +%!error sprandn (3, -1, 0.5) %!error sprandn (3, 3, -1) %!error sprandn (3, 3, 2) %!error sprandn (2, 2, 0.2, ones (3,3)) diff -r cf59b503db2a -r e9d57f6d6353 scripts/sparse/sprandsym.m --- a/scripts/sparse/sprandsym.m Wed Feb 12 12:20:49 2020 -0500 +++ b/scripts/sparse/sprandsym.m Wed Feb 12 12:21:26 2020 -0500 @@ -51,8 +51,13 @@ return; endif - if (!(isscalar (n) && n == fix (n) && n > 0)) - error ("sprandsym: N must be an integer greater than 0"); + if (!(isscalar (n) && n == fix (n) && n >= 0)) + error ("sprandsym: N must be a non-negative integer 0"); + endif + + if (n == 0) + S = sparse (n, n); + return; endif if (d < 0 || d > 1) @@ -172,11 +177,14 @@ %! assert (sort (i), [2 3]'); %! assert (sort (j), [2 3]'); +## Test empty array creation +%!assert (size (sprandsym (0, 0.5)), [0, 0]) + ## Test input validation %!error sprandsym () %!error sprandsym (1, 2, 3) %!error sprandsym (ones (3), 0.5) %!error sprandsym (3.5, 0.5) -%!error sprandsym (0, 0.5) +%!error sprandsym (-1, 0.5) %!error sprandsym (3, -1) %!error sprandsym (3, 2)