Mercurial > hg > octave-lyh
annotate scripts/linear-algebra/onenormest.m @ 8506:bc982528de11
comment style fixes
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 13 Jan 2009 11:56:00 -0500 |
parents | df9519e9990c |
children | cadc73247d65 |
rev | line source |
---|---|
7189 | 1 ## Copyright (C) 2007, Regents of the University of California |
2 ## | |
3 ## This file is part of Octave. | |
4 ## | |
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. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
16 ## along with Octave; see the file COPYING. If not, see | |
17 ## <http://www.gnu.org/licenses/>. | |
18 | |
19 ## -*- texinfo -*- | |
7191 | 20 ## @deftypefn {Function File} {[@var{est}, @var{v}, @var{w}, @var{iter}] =} onenormest (@var{a}, @var{t}) |
7189 | 21 ## @deftypefnx {Function File} {[@var{est}, @var{v}, @var{w}, @var{iter}] =} onenormest (@var{apply}, @var{apply_t}, @var{n}, @var{t}) |
22 ## | |
23 ## Apply Higham and Tisseur's randomized block 1-norm estimator to | |
7191 | 24 ## matrix @var{a} using @var{t} test vectors. If @var{t} exceeds 5, then |
7189 | 25 ## only 5 test vectors are used. |
26 ## | |
27 ## If the matrix is not explicit, e.g. when estimating the norm of | |
28 ## @code{inv (@var{A})} given an LU factorization, @code{onenormest} applies | |
29 ## @var{A} and its conjugate transpose through a pair of functions | |
30 ## @var{apply} and @var{apply_t}, respectively, to a dense matrix of size | |
31 ## @var{n} by @var{t}. The implicit version requires an explicit dimension | |
32 ## @var{n}. | |
33 ## | |
34 ## Returns the norm estimate @var{est}, two vectors @var{v} and | |
35 ## @var{w} related by norm | |
36 ## @code{(@var{w}, 1) = @var{est} * norm (@var{v}, 1)}, | |
37 ## and the number of iterations @var{iter}. The number of | |
38 ## iterations is limited to 10 and is at least 2. | |
39 ## | |
40 ## References: | |
41 ## @itemize | |
42 ## @item Nicholas J. Higham and Françoise Tisseur, "A Block Algorithm | |
43 ## for Matrix 1-Norm Estimation, with an Application to 1-Norm | |
44 ## Pseudospectra." SIMAX vol 21, no 4, pp 1185-1201. | |
45 ## @url{http://dx.doi.org/10.1137/S0895479899356080} | |
46 ## @item Nicholas J. Higham and Françoise Tisseur, "A Block Algorithm | |
47 ## for Matrix 1-Norm Estimation, with an Application to 1-Norm | |
48 ## Pseudospectra." @url{http://citeseer.ist.psu.edu/223007.html} | |
49 ## @end itemize | |
50 ## | |
51 ## @seealso{condest, norm, cond} | |
52 ## @end deftypefn | |
53 | |
54 ## Code originally licensed under | |
55 ## | |
56 ## Copyright (c) 2007, Regents of the University of California | |
57 ## All rights reserved. | |
7191 | 58 ## |
7189 | 59 ## Redistribution and use in source and binary forms, with or without |
7191 | 60 ## modification, are permitted provided that the following conditions |
61 ## are met: | |
7189 | 62 ## |
63 ## * Redistributions of source code must retain the above copyright | |
64 ## notice, this list of conditions and the following disclaimer. | |
7191 | 65 ## |
66 ## * Redistributions in binary form must reproduce the above | |
67 ## copyright notice, this list of conditions and the following | |
68 ## disclaimer in the documentation and/or other materials provided | |
69 ## with the distribution. | |
70 ## | |
71 ## * Neither the name of the University of California, Berkeley nor | |
72 ## the names of its contributors may be used to endorse or promote | |
73 ## products derived from this software without specific prior | |
74 ## written permission. | |
7189 | 75 ## |
7191 | 76 ## THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' |
77 ## AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | |
78 ## TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A | |
79 ## PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND | |
80 ## CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
81 ## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
82 ## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | |
83 ## USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
84 ## ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
85 ## OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |
86 ## OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
7189 | 87 ## SUCH DAMAGE. |
88 | |
89 ## Author: Jason Riedy <ejr@cs.berkeley.edu> | |
90 ## Keywords: linear-algebra norm estimation | |
91 ## Version: 0.2 | |
92 | |
93 function [est, v, w, iter] = onenormest (varargin) | |
94 | |
95 if (size (varargin, 2) < 1 || size (varargin, 2) > 4) | |
96 print_usage (); | |
97 endif | |
98 | |
99 default_t = 5; | |
100 itmax = 10; | |
101 | |
102 if (ismatrix (varargin{1})) | |
103 n = size (varargin{1}, 1); | |
104 if n != size (varargin{1}, 2), | |
105 error ("onenormest: matrix must be square."); | |
106 endif | |
107 apply = @(x) varargin{1} * x; | |
108 apply_t = @(x) varargin{1}' * x; | |
109 if (size (varargin) > 1) | |
110 t = varargin{2}; | |
111 else | |
112 t = min (n, default_t); | |
113 endif | |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7191
diff
changeset
|
114 issing = isa (varargin {1}, "single"); |
7189 | 115 else |
116 if (size (varargin, 2) < 3) | |
117 print_usage(); | |
118 endif | |
119 n = varargin{3}; | |
120 apply = varargin{1}; | |
121 apply_t = varargin{2}; | |
122 if (size (varargin) > 3) | |
123 t = varargin{4}; | |
124 else | |
125 t = default_t; | |
126 endif | |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7191
diff
changeset
|
127 issing = isa (varargin {3}, "single"); |
7189 | 128 endif |
129 | |
130 ## Initial test vectors X. | |
131 X = rand (n, t); | |
132 X = X ./ (ones (n,1) * sum (abs (X), 1)); | |
133 | |
8506 | 134 ## Track if a vertex has been visited. |
135 been_there = zeros (n, 1); | |
136 | |
137 ## To check if the estimate has increased. | |
138 est_old = 0; | |
139 | |
140 ## Normalized vector of signs. | |
141 S = zeros (n, t); | |
7189 | 142 |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7191
diff
changeset
|
143 if (issing) |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7191
diff
changeset
|
144 myeps = eps ("single"); |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7191
diff
changeset
|
145 X = single (X); |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7191
diff
changeset
|
146 else |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7191
diff
changeset
|
147 myeps = eps; |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7191
diff
changeset
|
148 endif |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7191
diff
changeset
|
149 |
7189 | 150 for iter = 1 : itmax + 1 |
151 Y = feval (apply, X); | |
152 | |
153 ## Find the initial estimate as the largest A*x. | |
154 [est, ind_best] = max (sum (abs (Y), 1)); | |
155 if (est > est_old || iter == 2) | |
156 w = Y(:,ind_best); | |
157 endif | |
158 if (iter >= 2 && est < est_old) | |
159 ## No improvement, so stop. | |
160 est = est_old; | |
161 break; | |
162 endif | |
163 | |
164 est_old = est; | |
165 S_old = S; | |
166 if (iter > itmax), | |
167 ## Gone too far. Stop. | |
168 break; | |
169 endif | |
170 | |
171 S = sign (Y); | |
172 | |
173 ## Test if any of S are approximately parallel to previous S | |
174 ## vectors or current S vectors. If everything is parallel, | |
175 ## stop. Otherwise, replace any parallel vectors with | |
176 ## rand{-1,+1}. | |
177 partest = any (abs (S_old' * S - n) < 4*eps*n); | |
178 if (all (partest)) | |
179 ## All the current vectors are parallel to old vectors. | |
180 ## We've hit a cycle, so stop. | |
181 break; | |
182 endif | |
183 if (any (partest)) | |
184 ## Some vectors are parallel to old ones and are cycling, | |
185 ## but not all of them. Replace the parallel vectors with | |
186 ## rand{-1,+1}. | |
187 numpar = sum (partest); | |
188 replacements = 2*(rand (n,numpar) < 0.5) - 1; | |
189 S(:,partest) = replacements; | |
190 endif | |
191 ## Now test for parallel vectors within S. | |
192 partest = any ( (S' * S - eye (t)) == n ); | |
193 if (any (partest)) | |
194 numpar = sum (partest); | |
195 replacements = 2*(rand (n,numpar) < 0.5) - 1; | |
196 S(:,partest) = replacements; | |
197 endif | |
198 | |
199 Z = feval (apply_t, S); | |
200 | |
201 ## Now find the largest non-previously-visted index per | |
202 ## vector. | |
203 h = max (abs (Z),2); | |
204 [mh, mhi] = max (h); | |
205 if (iter >= 2 && mhi == ind_best) | |
206 ## Hit a cycle, stop. | |
207 break; | |
208 endif | |
209 [h, ind] = sort (h, 'descend'); | |
210 if (t > 1) | |
211 firstind = ind(1:t); | |
212 if (all (been_there(firstind))) | |
213 ## Visited all these before, so stop. | |
214 break; | |
215 endif | |
216 ind = ind (!been_there (ind)); | |
217 if (length (ind) < t) | |
218 ## There aren't enough new vectors, so we're practically | |
219 ## in a cycle. Stop. | |
220 break; | |
221 endif | |
222 endif | |
223 | |
224 ## Visit the new indices. | |
225 X = zeros (n, t); | |
226 for zz = 1 : t | |
227 X(ind(zz),zz) = 1; | |
228 endfor | |
229 been_there (ind (1 : t)) = 1; | |
230 endfor | |
231 | |
232 ## The estimate est and vector w are set in the loop above. The | |
233 ## vector v selects the ind_best column of A. | |
234 v = zeros (n, 1); | |
235 v(ind_best) = 1; | |
236 endfunction | |
237 | |
238 %!demo | |
239 %! N = 100; | |
240 %! A = randn(N) + eye(N); | |
241 %! [L,U,P] = lu(A); | |
242 %! nm1inv = onenormest(@(x) U\(L\(P*x)), @(x) P'*(L'\(U'\x)), N, 30) | |
243 %! norm(inv(A), 1) | |
244 | |
245 %!test | |
246 %! N = 10; | |
247 %! A = ones (N); | |
248 %! [nm1, v1, w1] = onenormest (A); | |
249 %! [nminf, vinf, winf] = onenormest (A', 6); | |
250 %! assert (nm1, N, -2*eps); | |
251 %! assert (nminf, N, -2*eps); | |
252 %! assert (norm (w1, 1), nm1 * norm (v1, 1), -2*eps) | |
253 %! assert (norm (winf, 1), nminf * norm (vinf, 1), -2*eps) | |
254 | |
255 %!test | |
256 %! N = 10; | |
257 %! A = ones (N); | |
258 %! [nm1, v1, w1] = onenormest (@(x) A*x, @(x) A'*x, N, 3); | |
259 %! [nminf, vinf, winf] = onenormest (@(x) A'*x, @(x) A*x, N, 3); | |
260 %! assert (nm1, N, -2*eps); | |
261 %! assert (nminf, N, -2*eps); | |
262 %! assert (norm (w1, 1), nm1 * norm (v1, 1), -2*eps) | |
263 %! assert (norm (winf, 1), nminf * norm (vinf, 1), -2*eps) | |
264 | |
265 %!test | |
266 %! N = 5; | |
267 %! A = hilb (N); | |
268 %! [nm1, v1, w1] = onenormest (A); | |
269 %! [nminf, vinf, winf] = onenormest (A', 6); | |
270 %! assert (nm1, norm (A, 1), -2*eps); | |
271 %! assert (nminf, norm (A, inf), -2*eps); | |
272 %! assert (norm (w1, 1), nm1 * norm (v1, 1), -2*eps) | |
273 %! assert (norm (winf, 1), nminf * norm (vinf, 1), -2*eps) | |
274 | |
275 ## Only likely to be within a factor of 10. | |
276 %!test | |
277 %! N = 100; | |
278 %! A = rand (N); | |
279 %! [nm1, v1, w1] = onenormest (A); | |
280 %! [nminf, vinf, winf] = onenormest (A', 6); | |
281 %! assert (nm1, norm (A, 1), -.1); | |
282 %! assert (nminf, norm (A, inf), -.1); | |
283 %! assert (norm (w1, 1), nm1 * norm (v1, 1), -2*eps) | |
284 %! assert (norm (winf, 1), nminf * norm (vinf, 1), -2*eps) |