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