Mercurial > hg > octave-lyh
annotate scripts/linear-algebra/condest.m @ 16466:ac332eb727dd
Simplify calculation of colormaps
* scripts/image/copper.m: Replace slower linspace with range operator.
* scripts/image/hot.m: Add test for n==2 to allow elimination of subsequent
if conditional tests. Replace mod calculation with simpler expression.
* scripts/image/rainbow.m: Replace slower linspace with range operator.
Align columns of calculation for better code readability.
author | Rik <rik@octave.org> |
---|---|
date | Sun, 07 Apr 2013 22:24:34 -0700 |
parents | f739e30494c8 |
children | 12005245b645 |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
11593
diff
changeset
|
1 ## Copyright (C) 2007-2012 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: |
7189 | 59 ## @itemize |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
60 ## @item |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
61 ## N.J. Higham and F. Tisseur, @cite{A Block Algorithm |
7189 | 62 ## 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
|
63 ## Pseudospectra}. SIMAX vol 21, no 4, pp 1185-1201. |
7189 | 64 ## @url{http://dx.doi.org/10.1137/S0895479899356080} |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
65 ## |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
66 ## @item |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
67 ## N.J. Higham and F. Tisseur, @cite{A Block Algorithm |
7189 | 68 ## 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
|
69 ## Pseudospectra}. @url{http://citeseer.ist.psu.edu/223007.html} |
7189 | 70 ## @end itemize |
71 ## | |
9307
c2923c27c877
Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents:
9051
diff
changeset
|
72 ## @seealso{cond, norm, onenormest} |
7189 | 73 ## @end deftypefn |
74 | |
75 ## Code originally licensed under | |
76 ## | |
77 ## Copyright (c) 2007, Regents of the University of California | |
78 ## All rights reserved. | |
7191 | 79 ## |
7189 | 80 ## Redistribution and use in source and binary forms, with or without |
7191 | 81 ## modification, are permitted provided that the following conditions |
82 ## are met: | |
7189 | 83 ## |
84 ## * Redistributions of source code must retain the above copyright | |
85 ## notice, this list of conditions and the following disclaimer. | |
7191 | 86 ## |
87 ## * Redistributions in binary form must reproduce the above | |
88 ## copyright notice, this list of conditions and the following | |
89 ## disclaimer in the documentation and/or other materials provided | |
90 ## with the distribution. | |
91 ## | |
92 ## * Neither the name of the University of California, Berkeley nor | |
93 ## the names of its contributors may be used to endorse or promote | |
94 ## products derived from this software without specific prior | |
95 ## written permission. | |
7189 | 96 ## |
7191 | 97 ## THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' |
98 ## AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | |
99 ## TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A | |
100 ## PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND | |
101 ## CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
102 ## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
103 ## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | |
104 ## USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
105 ## ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
106 ## OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |
107 ## OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
7189 | 108 ## SUCH DAMAGE. |
109 | |
110 ## Author: Jason Riedy <ejr@cs.berkeley.edu> | |
111 ## Keywords: linear-algebra norm estimation | |
112 ## Version: 0.2 | |
113 | |
114 function [est, v] = condest (varargin) | |
7191 | 115 |
7309 | 116 if (nargin < 1 || nargin > 6) |
7191 | 117 print_usage (); |
7189 | 118 endif |
119 | |
120 default_t = 5; | |
121 | |
7312 | 122 have_A = false; |
123 have_t = false; | |
124 have_solve = false; | |
125 | |
7189 | 126 if (ismatrix (varargin{1})) |
7310 | 127 A = varargin{1}; |
9872
72d6e0de76c7
fix qp, condest and krylov
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
128 if (! issquare (A)) |
8664 | 129 error ("condest: matrix must be square"); |
7189 | 130 endif |
9872
72d6e0de76c7
fix qp, condest and krylov
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
131 n = rows (A); |
7312 | 132 have_A = true; |
7189 | 133 |
7312 | 134 if (nargin > 1) |
14522
f739e30494c8
condest.m: Fix failing %!test.
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
135 if (! is_function_handle (varargin{2})) |
10549 | 136 t = varargin{2}; |
137 have_t = true; | |
7312 | 138 elseif (nargin > 2) |
10549 | 139 solve = varargin{2}; |
140 solve_t = varargin{3}; | |
141 have_solve = true; | |
142 if (nargin > 3) | |
143 t = varargin{4}; | |
144 have_t = true; | |
145 endif | |
7312 | 146 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
|
147 error ("condest: must supply both SOLVE and SOLVE_T"); |
7189 | 148 endif |
149 endif | |
7311 | 150 elseif (nargin > 4) |
7189 | 151 apply = varargin{1}; |
152 apply_t = varargin{2}; | |
153 solve = varargin{3}; | |
154 solve_t = varargin{4}; | |
7312 | 155 have_solve = true; |
7189 | 156 n = varargin{5}; |
157 if (! isscalar (n)) | |
8664 | 158 error ("condest: dimension argument of implicit form must be scalar"); |
7189 | 159 endif |
7309 | 160 if (nargin > 5) |
7189 | 161 t = varargin{6}; |
7312 | 162 have_t = true; |
7189 | 163 endif |
7311 | 164 else |
8664 | 165 error ("condest: implicit form of condest requires at least 5 arguments"); |
7189 | 166 endif |
167 | |
7312 | 168 if (! have_t) |
7189 | 169 t = min (n, default_t); |
170 endif | |
171 | |
7312 | 172 if (! have_solve) |
7189 | 173 if (issparse (A)) |
8912 | 174 [L, U, P, Pc] = lu (A); |
7312 | 175 solve = @(x) Pc' * (U \ (L \ (P * x))); |
176 solve_t = @(x) P' * (L' \ (U' \ (Pc * x))); | |
7189 | 177 else |
178 [L, U, P] = lu (A); | |
7312 | 179 solve = @(x) U \ (L \ (P*x)); |
180 solve_t = @(x) P' * (L' \ (U' \ x)); | |
7189 | 181 endif |
182 endif | |
183 | |
7312 | 184 if (have_A) |
7189 | 185 Anorm = norm (A, 1); |
186 else | |
187 Anorm = onenormest (apply, apply_t, n, t); | |
188 endif | |
189 | |
190 [Ainv_norm, v, w] = onenormest (solve, solve_t, n, t); | |
191 | |
192 est = Anorm * Ainv_norm; | |
193 v = w / norm (w, 1); | |
194 | |
195 endfunction | |
196 | |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
197 |
7189 | 198 %!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
|
199 %! 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
|
200 %! 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
|
201 %! 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
|
202 %! [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
|
203 %! 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
|
204 %! 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
|
205 %! norm (inv (A), 1) * norm (A, 1) |
7189 | 206 |
207 ## Yes, these test bounds are really loose. There's | |
208 ## enough randomization to trigger odd cases with hilb(). | |
209 | |
210 %!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
|
211 %! 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
|
212 %! 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
|
213 %! 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
|
214 %! 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
|
215 %! assert (cA, cA_test, -2^-8); |
7189 | 216 |
217 %!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
|
218 %! 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
|
219 %! 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
|
220 %! 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
|
221 %! 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
|
222 %! 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
|
223 %! assert (cA, cA_test, -2^-8); |
7189 | 224 |
225 %!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
|
226 %! 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
|
227 %! 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
|
228 %! 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
|
229 %! 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
|
230 %! 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
|
231 %! 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
|
232 %! assert (cA, cA_test, -2^-6); |
7189 | 233 |
234 %!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
|
235 %! 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
|
236 %! 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
|
237 %! [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
|
238 %! x = A*v; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
239 %! 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
|
240 |