annotate scripts/sparse/sprandn.m @ 17744:d63878346099

maint: Update copyright notices for release.
author John W. Eaton <jwe@octave.org>
date Wed, 23 Oct 2013 22:09:27 -0400
parents 1c89599167a6
children 35a5e7740a6d 53af80da6781
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
17744
d63878346099 maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents: 17338
diff changeset
1 ## Copyright (C) 2004-2013 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: 9245
diff changeset
23 ## @deftypefn {Function File} {} sprandn (@var{m}, @var{n}, @var{d})
5610
9761b7d24e9e [project @ 2006-02-09 09:12:02 by dbateman]
dbateman
parents: 5164
diff changeset
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
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}.
14373
d00900b3dc4b doc: Use two spaces at start of sentence.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
27 ## @var{d} should be between 0 and 1. Values will be normally
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
28 ## distributed with mean of zero and variance 1.
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
29 ##
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
30 ## 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
31 ## generated wherever the matrix @var{S} is non-zero.
13197
6db186dfdeaa Refactor sprandn/sprand code, move common code to common function (bug #34352)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13064
diff changeset
32 ## @seealso{sprand, sprandsym}
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
33 ## @end deftypefn
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
34
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
35 ## Author: Paul Kienzle <pkienzle@users.sf.net>
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
36
6498
2c85044aa63f [project @ 2007-04-05 17:59:47 by jwe]
jwe
parents: 6046
diff changeset
37 function S = sprandn (m, n, d)
2c85044aa63f [project @ 2007-04-05 17:59:47 by jwe]
jwe
parents: 6046
diff changeset
38
13197
6db186dfdeaa Refactor sprandn/sprand code, move common code to common function (bug #34352)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13064
diff changeset
39 if (nargin == 1 )
6db186dfdeaa Refactor sprandn/sprand code, move common code to common function (bug #34352)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13064
diff changeset
40 S = __sprand_impl__ (m, @randn);
6db186dfdeaa Refactor sprandn/sprand code, move common code to common function (bug #34352)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13064
diff changeset
41 elseif ( nargin == 3)
6db186dfdeaa Refactor sprandn/sprand code, move common code to common function (bug #34352)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13064
diff changeset
42 S = __sprand_impl__ (m, n, d, "sprandn", @randn);
6db186dfdeaa Refactor sprandn/sprand code, move common code to common function (bug #34352)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13064
diff changeset
43 else
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5642
diff changeset
44 print_usage ();
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
45 endif
13058
14422cc782b2 codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
46
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
47 endfunction
13058
14422cc782b2 codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
48
14422cc782b2 codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
49
14422cc782b2 codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
50 %!test
14422cc782b2 codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
51 %! 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
52 %! assert (size (s), [4, 10]);
13197
6db186dfdeaa Refactor sprandn/sprand code, move common code to common function (bug #34352)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13064
diff changeset
53 %! assert (nnz (s) / numel (s), 0.1);
13058
14422cc782b2 codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
54
14422cc782b2 codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
55 %% Test 1-input calling form
14422cc782b2 codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
56 %!test
14422cc782b2 codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
57 %! 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
58 %! [i, j] = find (s);
13058
14422cc782b2 codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
59 %! 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
60 %! 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
61
14422cc782b2 codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
62 %% Test input validation
14422cc782b2 codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
63 %!error sprandn ()
14422cc782b2 codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
64 %!error sprandn (1, 2)
14422cc782b2 codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
65 %!error sprandn (1, 2, 3, 4)
14868
5d3a684236b0 maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents: 14373
diff changeset
66 %!error sprandn (ones (3), 3, 0.5)
13058
14422cc782b2 codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
67 %!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
68 %!error sprandn (0, 3, 0.5)
14868
5d3a684236b0 maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents: 14373
diff changeset
69 %!error sprandn (3, ones (3), 0.5)
13058
14422cc782b2 codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
70 %!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
71 %!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
72 %!error sprandn (3, 3, -1)
14422cc782b2 codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
73 %!error sprandn (3, 3, 2)
14422cc782b2 codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
74
16779
8fce0ed4894a Specialize is_empty and numel methods for sparse matrices (debian bug #706376)
David Bateman <dbateman@free.fr>
parents: 14868
diff changeset
75 %% Test very large, very low density matrix doesn't fail
8fce0ed4894a Specialize is_empty and numel methods for sparse matrices (debian bug #706376)
David Bateman <dbateman@free.fr>
parents: 14868
diff changeset
76 %!test
8fce0ed4894a Specialize is_empty and numel methods for sparse matrices (debian bug #706376)
David Bateman <dbateman@free.fr>
parents: 14868
diff changeset
77 %! s = sprandn(1e6,1e6,1e-7);
17338
1c89599167a6 maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents: 16779
diff changeset
78