Mercurial > hg > octave-thorsten
comparison scripts/sparse/sprandsym.m @ 13064:bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
* sprandsym.m: Add input validation and tests for sprandsym.m.
* sprandn.m: Remove unnecessary output from find()
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Sat, 03 Sep 2011 11:29:24 -0700 |
parents | c792872f8942 |
children | abf1e00111dd |
comparison
equal
deleted
inserted
replaced
13063:4b110dd204b9 | 13064:bae887ebea48 |
---|---|
32 ## triangular part. | 32 ## triangular part. |
33 ## @seealso{sprand, sprandn} | 33 ## @seealso{sprand, sprandn} |
34 ## @end deftypefn | 34 ## @end deftypefn |
35 | 35 |
36 function S = sprandsym (n, d) | 36 function S = sprandsym (n, d) |
37 if (nargin == 1) | |
38 [i, j, v] = find (tril (n)); | |
39 [nr, nc] = size (n); | |
40 S = sparse (i, j, randn (size (v)), nr, nc); | |
41 S = S + tril (S, -1)'; | |
42 elseif (nargin == 2) | |
43 m1 = floor (n/2); | |
44 n1 = m1 + rem (n, 2); | |
45 mn1 = m1*n1; | |
46 k1 = round (d*mn1); | |
47 idx1 = unique (fix (rand (min (k1*1.01, k1+10), 1) * mn1)) + 1; | |
48 ## idx contains random numbers in [1,mn] generate 1% or 10 more | |
49 ## random values than necessary in order to reduce the probability | |
50 ## that there are less than k distinct values; maybe a better | |
51 ## strategy could be used but I don't think it's worth the price. | |
52 | 37 |
53 ## Actual number of entries in S. | 38 if (nargin != 1 && nargin != 2) |
54 k1 = min (length (idx1), k1); | |
55 j1 = floor ((idx1(1:k1)-1)/m1); | |
56 i1 = idx1(1:k1) - j1*m1; | |
57 | |
58 n2 = ceil (n/2); | |
59 nn2 = n2*n2; | |
60 k2 = round (d*nn2); | |
61 idx2 = unique (fix (rand (min (k2*1.01, k1+10), 1) * nn2)) + 1; | |
62 k2 = min (length (idx2), k2); | |
63 j2 = floor ((idx2(1:k2)-1)/n2); | |
64 i2 = idx2(1:k2) - j2*n2; | |
65 | |
66 if (isempty (i1) && isempty (i2)) | |
67 S = sparse (n, n); | |
68 else | |
69 S1 = sparse (i1, j1+1, randn (k1, 1), m1, n1); | |
70 S = [tril(S1), sparse(m1,m1); ... | |
71 sparse(i2,j2+1,randn(k2,1),n2,n2), triu(S1,1)']; | |
72 S = S + tril (S, -1)'; | |
73 endif | |
74 else | |
75 print_usage (); | 39 print_usage (); |
76 endif | 40 endif |
41 | |
42 if (nargin == 1) | |
43 [i, j] = find (tril (n)); | |
44 [nr, nc] = size (n); | |
45 S = sparse (i, j, randn (size (i)), nr, nc); | |
46 S = S + tril (S, -1)'; | |
47 return; | |
48 endif | |
49 | |
50 if (!(isscalar (n) && n == fix (n) && n > 0)) | |
51 error ("sprand: N must be an integer greater than 0"); | |
52 endif | |
53 | |
54 if (d < 0 || d > 1) | |
55 error ("sprand: density D must be between 0 and 1"); | |
56 endif | |
57 | |
58 m1 = floor (n/2); | |
59 n1 = m1 + rem (n, 2); | |
60 mn1 = m1*n1; | |
61 k1 = round (d*mn1); | |
62 idx1 = unique (fix (rand (min (k1*1.01, k1+10), 1) * mn1)) + 1; | |
63 ## idx contains random numbers in [1,mn] generate 1% or 10 more | |
64 ## random values than necessary in order to reduce the probability | |
65 ## that there are less than k distinct values; maybe a better | |
66 ## strategy could be used but I don't think it's worth the price. | |
67 | |
68 ## Actual number of entries in S. | |
69 k1 = min (length (idx1), k1); | |
70 j1 = floor ((idx1(1:k1)-1)/m1); | |
71 i1 = idx1(1:k1) - j1*m1; | |
72 | |
73 n2 = ceil (n/2); | |
74 nn2 = n2*n2; | |
75 k2 = round (d*nn2); | |
76 idx2 = unique (fix (rand (min (k2*1.01, k1+10), 1) * nn2)) + 1; | |
77 k2 = min (length (idx2), k2); | |
78 j2 = floor ((idx2(1:k2)-1)/n2); | |
79 i2 = idx2(1:k2) - j2*n2; | |
80 | |
81 if (isempty (i1) && isempty (i2)) | |
82 S = sparse (n, n); | |
83 else | |
84 S1 = sparse (i1, j1+1, randn (k1, 1), m1, n1); | |
85 S = [tril(S1), sparse(m1,m1); ... | |
86 sparse(i2,j2+1,randn(k2,1),n2,n2), triu(S1,1)']; | |
87 S = S + tril (S, -1)'; | |
88 endif | |
89 | |
77 endfunction | 90 endfunction |
91 | |
92 | |
93 ## FIXME: Test for density can't happen until code of sprandsym is improved | |
94 %!test | |
95 %! s = sprandsym (10, 0.1); | |
96 %! assert (issparse (s)); | |
97 %! assert (issymmetric (s)); | |
98 %! assert (size (s), [10, 10]); | |
99 ##%! assert (nnz (s) / numel (s), 0.1, .01); | |
100 | |
101 %% Test 1-input calling form | |
102 %!test | |
103 %! s = sprandsym (sparse ([1 2 3], [3 2 3], [2 2 2])); | |
104 %! [i, j] = find (s); | |
105 %! assert (sort (i), [2 3]'); | |
106 %! assert (sort (j), [2 3]'); | |
107 | |
108 %% Test input validation | |
109 %!error sprandsym () | |
110 %!error sprandsym (1, 2, 3) | |
111 %!error sprandsym (ones(3), 0.5) | |
112 %!error sprandsym (3.5, 0.5) | |
113 %!error sprandsym (0, 0.5) | |
114 %!error sprandsym (3, -1) | |
115 %!error sprandsym (3, 2) | |
116 |