comparison scripts/linear-algebra/condest.m @ 7189:e8d953d03f6a

[project @ 2007-11-26 20:42:09 by dbateman]
author dbateman
date Mon, 26 Nov 2007 20:42:11 +0000
parents
children b48a21816f2e
comparison
equal deleted inserted replaced
7188:fdd7cd70dc14 7189:e8d953d03f6a
1 ## Copyright (C) 2007, Regents of the University of California
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 -*-
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})
22 ## @deftypefnx {Function File} {[@var{est}, @var{v}] =} condest (@var{apply}, @var{apply_t}, @var{solve}, @var{solve_t}, @var{n}, @var{t})
23 ##
24 ## Estimate the 1-norm condition number of a matrix matrix @var{A}
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 ##
28 ## If the matrix is not explicit, e.g. when estimating the condition
29 ## number of @var{A} given an LU factorization, @code{condest} uses the
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
49 ## a vector @var{v} satisfying @code{norm (@var{A}*@var{v}, 1) == norm
50 ## (@var{A}, 1) * norm (@var{v}, 1) / @var{est}}. When @var{est} is
51 ## large, @var{v} is an approximate null vector.
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 ##
64 ## @seealso{norm, cond, onenormest}
65 ##
66 ## @end deftypefn
67
68 ## Code originally licensed under
69 ##
70 ## Copyright (c) 2007, Regents of the University of California
71 ## All rights reserved.
72 ## Redistribution and use in source and binary forms, with or without
73 ## modification, are permitted provided that the following conditions are met:
74 ##
75 ## * Redistributions of source code must retain the above copyright
76 ## notice, this list of conditions and the following disclaimer.
77 ## * Redistributions in binary form must reproduce the above copyright
78 ## notice, this list of conditions and the following disclaimer in the
79 ## documentation and/or other materials provided with the distribution.
80 ## * Neither the name of the University of California, Berkeley nor the
81 ## names of its contributors may be used to endorse or promote products
82 ## derived from this software without specific prior written permission.
83 ##
84 ## THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
85 ## EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
86 ## WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
87 ## DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR
88 ## ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
89 ## DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
90 ## OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
91 ## HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
92 ## LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
93 ## OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
94 ## SUCH DAMAGE.
95 ##
96 ## Relicensed to GPL for inclusion in Octave.
97
98 ## Author: Jason Riedy <ejr@cs.berkeley.edu>
99 ## Keywords: linear-algebra norm estimation
100 ## Version: 0.2
101
102 function [est, v] = condest (varargin)
103 if size (varargin, 2) < 1 || size (varargin, 2) > 5,
104 usage("condest: Incorrect arguments.");
105 endif
106
107 default_t = 5;
108
109 if (ismatrix (varargin{1}))
110 n = size (varargin{1}, 1);
111 if (n != size (varargin{1}, 2))
112 error ("condest: matrix must be square.");
113 endif
114 A = varargin{1};
115
116 if (size (varargin, 2) > 1)
117 if (isscalar (varargin{2}))
118 t = varargin{2};
119 else
120 if (size (varargin, 2) < 3)
121 error ("condest: must supply both solve and solve_t.");
122 else
123 solve = varargin{2};
124 solve_t = varargin{3};
125 if size (varargin, 2) > 3,
126 t = varargin{4};
127 endif
128 endif
129 endif
130 endif
131 else
132 if (size (varargin, 2) < 5)
133 error ("condest: implicit form of condest requires at least 5 arguments.");
134 endif
135 apply = varargin{1};
136 apply_t = varargin{2};
137 solve = varargin{3};
138 solve_t = varargin{4};
139 n = varargin{5};
140 if (! isscalar (n))
141 error ("condest: dimension argument of implicit form must be scalar.");
142 endif
143 if (size (varargin, 2) > 5)
144 t = varargin{6};
145 endif
146 endif
147
148 if (! exist ("t", "var"))
149 t = min (n, default_t);
150 endif
151
152 if (! exist ("solve", "var"))
153 if (issparse (A))
154 [L, U, P, Pc] = splu (A);
155 solve = @(x) Pc' * (U\ (L\ (P*x)));
156 solve_t = @(x) P'*(L'\ (U'\ (Pc*x)));
157 else
158 [L, U, P] = lu (A);
159 solve = @(x) U\ (L\ (P*x));
160 solve_t = @(x) P' * (L'\ (U'\x));
161 endif
162 endif
163
164 if (exist ("A", "var"))
165 Anorm = norm (A, 1);
166 else
167 Anorm = onenormest (apply, apply_t, n, t);
168 endif
169
170 [Ainv_norm, v, w] = onenormest (solve, solve_t, n, t);
171
172 est = Anorm * Ainv_norm;
173 v = w / norm (w, 1);
174
175 endfunction
176
177 %!demo
178 %! N = 100;
179 %! A = randn (N) + eye (N);
180 %! condest (A)
181 %! [L,U,P] = lu (A);
182 %! condest (A, @(x) U\ (L\ (P*x)), @(x) P'*(L'\ (U'\x)))
183 %! condest (@(x) A*x, @(x) A'*x, @(x) U\ (L\ (P*x)), @(x) P'*(L'\ (U'\x)), N)
184 %! norm (inv (A), 1) * norm (A, 1)
185
186 ## Yes, these test bounds are really loose. There's
187 ## enough randomization to trigger odd cases with hilb().
188
189 %!test
190 %! N = 6;
191 %! A = hilb (N);
192 %! cA = condest (A);
193 %! cA_test = norm (inv (A), 1) * norm (A, 1);
194 %! assert (cA, cA_test, 2^-12);
195
196 %!test
197 %! N = 6;
198 %! A = hilb (N);
199 %! solve = @(x) A\x; solve_t = @(x) A'\x;
200 %! cA = condest (A, solve, solve_t);
201 %! cA_test = norm (inv (A), 1) * norm (A, 1);
202 %! assert (cA, cA_test, 2^-12);
203
204 %!test
205 %! N = 6;
206 %! A = hilb (N);
207 %! apply = @(x) A*x; apply_t = @(x) A'*x;
208 %! solve = @(x) A\x; solve_t = @(x) A'\x;
209 %! cA = condest (apply, apply_t, solve, solve_t, N);
210 %! cA_test = norm (inv (A), 1) * norm (A, 1);
211 %! assert (cA, cA_test, 2^-6);
212
213 %!test
214 %! N = 12;
215 %! A = hilb (N);
216 %! [rcondA, v] = condest (A);
217 %! x = A*v;
218 %! assert (norm(x, inf), 0, eps);