Mercurial > hg > octave-thorsten
comparison scripts/sparse/sprandsym.m @ 13205:abf1e00111dd
Completely new implementation of sprandsym
author | Jordi Gutiérrez Hermoso <jordigh@octave.org> |
---|---|
date | Sat, 24 Sep 2011 03:46:36 -0500 |
parents | bae887ebea48 |
children | 0257eb36e15a |
comparison
equal
deleted
inserted
replaced
13204:be7bfd59300a | 13205:abf1e00111dd |
---|---|
1 ## Copyright (C) 2004-2011 David Bateman and Andy Adler | 1 ## Copyright (C) 2004-2011 David Bateman and Andy Adler |
2 ## Copyright (C) 2011 Jordi GutiƩrrez Hermoso | |
2 ## | 3 ## |
3 ## This file is part of Octave. | 4 ## This file is part of Octave. |
4 ## | 5 ## |
5 ## Octave is free software; you can redistribute it and/or modify it | 6 ## 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 ## under the terms of the GNU General Public License as published by |
21 ## @deftypefnx {Function File} {} sprandsym (@var{s}) | 22 ## @deftypefnx {Function File} {} sprandsym (@var{s}) |
22 ## Generate a symmetric random sparse matrix. The size of the matrix will be | 23 ## 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{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 ## @var{d} should be between 0 and 1. Values will be normally |
25 ## distributed with mean of zero and variance 1. | 26 ## 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 ## | 27 ## |
30 ## If called with a single matrix argument, a random sparse matrix is | 28 ## 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 | 29 ## generated wherever the matrix @var{S} is non-zero in its lower |
32 ## triangular part. | 30 ## triangular part. |
33 ## @seealso{sprand, sprandn} | 31 ## @seealso{sprand, sprandn} |
53 | 51 |
54 if (d < 0 || d > 1) | 52 if (d < 0 || d > 1) |
55 error ("sprand: density D must be between 0 and 1"); | 53 error ("sprand: density D must be between 0 and 1"); |
56 endif | 54 endif |
57 | 55 |
58 m1 = floor (n/2); | 56 ## Actual number of nonzero entries |
59 n1 = m1 + rem (n, 2); | 57 k = round (n^2*d); |
60 mn1 = m1*n1; | |
61 k1 = round (d*mn1); | |
62 idx1 = unique (fix (rand (min (k1*1.01, k1+10), 1) * mn1)) + 1; | |
63 ## idx contains random numbers in [1,mn] generate 1% or 10 more | |
64 ## random values than necessary in order to reduce the probability | |
65 ## that there are less than k distinct values; maybe a better | |
66 ## strategy could be used but I don't think it's worth the price. | |
67 | 58 |
68 ## Actual number of entries in S. | 59 ## Diagonal nonzero entries, same parity as k |
69 k1 = min (length (idx1), k1); | 60 r = pick_rand_diag (n, k); |
70 j1 = floor ((idx1(1:k1)-1)/m1); | |
71 i1 = idx1(1:k1) - j1*m1; | |
72 | 61 |
73 n2 = ceil (n/2); | 62 ## Off diagonal nonzero entries |
74 nn2 = n2*n2; | 63 m = (k - r)/2; |
75 k2 = round (d*nn2); | |
76 idx2 = unique (fix (rand (min (k2*1.01, k1+10), 1) * nn2)) + 1; | |
77 k2 = min (length (idx2), k2); | |
78 j2 = floor ((idx2(1:k2)-1)/n2); | |
79 i2 = idx2(1:k2) - j2*n2; | |
80 | 64 |
81 if (isempty (i1) && isempty (i2)) | 65 ondiag = randperm (n, r); |
82 S = sparse (n, n); | 66 offdiag = randperm (n*(n - 1)/2, m); |
83 else | 67 |
84 S1 = sparse (i1, j1+1, randn (k1, 1), m1, n1); | 68 ## Do five Newton iterations to solve n(n - 1)/2 = offdiag (this is the |
85 S = [tril(S1), sparse(m1,m1); ... | 69 ## row index) |
86 sparse(i2,j2+1,randn(k2,1),n2,n2), triu(S1,1)']; | 70 x = sqrt (offdiag); |
87 S = S + tril (S, -1)'; | 71 for ii = 1:5 |
88 endif | 72 x = x - (x.^2 - x - 2*offdiag)./(2*x - 1); |
73 endfor | |
74 i = floor(x); | |
75 i(i.^2 - i - 2*offdiag != 0) += 1; | |
76 | |
77 ## Column index | |
78 j = offdiag - (i - 1).*(i - 2)/2; | |
79 | |
80 diagvals = randn (1, r); | |
81 offdiagvals = randn (1, m); | |
82 | |
83 S = sparse ([ondiag, i, j], [ondiag, j, i], | |
84 [diagvals, offdiagvals, offdiagvals], n, n); | |
89 | 85 |
90 endfunction | 86 endfunction |
91 | 87 |
88 function r = pick_rand_diag (n, k) | |
89 ## Pick a random number R of entries for the diagonal of a sparse NxN | |
90 ## square matrix with exactly K nonzero entries, ensuring that this R | |
91 ## is chosen uniformly over all such matrices. | |
92 ## | |
93 ## Let D be the number of diagonal entries and M the number of | |
94 ## off-diagonal entries. Then K = D + 2*M. Let A = N*(N-1)/2 be the | |
95 ## number of available entries in the upper triangle of the matrix. | |
96 ## Then, by a simple counting argument, there is a total of | |
97 ## | |
98 ## T = nchoosek (N, D) * nchoosek (A, M) | |
99 ## | |
100 ## symmetric NxN matrices with a total of K nonzero entries and D on | |
101 ## the diagonal. Letting D range from mod (K,2) through min (N,K), and | |
102 ## dividing by this sum, we obtain the probability P for D to be each | |
103 ## of those values. | |
104 ## | |
105 ## However, we cannot use this form for computation, as the binomial | |
106 ## coefficients become unmanageably large. Instead, we use the | |
107 ## successive quotients Q(i) = T(i+1)/T(i), which we easily compute to | |
108 ## be | |
109 ## | |
110 ## (N - D)*(N - D - 1)*M | |
111 ## Q = ------------------------------- | |
112 ## (D + 2)*(D + 1)*(A - M + 1) | |
113 ## | |
114 ## Then the cumprod of these quotients is | |
115 ## | |
116 ## C = [ T(2)/T(1), T(3)/T(1), ..., T(N)/T(1) ] | |
117 ## | |
118 ## Their sum + 1 is thus S = sum (T)/T(1), and then C(i)/S is the | |
119 ## desired probability P(i) for i = 2:N. The first P(1) can be | |
120 ## obtained by the condition that sum (P) = 1, and the cumsum will | |
121 ## give the distribution function for computing the random number of | |
122 ## entries on the diagonal R. | |
92 | 123 |
93 ## FIXME: Test for density can't happen until code of sprandsym is improved | 124 ## Compute the stuff described above |
125 a = n*(n - 1)/2; | |
126 d = [mod(k,2):2:min(n,k)-2]; | |
127 m = (k - d)/2; | |
128 q = (n - d).*(n - d - 1).*m ./ (d + 2)./(d + 1)./(a - m + 1); | |
129 c = cumprod (q); | |
130 s = sum (c) + 1; | |
131 p = c/s; | |
132 | |
133 ## Add missing entries | |
134 p = [1 - sum(p), p]; | |
135 d(end+1) = d(end) + 2; | |
136 | |
137 ## Pick a random r using this distribution | |
138 r = d(sum (cumsum (p) < rand) + 1); | |
139 | |
140 endfunction | |
141 | |
94 %!test | 142 %!test |
95 %! s = sprandsym (10, 0.1); | 143 %! s = sprandsym (10, 0.1); |
96 %! assert (issparse (s)); | 144 %! assert (issparse (s)); |
97 %! assert (issymmetric (s)); | 145 %! assert (issymmetric (s)); |
98 %! assert (size (s), [10, 10]); | 146 %! assert (size (s), [10, 10]); |
99 ##%! assert (nnz (s) / numel (s), 0.1, .01); | 147 %! assert (nnz (s) / numel (s), 0.1, .01); |
100 | 148 |
101 %% Test 1-input calling form | 149 %% Test 1-input calling form |
102 %!test | 150 %!test |
103 %! s = sprandsym (sparse ([1 2 3], [3 2 3], [2 2 2])); | 151 %! s = sprandsym (sparse ([1 2 3], [3 2 3], [2 2 2])); |
104 %! [i, j] = find (s); | 152 %! [i, j] = find (s); |