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