Mercurial > octave-nkf
annotate scripts/sparse/sprandn.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 | 14422cc782b2 |
children | 6db186dfdeaa |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 2004-2011 Paul Kienzle |
5164 | 2 ## |
7016 | 3 ## This file is part of Octave. |
4 ## | |
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. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
16 ## along with Octave; see the file COPYING. If not, see | |
17 ## <http://www.gnu.org/licenses/>. | |
18 ## | |
19 ## Original version by Paul Kienzle distributed as free software in the | |
20 ## public domain. | |
5164 | 21 |
22 ## -*- texinfo -*- | |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
23 ## @deftypefn {Function File} {} sprandn (@var{m}, @var{n}, @var{d}) |
5610 | 24 ## @deftypefnx {Function File} {} sprandn (@var{s}) |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
25 ## Generate a random sparse matrix. The size of the matrix will be |
5164 | 26 ## @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 normally | |
28 ## distributed with mean of zero and variance 1. | |
29 ## | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
30 ## Note: sometimes the actual density may be a bit smaller than @var{d}. |
5164 | 31 ## This is unlikely to happen for large really sparse matrices. |
32 ## | |
33 ## If called with a single matrix argument, a random sparse matrix is | |
34 ## generated wherever the matrix @var{S} is non-zero. | |
5642 | 35 ## @seealso{sprand} |
5164 | 36 ## @end deftypefn |
37 | |
38 ## Author: Paul Kienzle <pkienzle@users.sf.net> | |
39 | |
6498 | 40 function S = sprandn (m, n, d) |
41 | |
13058
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
42 if (nargin != 1 && nargin != 3) |
6046 | 43 print_usage (); |
5164 | 44 endif |
13058
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
45 |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
46 if (nargin == 1) |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
47 [i, j] = find (m); |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
48 [nr, nc] = size (m); |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
49 S = sparse (i, j, randn (size (i)), nr, nc); |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
50 return; |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
51 endif |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
52 |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
53 if (!(isscalar (m) && m == fix (m) && m > 0)) |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
54 error ("sprand: M must be an integer greater than 0"); |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
55 endif |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
56 |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
57 if (!(isscalar (n) && n == fix (n) && n > 0)) |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
58 error ("sprand: N must be an integer greater than 0"); |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
59 endif |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
60 |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
61 if (d < 0 || d > 1) |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
62 error ("sprand: density D must be between 0 and 1"); |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
63 endif |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
64 |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
65 mn = m*n; |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
66 k = round (d*mn); |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
67 idx = unique (fix (rand (min (k*1.01, k+10), 1) * mn)) + 1; |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
68 ## idx contains random numbers in [1,mn] |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
69 ## generate 1% or 10 more random values than necessary in order to |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
70 ## reduce the probability that there are less than k distinct |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
71 ## values; maybe a better strategy could be used but I don't think |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
72 ## it's worth the price. |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
73 |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
74 ## actual number of entries in S |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
75 k = min (length (idx), k); |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
76 j = floor ((idx(1:k)-1)/m); |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
77 i = idx(1:k) - j*m; |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
78 if (isempty (i)) |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
79 S = sparse (m, n); |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
80 else |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
81 S = sparse (i, j+1, randn (k, 1), m, n); |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
82 endif |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
83 |
5164 | 84 endfunction |
13058
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
85 |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
86 |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
87 ## FIXME: Test for density can't happen until code of sprandn is improved |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
88 %!test |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
89 %! s = sprandn (4, 10, 0.1); |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
90 %! assert (size (s), [4, 10]); |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
91 ##%! assert (nnz (s) / numel (s), 0.1, .01); |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
92 |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
93 %% Test 1-input calling form |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
94 %!test |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
95 %! s = sprandn (sparse ([1 2 3], [3 2 3], [2 2 2])); |
13064
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
13058
diff
changeset
|
96 %! [i, j] = find (s); |
13058
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
97 %! assert (sort (i), [1 2 3]'); |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
98 %! assert (sort (j), [2 3 3]'); |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
99 |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
100 %% Test input validation |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
101 %!error sprandn () |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
102 %!error sprandn (1, 2) |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
103 %!error sprandn (1, 2, 3, 4) |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
104 %!error sprandn (ones(3), 3, 0.5) |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
105 %!error sprandn (3.5, 3, 0.5) |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
106 %!error sprandn (0, 3, 0.5) |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
107 %!error sprandn (3, ones(3), 0.5) |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
108 %!error sprandn (3, 3.5, 0.5) |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
109 %!error sprandn (3, 0, 0.5) |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
110 %!error sprandn (3, 3, -1) |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
111 %!error sprandn (3, 3, 2) |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
112 |