1 ## Copyright (C) 2004-2012 Paul Kienzle
2 ## Copyright (C) 2012 Jordi GutiƩrrez Hermoso
4 ## This file is part of Octave.
6 ## Octave is free software; you can redistribute it and/or modify it
7 ## under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 3 of the License, or (at
9 ## your option) any later version.
11 ## Octave is distributed in the hope that it will be useful, but
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ## General Public License for more details.
16 ## You should have received a copy of the GNU General Public License
17 ## along with Octave; see the file COPYING. If not, see
18 ## <http://www.gnu.org/licenses/>.
20 ## Original version by Paul Kienzle distributed as free software in the
24 ## @deftypefn {Function File} {} __sprand_impl__ (@var{s}, @var{randfun})
25 ## @deftypefnx {Function File} {} __sprand_impl__ (@var{m}, @var{n}, @var{d}, @var{funname}, @var{randfun})
26 ## Undocumented internal function.
29 ## Actual implementation of sprand and sprandn happens here.
31 function S = __sprand_impl__ (varargin)
35 randfun = varargin{2};
38 S = sparse (i, j, randfun (size (i)), nr, nc);
42 [m, n, d, funname, randfun] = deal(varargin{:});
44 if (!(isscalar (m) && m == fix (m) && m > 0))
45 error ("%s: M must be an integer greater than 0", funname);
48 if (!(isscalar (n) && n == fix (n) && n > 0))
49 error ("%s: N must be an integer greater than 0", funname);
53 error ("%s: density D must be between 0 and 1", funname);
58 idx = randperm (mn, k);
60 [i, j] = ind2sub ([m, n], idx);
61 S = sparse (i, j, randfun (k, 1), m, n);