7017
|
1 ## Copyright (C) 2004, 2006, 2007 David Bateman & Andy Adler |
5610
|
2 ## |
7016
|
3 ## This file is part of Octave. |
5610
|
4 ## |
7016
|
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. |
5610
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
5610
|
18 |
|
19 ## -*- texinfo -*- |
|
20 ## @deftypefn {Function File} {} sprandsym (@var{n}, @var{d}) |
|
21 ## @deftypefnx {Function File} {} sprandsym (@var{s}) |
|
22 ## Generate a symmetric random sparse matrix. The size of the matrix will be |
|
23 ## @var{n} by @var{n}, with a density of values given by @var{d}. |
|
24 ## @var{d} should be between 0 and 1. Values will be normally |
|
25 ## distributed with mean of zero and variance 1. |
|
26 ## |
|
27 ## Note: sometimes the actual density may be a bit smaller than @var{d}. |
|
28 ## This is unlikely to happen for large really sparse matrices. |
|
29 ## |
|
30 ## If called with a single matrix argument, a random sparse matrix is |
|
31 ## generated wherever the matrix @var{S} is non-zero in its lower |
|
32 ## triangular part. |
5642
|
33 ## @seealso{sprand, sprandn} |
5610
|
34 ## @end deftypefn |
|
35 |
6252
|
36 function S = sprandsym (n, d) |
|
37 if (nargin == 1) |
|
38 [i, j, v, nr, nc] = spfind (tril (n)); |
|
39 S = sparse (i, j, randn (size (v)), nr, nc); |
|
40 S = S + tril (S, -1)'; |
|
41 elseif (nargin == 2) |
|
42 m1 = floor (n/2); |
|
43 n1 = m1 + rem (n, 2); |
5610
|
44 mn1 = m1*n1; |
6252
|
45 k1 = round (d*mn1); |
6498
|
46 idx1 = unique (fix (rand (min (k1*1.01, k1+10), 1) * mn1)) + 1; |
6252
|
47 ## idx contains random numbers in [1,mn] generate 1% or 10 more |
|
48 ## random values than necessary in order to reduce the probability |
|
49 ## that there are less than k distinct values; maybe a better |
|
50 ## strategy could be used but I don't think it's worth the price. |
|
51 k1 = min (length (idx1), k1); # actual number of entries in S |
|
52 j1 = floor ((idx1(1:k1)-1)/m1); |
5610
|
53 i1 = idx1(1:k1) - j1*m1; |
|
54 |
6252
|
55 n2 = ceil (n/2); |
5610
|
56 nn2 = n2*n2; |
6252
|
57 k2 = round (d*nn2); |
6498
|
58 idx2 = unique (fix (rand (min (k2*1.01, k1+10), 1) * nn2)) + 1; |
6252
|
59 k2 = min (length (idx2), k2); |
|
60 j2 = floor ((idx2(1:k2)-1)/n2); |
5610
|
61 i2 = idx2(1:k2) - j2*n2; |
|
62 |
6252
|
63 if (isempty (i1) && isempty (i2)) |
|
64 S = sparse (n, n); |
5610
|
65 else |
6252
|
66 S1 = sparse (i1, j1+1, randn (k1, 1), m1, n1); |
7017
|
67 S = [tril(S1), sparse(m1,m1); ... |
5610
|
68 sparse(i2,j2+1,randn(k2,1),n2,n2), triu(S1,1)']; |
6252
|
69 S = S + tril (S, -1)'; |
5610
|
70 endif |
|
71 else |
6046
|
72 print_usage (); |
5610
|
73 endif |
|
74 endfunction |