annotate scripts/linear-algebra/normest.m @ 33630:5113e80bdf8b default tip @

build: Do not generate "moc-console-lexer.cc" when building without QScintilla. * libgui/src/module.mk: Generate "moc-console-lexer.cc" only when building with QScintilla. Sort files alphabetically in lists.
author Markus Mützel <markus.muetzel@gmx.de>
date Mon, 27 May 2024 15:54:38 +0200
parents 2e484f9f1f18
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
1 ########################################################################
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
2 ##
32632
2e484f9f1f18 maint: update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 31706
diff changeset
3 ## Copyright (C) 2006-2024 The Octave Project Developers
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
4 ##
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
5 ## See the file COPYRIGHT.md in the top-level directory of this
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
6 ## distribution or <https://octave.org/copyright/>.
6212
cc34c0be7b00 [project @ 2006-12-08 11:17:12 by dbateman]
dbateman
parents:
diff changeset
7 ##
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6498
diff changeset
8 ## This file is part of Octave.
6212
cc34c0be7b00 [project @ 2006-12-08 11:17:12 by dbateman]
dbateman
parents:
diff changeset
9 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
10 ## Octave is free software: you can redistribute it and/or modify it
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6498
diff changeset
11 ## under the terms of the GNU General Public License as published by
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
12 ## the Free Software Foundation, either version 3 of the License, or
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
13 ## (at your option) any later version.
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6498
diff changeset
14 ##
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6498
diff changeset
15 ## 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
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
18 ## GNU General Public License for more details.
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 ## 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
21 ## along with Octave; see the file COPYING. If not, see
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
22 ## <https://www.gnu.org/licenses/>.
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
23 ##
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
24 ########################################################################
6212
cc34c0be7b00 [project @ 2006-12-08 11:17:12 by dbateman]
dbateman
parents:
diff changeset
25
cc34c0be7b00 [project @ 2006-12-08 11:17:12 by dbateman]
dbateman
parents:
diff changeset
26 ## -*- texinfo -*-
22827
c3d3a81ad986 doc: Update documentation for norm, normest, normest1, condest.
Rik <rik@octave.org>
parents: 22323
diff changeset
27 ## @deftypefn {} {@var{nest} =} normest (@var{A})
c3d3a81ad986 doc: Update documentation for norm, normest, normest1, condest.
Rik <rik@octave.org>
parents: 22323
diff changeset
28 ## @deftypefnx {} {@var{nest} =} normest (@var{A}, @var{tol})
c3d3a81ad986 doc: Update documentation for norm, normest, normest1, condest.
Rik <rik@octave.org>
parents: 22323
diff changeset
29 ## @deftypefnx {} {[@var{nest}, @var{iter}] =} normest (@dots{})
20160
03b9d17a2d95 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
30 ## Estimate the 2-norm of the matrix @var{A} using a power series analysis.
03b9d17a2d95 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
31 ##
03b9d17a2d95 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
32 ## This is typically used for large matrices, where the cost of calculating
03b9d17a2d95 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
33 ## @code{norm (@var{A})} is prohibitive and an approximation to the 2-norm is
03b9d17a2d95 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
34 ## acceptable.
6212
cc34c0be7b00 [project @ 2006-12-08 11:17:12 by dbateman]
dbateman
parents:
diff changeset
35 ##
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
36 ## @var{tol} is the tolerance to which the 2-norm is calculated. By default
20160
03b9d17a2d95 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
37 ## @var{tol} is 1e-6.
03b9d17a2d95 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
38 ##
22827
c3d3a81ad986 doc: Update documentation for norm, normest, normest1, condest.
Rik <rik@octave.org>
parents: 22323
diff changeset
39 ## The optional output @var{iter} returns the number of iterations that were
c3d3a81ad986 doc: Update documentation for norm, normest, normest1, condest.
Rik <rik@octave.org>
parents: 22323
diff changeset
40 ## required for @code{normest} to converge.
c3d3a81ad986 doc: Update documentation for norm, normest, normest1, condest.
Rik <rik@octave.org>
parents: 22323
diff changeset
41 ## @seealso{normest1, norm, cond, condest}
6212
cc34c0be7b00 [project @ 2006-12-08 11:17:12 by dbateman]
dbateman
parents:
diff changeset
42 ## @end deftypefn
cc34c0be7b00 [project @ 2006-12-08 11:17:12 by dbateman]
dbateman
parents:
diff changeset
43
22830
c88f88296789 normest.m: Overhaul function.
Rik <rik@octave.org>
parents: 22828
diff changeset
44 function [nest, iter] = normest (A, tol = 1e-6)
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
45
28891
de5f2f9a64ff maint: Use same coding style when checking for a minimum of 1 input.
Rik <rik@octave.org>
parents: 27923
diff changeset
46 if (nargin < 1)
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 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
48 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
49
22830
c88f88296789 normest.m: Overhaul function.
Rik <rik@octave.org>
parents: 22828
diff changeset
50 if (! isnumeric (A) || ndims (A) != 2)
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 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
52 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
53
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 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
55 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
56 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
57
9871
87595d714005 move normest to linear-algebra, improve it
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
58 if (! isfloat (A))
87595d714005 move normest to linear-algebra, improve it
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
59 A = double (A);
87595d714005 move normest to linear-algebra, improve it
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
60 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
61
9871
87595d714005 move normest to linear-algebra, improve it
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
62 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
63 ## 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
64 v = rand ("state");
20179
cc5ffacb15ab normest.m: Fix function to run with sparse complex inputs (bug #45080).
Rik <rik@octave.org>
parents: 20160
diff changeset
65 rand ("state", full (trace (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
66 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
67 ## 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
68 y = rand (ncols, 1);
22830
c88f88296789 normest.m: Overhaul function.
Rik <rik@octave.org>
parents: 22828
diff changeset
69 iter = 0;
c88f88296789 normest.m: Overhaul function.
Rik <rik@octave.org>
parents: 22828
diff changeset
70 nest = 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
71 do
22830
c88f88296789 normest.m: Overhaul function.
Rik <rik@octave.org>
parents: 22828
diff changeset
72 n0 = nest;
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
73 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
74 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
75 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
76 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
77 else
20231
83792dd9bcc1 Use in-place operators in m-files where possible.
Rik <rik@octave.org>
parents: 20179
diff changeset
78 x /= normx;
11113
a8ac114ec9ab Stylefixes, replace end by endif.
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10788
diff changeset
79 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
80 y = A' * x;
22830
c88f88296789 normest.m: Overhaul function.
Rik <rik@octave.org>
parents: 22828
diff changeset
81 nest = norm (y);
c88f88296789 normest.m: Overhaul function.
Rik <rik@octave.org>
parents: 22828
diff changeset
82 iter += 1;
c88f88296789 normest.m: Overhaul function.
Rik <rik@octave.org>
parents: 22828
diff changeset
83 until (abs (nest - n0) <= tol * nest)
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
84
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 rand ("state", v); # restore state of random number generator
21758
ffad2baa90f7 maint: Use newlines to make code more readable.
Rik <rik@octave.org>
parents: 20852
diff changeset
86
6212
cc34c0be7b00 [project @ 2006-12-08 11:17:12 by dbateman]
dbateman
parents:
diff changeset
87 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
88
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
89
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
90 %!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
91 %! A = toeplitz ([-2,1,0,0]);
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
92 %! assert (normest (A), norm (A), 1e-6);
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
93
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
94 %!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
95 %! A = rand (10);
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
96 %! assert (normest (A), norm (A), 1e-6);
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
97
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
98 ## Test input validation
28896
90fea9cc9caa test: Add expected error message <Invalid call> to BIST tests for nargin.
Rik <rik@octave.org>
parents: 28891
diff changeset
99 %!error <Invalid call> normest ()
22830
c88f88296789 normest.m: Overhaul function.
Rik <rik@octave.org>
parents: 22828
diff changeset
100 %!error <A must be a numeric .* matrix> normest ([true true])
c88f88296789 normest.m: Overhaul function.
Rik <rik@octave.org>
parents: 22828
diff changeset
101 %!error <A must be .* 2-D matrix> normest (ones (3,3,3))
c88f88296789 normest.m: Overhaul function.
Rik <rik@octave.org>
parents: 22828
diff changeset
102 %!error <TOL must be a real scalar> normest (1, [1, 2])
c88f88296789 normest.m: Overhaul function.
Rik <rik@octave.org>
parents: 22828
diff changeset
103 %!error <TOL must be a real scalar> normest (1, 1+1i)