Mercurial > hg > octave-lyh
annotate scripts/sparse/sprand.m @ 17176:19191d0ef0bf
build: Remove second re-definition of octetc_DATA from Makefile.am.
* Makefile.am: Remove second re-definition of octetc_DATA from Makefile.am.
author | Rik <rik@octave.org> |
---|---|
date | Sun, 04 Aug 2013 07:56:41 -0700 |
parents | 8fce0ed4894a |
children |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
13197
diff
changeset
|
1 ## Copyright (C) 2004-2012 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:
10549
diff
changeset
|
23 ## @deftypefn {Function File} {} sprand (@var{m}, @var{n}, @var{d}) |
5164 | 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 | 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 | 28 ## distributed between 0 and 1. |
5164 | 29 ## |
30 ## If called with a single matrix argument, a random sparse matrix is | |
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:
13196
diff
changeset
|
32 ## @seealso{sprandn, sprandsym} |
5164 | 33 ## @end deftypefn |
34 | |
35 ## Author: Paul Kienzle <pkienzle@users.sf.net> | |
36 ## | |
37 ## Changelog: | |
38 ## | |
39 ## Piotr Krzyzanowski <przykry2004@users.sf.net> | |
10549 | 40 ## 2004-09-27 use Paul's hint to allow larger random matrices |
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 | 44 |
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 | 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 |