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 |
|
114 else |
|
115 if (size (varargin, 2) < 3) |
|
116 print_usage(); |
|
117 endif |
|
118 n = varargin{3}; |
|
119 apply = varargin{1}; |
|
120 apply_t = varargin{2}; |
|
121 if (size (varargin) > 3) |
|
122 t = varargin{4}; |
|
123 else |
|
124 t = default_t; |
|
125 endif |
|
126 endif |
|
127 |
|
128 ## Initial test vectors X. |
|
129 X = rand (n, t); |
|
130 X = X ./ (ones (n,1) * sum (abs (X), 1)); |
|
131 |
|
132 been_there = zeros (n, 1); # Track if a vertex has been visited. |
|
133 est_old = 0; # To check if the estimate has increased. |
|
134 S = zeros (n, t); # Normalized vector of signs. The normalization is |
|
135 |
|
136 for iter = 1 : itmax + 1 |
|
137 Y = feval (apply, X); |
|
138 |
|
139 ## Find the initial estimate as the largest A*x. |
|
140 [est, ind_best] = max (sum (abs (Y), 1)); |
|
141 if (est > est_old || iter == 2) |
|
142 w = Y(:,ind_best); |
|
143 endif |
|
144 if (iter >= 2 && est < est_old) |
|
145 ## No improvement, so stop. |
|
146 est = est_old; |
|
147 break; |
|
148 endif |
|
149 |
|
150 est_old = est; |
|
151 S_old = S; |
|
152 if (iter > itmax), |
|
153 ## Gone too far. Stop. |
|
154 break; |
|
155 endif |
|
156 |
|
157 S = sign (Y); |
|
158 |
|
159 ## Test if any of S are approximately parallel to previous S |
|
160 ## vectors or current S vectors. If everything is parallel, |
|
161 ## stop. Otherwise, replace any parallel vectors with |
|
162 ## rand{-1,+1}. |
|
163 partest = any (abs (S_old' * S - n) < 4*eps*n); |
|
164 if (all (partest)) |
|
165 ## All the current vectors are parallel to old vectors. |
|
166 ## We've hit a cycle, so stop. |
|
167 break; |
|
168 endif |
|
169 if (any (partest)) |
|
170 ## Some vectors are parallel to old ones and are cycling, |
|
171 ## but not all of them. Replace the parallel vectors with |
|
172 ## rand{-1,+1}. |
|
173 numpar = sum (partest); |
|
174 replacements = 2*(rand (n,numpar) < 0.5) - 1; |
|
175 S(:,partest) = replacements; |
|
176 endif |
|
177 ## Now test for parallel vectors within S. |
|
178 partest = any ( (S' * S - eye (t)) == n ); |
|
179 if (any (partest)) |
|
180 numpar = sum (partest); |
|
181 replacements = 2*(rand (n,numpar) < 0.5) - 1; |
|
182 S(:,partest) = replacements; |
|
183 endif |
|
184 |
|
185 Z = feval (apply_t, S); |
|
186 |
|
187 ## Now find the largest non-previously-visted index per |
|
188 ## vector. |
|
189 h = max (abs (Z),2); |
|
190 [mh, mhi] = max (h); |
|
191 if (iter >= 2 && mhi == ind_best) |
|
192 ## Hit a cycle, stop. |
|
193 break; |
|
194 endif |
|
195 [h, ind] = sort (h, 'descend'); |
|
196 if (t > 1) |
|
197 firstind = ind(1:t); |
|
198 if (all (been_there(firstind))) |
|
199 ## Visited all these before, so stop. |
|
200 break; |
|
201 endif |
|
202 ind = ind (!been_there (ind)); |
|
203 if (length (ind) < t) |
|
204 ## There aren't enough new vectors, so we're practically |
|
205 ## in a cycle. Stop. |
|
206 break; |
|
207 endif |
|
208 endif |
|
209 |
|
210 ## Visit the new indices. |
|
211 X = zeros (n, t); |
|
212 for zz = 1 : t |
|
213 X(ind(zz),zz) = 1; |
|
214 endfor |
|
215 been_there (ind (1 : t)) = 1; |
|
216 endfor |
|
217 |
|
218 ## The estimate est and vector w are set in the loop above. The |
|
219 ## vector v selects the ind_best column of A. |
|
220 v = zeros (n, 1); |
|
221 v(ind_best) = 1; |
|
222 endfunction |
|
223 |
|
224 %!demo |
|
225 %! N = 100; |
|
226 %! A = randn(N) + eye(N); |
|
227 %! [L,U,P] = lu(A); |
|
228 %! nm1inv = onenormest(@(x) U\(L\(P*x)), @(x) P'*(L'\(U'\x)), N, 30) |
|
229 %! norm(inv(A), 1) |
|
230 |
|
231 %!test |
|
232 %! N = 10; |
|
233 %! A = ones (N); |
|
234 %! [nm1, v1, w1] = onenormest (A); |
|
235 %! [nminf, vinf, winf] = onenormest (A', 6); |
|
236 %! assert (nm1, N, -2*eps); |
|
237 %! assert (nminf, N, -2*eps); |
|
238 %! assert (norm (w1, 1), nm1 * norm (v1, 1), -2*eps) |
|
239 %! assert (norm (winf, 1), nminf * norm (vinf, 1), -2*eps) |
|
240 |
|
241 %!test |
|
242 %! N = 10; |
|
243 %! A = ones (N); |
|
244 %! [nm1, v1, w1] = onenormest (@(x) A*x, @(x) A'*x, N, 3); |
|
245 %! [nminf, vinf, winf] = onenormest (@(x) A'*x, @(x) A*x, N, 3); |
|
246 %! assert (nm1, N, -2*eps); |
|
247 %! assert (nminf, N, -2*eps); |
|
248 %! assert (norm (w1, 1), nm1 * norm (v1, 1), -2*eps) |
|
249 %! assert (norm (winf, 1), nminf * norm (vinf, 1), -2*eps) |
|
250 |
|
251 %!test |
|
252 %! N = 5; |
|
253 %! A = hilb (N); |
|
254 %! [nm1, v1, w1] = onenormest (A); |
|
255 %! [nminf, vinf, winf] = onenormest (A', 6); |
|
256 %! assert (nm1, norm (A, 1), -2*eps); |
|
257 %! assert (nminf, norm (A, inf), -2*eps); |
|
258 %! assert (norm (w1, 1), nm1 * norm (v1, 1), -2*eps) |
|
259 %! assert (norm (winf, 1), nminf * norm (vinf, 1), -2*eps) |
|
260 |
|
261 ## Only likely to be within a factor of 10. |
|
262 %!test |
|
263 %! N = 100; |
|
264 %! A = rand (N); |
|
265 %! [nm1, v1, w1] = onenormest (A); |
|
266 %! [nminf, vinf, winf] = onenormest (A', 6); |
|
267 %! assert (nm1, norm (A, 1), -.1); |
|
268 %! assert (nminf, norm (A, inf), -.1); |
|
269 %! assert (norm (w1, 1), nm1 * norm (v1, 1), -2*eps) |
|
270 %! assert (norm (winf, 1), nminf * norm (vinf, 1), -2*eps) |