Mercurial > hg > octave-nkf
annotate scripts/linear-algebra/normest.m @ 20441:83792dd9bcc1
Use in-place operators in m-files where possible.
* scripts/audio/@audioplayer/set.m, scripts/audio/@audiorecorder/set.m,
scripts/audio/mu2lin.m, scripts/elfun/cosd.m, scripts/general/del2.m,
scripts/general/profexplore.m, scripts/general/quadl.m, scripts/general/rat.m,
scripts/general/rotdim.m, scripts/help/get_first_help_sentence.m,
scripts/help/private/__strip_html_tags__.m, scripts/image/cubehelix.m,
scripts/io/textread.m, scripts/linear-algebra/duplication_matrix.m,
scripts/linear-algebra/housh.m, scripts/linear-algebra/krylov.m,
scripts/linear-algebra/logm.m, scripts/linear-algebra/normest.m,
scripts/linear-algebra/onenormest.m, scripts/optimization/fminsearch.m,
scripts/optimization/lsqnonneg.m, scripts/optimization/qp.m,
scripts/plot/appearance/annotation.m, scripts/plot/appearance/axis.m,
scripts/plot/appearance/legend.m, scripts/plot/appearance/specular.m,
scripts/plot/draw/colorbar.m, scripts/plot/draw/hist.m,
scripts/plot/draw/plotmatrix.m, scripts/plot/draw/private/__stem__.m,
scripts/plot/util/__actual_axis_position__.m,
scripts/plot/util/__gnuplot_drawnow__.m, scripts/plot/util/findobj.m,
scripts/plot/util/print.m, scripts/plot/util/private/__go_draw_axes__.m,
scripts/plot/util/private/__print_parse_opts__.m, scripts/plot/util/rotate.m,
scripts/polynomial/pchip.m, scripts/polynomial/polyaffine.m,
scripts/polynomial/polyder.m, scripts/polynomial/private/__splinefit__.m,
scripts/polynomial/residue.m, scripts/signal/arch_fit.m,
scripts/signal/arch_rnd.m, scripts/signal/bartlett.m,
scripts/signal/blackman.m, scripts/signal/freqz.m, scripts/signal/hamming.m,
scripts/signal/hanning.m, scripts/signal/spectral_adf.m,
scripts/signal/spectral_xdf.m, scripts/signal/stft.m,
scripts/sparse/bicgstab.m, scripts/sparse/cgs.m,
scripts/sparse/private/__sprand_impl__.m, scripts/sparse/qmr.m,
scripts/sparse/sprandsym.m, scripts/sparse/svds.m, scripts/specfun/legendre.m,
scripts/special-matrix/gallery.m, scripts/statistics/base/gls.m,
scripts/statistics/models/logistic_regression.m,
scripts/statistics/tests/kruskal_wallis_test.m,
scripts/statistics/tests/manova.m, scripts/statistics/tests/wilcoxon_test.m,
scripts/time/datevec.m:
Use in-place operators in m-files where possible.
author | Rik <rik@octave.org> |
---|---|
date | Tue, 26 May 2015 21:07:42 -0700 |
parents | cc5ffacb15ab |
children |
rev | line source |
---|---|
19898
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
1 ## Copyright (C) 2006-2015 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 | 3 ## |
7016 | 4 ## This file is part of Octave. |
6212 | 5 ## |
7016 | 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. | |
6212 | 15 ## |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
6212 | 19 |
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{}) |
20370
03b9d17a2d95
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
24 ## 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:
20038
diff
changeset
|
25 ## |
03b9d17a2d95
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
26 ## 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:
20038
diff
changeset
|
27 ## @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:
20038
diff
changeset
|
28 ## acceptable. |
6212 | 29 ## |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
30 ## @var{tol} is the tolerance to which the 2-norm is calculated. By default |
20370
03b9d17a2d95
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
31 ## @var{tol} is 1e-6. |
03b9d17a2d95
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
32 ## |
03b9d17a2d95
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
33 ## The optional output @var{c} returns the number of iterations needed for |
6212 | 34 ## @code{normest} to converge. |
35 ## @end deftypefn | |
36 | |
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
|
37 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
|
38 |
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 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
|
40 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
|
41 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
|
42 |
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 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
|
44 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
|
45 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
|
46 |
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 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
|
48 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
|
49 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
|
50 |
9871
87595d714005
move normest to linear-algebra, improve it
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
51 if (! isfloat (A)) |
87595d714005
move normest to linear-algebra, improve it
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
52 A = double (A); |
87595d714005
move normest to linear-algebra, improve it
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
53 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
|
54 |
9871
87595d714005
move normest to linear-algebra, improve it
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
55 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
|
56 ## 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
|
57 v = rand ("state"); |
20389
cc5ffacb15ab
normest.m: Fix function to run with sparse complex inputs (bug #45080).
Rik <rik@octave.org>
parents:
20370
diff
changeset
|
58 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
|
59 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
|
60 ## 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
|
61 y = rand (ncols, 1); |
6212 | 62 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
|
63 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
|
64 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
|
65 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
|
66 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
|
67 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
|
68 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
|
69 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
|
70 else |
20441
83792dd9bcc1
Use in-place operators in m-files where possible.
Rik <rik@octave.org>
parents:
20389
diff
changeset
|
71 x /= normx; |
11113
a8ac114ec9ab
Stylefixes, replace end by endif.
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10788
diff
changeset
|
72 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
|
73 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
|
74 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
|
75 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
|
76 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
|
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 rand ("state", v); # restore state of random number generator |
6212 | 79 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
|
80 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
81 |
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
|
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 = 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
|
84 %! 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
|
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 |
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 %! A = rand (10); |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
88 %! 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
|
89 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
90 ## Test input validation |
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
|
91 %!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
|
92 %!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
|
93 %!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
|
94 %!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
|
95 %!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
|
96 %!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
|
97 |