Mercurial > hg > octave-nkf
annotate scripts/sparse/sprand.m @ 18724:35a5e7740a6d
Added implementation for 4th argument of sprand/sprandn (bug #41839).
* __sprand_impl__.m: Implementation done here
* sprand.m: Added documentation and some tests.
* sprandn.m: Added documentation and some tests.
author | Eduardo Ramos (edu159) <eduradical951@gmail.com> |
---|---|
date | Sat, 22 Mar 2014 13:23:41 +0100 |
parents | d63878346099 |
children | 54a1e95365e1 |
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 | 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}) |
18724
35a5e7740a6d
Added implementation for 4th argument of sprand/sprandn (bug #41839).
Eduardo Ramos (edu159) <eduradical951@gmail.com>
parents:
17744
diff
changeset
|
24 ## @deftypefnx {Function File} {} sprand (@var{m}, @var{n}, @var{d}, @var{rc}) |
5164 | 25 ## @deftypefnx {Function File} {} sprand (@var{s}) |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
26 ## Generate a random sparse matrix. The size of the matrix will be |
5164 | 27 ## @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
|
28 ## @var{d} should be between 0 and 1. Values will be uniformly |
5610 | 29 ## distributed between 0 and 1. |
5164 | 30 ## |
31 ## If called with a single matrix argument, a random sparse matrix is | |
32 ## generated wherever the matrix @var{S} is non-zero. | |
18724
35a5e7740a6d
Added implementation for 4th argument of sprand/sprandn (bug #41839).
Eduardo Ramos (edu159) <eduradical951@gmail.com>
parents:
17744
diff
changeset
|
33 ## |
35a5e7740a6d
Added implementation for 4th argument of sprand/sprandn (bug #41839).
Eduardo Ramos (edu159) <eduradical951@gmail.com>
parents:
17744
diff
changeset
|
34 ## If called with the rc parameter as a scalar, a random sparse matrix with a |
35a5e7740a6d
Added implementation for 4th argument of sprand/sprandn (bug #41839).
Eduardo Ramos (edu159) <eduradical951@gmail.com>
parents:
17744
diff
changeset
|
35 ## reciprocal condition number rc is generated. If rc is a vector, then it |
35a5e7740a6d
Added implementation for 4th argument of sprand/sprandn (bug #41839).
Eduardo Ramos (edu159) <eduradical951@gmail.com>
parents:
17744
diff
changeset
|
36 ## contains the first singular values of the matrix generated (length(rc) <= min(m, n)). |
35a5e7740a6d
Added implementation for 4th argument of sprand/sprandn (bug #41839).
Eduardo Ramos (edu159) <eduradical951@gmail.com>
parents:
17744
diff
changeset
|
37 ## |
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
|
38 ## @seealso{sprandn, sprandsym} |
5164 | 39 ## @end deftypefn |
40 | |
41 ## Author: Paul Kienzle <pkienzle@users.sf.net> | |
42 ## | |
43 ## Changelog: | |
44 ## | |
45 ## Piotr Krzyzanowski <przykry2004@users.sf.net> | |
10549 | 46 ## 2004-09-27 use Paul's hint to allow larger random matrices |
47 ## 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
|
48 ## David Bateman |
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
49 ## 2004-10-20 Texinfo help and copyright message |
5164 | 50 |
18724
35a5e7740a6d
Added implementation for 4th argument of sprand/sprandn (bug #41839).
Eduardo Ramos (edu159) <eduradical951@gmail.com>
parents:
17744
diff
changeset
|
51 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
|
52 |
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
|
53 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
|
54 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
|
55 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
|
56 S = __sprand_impl__ (m, n, d, "sprand", @rand); |
18724
35a5e7740a6d
Added implementation for 4th argument of sprand/sprandn (bug #41839).
Eduardo Ramos (edu159) <eduradical951@gmail.com>
parents:
17744
diff
changeset
|
57 elseif (nargin == 4) |
35a5e7740a6d
Added implementation for 4th argument of sprand/sprandn (bug #41839).
Eduardo Ramos (edu159) <eduradical951@gmail.com>
parents:
17744
diff
changeset
|
58 S = __sprand_impl__ (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
|
59 else |
11203
d468f5c10955
sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
60 print_usage (); |
d468f5c10955
sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
61 endif |
d468f5c10955
sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
62 |
5164 | 63 endfunction |
11203
d468f5c10955
sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
64 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
65 |
13054
63d06af0376a
codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
66 %!test |
63d06af0376a
codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
67 %! s = sprand (4, 10, 0.1); |
63d06af0376a
codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
68 %! 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
|
69 %! assert (nnz (s) / numel (s), 0.1); |
13054
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 1-input calling form |
63d06af0376a
codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
72 %!test |
63d06af0376a
codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
73 %! 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
|
74 %! [i, j, v] = find (s); |
63d06af0376a
codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
75 %! assert (sort (i), [1 2 3]'); |
63d06af0376a
codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
76 %! assert (sort (j), [2 3 3]'); |
63d06af0376a
codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
77 %! assert (all (v > 0 & v < 1)); |
63d06af0376a
codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
78 |
18724
35a5e7740a6d
Added implementation for 4th argument of sprand/sprandn (bug #41839).
Eduardo Ramos (edu159) <eduradical951@gmail.com>
parents:
17744
diff
changeset
|
79 %% Test 4-input calling form |
35a5e7740a6d
Added implementation for 4th argument of sprand/sprandn (bug #41839).
Eduardo Ramos (edu159) <eduradical951@gmail.com>
parents:
17744
diff
changeset
|
80 %!test |
35a5e7740a6d
Added implementation for 4th argument of sprand/sprandn (bug #41839).
Eduardo Ramos (edu159) <eduradical951@gmail.com>
parents:
17744
diff
changeset
|
81 %! d = rand(); |
35a5e7740a6d
Added implementation for 4th argument of sprand/sprandn (bug #41839).
Eduardo Ramos (edu159) <eduradical951@gmail.com>
parents:
17744
diff
changeset
|
82 %! 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
|
83 %! 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
|
84 %! 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
|
85 %! s3 = sprand (6, 4, d, rc); |
35a5e7740a6d
Added implementation for 4th argument of sprand/sprandn (bug #41839).
Eduardo Ramos (edu159) <eduradical951@gmail.com>
parents:
17744
diff
changeset
|
86 %! assert (svd (s2)'(1:length (rc)), rc, 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 (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
|
88 %! assert (nnz (s1) / (100*100), d, 0.02); |
35a5e7740a6d
Added implementation for 4th argument of sprand/sprandn (bug #41839).
Eduardo Ramos (edu159) <eduradical951@gmail.com>
parents:
17744
diff
changeset
|
89 %! assert (nnz (s2) / (100*100), d, 0.02); |
35a5e7740a6d
Added implementation for 4th argument of sprand/sprandn (bug #41839).
Eduardo Ramos (edu159) <eduradical951@gmail.com>
parents:
17744
diff
changeset
|
90 %! 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
|
91 |
13054
63d06af0376a
codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
92 %% Test input validation |
63d06af0376a
codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
93 %!error sprand () |
63d06af0376a
codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
94 %!error sprand (1, 2) |
63d06af0376a
codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
95 %!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
|
96 %!error sprand (ones (3), 3, 0.5) |
13054
63d06af0376a
codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
97 %!error sprand (3.5, 3, 0.5) |
63d06af0376a
codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
98 %!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
|
99 %!error sprand (3, ones (3), 0.5) |
13054
63d06af0376a
codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
100 %!error sprand (3, 3.5, 0.5) |
63d06af0376a
codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
101 %!error sprand (3, 0, 0.5) |
63d06af0376a
codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
102 %!error sprand (3, 3, -1) |
63d06af0376a
codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
103 %!error sprand (3, 3, 2) |
18724
35a5e7740a6d
Added implementation for 4th argument of sprand/sprandn (bug #41839).
Eduardo Ramos (edu159) <eduradical951@gmail.com>
parents:
17744
diff
changeset
|
104 %!error sprand (2, 2, 0.2, -1) |
35a5e7740a6d
Added implementation for 4th argument of sprand/sprandn (bug #41839).
Eduardo Ramos (edu159) <eduradical951@gmail.com>
parents:
17744
diff
changeset
|
105 %!error sprand (2, 2, 0.2, 2) |
13054
63d06af0376a
codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
106 |
16779
8fce0ed4894a
Specialize is_empty and numel methods for sparse matrices (debian bug #706376)
David Bateman <dbateman@free.fr>
parents:
14363
diff
changeset
|
107 %% 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
|
108 %!test |
8fce0ed4894a
Specialize is_empty and numel methods for sparse matrices (debian bug #706376)
David Bateman <dbateman@free.fr>
parents:
14363
diff
changeset
|
109 %! 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
|
110 |