Mercurial > octave
annotate scripts/sparse/sprandn.m @ 30875:5d3faba0342e
doc: Ensure documentation lists output argument when it exists for all m-files.
For new users of Octave it is best to show explicit calling forms
in the documentation and to show a return argument when it exists.
* bp-table.cc, shift.m, accumarray.m, accumdim.m, bincoeff.m, bitcmp.m,
bitget.m, bitset.m, blkdiag.m, celldisp.m, cplxpair.m, dblquad.m, flip.m,
fliplr.m, flipud.m, idivide.m, int2str.m, interpft.m, logspace.m, num2str.m,
polyarea.m, postpad.m, prepad.m, randi.m, repmat.m, rng.m, rot90.m, rotdim.m,
structfun.m, triplequad.m, uibuttongroup.m, uicontrol.m, uipanel.m,
uipushtool.m, uitoggletool.m, uitoolbar.m, waitforbuttonpress.m, help.m,
__additional_help_message__.m, hsv.m, im2double.m, im2frame.m, javachk.m,
usejava.m, argnames.m, char.m, formula.m, inline.m, __vectorize__.m, findstr.m,
flipdim.m, strmatch.m, vectorize.m, commutation_matrix.m, cond.m, cross.m,
duplication_matrix.m, expm.m, orth.m, rank.m, rref.m, trace.m, vech.m, cast.m,
compare_versions.m, delete.m, dir.m, fileattrib.m, grabcode.m, gunzip.m,
inputname.m, license.m, list_primes.m, ls.m, mexext.m, movefile.m,
namelengthmax.m, nargoutchk.m, nthargout.m, substruct.m, swapbytes.m, ver.m,
verLessThan.m, what.m, fminunc.m, fsolve.m, fzero.m, optimget.m, __fdjac__.m,
matlabroot.m, savepath.m, campos.m, camroll.m, camtarget.m, camup.m, camva.m,
camzoom.m, clabel.m, diffuse.m, legend.m, orient.m, rticks.m, specular.m,
thetaticks.m, xlim.m, xtickangle.m, xticklabels.m, xticks.m, ylim.m,
ytickangle.m, yticklabels.m, yticks.m, zlim.m, ztickangle.m, zticklabels.m,
zticks.m, ellipsoid.m, isocolors.m, isonormals.m, stairs.m, surfnorm.m,
__actual_axis_position__.m, __pltopt__.m, close.m, graphics_toolkit.m, pan.m,
print.m, printd.m, __ghostscript__.m, __gnuplot_print__.m, __opengl_print__.m,
rotate3d.m, subplot.m, zoom.m, compan.m, conv.m, poly.m, polyaffine.m,
polyder.m, polyint.m, polyout.m, polyreduce.m, polyvalm.m, roots.m, prefdir.m,
prefsfile.m, profexplore.m, profexport.m, profshow.m, powerset.m, unique.m,
arch_rnd.m, arma_rnd.m, autoreg_matrix.m, bartlett.m, blackman.m, detrend.m,
durbinlevinson.m, fftconv.m, fftfilt.m, fftshift.m, fractdiff.m, hamming.m,
hanning.m, hurst.m, ifftshift.m, rectangle_lw.m, rectangle_sw.m, triangle_lw.m,
sinc.m, sinetone.m, sinewave.m, spectral_adf.m, spectral_xdf.m, spencer.m,
ilu.m, __sprand__.m, sprand.m, sprandn.m, sprandsym.m, treelayout.m, beta.m,
betainc.m, betaincinv.m, betaln.m, cosint.m, expint.m, factorial.m, gammainc.m,
gammaincinv.m, lcm.m, nthroot.m, perms.m, reallog.m, realpow.m, realsqrt.m,
sinint.m, hadamard.m, hankel.m, hilb.m, invhilb.m, magic.m, pascal.m, rosser.m,
toeplitz.m, vander.m, wilkinson.m, center.m, corr.m, cov.m, discrete_cdf.m,
discrete_inv.m, discrete_pdf.m, discrete_rnd.m, empirical_cdf.m,
empirical_inv.m, empirical_pdf.m, empirical_rnd.m, kendall.m, kurtosis.m,
mad.m, mean.m, meansq.m, median.m, mode.m, moment.m, range.m, ranks.m,
run_count.m, skewness.m, spearman.m, statistics.m, std.m, base2dec.m,
bin2dec.m, blanks.m, cstrcat.m, deblank.m, dec2base.m, dec2bin.m, dec2hex.m,
hex2dec.m, index.m, regexptranslate.m, rindex.m, strcat.m, strjust.m,
strtrim.m, strtrunc.m, substr.m, untabify.m, __have_feature__.m,
__prog_output_assert__.m, __run_test_suite__.m, example.m, fail.m, asctime.m,
calendar.m, ctime.m, date.m, etime.m:
Add return arguments to @deftypefn macros where they were missing. Rename
variables in functions (particularly generic "retval") to match documentation.
Rename some return variables for (hopefully) better clarity (e.g., 'ax' to 'hax'
to indicate it is a graphics handle to an axes object).
author | Rik <rik@octave.org> |
---|---|
date | Wed, 30 Mar 2022 20:40:27 -0700 |
parents | 796f54d4ddbf |
children | 597f3ee61a48 |
rev | line source |
---|---|
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
1 ######################################################################## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
2 ## |
30564
796f54d4ddbf
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
29359
diff
changeset
|
3 ## Copyright (C) 2004-2022 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
|
4 ## |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
5 ## See the file COPYRIGHT.md in the top-level directory of this |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
6 ## distribution or <https://octave.org/copyright/>. |
5164 | 7 ## |
7016 | 8 ## This file is part of Octave. |
9 ## | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
10 ## Octave is free software: you can redistribute it and/or modify it |
7016 | 11 ## 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
|
12 ## 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
|
13 ## (at your option) any later version. |
7016 | 14 ## |
15 ## Octave is distributed in the hope that it will be useful, but | |
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
18 ## GNU General Public License for more details. |
7016 | 19 ## |
20 ## You should have received a copy of the GNU General Public License | |
21 ## 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
|
22 ## <https://www.gnu.org/licenses/>. |
7016 | 23 ## |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
24 ######################################################################## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
25 ## |
7016 | 26 ## Original version by Paul Kienzle distributed as free software in the |
27 ## public domain. | |
5164 | 28 |
29 ## -*- texinfo -*- | |
30875
5d3faba0342e
doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
30 ## @deftypefn {} {@var{s} =} sprandn (@var{m}, @var{n}, @var{d}) |
5d3faba0342e
doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
31 ## @deftypefnx {} {@var{s} =} sprandn (@var{m}, @var{n}, @var{d}, @var{rc}) |
5d3faba0342e
doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
32 ## @deftypefnx {} {@var{s} =} sprandn (@var{s}) |
18590 | 33 ## Generate a sparse matrix with normally distributed random values. |
34 ## | |
35 ## The size of the matrix is @var{m}x@var{n} with a density of values @var{d}. | |
36 ## @var{d} must be between 0 and 1. Values will be normally distributed with a | |
37 ## mean of 0 and a variance of 1. | |
5164 | 38 ## |
18590 | 39 ## 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
|
40 ## random values wherever the matrix @var{s} is nonzero. |
18590 | 41 ## |
42 ## If called with a scalar fourth argument @var{rc}, a random sparse matrix | |
43 ## with reciprocal condition number @var{rc} is generated. If @var{rc} is | |
44 ## a vector, then it specifies the first singular values of the generated | |
45 ## matrix (@code{length (@var{rc}) <= min (@var{m}, @var{n})}). | |
19597
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19596
diff
changeset
|
46 ## |
18590 | 47 ## @seealso{sprand, sprandsym, randn} |
5164 | 48 ## @end deftypefn |
49 | |
18590 | 50 function s = sprandn (m, n, d, rc) |
6498 | 51 |
28789
28de41192f3c
Eliminate unneeded verification of nargin, nargout in m-files.
Rik <rik@octave.org>
parents:
28059
diff
changeset
|
52 if (nargin == 1) |
20399
26bd6008fc9c
maint: Rename __sprand_impl__.m to __sprand__.m
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
53 s = __sprand__ (m, @randn); |
28789
28de41192f3c
Eliminate unneeded verification of nargin, nargout in m-files.
Rik <rik@octave.org>
parents:
28059
diff
changeset
|
54 elseif (nargin == 3) |
20399
26bd6008fc9c
maint: Rename __sprand_impl__.m to __sprand__.m
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
55 s = __sprand__ (m, n, d, "sprandn", @randn); |
18589
35a5e7740a6d
Added implementation for 4th argument of sprand/sprandn (bug #41839).
Eduardo Ramos (edu159) <eduradical951@gmail.com>
parents:
17744
diff
changeset
|
56 elseif (nargin == 4) |
20399
26bd6008fc9c
maint: Rename __sprand_impl__.m to __sprand__.m
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
57 s = __sprand__ (m, n, d, rc, "sprandn", @randn); |
13197
6db186dfdeaa
Refactor sprandn/sprand code, move common code to common function (bug #34352)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
58 else |
6046 | 59 print_usage (); |
5164 | 60 endif |
13058
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
61 |
5164 | 62 endfunction |
13058
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
63 |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
64 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
65 ## Test 3-input calling form |
13058
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
66 %!test |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
67 %! s = sprandn (4, 10, 0.1); |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
68 %! assert (size (s), [4, 10]); |
13197
6db186dfdeaa
Refactor sprandn/sprand code, move common code to common function (bug #34352)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
69 %! assert (nnz (s) / numel (s), 0.1); |
13058
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
70 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
71 ## 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
|
72 %!test |
18590 | 73 %! d = rand (); |
18589
35a5e7740a6d
Added implementation for 4th argument of sprand/sprandn (bug #41839).
Eduardo Ramos (edu159) <eduradical951@gmail.com>
parents:
17744
diff
changeset
|
74 %! s1 = sprandn (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
|
75 %! 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
|
76 %! s2 = sprandn (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
|
77 %! s3 = sprandn (6, 4, d, rc); |
19597
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19596
diff
changeset
|
78 %! 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
|
79 %! assert (1/cond (s1), 0.4, sqrt (eps)); |
19597
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19596
diff
changeset
|
80 %! assert (nnz (s1) / (100*100), d, 0.02); |
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19596
diff
changeset
|
81 %! 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
|
82 %! 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
|
83 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
84 ## Test 1-input calling form |
13058
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
85 %!test |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
86 %! s = sprandn (sparse ([1 2 3], [3 2 3], [2 2 2])); |
13064
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
13058
diff
changeset
|
87 %! [i, j] = find (s); |
13058
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
88 %! assert (sort (i), [1 2 3]'); |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
89 %! assert (sort (j), [2 3 3]'); |
14422cc782b2
codesprint: Write input validation and tests for sprandn.m
Rik <octave@nomad.inbox5.com>
parents:
11587
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 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
|
92 %!test |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
93 %! s = sprandn (1e6,1e6,1e-7); |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
94 |
28059
e9d57f6d6353
allow sprand and sprandsym to create empty sparse matrices
John W. Eaton <jwe@octave.org>
parents:
27978
diff
changeset
|
95 ## Test empty array creation |
e9d57f6d6353
allow sprand and sprandsym to create empty sparse matrices
John W. Eaton <jwe@octave.org>
parents:
27978
diff
changeset
|
96 %!assert (size (sprandn (0, 0, 0.5)), [0, 0]) |
e9d57f6d6353
allow sprand and sprandsym to create empty sparse matrices
John W. Eaton <jwe@octave.org>
parents:
27978
diff
changeset
|
97 %!assert (size (sprandn (0, 3, 0.5)), [0, 3]) |
e9d57f6d6353
allow sprand and sprandsym to create empty sparse matrices
John W. Eaton <jwe@octave.org>
parents:
27978
diff
changeset
|
98 %!assert (size (sprandn (3, 0, 0.5)), [3, 0]) |
e9d57f6d6353
allow sprand and sprandsym to create empty sparse matrices
John W. Eaton <jwe@octave.org>
parents:
27978
diff
changeset
|
99 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
100 ## Test input validation |
28886
d8318c12d903
test: remove unnecessary BIST tests in m-files checking for excessive number of inputs.
Rik <rik@octave.org>
parents:
28789
diff
changeset
|
101 %!error <Invalid call> sprandn () |
d8318c12d903
test: remove unnecessary BIST tests in m-files checking for excessive number of inputs.
Rik <rik@octave.org>
parents:
28789
diff
changeset
|
102 %!error <Invalid call> sprandn (1, 2) |
28059
e9d57f6d6353
allow sprand and sprandsym to create empty sparse matrices
John W. Eaton <jwe@octave.org>
parents:
27978
diff
changeset
|
103 %!error <M must be a non-negative integer> sprand (-1, -1, 0.5) |
e9d57f6d6353
allow sprand and sprandsym to create empty sparse matrices
John W. Eaton <jwe@octave.org>
parents:
27978
diff
changeset
|
104 %!error <M must be a non-negative integer> sprandn (ones (3), 3, 0.5) |
e9d57f6d6353
allow sprand and sprandsym to create empty sparse matrices
John W. Eaton <jwe@octave.org>
parents:
27978
diff
changeset
|
105 %!error <M must be a non-negative integer> sprandn (3.5, 3, 0.5) |
e9d57f6d6353
allow sprand and sprandsym to create empty sparse matrices
John W. Eaton <jwe@octave.org>
parents:
27978
diff
changeset
|
106 %!error <M must be a non-negative integer> sprandn (-1, 3, 0.5) |
e9d57f6d6353
allow sprand and sprandsym to create empty sparse matrices
John W. Eaton <jwe@octave.org>
parents:
27978
diff
changeset
|
107 %!error <N must be a non-negative integer> sprandn (3, ones (3), 0.5) |
e9d57f6d6353
allow sprand and sprandsym to create empty sparse matrices
John W. Eaton <jwe@octave.org>
parents:
27978
diff
changeset
|
108 %!error <N must be a non-negative integer> sprandn (3, 3.5, 0.5) |
e9d57f6d6353
allow sprand and sprandsym to create empty sparse matrices
John W. Eaton <jwe@octave.org>
parents:
27978
diff
changeset
|
109 %!error <N must be a non-negative integer> sprandn (3, -1, 0.5) |
18590 | 110 %!error <D must be between 0 and 1> sprandn (3, 3, -1) |
111 %!error <D must be between 0 and 1> sprandn (3, 3, 2) | |
112 %!error <RC must be a scalar or vector> sprandn (2, 2, 0.2, ones (3,3)) | |
113 %!error <RC must be between 0 and 1> sprandn (2, 2, 0.2, -1) | |
114 %!error <RC must be between 0 and 1> sprandn (2, 2, 0.2, 2) |