]> Creatis software - CreaPhase.git/blob - octave_packages/m/sparse/sprandn.m
update packages
[CreaPhase.git] / octave_packages / m / sparse / sprandn.m
1 ## Copyright (C) 2004-2012 Paul Kienzle
2 ##
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.
21
22 ## -*- texinfo -*-
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.
29 ##
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}
33 ## @end deftypefn
34
35 ## Author: Paul Kienzle <pkienzle@users.sf.net>
36
37 function S = sprandn (m, n, d)
38
39   if (nargin == 1 )
40     S = __sprand_impl__ (m, @randn);
41   elseif ( nargin == 3)
42     S = __sprand_impl__ (m, n, d, "sprandn", @randn);
43   else
44     print_usage ();
45   endif
46
47 endfunction
48
49
50 %!test
51 %! s = sprandn (4, 10, 0.1);
52 %! assert (size (s), [4, 10]);
53 %! assert (nnz (s) / numel (s), 0.1);
54
55 %% Test 1-input calling form
56 %!test
57 %! s = sprandn (sparse ([1 2 3], [3 2 3], [2 2 2]));
58 %! [i, j] = find (s);
59 %! assert (sort (i), [1 2 3]');
60 %! assert (sort (j), [2 3 3]');
61
62 %% Test input validation
63 %!error sprandn ()
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)
74