Mercurial > hg > octave-nkf
annotate scripts/linear-algebra/condest.m @ 10793:be55736a0783
Grammarcheck the documentation from m-files.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Sun, 18 Jul 2010 20:35:16 -0700 |
parents | 3140cb7a05a1 |
children | 693e22af08ae |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2007, 2008, 2009 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 -*- | |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
20 ## @deftypefn {Function File} {} condest (@var{a}) |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
21 ## @deftypefnx {Function File} {} condest (@var{a}, @var{t}) |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
22 ## @deftypefnx {Function File} {[@var{est}, @var{v}] =} condest (@dots{}) |
7191 | 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 ## | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
30 ## If the matrix is not explicit, e.g., when estimating the condition |
7191 | 31 ## number of @var{a} given an LU 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}. | |
37 ## @item apply_t | |
38 ## @code{A'*x} for a matrix @code{x} of size @var{n} by @var{t}. | |
39 ## @item solve | |
40 ## @code{A \ b} for a matrix @code{b} of size @var{n} by @var{t}. | |
41 ## @item solve_t | |
42 ## @code{A' \ b} for a matrix @code{b} of size @var{n} by @var{t}. | |
43 ## @end table | |
44 ## | |
45 ## The implicit version requires an explicit dimension @var{n}. | |
46 ## | |
47 ## @code{condest} uses a randomized algorithm to approximate | |
48 ## the 1-norms. | |
49 ## | |
50 ## @code{condest} returns the 1-norm condition estimate @var{est} and | |
7191 | 51 ## 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
|
52 ## (@var{v}, 1) / @var{est}}. When @var{est} is large, @var{v} is an |
7191 | 53 ## approximate null vector. |
7189 | 54 ## |
55 ## References: | |
56 ## @itemize | |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
57 ## @item |
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
58 ## N.J. Higham and F. Tisseur, @cite{A Block Algorithm |
7189 | 59 ## 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
|
60 ## Pseudospectra}. SIMAX vol 21, no 4, pp 1185-1201. |
7189 | 61 ## @url{http://dx.doi.org/10.1137/S0895479899356080} |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
62 ## @item |
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
63 ## N.J. Higham and F. Tisseur, @cite{A Block Algorithm |
7189 | 64 ## 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
|
65 ## Pseudospectra}. @url{http://citeseer.ist.psu.edu/223007.html} |
7189 | 66 ## @end itemize |
67 ## | |
9307
c2923c27c877
Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents:
9051
diff
changeset
|
68 ## @seealso{cond, norm, onenormest} |
7189 | 69 ## @end deftypefn |
70 | |
71 ## Code originally licensed under | |
72 ## | |
73 ## Copyright (c) 2007, Regents of the University of California | |
74 ## All rights reserved. | |
7191 | 75 ## |
7189 | 76 ## Redistribution and use in source and binary forms, with or without |
7191 | 77 ## modification, are permitted provided that the following conditions |
78 ## are met: | |
7189 | 79 ## |
80 ## * Redistributions of source code must retain the above copyright | |
81 ## notice, this list of conditions and the following disclaimer. | |
7191 | 82 ## |
83 ## * Redistributions in binary form must reproduce the above | |
84 ## copyright notice, this list of conditions and the following | |
85 ## disclaimer in the documentation and/or other materials provided | |
86 ## with the distribution. | |
87 ## | |
88 ## * Neither the name of the University of California, Berkeley nor | |
89 ## the names of its contributors may be used to endorse or promote | |
90 ## products derived from this software without specific prior | |
91 ## written permission. | |
7189 | 92 ## |
7191 | 93 ## THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' |
94 ## AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | |
95 ## TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A | |
96 ## PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND | |
97 ## CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
98 ## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
99 ## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | |
100 ## USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
101 ## ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
102 ## OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |
103 ## OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
7189 | 104 ## SUCH DAMAGE. |
105 | |
106 ## Author: Jason Riedy <ejr@cs.berkeley.edu> | |
107 ## Keywords: linear-algebra norm estimation | |
108 ## Version: 0.2 | |
109 | |
110 function [est, v] = condest (varargin) | |
7191 | 111 |
7309 | 112 if (nargin < 1 || nargin > 6) |
7191 | 113 print_usage (); |
7189 | 114 endif |
115 | |
116 default_t = 5; | |
117 | |
7312 | 118 have_A = false; |
119 have_t = false; | |
120 have_solve = false; | |
121 | |
7189 | 122 if (ismatrix (varargin{1})) |
7310 | 123 A = varargin{1}; |
9872
72d6e0de76c7
fix qp, condest and krylov
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
124 if (! issquare (A)) |
8664 | 125 error ("condest: matrix must be square"); |
7189 | 126 endif |
9872
72d6e0de76c7
fix qp, condest and krylov
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
127 n = rows (A); |
7312 | 128 have_A = true; |
7189 | 129 |
7312 | 130 if (nargin > 1) |
131 if (isscalar (varargin{2})) | |
10549 | 132 t = varargin{2}; |
133 have_t = true; | |
7312 | 134 elseif (nargin > 2) |
10549 | 135 solve = varargin{2}; |
136 solve_t = varargin{3}; | |
137 have_solve = true; | |
138 if (nargin > 3) | |
139 t = varargin{4}; | |
140 have_t = true; | |
141 endif | |
7312 | 142 else |
10549 | 143 error ("condest: must supply both solve and solve_t"); |
7189 | 144 endif |
145 endif | |
7311 | 146 elseif (nargin > 4) |
7189 | 147 apply = varargin{1}; |
148 apply_t = varargin{2}; | |
149 solve = varargin{3}; | |
150 solve_t = varargin{4}; | |
7312 | 151 have_solve = true; |
7189 | 152 n = varargin{5}; |
153 if (! isscalar (n)) | |
8664 | 154 error ("condest: dimension argument of implicit form must be scalar"); |
7189 | 155 endif |
7309 | 156 if (nargin > 5) |
7189 | 157 t = varargin{6}; |
7312 | 158 have_t = true; |
7189 | 159 endif |
7311 | 160 else |
8664 | 161 error ("condest: implicit form of condest requires at least 5 arguments"); |
7189 | 162 endif |
163 | |
7312 | 164 if (! have_t) |
7189 | 165 t = min (n, default_t); |
166 endif | |
167 | |
7312 | 168 if (! have_solve) |
7189 | 169 if (issparse (A)) |
8912 | 170 [L, U, P, Pc] = lu (A); |
7312 | 171 solve = @(x) Pc' * (U \ (L \ (P * x))); |
172 solve_t = @(x) P' * (L' \ (U' \ (Pc * x))); | |
7189 | 173 else |
174 [L, U, P] = lu (A); | |
7312 | 175 solve = @(x) U \ (L \ (P*x)); |
176 solve_t = @(x) P' * (L' \ (U' \ x)); | |
7189 | 177 endif |
178 endif | |
179 | |
7312 | 180 if (have_A) |
7189 | 181 Anorm = norm (A, 1); |
182 else | |
183 Anorm = onenormest (apply, apply_t, n, t); | |
184 endif | |
185 | |
186 [Ainv_norm, v, w] = onenormest (solve, solve_t, n, t); | |
187 | |
188 est = Anorm * Ainv_norm; | |
189 v = w / norm (w, 1); | |
190 | |
191 endfunction | |
192 | |
193 %!demo | |
194 %! N = 100; | |
195 %! A = randn (N) + eye (N); | |
196 %! condest (A) | |
197 %! [L,U,P] = lu (A); | |
198 %! condest (A, @(x) U\ (L\ (P*x)), @(x) P'*(L'\ (U'\x))) | |
199 %! condest (@(x) A*x, @(x) A'*x, @(x) U\ (L\ (P*x)), @(x) P'*(L'\ (U'\x)), N) | |
200 %! norm (inv (A), 1) * norm (A, 1) | |
201 | |
202 ## Yes, these test bounds are really loose. There's | |
203 ## enough randomization to trigger odd cases with hilb(). | |
204 | |
205 %!test | |
206 %! N = 6; | |
207 %! A = hilb (N); | |
208 %! cA = condest (A); | |
209 %! cA_test = norm (inv (A), 1) * norm (A, 1); | |
8912 | 210 %! assert (cA, cA_test, -2^-8); |
7189 | 211 |
212 %!test | |
213 %! N = 6; | |
214 %! A = hilb (N); | |
215 %! solve = @(x) A\x; solve_t = @(x) A'\x; | |
216 %! cA = condest (A, solve, solve_t); | |
217 %! cA_test = norm (inv (A), 1) * norm (A, 1); | |
8912 | 218 %! assert (cA, cA_test, -2^-8); |
7189 | 219 |
220 %!test | |
221 %! N = 6; | |
222 %! A = hilb (N); | |
223 %! apply = @(x) A*x; apply_t = @(x) A'*x; | |
224 %! solve = @(x) A\x; solve_t = @(x) A'\x; | |
225 %! cA = condest (apply, apply_t, solve, solve_t, N); | |
226 %! cA_test = norm (inv (A), 1) * norm (A, 1); | |
8912 | 227 %! assert (cA, cA_test, -2^-6); |
7189 | 228 |
229 %!test | |
230 %! N = 12; | |
231 %! A = hilb (N); | |
232 %! [rcondA, v] = condest (A); | |
233 %! x = A*v; | |
234 %! assert (norm(x, inf), 0, eps); |