Mercurial > hg > octave-nkf
annotate scripts/sparse/sprand.m @ 20737:2d9ec16fa960
Print error, rather than aborting, if mex function mxIsFromGlobalWS is used (bug #46070).
* mex.cc (mxIsFromGlobalWS): Call mexErrMsgTxt rather than abort() in function.
author | Rik <rik@octave.org> |
---|---|
date | Tue, 29 Sep 2015 12:00:11 -0700 |
parents | 26bd6008fc9c |
children |
rev | line source |
---|---|
19898
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19794
diff
changeset
|
1 ## Copyright (C) 2004-2015 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}) |
18725 | 24 ## @deftypefnx {Function File} {} sprand (@var{m}, @var{n}, @var{d}, @var{rc}) |
5164 | 25 ## @deftypefnx {Function File} {} sprand (@var{s}) |
18725 | 26 ## Generate a sparse matrix with uniformly distributed random values. |
27 ## | |
28 ## The size of the matrix is @var{m}x@var{n} with a density of values @var{d}. | |
29 ## @var{d} must be between 0 and 1. Values will be uniformly distributed on | |
30 ## the interval (0, 1). | |
5164 | 31 ## |
18725 | 32 ## If called with a single matrix argument, a sparse matrix is generated with |
19007
9ac2357f19bc
doc: Replace "non-zero" with "nonzero" to match existing usage.
Rik <rik@octave.org>
parents:
19006
diff
changeset
|
33 ## random values wherever the matrix @var{s} is nonzero. |
18724
35a5e7740a6d
Added implementation for 4th argument of sprand/sprandn (bug #41839).
Eduardo Ramos (edu159) <eduradical951@gmail.com>
parents:
17744
diff
changeset
|
34 ## |
18725 | 35 ## If called with a scalar fourth argument @var{rc}, a random sparse matrix |
36 ## with reciprocal condition number @var{rc} is generated. If @var{rc} is | |
37 ## a vector, then it specifies the first singular values of the generated | |
38 ## matrix (@code{length (@var{rc}) <= min (@var{m}, @var{n})}). | |
18724
35a5e7740a6d
Added implementation for 4th argument of sprand/sprandn (bug #41839).
Eduardo Ramos (edu159) <eduradical951@gmail.com>
parents:
17744
diff
changeset
|
39 ## |
18725 | 40 ## @seealso{sprandn, sprandsym, rand} |
5164 | 41 ## @end deftypefn |
42 | |
43 ## Author: Paul Kienzle <pkienzle@users.sf.net> | |
44 ## | |
45 ## Changelog: | |
46 ## | |
47 ## Piotr Krzyzanowski <przykry2004@users.sf.net> | |
10549 | 48 ## 2004-09-27 use Paul's hint to allow larger random matrices |
49 ## 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
|
50 ## David Bateman |
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
51 ## 2004-10-20 Texinfo help and copyright message |
5164 | 52 |
18725 | 53 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
|
54 |
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
|
55 if (nargin == 1 ) |
20609
26bd6008fc9c
maint: Rename __sprand_impl__.m to __sprand__.m
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
56 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
|
57 elseif ( nargin == 3) |
20609
26bd6008fc9c
maint: Rename __sprand_impl__.m to __sprand__.m
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
58 s = __sprand__ (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
|
59 elseif (nargin == 4) |
20609
26bd6008fc9c
maint: Rename __sprand_impl__.m to __sprand__.m
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
60 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
|
61 else |
11203
d468f5c10955
sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
62 print_usage (); |
d468f5c10955
sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
63 endif |
d468f5c10955
sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
64 |
5164 | 65 endfunction |
11203
d468f5c10955
sprand.m: Add input validation to function.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
66 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
67 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
68 ## Test 3-input calling form |
13054
63d06af0376a
codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
69 %!test |
63d06af0376a
codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
70 %! s = sprand (4, 10, 0.1); |
63d06af0376a
codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
71 %! 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
|
72 %! assert (nnz (s) / numel (s), 0.1); |
13054
63d06af0376a
codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
73 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
74 ## Test 4-input calling form |
18724
35a5e7740a6d
Added implementation for 4th argument of sprand/sprandn (bug #41839).
Eduardo Ramos (edu159) <eduradical951@gmail.com>
parents:
17744
diff
changeset
|
75 %!test |
18725 | 76 %! d = rand (); |
18724
35a5e7740a6d
Added implementation for 4th argument of sprand/sprandn (bug #41839).
Eduardo Ramos (edu159) <eduradical951@gmail.com>
parents:
17744
diff
changeset
|
77 %! 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
|
78 %! 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
|
79 %! 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
|
80 %! s3 = sprand (6, 4, d, rc); |
19794
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19793
diff
changeset
|
81 %! assert (svd (s2)'(1:length (rc)), rc, sqrt (eps)); |
18724
35a5e7740a6d
Added implementation for 4th argument of sprand/sprandn (bug #41839).
Eduardo Ramos (edu159) <eduradical951@gmail.com>
parents:
17744
diff
changeset
|
82 %! 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
|
83 %! assert (nnz (s1) / (100*100), d, 0.02); |
19794
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19793
diff
changeset
|
84 %! assert (nnz (s2) / (100*100), d, 0.02); |
18724
35a5e7740a6d
Added implementation for 4th argument of sprand/sprandn (bug #41839).
Eduardo Ramos (edu159) <eduradical951@gmail.com>
parents:
17744
diff
changeset
|
85 %! 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
|
86 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
87 ## Test 1-input calling form |
13054
63d06af0376a
codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
88 %!test |
63d06af0376a
codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
89 %! 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
|
90 %! [i, j, v] = find (s); |
63d06af0376a
codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
91 %! assert (sort (i), [1 2 3]'); |
63d06af0376a
codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
92 %! assert (sort (j), [2 3 3]'); |
63d06af0376a
codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
93 %! assert (all (v > 0 & v < 1)); |
63d06af0376a
codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
94 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
95 ## 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:
19898
diff
changeset
|
96 %!test |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
97 %! s = sprand (1e6, 1e6, 1e-7); |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
98 |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
99 ## Test input validation |
13054
63d06af0376a
codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
100 %!error sprand () |
63d06af0376a
codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
101 %!error sprand (1, 2) |
63d06af0376a
codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
102 %!error sprand (1, 2, 3, 4) |
18725 | 103 %!error <M must be an integer greater than 0> sprand (ones (3), 3, 0.5) |
104 %!error <M must be an integer greater than 0> sprand (3.5, 3, 0.5) | |
105 %!error <M must be an integer greater than 0> sprand (0, 3, 0.5) | |
106 %!error <N must be an integer greater than 0> sprand (3, ones (3), 0.5) | |
107 %!error <N must be an integer greater than 0> sprand (3, 3.5, 0.5) | |
108 %!error <N must be an integer greater than 0> sprand (3, 0, 0.5) | |
109 %!error <D must be between 0 and 1> sprand (3, 3, -1) | |
110 %!error <D must be between 0 and 1> sprand (3, 3, 2) | |
111 %!error <RC must be a scalar or vector> sprand (2, 2, 0.2, ones (3,3)) | |
112 %!error <RC must be between 0 and 1> sprand (2, 2, 0.2, -1) | |
113 %!error <RC must be between 0 and 1> sprand (2, 2, 0.2, 2) | |
13054
63d06af0376a
codesprint: Tests for sprand.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
114 |