Mercurial > octave
diff scripts/sparse/sprandn.m @ 18589:35a5e7740a6d
Added implementation for 4th argument of sprand/sprandn (bug #41839).
* __sprand_impl__.m: Implementation done here
* sprand.m: Added documentation and some tests.
* sprandn.m: Added documentation and some tests.
author | Eduardo Ramos (edu159) <eduradical951@gmail.com> |
---|---|
date | Sat, 22 Mar 2014 13:23:41 +0100 |
parents | d63878346099 |
children | 54a1e95365e1 |
line wrap: on
line diff
--- a/scripts/sparse/sprandn.m Sun Mar 23 12:41:15 2014 +0100 +++ b/scripts/sparse/sprandn.m Sat Mar 22 13:23:41 2014 +0100 @@ -21,6 +21,7 @@ ## -*- texinfo -*- ## @deftypefn {Function File} {} sprandn (@var{m}, @var{n}, @var{d}) +## @deftypefnx {Function File} {} sprandn (@var{m}, @var{n}, @var{d}, @var{rc}) ## @deftypefnx {Function File} {} sprandn (@var{s}) ## Generate a random sparse matrix. The size of the matrix will be ## @var{m} by @var{n}, with a density of values given by @var{d}. @@ -29,17 +30,25 @@ ## ## If called with a single matrix argument, a random sparse matrix is ## generated wherever the matrix @var{S} is non-zero. +## +## If called with the rc parameter, then a random sparse matrix with a +## reciprocal condition number rc is generated if rc is scalar. If rc +## is a vector, then it contains the first singular values of the matrix +## generated (length(rc) <= min(m, n)). +## ## @seealso{sprand, sprandsym} ## @end deftypefn ## Author: Paul Kienzle <pkienzle@users.sf.net> -function S = sprandn (m, n, d) +function S = sprandn (m, n, d, rc) if (nargin == 1 ) S = __sprand_impl__ (m, @randn); elseif ( nargin == 3) S = __sprand_impl__ (m, n, d, "sprandn", @randn); + elseif (nargin == 4) + S = __sprand_impl__ (m, n, d, rc, "sprandn", @randn); else print_usage (); endif @@ -59,6 +68,19 @@ %! assert (sort (i), [1 2 3]'); %! assert (sort (j), [2 3 3]'); +%% Test 4-input calling form +%!test +%! d = rand(); +%! s1 = sprandn (100, 100, d, 0.4); +%! rc = [5, 4, 3, 2, 1, 0.1]; +%! s2 = sprandn (100, 100, d, rc); +%! s3 = sprandn (6, 4, d, rc); +%! assert (svd (s2)'(1:length (rc)), rc, sqrt (eps)); +%! assert (1/cond (s1), 0.4, sqrt (eps)); +%! assert (nnz (s1) / (100*100), d, 0.02); +%! assert (nnz (s2) / (100*100), d, 0.02); +%! assert (svd (s3)', [5 4 3 2], sqrt (eps)); + %% Test input validation %!error sprandn () %!error sprandn (1, 2) @@ -71,6 +93,8 @@ %!error sprandn (3, 0, 0.5) %!error sprandn (3, 3, -1) %!error sprandn (3, 3, 2) +%!error sprandn (2, 2, 0.2, -1) +%!error sprandn (2, 2, 0.2, 2) %% Test very large, very low density matrix doesn't fail %!test