comparison scripts/sparse/sprand.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
comparison
equal deleted inserted replaced
18588:5032ac119d52 18589:35a5e7740a6d
19 ## Original version by Paul Kienzle distributed as free software in the 19 ## Original version by Paul Kienzle distributed as free software in the
20 ## public domain. 20 ## public domain.
21 21
22 ## -*- texinfo -*- 22 ## -*- texinfo -*-
23 ## @deftypefn {Function File} {} sprand (@var{m}, @var{n}, @var{d}) 23 ## @deftypefn {Function File} {} sprand (@var{m}, @var{n}, @var{d})
24 ## @deftypefnx {Function File} {} sprand (@var{m}, @var{n}, @var{d}, @var{rc})
24 ## @deftypefnx {Function File} {} sprand (@var{s}) 25 ## @deftypefnx {Function File} {} sprand (@var{s})
25 ## Generate a random sparse matrix. The size of the matrix will be 26 ## Generate a random sparse matrix. The size of the matrix will be
26 ## @var{m} by @var{n}, with a density of values given by @var{d}. 27 ## @var{m} by @var{n}, with a density of values given by @var{d}.
27 ## @var{d} should be between 0 and 1. Values will be uniformly 28 ## @var{d} should be between 0 and 1. Values will be uniformly
28 ## distributed between 0 and 1. 29 ## distributed between 0 and 1.
29 ## 30 ##
30 ## If called with a single matrix argument, a random sparse matrix is 31 ## If called with a single matrix argument, a random sparse matrix is
31 ## generated wherever the matrix @var{S} is non-zero. 32 ## generated wherever the matrix @var{S} is non-zero.
33 ##
34 ## If called with the rc parameter as a scalar, a random sparse matrix with a
35 ## reciprocal condition number rc is generated. If rc is a vector, then it
36 ## contains the first singular values of the matrix generated (length(rc) <= min(m, n)).
37 ##
32 ## @seealso{sprandn, sprandsym} 38 ## @seealso{sprandn, sprandsym}
33 ## @end deftypefn 39 ## @end deftypefn
34 40
35 ## Author: Paul Kienzle <pkienzle@users.sf.net> 41 ## Author: Paul Kienzle <pkienzle@users.sf.net>
36 ## 42 ##
40 ## 2004-09-27 use Paul's hint to allow larger random matrices 46 ## 2004-09-27 use Paul's hint to allow larger random matrices
41 ## at the price of sometimes lower density than desired 47 ## at the price of sometimes lower density than desired
42 ## David Bateman 48 ## David Bateman
43 ## 2004-10-20 Texinfo help and copyright message 49 ## 2004-10-20 Texinfo help and copyright message
44 50
45 function S = sprand (m, n, d) 51 function S = sprand (m, n, d, rc)
46 52
47 if (nargin == 1 ) 53 if (nargin == 1 )
48 S = __sprand_impl__ (m, @rand); 54 S = __sprand_impl__ (m, @rand);
49 elseif ( nargin == 3) 55 elseif ( nargin == 3)
50 S = __sprand_impl__ (m, n, d, "sprand", @rand); 56 S = __sprand_impl__ (m, n, d, "sprand", @rand);
57 elseif (nargin == 4)
58 S = __sprand_impl__ (m, n, d, rc, "sprand", @rand);
51 else 59 else
52 print_usage (); 60 print_usage ();
53 endif 61 endif
54 62
55 endfunction 63 endfunction
66 %! [i, j, v] = find (s); 74 %! [i, j, v] = find (s);
67 %! assert (sort (i), [1 2 3]'); 75 %! assert (sort (i), [1 2 3]');
68 %! assert (sort (j), [2 3 3]'); 76 %! assert (sort (j), [2 3 3]');
69 %! assert (all (v > 0 & v < 1)); 77 %! assert (all (v > 0 & v < 1));
70 78
79 %% Test 4-input calling form
80 %!test
81 %! d = rand();
82 %! s1 = sprand (100, 100, d, 0.4);
83 %! rc = [5, 4, 3, 2, 1, 0.1];
84 %! s2 = sprand (100, 100, d, rc);
85 %! s3 = sprand (6, 4, d, rc);
86 %! assert (svd (s2)'(1:length (rc)), rc, sqrt (eps));
87 %! assert (1/cond (s1), 0.4, sqrt (eps));
88 %! assert (nnz (s1) / (100*100), d, 0.02);
89 %! assert (nnz (s2) / (100*100), d, 0.02);
90 %! assert (svd (s3)', [5 4 3 2], sqrt (eps));
91
71 %% Test input validation 92 %% Test input validation
72 %!error sprand () 93 %!error sprand ()
73 %!error sprand (1, 2) 94 %!error sprand (1, 2)
74 %!error sprand (1, 2, 3, 4) 95 %!error sprand (1, 2, 3, 4)
75 %!error sprand (ones (3), 3, 0.5) 96 %!error sprand (ones (3), 3, 0.5)
78 %!error sprand (3, ones (3), 0.5) 99 %!error sprand (3, ones (3), 0.5)
79 %!error sprand (3, 3.5, 0.5) 100 %!error sprand (3, 3.5, 0.5)
80 %!error sprand (3, 0, 0.5) 101 %!error sprand (3, 0, 0.5)
81 %!error sprand (3, 3, -1) 102 %!error sprand (3, 3, -1)
82 %!error sprand (3, 3, 2) 103 %!error sprand (3, 3, 2)
104 %!error sprand (2, 2, 0.2, -1)
105 %!error sprand (2, 2, 0.2, 2)
83 106
84 %% Test very large, very low density matrix doesn't fail 107 %% Test very large, very low density matrix doesn't fail
85 %!test 108 %!test
86 %! s = sprand(1e6,1e6,1e-7); 109 %! s = sprand(1e6,1e6,1e-7);
87 110