/
normest_2.m
44 lines (42 loc) · 1.18 KB
/
normest_2.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
function [e,cnt] = normest_2(S,tol)
%NORMEST Estimate the matrix 2-norm.
% NORMEST(S) is an estimate of the 2-norm of the matrix S.
% NORMEST(S,tol) uses relative error tol instead of 1.e-6.
% [nrm,cnt] = NORMEST(..) also gives the number of iterations used.
%
% This function is intended primarily for sparse matrices,
% although it works correctly and may be useful for large, full
% matrices as well. Use NORMEST when your problem is large
% enough that NORM takes too long to compute and an approximate
% norm is acceptable.
%
% Class support for input S:
% float: double, single
%
% See also NORM, COND, RCOND, CONDEST.
% Copyright 1984-2010 The MathWorks, Inc.
if nargin < 2, tol = 1.e-6; end
maxiter = 100; % should never take this many iterations.
x = sum(abs(S),1)';
cnt = 0;
e = norm(x);
ST = S' ;
if e == 0, return, end
x = x/e;
e0 = 0;
while abs(e-e0) > tol*e
e0 = e;
Sx = S*x;
if nnz(Sx) == 0
Sx = rand(size(Sx),class(Sx));
end
x = ST*Sx;
normx = norm(x);
e = normx/norm(Sx);
x = x/normx;
cnt = cnt+1;
if cnt > maxiter
warning(message('MATLAB:normest:notconverge', maxiter, sprintf('%g',tol)));
break;
end
end