1 ## Copyright (C) 2004-2012 Paul Kienzle
3 ## This file is part of Octave.
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.
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.
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/>.
19 ## Original version by Paul Kienzle distributed as free software in the
23 ## @deftypefn {Function File} {} sprandn (@var{m}, @var{n}, @var{d})
24 ## @deftypefnx {Function File} {} sprandn (@var{s})
25 ## Generate a random sparse matrix. The size of the matrix will be
26 ## @var{m} by @var{n}, with a density of values given by @var{d}.
27 ## @var{d} should be between 0 and 1. Values will be normally
28 ## distributed with mean of zero and variance 1.
30 ## If called with a single matrix argument, a random sparse matrix is
31 ## generated wherever the matrix @var{S} is non-zero.
32 ## @seealso{sprand, sprandsym}
35 ## Author: Paul Kienzle <pkienzle@users.sf.net>
37 function S = sprandn (m, n, d)
40 S = __sprand_impl__ (m, @randn);
42 S = __sprand_impl__ (m, n, d, "sprandn", @randn);
51 %! s = sprandn (4, 10, 0.1);
52 %! assert (size (s), [4, 10]);
53 %! assert (nnz (s) / numel (s), 0.1);
55 %% Test 1-input calling form
57 %! s = sprandn (sparse ([1 2 3], [3 2 3], [2 2 2]));
59 %! assert (sort (i), [1 2 3]');
60 %! assert (sort (j), [2 3 3]');
62 %% Test input validation
64 %!error sprandn (1, 2)
65 %!error sprandn (1, 2, 3, 4)
66 %!error sprandn (ones(3), 3, 0.5)
67 %!error sprandn (3.5, 3, 0.5)
68 %!error sprandn (0, 3, 0.5)
69 %!error sprandn (3, ones(3), 0.5)
70 %!error sprandn (3, 3.5, 0.5)
71 %!error sprandn (3, 0, 0.5)
72 %!error sprandn (3, 3, -1)
73 %!error sprandn (3, 3, 2)