]> Creatis software - CreaPhase.git/blob - octave_packages/m/linear-algebra/normest.m
update packages
[CreaPhase.git] / octave_packages / m / linear-algebra / normest.m
1 ## Copyright (C) 2006-2012 David Bateman and Marco Caliari
2 ## Copyright (C) 2009 VZLU Prague
3 ##
4 ## This file is part of Octave.
5 ##
6 ## Octave is free software; you can redistribute it and/or modify it
7 ## under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 3 of the License, or (at
9 ## your option) any later version.
10 ##
11 ## Octave is distributed in the hope that it will be useful, but
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 ## General Public License for more details.
15 ##
16 ## You should have received a copy of the GNU General Public License
17 ## along with Octave; see the file COPYING.  If not, see
18 ## <http://www.gnu.org/licenses/>.
19
20 ## -*- texinfo -*-
21 ## @deftypefn  {Function File} {@var{n} =} normest (@var{A})
22 ## @deftypefnx {Function File} {@var{n} =} normest (@var{A}, @var{tol})
23 ## @deftypefnx {Function File} {[@var{n}, @var{c}] =} normest (@dots{})
24 ## Estimate the 2-norm of the matrix @var{A} using a power series
25 ## analysis.  This is typically used for large matrices, where the cost
26 ## of calculating @code{norm (@var{A})} is prohibitive and an approximation
27 ## to the 2-norm is acceptable.
28 ##
29 ## @var{tol} is the tolerance to which the 2-norm is calculated.  By default
30 ## @var{tol} is 1e-6.  @var{c} returns the number of iterations needed for
31 ## @code{normest} to converge.
32 ## @end deftypefn
33
34 function [n, c] = normest (A, tol = 1e-6)
35
36   if (nargin != 1 && nargin != 2)
37     print_usage ();
38   endif
39
40   if (! (isnumeric (A) && ndims (A) == 2))
41     error ("normest: A must be a numeric 2-D matrix");
42   endif
43
44   if (! (isscalar (tol) && isreal (tol)))
45     error ("normest: TOL must be a real scalar");
46   endif
47
48   if (! isfloat (A))
49     A = double (A);
50   endif
51
52   tol = max (tol, eps (class (A)));
53   ## Set random number generator to depend on target matrix
54   v = rand ("state");
55   rand ("state", trace (A));
56   ncols = columns (A);
57   ## Randomize y to avoid bad guesses for important matrices.
58   y = rand (ncols, 1);
59   c = 0;
60   n = 0;
61   do
62     n0 = n;
63     x = A * y;
64     normx = norm (x);
65     if (normx == 0)
66       x = rand (ncols, 1);
67     else
68       x = x / normx;
69     endif
70     y = A' * x;
71     n = norm (y);
72     c += 1;
73   until (abs (n - n0) <= tol * n)
74
75   rand ("state", v);    # restore state of random number generator
76 endfunction
77
78 %!test
79 %! A = toeplitz ([-2,1,0,0]);
80 %! assert (normest(A), norm(A), 1e-6);
81
82 %!test
83 %! A = rand (10);
84 %! assert (normest(A), norm(A), 1e-6);
85
86 %% Test input validation
87 %!error normest ()
88 %!error normest (1, 2, 3)
89 %!error normest ([true true])
90 %!error normest (ones (3,3,3))
91 %!error normest (1, [1, 2])
92 %!error normest (1, 1+1i)
93