annotate scripts/sparse/sprand.m @ 18848:53af80da6781 stable

doc: Update documentation of sparse functions including seealso links. * data.cc (Fall, Fany): Use non-zero instead of nonzero. * data.cc (Fnnz, Fnzmax, Ffull): Rewrite docstrings, include more seealso links. * sparse.cc (Fsparse): Rewrite docstring to include example of "sum" behavior and "unique" behavior. Add more seealso links. * sparse.cc (Fspalloc): Rewrite docstring, include more seealso links. * spparms.cc (Fspparms): Add seealso links. * amd.cc (Famd): Use nonzero instead of "non zero". * nonzeros.m: Add seealso links. * spaugment.m: Rewrite docstring, include more seealso links. * spconvert.m: Rewrite docstring, include more seealso links. * speye.m: Rewrite docstring, include more seealso links. * spones.m: Add seealso links. * sprand.m: Rewrite docstring, include more seealso links. * sprandn.m: Rewrite docstring, include more seealso links. * sprandsym.m: Rewrite docstring, include more seealso links. * spy.m: Rewrite docstring, include more seealso links.
author Rik <rik@octave.org>
date Sun, 08 Jun 2014 16:32:12 -0700
parents d63878346099
children c53e11fab75f 446c46af4b42
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: 16779
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: 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
18848
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 17744
diff changeset
26 ## @var{m}x@var{n}, with a density of values given by @var{d}. @var{d} must
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 17744
diff changeset
27 ## be between 0 and 1 inclusive. Values will be uniformly distributed between
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 17744
diff changeset
28 ## 0 and 1.
5164
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.
18848
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 17744
diff changeset
32 ## @seealso{sprandn, sprandsym, spones, sparse}
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 ##
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
37 ## Changelog:
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
38 ##
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
39 ## Piotr Krzyzanowski <przykry2004@users.sf.net>
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9245
diff changeset
40 ## 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
41 ## 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
42 ## David Bateman
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
43 ## 2004-10-20 Texinfo help and copyright message
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
44
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
45 function S = sprand (m, n, d)
11203
d468f5c10955 sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
46
13197
6db186dfdeaa Refactor sprandn/sprand code, move common code to common function (bug #34352)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13196
diff changeset
47 if (nargin == 1 )
6db186dfdeaa Refactor sprandn/sprand code, move common code to common function (bug #34352)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13196
diff changeset
48 S = __sprand_impl__ (m, @rand);
6db186dfdeaa Refactor sprandn/sprand code, move common code to common function (bug #34352)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13196
diff changeset
49 elseif ( nargin == 3)
6db186dfdeaa Refactor sprandn/sprand code, move common code to common function (bug #34352)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13196
diff changeset
50 S = __sprand_impl__ (m, n, d, "sprand", @rand);
6db186dfdeaa Refactor sprandn/sprand code, move common code to common function (bug #34352)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13196
diff changeset
51 else
11203
d468f5c10955 sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
52 print_usage ();
d468f5c10955 sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
53 endif
d468f5c10955 sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
54
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
55 endfunction
11203
d468f5c10955 sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
56
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
57
13054
63d06af0376a codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
58 %!test
63d06af0376a codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
59 %! s = sprand (4, 10, 0.1);
63d06af0376a codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
60 %! assert (size (s), [4, 10]);
13196
5976ba269538 Simplify code in sprand and use two-arg form of randperm for precise density
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13058
diff changeset
61 %! assert (nnz (s) / numel (s), 0.1);
13054
63d06af0376a codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
62
63d06af0376a codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
63 %% Test 1-input calling form
63d06af0376a codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
64 %!test
63d06af0376a codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
65 %! s = sprand (sparse ([1 2 3], [3 2 3], [2 2 2]));
63d06af0376a codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
66 %! [i, j, v] = find (s);
63d06af0376a codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
67 %! assert (sort (i), [1 2 3]');
63d06af0376a codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
68 %! assert (sort (j), [2 3 3]');
63d06af0376a codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
69 %! assert (all (v > 0 & v < 1));
63d06af0376a codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
70
63d06af0376a codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
71 %% Test input validation
63d06af0376a codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
72 %!error sprand ()
63d06af0376a codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
73 %!error sprand (1, 2)
63d06af0376a codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
74 %!error sprand (1, 2, 3, 4)
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
75 %!error sprand (ones (3), 3, 0.5)
13054
63d06af0376a codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
76 %!error sprand (3.5, 3, 0.5)
63d06af0376a codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
77 %!error sprand (0, 3, 0.5)
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
78 %!error sprand (3, ones (3), 0.5)
13054
63d06af0376a codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
79 %!error sprand (3, 3.5, 0.5)
63d06af0376a codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
80 %!error sprand (3, 0, 0.5)
63d06af0376a codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
81 %!error sprand (3, 3, -1)
63d06af0376a codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
82 %!error sprand (3, 3, 2)
63d06af0376a codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
83
16779
8fce0ed4894a Specialize is_empty and numel methods for sparse matrices (debian bug #706376)
David Bateman <dbateman@free.fr>
parents: 14363
diff changeset
84 %% 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: 14363
diff changeset
85 %!test
8fce0ed4894a Specialize is_empty and numel methods for sparse matrices (debian bug #706376)
David Bateman <dbateman@free.fr>
parents: 14363
diff changeset
86 %! s = sprand(1e6,1e6,1e-7);
8fce0ed4894a Specialize is_empty and numel methods for sparse matrices (debian bug #706376)
David Bateman <dbateman@free.fr>
parents: 14363
diff changeset
87