annotate scripts/sparse/sprand.m @ 12749:e7cc2d4a6db3 stable

Fix range of sigma in normal distribution to exclude 0. * normcdf.m, normpdf.m: Correct 's >= 0' to 's > 0'.
author Rik <octave@nomad.inbox5.com>
date Thu, 16 Jun 2011 21:05:10 -0700
parents c792872f8942
children 63d06af0376a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11523
fd0a3ac60b0e update copyright notices
John W. Eaton <jwe@octave.org>
parents: 11203
diff changeset
1 ## Copyright (C) 2004-2011 Paul Kienzle
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
2 ##
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6498
diff changeset
3 ## This file is part of Octave.
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6498
diff changeset
4 ##
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6498
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6498
diff changeset
6 ## under the terms of the GNU General Public License as published by
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6498
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6498
diff changeset
8 ## your option) any later version.
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6498
diff changeset
9 ##
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6498
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6498
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6498
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6498
diff changeset
13 ## General Public License for more details.
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6498
diff changeset
14 ##
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6498
diff changeset
15 ## You should have received a copy of the GNU General Public License
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6498
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6498
diff changeset
17 ## <http://www.gnu.org/licenses/>.
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6498
diff changeset
18 ##
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6498
diff changeset
19 ## Original version by Paul Kienzle distributed as free software in the
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6498
diff changeset
20 ## public domain.
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
21
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
22 ## -*- texinfo -*-
10793
be55736a0783 Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
23 ## @deftypefn {Function File} {} sprand (@var{m}, @var{n}, @var{d})
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
24 ## @deftypefnx {Function File} {} sprand (@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
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
26 ## @var{m} by @var{n}, with a density of values given by @var{d}.
11203
d468f5c10955 sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
27 ## @var{d} should be between 0 and 1. Values will be uniformly
5610
9761b7d24e9e [project @ 2006-02-09 09:12:02 by dbateman]
dbateman
parents: 5164
diff changeset
28 ## distributed between 0 and 1.
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
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}.
11203
d468f5c10955 sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
31 ## This is unlikely to happen for large, truly sparse, matrices.
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
32 ##
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
33 ## If called with a single matrix argument, a random sparse matrix is
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
34 ## generated wherever the matrix @var{S} is non-zero.
5642
2618a0750ae6 [project @ 2006-03-06 21:26:48 by jwe]
jwe
parents: 5610
diff changeset
35 ## @seealso{sprandn}
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
36 ## @end deftypefn
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
37
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
38 ## Author: Paul Kienzle <pkienzle@users.sf.net>
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
39 ##
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
40 ## Changelog:
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
41 ##
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
42 ## Piotr Krzyzanowski <przykry2004@users.sf.net>
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9245
diff changeset
43 ## 2004-09-27 use Paul's hint to allow larger random matrices
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9245
diff changeset
44 ## at the price of sometimes lower density than desired
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
45 ## David Bateman
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
46 ## 2004-10-20 Texinfo help and copyright message
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
47
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
48 function S = sprand (m, n, d)
11203
d468f5c10955 sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
49
d468f5c10955 sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
50 if (nargin != 1 && nargin != 3)
d468f5c10955 sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
51 print_usage ();
d468f5c10955 sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
52 endif
d468f5c10955 sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
53
6498
2c85044aa63f [project @ 2007-04-05 17:59:47 by jwe]
jwe
parents: 6046
diff changeset
54 if (nargin == 1)
7505
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
55 [i, j, v] = find (m);
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
56 [nr, nc] = size (m);
6498
2c85044aa63f [project @ 2007-04-05 17:59:47 by jwe]
jwe
parents: 6046
diff changeset
57 S = sparse (i, j, rand (size (v)), nr, nc);
11203
d468f5c10955 sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
58 return;
d468f5c10955 sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
59 endif
d468f5c10955 sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
60
d468f5c10955 sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
61 if (!(isscalar (m) && m == fix (m) && m > 0))
d468f5c10955 sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
62 error ("sprand: M must be an integer greater than 0");
d468f5c10955 sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
63 endif
d468f5c10955 sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
64
d468f5c10955 sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
65 if (!(isscalar (n) && n == fix (n) && n > 0))
d468f5c10955 sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
66 error ("sprand: N must be an integer greater than 0");
d468f5c10955 sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
67 endif
d468f5c10955 sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
68
d468f5c10955 sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
69 if (d < 0 || d > 1)
d468f5c10955 sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
70 error ("sprand: density D must be between 0 and 1");
d468f5c10955 sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
71 endif
6498
2c85044aa63f [project @ 2007-04-05 17:59:47 by jwe]
jwe
parents: 6046
diff changeset
72
11203
d468f5c10955 sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
73 mn = m*n;
d468f5c10955 sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
74 ## how many entries in S would be satisfactory?
d468f5c10955 sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
75 k = round (d*mn);
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
76 idx = unique (fix (rand (min (k*1.01, k+10), 1) * mn)) + 1;
11203
d468f5c10955 sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
77 ## idx contains random numbers in [1,mn]
d468f5c10955 sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
78 ## generate 1% or 10 more random values than necessary in order to
d468f5c10955 sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
79 ## reduce the probability that there are less than k distinct
d468f5c10955 sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
80 ## values; maybe a better strategy could be used but I don't think
d468f5c10955 sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
81 ## it's worth the price
d468f5c10955 sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
82
d468f5c10955 sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
83 ## actual number of entries in S
d468f5c10955 sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
84 k = min (length (idx), k);
d468f5c10955 sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
85 j = floor ((idx(1:k)-1)/m);
d468f5c10955 sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
86 i = idx(1:k) - j*m;
d468f5c10955 sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
87 if (isempty (i))
d468f5c10955 sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
88 S = sparse (m, n);
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
89 else
11203
d468f5c10955 sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
90 S = sparse (i, j+1, rand (k, 1), m, n);
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
91 endif
11203
d468f5c10955 sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
92
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
93 endfunction
11203
d468f5c10955 sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
94