Mercurial > octave
annotate scripts/sparse/sprand.m @ 27919:1891570abac8
update Octave Project Developers copyright for the new year
In files that have the "Octave Project Developers" copyright notice,
update for 2020.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 06 Jan 2020 22:29:51 -0500 |
parents | b442ec6dda5c |
children | bd51beb6205e |
rev | line source |
---|---|
27919
1891570abac8
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
27918
diff
changeset
|
1 ## Copyright (C) 2004-2020 The Octave Project Developers |
27918
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
2 ## |
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
3 ## See the file COPYRIGHT.md in the top-level directory of this distribution |
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
4 ## or <https://octave.org/COPYRIGHT.html/>. |
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
5 ## |
5164 | 6 ## |
7016 | 7 ## This file is part of Octave. |
8 ## | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
9 ## Octave is free software: you can redistribute it and/or modify it |
7016 | 10 ## under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
11 ## the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
12 ## (at your option) any later version. |
7016 | 13 ## |
14 ## Octave is distributed in the hope that it will be useful, but | |
15 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
16 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
17 ## GNU General Public License for more details. |
7016 | 18 ## |
19 ## You should have received a copy of the GNU General Public License | |
20 ## along with Octave; see the file COPYING. If not, see | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
21 ## <https://www.gnu.org/licenses/>. |
7016 | 22 ## |
23 ## Original version by Paul Kienzle distributed as free software in the | |
24 ## public domain. | |
5164 | 25 |
26 ## -*- texinfo -*- | |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20399
diff
changeset
|
27 ## @deftypefn {} {} sprand (@var{m}, @var{n}, @var{d}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20399
diff
changeset
|
28 ## @deftypefnx {} {} sprand (@var{m}, @var{n}, @var{d}, @var{rc}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20399
diff
changeset
|
29 ## @deftypefnx {} {} sprand (@var{s}) |
18590 | 30 ## Generate a sparse matrix with uniformly distributed random values. |
31 ## | |
32 ## The size of the matrix is @var{m}x@var{n} with a density of values @var{d}. | |
33 ## @var{d} must be between 0 and 1. Values will be uniformly distributed on | |
34 ## the interval (0, 1). | |
5164 | 35 ## |
18590 | 36 ## If called with a single matrix argument, a sparse matrix is generated with |
18812
9ac2357f19bc
doc: Replace "non-zero" with "nonzero" to match existing usage.
Rik <rik@octave.org>
parents:
18811
diff
changeset
|
37 ## random values wherever the matrix @var{s} is nonzero. |
18589
35a5e7740a6d
Added implementation for 4th argument of sprand/sprandn (bug #41839).
Eduardo Ramos (edu159) <eduradical951@gmail.com>
parents:
17744
diff
changeset
|
38 ## |
18590 | 39 ## If called with a scalar fourth argument @var{rc}, a random sparse matrix |
40 ## with reciprocal condition number @var{rc} is generated. If @var{rc} is | |
41 ## a vector, then it specifies the first singular values of the generated | |
42 ## matrix (@code{length (@var{rc}) <= min (@var{m}, @var{n})}). | |
18589
35a5e7740a6d
Added implementation for 4th argument of sprand/sprandn (bug #41839).
Eduardo Ramos (edu159) <eduradical951@gmail.com>
parents:
17744
diff
changeset
|
43 ## |
18590 | 44 ## @seealso{sprandn, sprandsym, rand} |
5164 | 45 ## @end deftypefn |
46 | |
47 ## Author: Paul Kienzle <pkienzle@users.sf.net> | |
48 ## | |
49 ## Changelog: | |
50 ## | |
51 ## Piotr Krzyzanowski <przykry2004@users.sf.net> | |
10549 | 52 ## 2004-09-27 use Paul's hint to allow larger random matrices |
53 ## 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
|
54 ## David Bateman |
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
55 ## 2004-10-20 Texinfo help and copyright message |
5164 | 56 |
18590 | 57 function s = sprand (m, n, d, rc) |
11203
d468f5c10955
sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
58 |
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
|
59 if (nargin == 1 ) |
20399
26bd6008fc9c
maint: Rename __sprand_impl__.m to __sprand__.m
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
60 s = __sprand__ (m, @rand); |
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
|
61 elseif ( nargin == 3) |
20399
26bd6008fc9c
maint: Rename __sprand_impl__.m to __sprand__.m
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
62 s = __sprand__ (m, n, d, "sprand", @rand); |
18589
35a5e7740a6d
Added implementation for 4th argument of sprand/sprandn (bug #41839).
Eduardo Ramos (edu159) <eduradical951@gmail.com>
parents:
17744
diff
changeset
|
63 elseif (nargin == 4) |
20399
26bd6008fc9c
maint: Rename __sprand_impl__.m to __sprand__.m
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
64 s = __sprand__ (m, n, d, rc, "sprand", @rand); |
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
|
65 else |
11203
d468f5c10955
sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
66 print_usage (); |
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 |
5164 | 69 endfunction |
11203
d468f5c10955
sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
70 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
71 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
72 ## Test 3-input calling form |
13054
63d06af0376a
codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
73 %!test |
63d06af0376a
codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
74 %! s = sprand (4, 10, 0.1); |
63d06af0376a
codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
75 %! 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
|
76 %! assert (nnz (s) / numel (s), 0.1); |
13054
63d06af0376a
codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
77 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
78 ## Test 4-input calling form |
18589
35a5e7740a6d
Added implementation for 4th argument of sprand/sprandn (bug #41839).
Eduardo Ramos (edu159) <eduradical951@gmail.com>
parents:
17744
diff
changeset
|
79 %!test |
18590 | 80 %! d = rand (); |
18589
35a5e7740a6d
Added implementation for 4th argument of sprand/sprandn (bug #41839).
Eduardo Ramos (edu159) <eduradical951@gmail.com>
parents:
17744
diff
changeset
|
81 %! s1 = sprand (100, 100, d, 0.4); |
35a5e7740a6d
Added implementation for 4th argument of sprand/sprandn (bug #41839).
Eduardo Ramos (edu159) <eduradical951@gmail.com>
parents:
17744
diff
changeset
|
82 %! rc = [5, 4, 3, 2, 1, 0.1]; |
35a5e7740a6d
Added implementation for 4th argument of sprand/sprandn (bug #41839).
Eduardo Ramos (edu159) <eduradical951@gmail.com>
parents:
17744
diff
changeset
|
83 %! s2 = sprand (100, 100, d, rc); |
35a5e7740a6d
Added implementation for 4th argument of sprand/sprandn (bug #41839).
Eduardo Ramos (edu159) <eduradical951@gmail.com>
parents:
17744
diff
changeset
|
84 %! s3 = sprand (6, 4, d, rc); |
19597
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19596
diff
changeset
|
85 %! assert (svd (s2)'(1:length (rc)), rc, sqrt (eps)); |
18589
35a5e7740a6d
Added implementation for 4th argument of sprand/sprandn (bug #41839).
Eduardo Ramos (edu159) <eduradical951@gmail.com>
parents:
17744
diff
changeset
|
86 %! assert (1/cond (s1), 0.4, sqrt (eps)); |
35a5e7740a6d
Added implementation for 4th argument of sprand/sprandn (bug #41839).
Eduardo Ramos (edu159) <eduradical951@gmail.com>
parents:
17744
diff
changeset
|
87 %! assert (nnz (s1) / (100*100), d, 0.02); |
19597
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19596
diff
changeset
|
88 %! assert (nnz (s2) / (100*100), d, 0.02); |
18589
35a5e7740a6d
Added implementation for 4th argument of sprand/sprandn (bug #41839).
Eduardo Ramos (edu159) <eduradical951@gmail.com>
parents:
17744
diff
changeset
|
89 %! assert (svd (s3)', [5 4 3 2], sqrt (eps)); |
35a5e7740a6d
Added implementation for 4th argument of sprand/sprandn (bug #41839).
Eduardo Ramos (edu159) <eduradical951@gmail.com>
parents:
17744
diff
changeset
|
90 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
91 ## Test 1-input calling form |
13054
63d06af0376a
codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
92 %!test |
63d06af0376a
codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
93 %! 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
|
94 %! [i, j, v] = find (s); |
63d06af0376a
codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
95 %! assert (sort (i), [1 2 3]'); |
63d06af0376a
codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
96 %! assert (sort (j), [2 3 3]'); |
63d06af0376a
codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
97 %! assert (all (v > 0 & v < 1)); |
63d06af0376a
codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
98 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
99 ## Test very large, very low density matrix doesn't fail |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
100 %!test |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
101 %! s = sprand (1e6, 1e6, 1e-7); |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
102 |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
103 ## Test input validation |
13054
63d06af0376a
codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
104 %!error sprand () |
63d06af0376a
codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
105 %!error sprand (1, 2) |
63d06af0376a
codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
106 %!error sprand (1, 2, 3, 4) |
18590 | 107 %!error <M must be an integer greater than 0> sprand (ones (3), 3, 0.5) |
108 %!error <M must be an integer greater than 0> sprand (3.5, 3, 0.5) | |
109 %!error <M must be an integer greater than 0> sprand (0, 3, 0.5) | |
110 %!error <N must be an integer greater than 0> sprand (3, ones (3), 0.5) | |
111 %!error <N must be an integer greater than 0> sprand (3, 3.5, 0.5) | |
112 %!error <N must be an integer greater than 0> sprand (3, 0, 0.5) | |
113 %!error <D must be between 0 and 1> sprand (3, 3, -1) | |
114 %!error <D must be between 0 and 1> sprand (3, 3, 2) | |
115 %!error <RC must be a scalar or vector> sprand (2, 2, 0.2, ones (3,3)) | |
116 %!error <RC must be between 0 and 1> sprand (2, 2, 0.2, -1) | |
117 %!error <RC must be between 0 and 1> sprand (2, 2, 0.2, 2) |