Mercurial > hg > octave-nkf
annotate 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 |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 2004-2011 David Bateman and Andy Adler |
13205
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
2 ## Copyright (C) 2011 Jordi GutiƩrrez Hermoso |
5610 | 3 ## |
7016 | 4 ## This file is part of Octave. |
5610 | 5 ## |
7016 | 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. | |
10 ## | |
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. | |
5610 | 15 ## |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
5610 | 19 |
20 ## -*- texinfo -*- | |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
21 ## @deftypefn {Function File} {} sprandsym (@var{n}, @var{d}) |
5610 | 22 ## @deftypefnx {Function File} {} sprandsym (@var{s}) |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
23 ## Generate a symmetric random sparse matrix. The size of the matrix will be |
5610 | 24 ## @var{n} by @var{n}, with a density of values given by @var{d}. |
25 ## @var{d} should be between 0 and 1. Values will be normally | |
26 ## distributed with mean of zero and variance 1. | |
27 ## | |
28 ## If called with a single matrix argument, a random sparse matrix is | |
29 ## generated wherever the matrix @var{S} is non-zero in its lower | |
30 ## triangular part. | |
5642 | 31 ## @seealso{sprand, sprandn} |
5610 | 32 ## @end deftypefn |
33 | |
6252 | 34 function S = sprandsym (n, d) |
8506 | 35 |
13064
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
36 if (nargin != 1 && nargin != 2) |
6046 | 37 print_usage (); |
5610 | 38 endif |
13064
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
39 |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
40 if (nargin == 1) |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
41 [i, j] = find (tril (n)); |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
42 [nr, nc] = size (n); |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
43 S = sparse (i, j, randn (size (i)), nr, nc); |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
44 S = S + tril (S, -1)'; |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
45 return; |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
46 endif |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
47 |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
48 if (!(isscalar (n) && n == fix (n) && n > 0)) |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
49 error ("sprand: N must be an integer greater than 0"); |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
50 endif |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
51 |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
52 if (d < 0 || d > 1) |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
53 error ("sprand: density D must be between 0 and 1"); |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
54 endif |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
55 |
13205
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
56 ## Actual number of nonzero entries |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
57 k = round (n^2*d); |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
58 |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
59 ## Diagonal nonzero entries, same parity as k |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
60 r = pick_rand_diag (n, k); |
13064
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
61 |
13205
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
62 ## Off diagonal nonzero entries |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
63 m = (k - r)/2; |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
64 |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
65 ondiag = randperm (n, r); |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
66 offdiag = randperm (n*(n - 1)/2, m); |
13064
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
67 |
13205
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
68 ## Do five Newton iterations to solve n(n - 1)/2 = offdiag (this is the |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
69 ## row index) |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
70 x = sqrt (offdiag); |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
71 for ii = 1:5 |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
72 x = x - (x.^2 - x - 2*offdiag)./(2*x - 1); |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
73 endfor |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
74 i = floor(x); |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
75 i(i.^2 - i - 2*offdiag != 0) += 1; |
13064
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
76 |
13205
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
77 ## Column index |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
78 j = offdiag - (i - 1).*(i - 2)/2; |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
79 |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
80 diagvals = randn (1, r); |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
81 offdiagvals = randn (1, m); |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
82 |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
83 S = sparse ([ondiag, i, j], [ondiag, j, i], |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
84 [diagvals, offdiagvals, offdiagvals], n, n); |
13064
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
85 |
5610 | 86 endfunction |
13064
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
87 |
13205
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
88 function r = pick_rand_diag (n, k) |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
89 ## Pick a random number R of entries for the diagonal of a sparse NxN |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
90 ## square matrix with exactly K nonzero entries, ensuring that this R |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
91 ## is chosen uniformly over all such matrices. |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
92 ## |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
93 ## Let D be the number of diagonal entries and M the number of |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
94 ## off-diagonal entries. Then K = D + 2*M. Let A = N*(N-1)/2 be the |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
95 ## number of available entries in the upper triangle of the matrix. |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
96 ## Then, by a simple counting argument, there is a total of |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
97 ## |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
98 ## T = nchoosek (N, D) * nchoosek (A, M) |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
99 ## |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
100 ## symmetric NxN matrices with a total of K nonzero entries and D on |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
101 ## the diagonal. Letting D range from mod (K,2) through min (N,K), and |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
102 ## dividing by this sum, we obtain the probability P for D to be each |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
103 ## of those values. |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
104 ## |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
105 ## However, we cannot use this form for computation, as the binomial |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
106 ## coefficients become unmanageably large. Instead, we use the |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
107 ## successive quotients Q(i) = T(i+1)/T(i), which we easily compute to |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
108 ## be |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
109 ## |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
110 ## (N - D)*(N - D - 1)*M |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
111 ## Q = ------------------------------- |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
112 ## (D + 2)*(D + 1)*(A - M + 1) |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
113 ## |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
114 ## Then the cumprod of these quotients is |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
115 ## |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
116 ## C = [ T(2)/T(1), T(3)/T(1), ..., T(N)/T(1) ] |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
117 ## |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
118 ## Their sum + 1 is thus S = sum (T)/T(1), and then C(i)/S is the |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
119 ## desired probability P(i) for i = 2:N. The first P(1) can be |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
120 ## obtained by the condition that sum (P) = 1, and the cumsum will |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
121 ## give the distribution function for computing the random number of |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
122 ## entries on the diagonal R. |
13064
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
123 |
13205
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
124 ## Compute the stuff described above |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
125 a = n*(n - 1)/2; |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
126 d = [mod(k,2):2:min(n,k)-2]; |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
127 m = (k - d)/2; |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
128 q = (n - d).*(n - d - 1).*m ./ (d + 2)./(d + 1)./(a - m + 1); |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
129 c = cumprod (q); |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
130 s = sum (c) + 1; |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
131 p = c/s; |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
132 |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
133 ## Add missing entries |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
134 p = [1 - sum(p), p]; |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
135 d(end+1) = d(end) + 2; |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
136 |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
137 ## Pick a random r using this distribution |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
138 r = d(sum (cumsum (p) < rand) + 1); |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
139 |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
140 endfunction |
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
141 |
13064
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
142 %!test |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
143 %! s = sprandsym (10, 0.1); |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
144 %! assert (issparse (s)); |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
145 %! assert (issymmetric (s)); |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
146 %! assert (size (s), [10, 10]); |
13205
abf1e00111dd
Completely new implementation of sprandsym
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13064
diff
changeset
|
147 %! assert (nnz (s) / numel (s), 0.1, .01); |
13064
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
148 |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
149 %% Test 1-input calling form |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
150 %!test |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
151 %! s = sprandsym (sparse ([1 2 3], [3 2 3], [2 2 2])); |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
152 %! [i, j] = find (s); |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
153 %! assert (sort (i), [2 3]'); |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
154 %! assert (sort (j), [2 3]'); |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
155 |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
156 %% Test input validation |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
157 %!error sprandsym () |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
158 %!error sprandsym (1, 2, 3) |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
159 %!error sprandsym (ones(3), 0.5) |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
160 %!error sprandsym (3.5, 0.5) |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
161 %!error sprandsym (0, 0.5) |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
162 %!error sprandsym (3, -1) |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
163 %!error sprandsym (3, 2) |
bae887ebea48
codesprint: Add input validation and tests for sprandsym.m
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
164 |