Mercurial > hg > octave-nkf
annotate scripts/sparse/sprandsym.m @ 10793:be55736a0783
Grammarcheck the documentation from m-files.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Sun, 18 Jul 2010 20:35:16 -0700 |
parents | 95c3e38098bf |
children | fd0a3ac60b0e |
rev | line source |
---|---|
9245 | 1 ## Copyright (C) 2004, 2006, 2007, 2008, 2009 David Bateman and 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 -*- | |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
20 ## @deftypefn {Function File} {} sprandsym (@var{n}, @var{d}) |
5610 | 21 ## @deftypefnx {Function File} {} sprandsym (@var{s}) |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
22 ## Generate a symmetric random sparse matrix. The size of the matrix will be |
5610 | 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 ## | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
27 ## Note: sometimes the actual density may be a bit smaller than @var{d}. |
5610 | 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) | |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
38 [i, j, v] = find (tril (n)); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
39 [nr, nc] = size (n); |
6252 | 40 S = sparse (i, j, randn (size (v)), nr, nc); |
41 S = S + tril (S, -1)'; | |
42 elseif (nargin == 2) | |
43 m1 = floor (n/2); | |
44 n1 = m1 + rem (n, 2); | |
5610 | 45 mn1 = m1*n1; |
6252 | 46 k1 = round (d*mn1); |
6498 | 47 idx1 = unique (fix (rand (min (k1*1.01, k1+10), 1) * mn1)) + 1; |
6252 | 48 ## idx contains random numbers in [1,mn] generate 1% or 10 more |
49 ## random values than necessary in order to reduce the probability | |
50 ## that there are less than k distinct values; maybe a better | |
51 ## strategy could be used but I don't think it's worth the price. | |
8506 | 52 |
53 ## Actual number of entries in S. | |
54 k1 = min (length (idx1), k1); | |
6252 | 55 j1 = floor ((idx1(1:k1)-1)/m1); |
5610 | 56 i1 = idx1(1:k1) - j1*m1; |
57 | |
6252 | 58 n2 = ceil (n/2); |
5610 | 59 nn2 = n2*n2; |
6252 | 60 k2 = round (d*nn2); |
6498 | 61 idx2 = unique (fix (rand (min (k2*1.01, k1+10), 1) * nn2)) + 1; |
6252 | 62 k2 = min (length (idx2), k2); |
63 j2 = floor ((idx2(1:k2)-1)/n2); | |
5610 | 64 i2 = idx2(1:k2) - j2*n2; |
65 | |
6252 | 66 if (isempty (i1) && isempty (i2)) |
67 S = sparse (n, n); | |
5610 | 68 else |
6252 | 69 S1 = sparse (i1, j1+1, randn (k1, 1), m1, n1); |
7017 | 70 S = [tril(S1), sparse(m1,m1); ... |
10549 | 71 sparse(i2,j2+1,randn(k2,1),n2,n2), triu(S1,1)']; |
6252 | 72 S = S + tril (S, -1)'; |
5610 | 73 endif |
74 else | |
6046 | 75 print_usage (); |
5610 | 76 endif |
77 endfunction |