Mercurial > hg > octave-lyh
annotate 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 |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 2004-2011 David Bateman and Andy Adler |
5610 | 2 ## |
7016 | 3 ## This file is part of Octave. |
5610 | 4 ## |
7016 | 5 ## Octave is free software; you can redistribute it and/or modify it |
6 ## under the terms of the GNU General Public License as published by | |
7 ## the Free Software Foundation; either version 3 of the License, or (at | |
8 ## your option) any later version. | |
9 ## | |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
5610 | 14 ## |
15 ## You should have received a copy of the GNU General Public License | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
5610 | 18 |
19 ## -*- texinfo -*- | |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
20 ## @deftypefn {Function File} {} sprandsym (@var{n}, @var{d}) |
5610 | 21 ## @deftypefnx {Function File} {} sprandsym (@var{s}) |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
22 ## Generate a symmetric random sparse matrix. The size of the matrix will be |
5610 | 23 ## @var{n} by @var{n}, with a density of values given by @var{d}. |
24 ## @var{d} should be between 0 and 1. Values will be normally | |
25 ## distributed with mean of zero and variance 1. | |
26 ## | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
27 ## Note: sometimes the actual density may be a bit smaller than @var{d}. |
5610 | 28 ## This is unlikely to happen for large really sparse matrices. |
29 ## | |
30 ## If called with a single matrix argument, a random sparse matrix is | |
31 ## generated wherever the matrix @var{S} is non-zero in its lower | |
32 ## triangular part. | |
5642 | 33 ## @seealso{sprand, sprandn} |
5610 | 34 ## @end deftypefn |
35 | |
6252 | 36 function S = sprandsym (n, d) |
8506 | 37 |
13064
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
38 if (nargin != 1 && nargin != 2) |
6046 | 39 print_usage (); |
5610 | 40 endif |
13064
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
41 |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
42 if (nargin == 1) |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
43 [i, j] = find (tril (n)); |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
44 [nr, nc] = size (n); |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
45 S = sparse (i, j, randn (size (i)), nr, nc); |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
46 S = S + tril (S, -1)'; |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
47 return; |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
48 endif |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
49 |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
50 if (!(isscalar (n) && n == fix (n) && n > 0)) |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
51 error ("sprand: N must be an integer greater than 0"); |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
52 endif |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
53 |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
54 if (d < 0 || d > 1) |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
55 error ("sprand: density D must be between 0 and 1"); |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
56 endif |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
57 |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
58 m1 = floor (n/2); |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
59 n1 = m1 + rem (n, 2); |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
60 mn1 = m1*n1; |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
61 k1 = round (d*mn1); |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
62 idx1 = unique (fix (rand (min (k1*1.01, k1+10), 1) * mn1)) + 1; |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
63 ## idx contains random numbers in [1,mn] generate 1% or 10 more |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
64 ## random values than necessary in order to reduce the probability |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
65 ## that there are less than k distinct values; maybe a better |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
66 ## strategy could be used but I don't think it's worth the price. |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
67 |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
68 ## Actual number of entries in S. |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
69 k1 = min (length (idx1), k1); |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
70 j1 = floor ((idx1(1:k1)-1)/m1); |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
71 i1 = idx1(1:k1) - j1*m1; |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
72 |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
73 n2 = ceil (n/2); |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
74 nn2 = n2*n2; |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
75 k2 = round (d*nn2); |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
76 idx2 = unique (fix (rand (min (k2*1.01, k1+10), 1) * nn2)) + 1; |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
77 k2 = min (length (idx2), k2); |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
78 j2 = floor ((idx2(1:k2)-1)/n2); |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
79 i2 = idx2(1:k2) - j2*n2; |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
80 |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
81 if (isempty (i1) && isempty (i2)) |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
82 S = sparse (n, n); |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
83 else |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
84 S1 = sparse (i1, j1+1, randn (k1, 1), m1, n1); |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
85 S = [tril(S1), sparse(m1,m1); ... |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
86 sparse(i2,j2+1,randn(k2,1),n2,n2), triu(S1,1)']; |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
87 S = S + tril (S, -1)'; |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
88 endif |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
89 |
5610 | 90 endfunction |
13064
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
91 |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
92 |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
93 ## FIXME: Test for density can't happen until code of sprandsym is improved |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
94 %!test |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
95 %! s = sprandsym (10, 0.1); |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
96 %! assert (issparse (s)); |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
97 %! assert (issymmetric (s)); |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
98 %! assert (size (s), [10, 10]); |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
99 ##%! assert (nnz (s) / numel (s), 0.1, .01); |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
100 |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
101 %% Test 1-input calling form |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
102 %!test |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
103 %! s = sprandsym (sparse ([1 2 3], [3 2 3], [2 2 2])); |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
104 %! [i, j] = find (s); |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
105 %! assert (sort (i), [2 3]'); |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
106 %! assert (sort (j), [2 3]'); |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
107 |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
108 %% Test input validation |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
109 %!error sprandsym () |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
110 %!error sprandsym (1, 2, 3) |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
111 %!error sprandsym (ones(3), 0.5) |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
112 %!error sprandsym (3.5, 0.5) |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
113 %!error sprandsym (0, 0.5) |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
114 %!error sprandsym (3, -1) |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
115 %!error sprandsym (3, 2) |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
116 |