Mercurial > hg > octave-nkf
annotate scripts/linear-algebra/condest.m @ 19840:c5270263d466 gui-release
close gui-release branch
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 30 Jan 2015 17:41:50 -0500 |
parents | d63878346099 |
children | 0850b5212619 |
rev | line source |
---|---|
17744
d63878346099
maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents:
16816
diff
changeset
|
1 ## Copyright (C) 2007-2013 Regents of the University of California |
7189 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7 ## the Free Software Foundation; either version 3 of the License, or (at | |
8 ## your option) any later version. | |
9 ## | |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
16 ## along with Octave; see the file COPYING. If not, see | |
17 ## <http://www.gnu.org/licenses/>. | |
18 | |
19 ## -*- texinfo -*- | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
20 ## @deftypefn {Function File} {} condest (@var{A}) |
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
21 ## @deftypefnx {Function File} {} condest (@var{A}, @var{t}) |
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
22 ## @deftypefnx {Function File} {[@var{est}, @var{v}] =} condest (@dots{}) |
11471
994e2a93a8e2
Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
23 ## @deftypefnx {Function File} {[@var{est}, @var{v}] =} condest (@var{A}, @var{solve}, @var{solve_t}, @var{t}) |
7189 | 24 ## @deftypefnx {Function File} {[@var{est}, @var{v}] =} condest (@var{apply}, @var{apply_t}, @var{solve}, @var{solve_t}, @var{n}, @var{t}) |
25 ## | |
8347
fa78cb8d8a5c
corrections for typos
Brian Gough<bjg@network-theory.co.uk>
parents:
7312
diff
changeset
|
26 ## Estimate the 1-norm condition number of a matrix @var{A} |
7189 | 27 ## using @var{t} test vectors using a randomized 1-norm estimator. |
28 ## If @var{t} exceeds 5, then only 5 test vectors are used. | |
29 ## | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
30 ## If the matrix is not explicit, e.g., when estimating the condition |
11593
1577c6f80926
Use non-breaking spaces between certain adjectives and their nouns in docstrings.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
31 ## number of @var{A} given an LU@tie{}factorization, @code{condest} uses the |
7189 | 32 ## following functions: |
33 ## | |
34 ## @table @var | |
35 ## @item apply | |
36 ## @code{A*x} for a matrix @code{x} of size @var{n} by @var{t}. | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
37 ## |
7189 | 38 ## @item apply_t |
39 ## @code{A'*x} for a matrix @code{x} of size @var{n} by @var{t}. | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
40 ## |
7189 | 41 ## @item solve |
42 ## @code{A \ b} for a matrix @code{b} of size @var{n} by @var{t}. | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
43 ## |
7189 | 44 ## @item solve_t |
45 ## @code{A' \ b} for a matrix @code{b} of size @var{n} by @var{t}. | |
46 ## @end table | |
47 ## | |
48 ## The implicit version requires an explicit dimension @var{n}. | |
49 ## | |
50 ## @code{condest} uses a randomized algorithm to approximate | |
51 ## the 1-norms. | |
52 ## | |
53 ## @code{condest} returns the 1-norm condition estimate @var{est} and | |
7191 | 54 ## a vector @var{v} satisfying @code{norm (A*v, 1) == norm (A, 1) * norm |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
55 ## (@var{v}, 1) / @var{est}}. When @var{est} is large, @var{v} is an |
7191 | 56 ## approximate null vector. |
7189 | 57 ## |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
58 ## References: |
16816
12005245b645
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
14522
diff
changeset
|
59 ## |
7189 | 60 ## @itemize |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
61 ## @item |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
62 ## N.J. Higham and F. Tisseur, @cite{A Block Algorithm |
7189 | 63 ## for Matrix 1-Norm Estimation, with an Application to 1-Norm |
10791
3140cb7a05a1
Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
64 ## Pseudospectra}. SIMAX vol 21, no 4, pp 1185-1201. |
7189 | 65 ## @url{http://dx.doi.org/10.1137/S0895479899356080} |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
66 ## |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
67 ## @item |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
68 ## N.J. Higham and F. Tisseur, @cite{A Block Algorithm |
7189 | 69 ## for Matrix 1-Norm Estimation, with an Application to 1-Norm |
10791
3140cb7a05a1
Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
70 ## Pseudospectra}. @url{http://citeseer.ist.psu.edu/223007.html} |
7189 | 71 ## @end itemize |
72 ## | |
9307
c2923c27c877
Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents:
9051
diff
changeset
|
73 ## @seealso{cond, norm, onenormest} |
7189 | 74 ## @end deftypefn |
75 | |
76 ## Code originally licensed under | |
77 ## | |
78 ## Copyright (c) 2007, Regents of the University of California | |
79 ## All rights reserved. | |
7191 | 80 ## |
7189 | 81 ## Redistribution and use in source and binary forms, with or without |
7191 | 82 ## modification, are permitted provided that the following conditions |
83 ## are met: | |
7189 | 84 ## |
85 ## * Redistributions of source code must retain the above copyright | |
86 ## notice, this list of conditions and the following disclaimer. | |
7191 | 87 ## |
88 ## * Redistributions in binary form must reproduce the above | |
89 ## copyright notice, this list of conditions and the following | |
90 ## disclaimer in the documentation and/or other materials provided | |
91 ## with the distribution. | |
92 ## | |
93 ## * Neither the name of the University of California, Berkeley nor | |
94 ## the names of its contributors may be used to endorse or promote | |
95 ## products derived from this software without specific prior | |
96 ## written permission. | |
7189 | 97 ## |
7191 | 98 ## THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' |
99 ## AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | |
100 ## TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A | |
101 ## PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND | |
102 ## CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
103 ## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
104 ## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | |
105 ## USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
106 ## ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
107 ## OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |
108 ## OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
7189 | 109 ## SUCH DAMAGE. |
110 | |
111 ## Author: Jason Riedy <ejr@cs.berkeley.edu> | |
112 ## Keywords: linear-algebra norm estimation | |
113 ## Version: 0.2 | |
114 | |
115 function [est, v] = condest (varargin) | |
7191 | 116 |
7309 | 117 if (nargin < 1 || nargin > 6) |
7191 | 118 print_usage (); |
7189 | 119 endif |
120 | |
121 default_t = 5; | |
122 | |
7312 | 123 have_A = false; |
124 have_t = false; | |
125 have_solve = false; | |
126 | |
7189 | 127 if (ismatrix (varargin{1})) |
7310 | 128 A = varargin{1}; |
9872
72d6e0de76c7
fix qp, condest and krylov
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
129 if (! issquare (A)) |
8664 | 130 error ("condest: matrix must be square"); |
7189 | 131 endif |
9872
72d6e0de76c7
fix qp, condest and krylov
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
132 n = rows (A); |
7312 | 133 have_A = true; |
7189 | 134 |
7312 | 135 if (nargin > 1) |
14522
f739e30494c8
condest.m: Fix failing %!test.
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
136 if (! is_function_handle (varargin{2})) |
10549 | 137 t = varargin{2}; |
138 have_t = true; | |
7312 | 139 elseif (nargin > 2) |
10549 | 140 solve = varargin{2}; |
141 solve_t = varargin{3}; | |
142 have_solve = true; | |
143 if (nargin > 3) | |
144 t = varargin{4}; | |
145 have_t = true; | |
146 endif | |
7312 | 147 else |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
11471
diff
changeset
|
148 error ("condest: must supply both SOLVE and SOLVE_T"); |
7189 | 149 endif |
150 endif | |
7311 | 151 elseif (nargin > 4) |
7189 | 152 apply = varargin{1}; |
153 apply_t = varargin{2}; | |
154 solve = varargin{3}; | |
155 solve_t = varargin{4}; | |
7312 | 156 have_solve = true; |
7189 | 157 n = varargin{5}; |
158 if (! isscalar (n)) | |
8664 | 159 error ("condest: dimension argument of implicit form must be scalar"); |
7189 | 160 endif |
7309 | 161 if (nargin > 5) |
7189 | 162 t = varargin{6}; |
7312 | 163 have_t = true; |
7189 | 164 endif |
7311 | 165 else |
8664 | 166 error ("condest: implicit form of condest requires at least 5 arguments"); |
7189 | 167 endif |
168 | |
7312 | 169 if (! have_t) |
7189 | 170 t = min (n, default_t); |
171 endif | |
172 | |
7312 | 173 if (! have_solve) |
7189 | 174 if (issparse (A)) |
8912 | 175 [L, U, P, Pc] = lu (A); |
7312 | 176 solve = @(x) Pc' * (U \ (L \ (P * x))); |
177 solve_t = @(x) P' * (L' \ (U' \ (Pc * x))); | |
7189 | 178 else |
179 [L, U, P] = lu (A); | |
7312 | 180 solve = @(x) U \ (L \ (P*x)); |
181 solve_t = @(x) P' * (L' \ (U' \ x)); | |
7189 | 182 endif |
183 endif | |
184 | |
7312 | 185 if (have_A) |
7189 | 186 Anorm = norm (A, 1); |
187 else | |
188 Anorm = onenormest (apply, apply_t, n, t); | |
189 endif | |
190 | |
191 [Ainv_norm, v, w] = onenormest (solve, solve_t, n, t); | |
192 | |
193 est = Anorm * Ainv_norm; | |
194 v = w / norm (w, 1); | |
195 | |
196 endfunction | |
197 | |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
198 |
7189 | 199 %!demo |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
200 %! N = 100; |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
201 %! A = randn (N) + eye (N); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
202 %! condest (A) |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
203 %! [L,U,P] = lu (A); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
204 %! condest (A, @(x) U \ (L \ (P*x)), @(x) P'*(L' \ (U'\x))) |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
205 %! condest (@(x) A*x, @(x) A'*x, @(x) U \ (L \ (P*x)), @(x) P'*(L' \ (U'\x)), N) |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
206 %! norm (inv (A), 1) * norm (A, 1) |
7189 | 207 |
208 ## Yes, these test bounds are really loose. There's | |
209 ## enough randomization to trigger odd cases with hilb(). | |
210 | |
211 %!test | |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
212 %! N = 6; |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
213 %! A = hilb (N); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
214 %! cA = condest (A); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
215 %! cA_test = norm (inv (A), 1) * norm (A, 1); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
216 %! assert (cA, cA_test, -2^-8); |
7189 | 217 |
218 %!test | |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
219 %! N = 6; |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
220 %! A = hilb (N); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
221 %! solve = @(x) A\x; solve_t = @(x) A'\x; |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
222 %! cA = condest (A, solve, solve_t); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
223 %! cA_test = norm (inv (A), 1) * norm (A, 1); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
224 %! assert (cA, cA_test, -2^-8); |
7189 | 225 |
226 %!test | |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
227 %! N = 6; |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
228 %! A = hilb (N); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
229 %! apply = @(x) A*x; apply_t = @(x) A'*x; |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
230 %! solve = @(x) A\x; solve_t = @(x) A'\x; |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
231 %! cA = condest (apply, apply_t, solve, solve_t, N); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
232 %! cA_test = norm (inv (A), 1) * norm (A, 1); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
233 %! assert (cA, cA_test, -2^-6); |
7189 | 234 |
235 %!test | |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
236 %! N = 12; |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
237 %! A = hilb (N); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
238 %! [rcondA, v] = condest (A); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
239 %! x = A*v; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
240 %! assert (norm (x, inf), 0, eps); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
241 |