5610
|
1 ## Copyright (C) 2004 David Bateman & Andy Adler |
|
2 ## |
|
3 ## This program is free software; you can redistribute it and/or modify |
|
4 ## it under the terms of the GNU General Public License as published by |
|
5 ## the Free Software Foundation; either version 2 of the License, or |
|
6 ## (at your option) any later version. |
|
7 ## |
|
8 ## This program is distributed in the hope that it will be useful, |
|
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 ## GNU General Public License for more details. |
|
12 ## |
|
13 ## You should have received a copy of the GNU General Public License |
|
14 ## along with this program; if not, write to the Free Software |
|
15 ## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
16 ## 02110-1301 USA |
|
17 |
|
18 ## -*- texinfo -*- |
|
19 ## @deftypefn {Function File} {} sprandsym (@var{n}, @var{d}) |
|
20 ## @deftypefnx {Function File} {} sprandsym (@var{s}) |
|
21 ## Generate a symmetric random sparse matrix. The size of the matrix will be |
|
22 ## @var{n} by @var{n}, with a density of values given by @var{d}. |
|
23 ## @var{d} should be between 0 and 1. Values will be normally |
|
24 ## distributed with mean of zero and variance 1. |
|
25 ## |
|
26 ## Note: sometimes the actual density may be a bit smaller than @var{d}. |
|
27 ## This is unlikely to happen for large really sparse matrices. |
|
28 ## |
|
29 ## If called with a single matrix argument, a random sparse matrix is |
|
30 ## generated wherever the matrix @var{S} is non-zero in its lower |
|
31 ## triangular part. |
|
32 ## @end deftypefn |
|
33 ## @seealso{sprand, sprandn} |
|
34 |
|
35 function S = sprandsym(n,d) |
|
36 if nargin == 1 |
|
37 [i,j,v,nr,nc] = spfind(tril(n)); |
|
38 S = sparse(i,j,randn(size(v)),nr,nc); |
|
39 S = S + tril(S,-1)'; |
|
40 elseif nargin == 2 |
|
41 m1 = floor(n/2); |
|
42 n1 = m1 + 1; |
|
43 mn1 = m1*n1; |
|
44 k1 = round(d*mn1); |
|
45 idx1=unique(fix(rand(min(k1*1.01,k1+10),1)*mn1))+1; |
|
46 # idx contains random numbers in [1,mn] |
|
47 # generate 1% or 10 more random values than necessary |
|
48 # in order to reduce the probability that there are less than k |
|
49 # distinct values; |
|
50 # maybe a better strategy could be used |
|
51 # but I don't think it's worth the price |
|
52 k1 = min(length(idx1),k1); # actual number of entries in S |
|
53 j1 = floor((idx1(1:k1)-1)/m1); |
|
54 i1 = idx1(1:k1) - j1*m1; |
|
55 |
|
56 n2 = ceil(n/2); |
|
57 nn2 = n2*n2; |
|
58 k2 = round(d*nn2); |
|
59 idx2=unique(fix(rand(min(k2*1.01,k1+10),1)*nn2))+1; |
|
60 k2 = min(length(idx2),k2); |
|
61 j2 = floor((idx2(1:k2)-1)/n2); |
|
62 i2 = idx2(1:k2) - j2*n2; |
|
63 |
|
64 if isempty(i1) && isempty(i2) |
|
65 S = sparse(n,n); |
|
66 else |
|
67 S1 = sparse(i1,j1+1,randn(k1,1),m1,n1); |
|
68 S = [tril(S1), sparse(m1,m1); ... |
|
69 sparse(i2,j2+1,randn(k2,1),n2,n2), triu(S1,1)']; |
|
70 S = S + tril(S,-1)'; |
|
71 endif |
|
72 else |
|
73 usage("sprandsym(n,density) OR sprandsym(S)"); |
|
74 endif |
|
75 endfunction |