annotate scripts/linear-algebra/normest.m @ 14138:72c96de7a403 stable

maint: update copyright notices for 2012
author John W. Eaton <jwe@octave.org>
date Mon, 02 Jan 2012 14:25:41 -0500
parents c792872f8942
children f3d52523cde1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14138
72c96de7a403 maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents: 11587
diff changeset
1 ## Copyright (C) 2006-2012 David Bateman and Marco Caliari
9871
87595d714005 move normest to linear-algebra, improve it
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
2 ## Copyright (C) 2009 VZLU Prague
6212
cc34c0be7b00 [project @ 2006-12-08 11:17:12 by dbateman]
dbateman
parents:
diff changeset
3 ##
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6498
diff changeset
4 ## This file is part of Octave.
6212
cc34c0be7b00 [project @ 2006-12-08 11:17:12 by dbateman]
dbateman
parents:
diff changeset
5 ##
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6498
diff changeset
6 ## Octave is free software; you can redistribute it and/or modify it
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6498
diff changeset
7 ## under the terms of the GNU General Public License as published by
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6498
diff changeset
8 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6498
diff changeset
9 ## your option) any later version.
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6498
diff changeset
10 ##
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6498
diff changeset
11 ## Octave is distributed in the hope that it will be useful, but
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6498
diff changeset
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6498
diff changeset
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6498
diff changeset
14 ## General Public License for more details.
6212
cc34c0be7b00 [project @ 2006-12-08 11:17:12 by dbateman]
dbateman
parents:
diff changeset
15 ##
cc34c0be7b00 [project @ 2006-12-08 11:17:12 by dbateman]
dbateman
parents:
diff changeset
16 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6498
diff changeset
17 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6498
diff changeset
18 ## <http://www.gnu.org/licenses/>.
6212
cc34c0be7b00 [project @ 2006-12-08 11:17:12 by dbateman]
dbateman
parents:
diff changeset
19
cc34c0be7b00 [project @ 2006-12-08 11:17:12 by dbateman]
dbateman
parents:
diff changeset
20 ## -*- texinfo -*-
11470
eb9e0b597d61 Use common names for variables in documentation and code for a few more m-script files.
Rik <octave@nomad.inbox5.com>
parents: 11113
diff changeset
21 ## @deftypefn {Function File} {@var{n} =} normest (@var{A})
eb9e0b597d61 Use common names for variables in documentation and code for a few more m-script files.
Rik <octave@nomad.inbox5.com>
parents: 11113
diff changeset
22 ## @deftypefnx {Function File} {@var{n} =} normest (@var{A}, @var{tol})
10788
c69252eb2f2b normest.m: Set the "state" of the random number generator to depend on trace(A).
Rik <octave@nomad.inbox5.com>
parents: 9875
diff changeset
23 ## @deftypefnx {Function File} {[@var{n}, @var{c}] =} normest (@dots{})
11470
eb9e0b597d61 Use common names for variables in documentation and code for a few more m-script files.
Rik <octave@nomad.inbox5.com>
parents: 11113
diff changeset
24 ## Estimate the 2-norm of the matrix @var{A} using a power series
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
25 ## analysis. This is typically used for large matrices, where the cost
11470
eb9e0b597d61 Use common names for variables in documentation and code for a few more m-script files.
Rik <octave@nomad.inbox5.com>
parents: 11113
diff changeset
26 ## of calculating @code{norm (@var{A})} is prohibitive and an approximation
6212
cc34c0be7b00 [project @ 2006-12-08 11:17:12 by dbateman]
dbateman
parents:
diff changeset
27 ## to the 2-norm is acceptable.
cc34c0be7b00 [project @ 2006-12-08 11:17:12 by dbateman]
dbateman
parents:
diff changeset
28 ##
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
29 ## @var{tol} is the tolerance to which the 2-norm is calculated. By default
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
30 ## @var{tol} is 1e-6. @var{c} returns the number of iterations needed for
6212
cc34c0be7b00 [project @ 2006-12-08 11:17:12 by dbateman]
dbateman
parents:
diff changeset
31 ## @code{normest} to converge.
cc34c0be7b00 [project @ 2006-12-08 11:17:12 by dbateman]
dbateman
parents:
diff changeset
32 ## @end deftypefn
cc34c0be7b00 [project @ 2006-12-08 11:17:12 by dbateman]
dbateman
parents:
diff changeset
33
10788
c69252eb2f2b normest.m: Set the "state" of the random number generator to depend on trace(A).
Rik <octave@nomad.inbox5.com>
parents: 9875
diff changeset
34 function [n, c] = normest (A, tol = 1e-6)
c69252eb2f2b normest.m: Set the "state" of the random number generator to depend on trace(A).
Rik <octave@nomad.inbox5.com>
parents: 9875
diff changeset
35
c69252eb2f2b normest.m: Set the "state" of the random number generator to depend on trace(A).
Rik <octave@nomad.inbox5.com>
parents: 9875
diff changeset
36 if (nargin != 1 && nargin != 2)
c69252eb2f2b normest.m: Set the "state" of the random number generator to depend on trace(A).
Rik <octave@nomad.inbox5.com>
parents: 9875
diff changeset
37 print_usage ();
c69252eb2f2b normest.m: Set the "state" of the random number generator to depend on trace(A).
Rik <octave@nomad.inbox5.com>
parents: 9875
diff changeset
38 endif
c69252eb2f2b normest.m: Set the "state" of the random number generator to depend on trace(A).
Rik <octave@nomad.inbox5.com>
parents: 9875
diff changeset
39
c69252eb2f2b normest.m: Set the "state" of the random number generator to depend on trace(A).
Rik <octave@nomad.inbox5.com>
parents: 9875
diff changeset
40 if (! (isnumeric (A) && ndims (A) == 2))
c69252eb2f2b normest.m: Set the "state" of the random number generator to depend on trace(A).
Rik <octave@nomad.inbox5.com>
parents: 9875
diff changeset
41 error ("normest: A must be a numeric 2-D matrix");
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
42 endif
10788
c69252eb2f2b normest.m: Set the "state" of the random number generator to depend on trace(A).
Rik <octave@nomad.inbox5.com>
parents: 9875
diff changeset
43
c69252eb2f2b normest.m: Set the "state" of the random number generator to depend on trace(A).
Rik <octave@nomad.inbox5.com>
parents: 9875
diff changeset
44 if (! (isscalar (tol) && isreal (tol)))
c69252eb2f2b normest.m: Set the "state" of the random number generator to depend on trace(A).
Rik <octave@nomad.inbox5.com>
parents: 9875
diff changeset
45 error ("normest: TOL must be a real scalar");
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
46 endif
10788
c69252eb2f2b normest.m: Set the "state" of the random number generator to depend on trace(A).
Rik <octave@nomad.inbox5.com>
parents: 9875
diff changeset
47
9871
87595d714005 move normest to linear-algebra, improve it
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
48 if (! isfloat (A))
87595d714005 move normest to linear-algebra, improve it
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
49 A = double (A);
87595d714005 move normest to linear-algebra, improve it
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
50 endif
10788
c69252eb2f2b normest.m: Set the "state" of the random number generator to depend on trace(A).
Rik <octave@nomad.inbox5.com>
parents: 9875
diff changeset
51
9871
87595d714005 move normest to linear-algebra, improve it
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
52 tol = max (tol, eps (class (A)));
10788
c69252eb2f2b normest.m: Set the "state" of the random number generator to depend on trace(A).
Rik <octave@nomad.inbox5.com>
parents: 9875
diff changeset
53 ## Set random number generator to depend on target matrix
c69252eb2f2b normest.m: Set the "state" of the random number generator to depend on trace(A).
Rik <octave@nomad.inbox5.com>
parents: 9875
diff changeset
54 v = rand ("state");
c69252eb2f2b normest.m: Set the "state" of the random number generator to depend on trace(A).
Rik <octave@nomad.inbox5.com>
parents: 9875
diff changeset
55 rand ("state", trace (A));
c69252eb2f2b normest.m: Set the "state" of the random number generator to depend on trace(A).
Rik <octave@nomad.inbox5.com>
parents: 9875
diff changeset
56 ncols = columns (A);
c69252eb2f2b normest.m: Set the "state" of the random number generator to depend on trace(A).
Rik <octave@nomad.inbox5.com>
parents: 9875
diff changeset
57 ## Randomize y to avoid bad guesses for important matrices.
c69252eb2f2b normest.m: Set the "state" of the random number generator to depend on trace(A).
Rik <octave@nomad.inbox5.com>
parents: 9875
diff changeset
58 y = rand (ncols, 1);
6212
cc34c0be7b00 [project @ 2006-12-08 11:17:12 by dbateman]
dbateman
parents:
diff changeset
59 c = 0;
10788
c69252eb2f2b normest.m: Set the "state" of the random number generator to depend on trace(A).
Rik <octave@nomad.inbox5.com>
parents: 9875
diff changeset
60 n = 0;
c69252eb2f2b normest.m: Set the "state" of the random number generator to depend on trace(A).
Rik <octave@nomad.inbox5.com>
parents: 9875
diff changeset
61 do
c69252eb2f2b normest.m: Set the "state" of the random number generator to depend on trace(A).
Rik <octave@nomad.inbox5.com>
parents: 9875
diff changeset
62 n0 = n;
c69252eb2f2b normest.m: Set the "state" of the random number generator to depend on trace(A).
Rik <octave@nomad.inbox5.com>
parents: 9875
diff changeset
63 x = A * y;
c69252eb2f2b normest.m: Set the "state" of the random number generator to depend on trace(A).
Rik <octave@nomad.inbox5.com>
parents: 9875
diff changeset
64 normx = norm (x);
c69252eb2f2b normest.m: Set the "state" of the random number generator to depend on trace(A).
Rik <octave@nomad.inbox5.com>
parents: 9875
diff changeset
65 if (normx == 0)
c69252eb2f2b normest.m: Set the "state" of the random number generator to depend on trace(A).
Rik <octave@nomad.inbox5.com>
parents: 9875
diff changeset
66 x = rand (ncols, 1);
c69252eb2f2b normest.m: Set the "state" of the random number generator to depend on trace(A).
Rik <octave@nomad.inbox5.com>
parents: 9875
diff changeset
67 else
c69252eb2f2b normest.m: Set the "state" of the random number generator to depend on trace(A).
Rik <octave@nomad.inbox5.com>
parents: 9875
diff changeset
68 x = x / normx;
11113
a8ac114ec9ab Stylefixes, replace end by endif.
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10788
diff changeset
69 endif
10788
c69252eb2f2b normest.m: Set the "state" of the random number generator to depend on trace(A).
Rik <octave@nomad.inbox5.com>
parents: 9875
diff changeset
70 y = A' * x;
c69252eb2f2b normest.m: Set the "state" of the random number generator to depend on trace(A).
Rik <octave@nomad.inbox5.com>
parents: 9875
diff changeset
71 n = norm (y);
c69252eb2f2b normest.m: Set the "state" of the random number generator to depend on trace(A).
Rik <octave@nomad.inbox5.com>
parents: 9875
diff changeset
72 c += 1;
c69252eb2f2b normest.m: Set the "state" of the random number generator to depend on trace(A).
Rik <octave@nomad.inbox5.com>
parents: 9875
diff changeset
73 until (abs (n - n0) <= tol * n)
c69252eb2f2b normest.m: Set the "state" of the random number generator to depend on trace(A).
Rik <octave@nomad.inbox5.com>
parents: 9875
diff changeset
74
c69252eb2f2b normest.m: Set the "state" of the random number generator to depend on trace(A).
Rik <octave@nomad.inbox5.com>
parents: 9875
diff changeset
75 rand ("state", v); # restore state of random number generator
6212
cc34c0be7b00 [project @ 2006-12-08 11:17:12 by dbateman]
dbateman
parents:
diff changeset
76 endfunction
10788
c69252eb2f2b normest.m: Set the "state" of the random number generator to depend on trace(A).
Rik <octave@nomad.inbox5.com>
parents: 9875
diff changeset
77
c69252eb2f2b normest.m: Set the "state" of the random number generator to depend on trace(A).
Rik <octave@nomad.inbox5.com>
parents: 9875
diff changeset
78 %!test
c69252eb2f2b normest.m: Set the "state" of the random number generator to depend on trace(A).
Rik <octave@nomad.inbox5.com>
parents: 9875
diff changeset
79 %! A = toeplitz ([-2,1,0,0]);
c69252eb2f2b normest.m: Set the "state" of the random number generator to depend on trace(A).
Rik <octave@nomad.inbox5.com>
parents: 9875
diff changeset
80 %! assert (normest(A), norm(A), 1e-6);
c69252eb2f2b normest.m: Set the "state" of the random number generator to depend on trace(A).
Rik <octave@nomad.inbox5.com>
parents: 9875
diff changeset
81
c69252eb2f2b normest.m: Set the "state" of the random number generator to depend on trace(A).
Rik <octave@nomad.inbox5.com>
parents: 9875
diff changeset
82 %!test
c69252eb2f2b normest.m: Set the "state" of the random number generator to depend on trace(A).
Rik <octave@nomad.inbox5.com>
parents: 9875
diff changeset
83 %! A = rand (10);
c69252eb2f2b normest.m: Set the "state" of the random number generator to depend on trace(A).
Rik <octave@nomad.inbox5.com>
parents: 9875
diff changeset
84 %! assert (normest(A), norm(A), 1e-6);
c69252eb2f2b normest.m: Set the "state" of the random number generator to depend on trace(A).
Rik <octave@nomad.inbox5.com>
parents: 9875
diff changeset
85
c69252eb2f2b normest.m: Set the "state" of the random number generator to depend on trace(A).
Rik <octave@nomad.inbox5.com>
parents: 9875
diff changeset
86 %% Test input validation
c69252eb2f2b normest.m: Set the "state" of the random number generator to depend on trace(A).
Rik <octave@nomad.inbox5.com>
parents: 9875
diff changeset
87 %!error normest ()
c69252eb2f2b normest.m: Set the "state" of the random number generator to depend on trace(A).
Rik <octave@nomad.inbox5.com>
parents: 9875
diff changeset
88 %!error normest (1, 2, 3)
c69252eb2f2b normest.m: Set the "state" of the random number generator to depend on trace(A).
Rik <octave@nomad.inbox5.com>
parents: 9875
diff changeset
89 %!error normest ([true true])
c69252eb2f2b normest.m: Set the "state" of the random number generator to depend on trace(A).
Rik <octave@nomad.inbox5.com>
parents: 9875
diff changeset
90 %!error normest (ones (3,3,3))
c69252eb2f2b normest.m: Set the "state" of the random number generator to depend on trace(A).
Rik <octave@nomad.inbox5.com>
parents: 9875
diff changeset
91 %!error normest (1, [1, 2])
c69252eb2f2b normest.m: Set the "state" of the random number generator to depend on trace(A).
Rik <octave@nomad.inbox5.com>
parents: 9875
diff changeset
92 %!error normest (1, 1+1i)
c69252eb2f2b normest.m: Set the "state" of the random number generator to depend on trace(A).
Rik <octave@nomad.inbox5.com>
parents: 9875
diff changeset
93