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