6222
|
1 ## Copyright (C) 2006 David Bateman and Marco Caliari |
6212
|
2 ## |
|
3 ## This program is free software; you can redistribute it and/or modify |
|
4 ## it under the terms of the GNU General Public License as published by |
|
5 ## the Free Software Foundation; either version 2 of the License, or |
|
6 ## (at your option) any later version. |
|
7 ## |
|
8 ## This program is distributed in the hope that it will be useful, |
|
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 ## GNU General Public License for more details. |
|
12 ## |
|
13 ## You should have received a copy of the GNU General Public License |
|
14 ## along with this program; if not, write to the Free Software |
|
15 ## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
16 ## 02110-1301 USA |
|
17 |
|
18 ## -*- texinfo -*- |
|
19 ## @deftypefn {Function File} {[@var{n}, @var{c}] =} normest (@var{a}, @var{tol}) |
6222
|
20 ## Estimate the 2-norm of the matrix @var{a} using a power series |
6212
|
21 ## analysis. This is typically used for large matrices, where the cost |
6222
|
22 ## of calculating the @code{norm (@var{a})} is prohibitive and an approximation |
6212
|
23 ## to the 2-norm is acceptable. |
|
24 ## |
|
25 ## @var{tol} is the tolerance to which the 2-norm is calculated. By default |
|
26 ## @var{tol} is 1e-6. @var{c} returns the number of iterations needed for |
|
27 ## @code{normest} to converge. |
|
28 ## @end deftypefn |
|
29 |
6498
|
30 function [e1, c] = normest (A, tol) |
6212
|
31 if (nargin < 2) |
|
32 tol = 1e-6; |
|
33 endif |
|
34 if (tol < eps) |
|
35 tol = eps |
|
36 endif |
|
37 if (ndims(A) != 2) |
6498
|
38 error ("normest: A must be a matrix"); |
6212
|
39 endif |
6498
|
40 maxA = max (max (abs (A))); |
6212
|
41 c = 0; |
|
42 if (maxA == 0) |
|
43 e1 = 0 |
|
44 else |
6498
|
45 [m, n] = size (A); |
6212
|
46 B = A / maxA; |
|
47 Bt = B'; |
|
48 if (m > n) |
|
49 tmp = B; |
|
50 B = Bt; |
|
51 Bt = tmp; |
|
52 endif |
|
53 e0 = 0; |
6498
|
54 x = randn (min (m, n), 1); |
|
55 e1 = norm (x); |
6212
|
56 x = x / e1; |
6498
|
57 e1 = sqrt (e1); |
|
58 if (issparse (A)) |
|
59 while (abs (e1 - e0) > tol * e1) |
6212
|
60 e0 = e1; |
|
61 x = B * (Bt * x); |
6498
|
62 e1 = norm (x); |
6212
|
63 x = x / e1; |
6498
|
64 e1 = sqrt (e1); |
6212
|
65 c = c + 1; |
|
66 endwhile |
|
67 else |
6213
|
68 B = B * Bt; |
6498
|
69 while (abs (e1 - e0) > tol * e1) |
6212
|
70 e0 = e1; |
|
71 x = B * x; |
6498
|
72 e1 = norm (x); |
6212
|
73 x = x / e1; |
6498
|
74 e1 = sqrt (e1); |
6212
|
75 c = c + 1; |
|
76 endwhile |
|
77 endif |
|
78 e1 = e1 * maxA; |
|
79 endif |
|
80 endfunction |