1 ## Copyright (C) 2007-2012 Regents of the University of California
3 ## This file is part of Octave.
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.
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.
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/>.
20 ## @deftypefn {Function File} {} condest (@var{A})
21 ## @deftypefnx {Function File} {} condest (@var{A}, @var{t})
22 ## @deftypefnx {Function File} {[@var{est}, @var{v}] =} condest (@dots{})
23 ## @deftypefnx {Function File} {[@var{est}, @var{v}] =} condest (@var{A}, @var{solve}, @var{solve_t}, @var{t})
24 ## @deftypefnx {Function File} {[@var{est}, @var{v}] =} condest (@var{apply}, @var{apply_t}, @var{solve}, @var{solve_t}, @var{n}, @var{t})
26 ## Estimate the 1-norm condition number of a matrix @var{A}
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.
30 ## If the matrix is not explicit, e.g., when estimating the condition
31 ## number of @var{A} given an LU@tie{}factorization, @code{condest} uses the
32 ## following functions:
36 ## @code{A*x} for a matrix @code{x} of size @var{n} by @var{t}.
39 ## @code{A'*x} for a matrix @code{x} of size @var{n} by @var{t}.
42 ## @code{A \ b} for a matrix @code{b} of size @var{n} by @var{t}.
45 ## @code{A' \ b} for a matrix @code{b} of size @var{n} by @var{t}.
48 ## The implicit version requires an explicit dimension @var{n}.
50 ## @code{condest} uses a randomized algorithm to approximate
53 ## @code{condest} returns the 1-norm condition estimate @var{est} and
54 ## a vector @var{v} satisfying @code{norm (A*v, 1) == norm (A, 1) * norm
55 ## (@var{v}, 1) / @var{est}}. When @var{est} is large, @var{v} is an
56 ## approximate null vector.
61 ## N.J. Higham and F. Tisseur, @cite{A Block Algorithm
62 ## for Matrix 1-Norm Estimation, with an Application to 1-Norm
63 ## Pseudospectra}. SIMAX vol 21, no 4, pp 1185-1201.
64 ## @url{http://dx.doi.org/10.1137/S0895479899356080}
67 ## N.J. Higham and F. Tisseur, @cite{A Block Algorithm
68 ## for Matrix 1-Norm Estimation, with an Application to 1-Norm
69 ## Pseudospectra}. @url{http://citeseer.ist.psu.edu/223007.html}
72 ## @seealso{cond, norm, onenormest}
75 ## Code originally licensed under
77 ## Copyright (c) 2007, Regents of the University of California
78 ## All rights reserved.
80 ## Redistribution and use in source and binary forms, with or without
81 ## modification, are permitted provided that the following conditions
84 ## * Redistributions of source code must retain the above copyright
85 ## notice, this list of conditions and the following disclaimer.
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.
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.
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
110 ## Author: Jason Riedy <ejr@cs.berkeley.edu>
111 ## Keywords: linear-algebra norm estimation
114 function [est, v] = condest (varargin)
116 if (nargin < 1 || nargin > 6)
126 if (ismatrix (varargin{1}))
129 error ("condest: matrix must be square");
135 if (isscalar (varargin{2}))
140 solve_t = varargin{3};
147 error ("condest: must supply both SOLVE and SOLVE_T");
152 apply_t = varargin{2};
154 solve_t = varargin{4};
158 error ("condest: dimension argument of implicit form must be scalar");
165 error ("condest: implicit form of condest requires at least 5 arguments");
169 t = min (n, default_t);
174 [L, U, P, Pc] = lu (A);
175 solve = @(x) Pc' * (U \ (L \ (P * x)));
176 solve_t = @(x) P' * (L' \ (U' \ (Pc * x)));
179 solve = @(x) U \ (L \ (P*x));
180 solve_t = @(x) P' * (L' \ (U' \ x));
187 Anorm = onenormest (apply, apply_t, n, t);
190 [Ainv_norm, v, w] = onenormest (solve, solve_t, n, t);
192 est = Anorm * Ainv_norm;
199 %! A = randn (N) + eye (N);
202 %! condest (A, @(x) U\ (L\ (P*x)), @(x) P'*(L'\ (U'\x)))
203 %! condest (@(x) A*x, @(x) A'*x, @(x) U\ (L\ (P*x)), @(x) P'*(L'\ (U'\x)), N)
204 %! norm (inv (A), 1) * norm (A, 1)
206 ## Yes, these test bounds are really loose. There's
207 ## enough randomization to trigger odd cases with hilb().
213 %! cA_test = norm (inv (A), 1) * norm (A, 1);
214 %! assert (cA, cA_test, -2^-8);
219 %! solve = @(x) A\x; solve_t = @(x) A'\x;
220 %! cA = condest (A, solve, solve_t);
221 %! cA_test = norm (inv (A), 1) * norm (A, 1);
222 %! assert (cA, cA_test, -2^-8);
227 %! apply = @(x) A*x; apply_t = @(x) A'*x;
228 %! solve = @(x) A\x; solve_t = @(x) A'\x;
229 %! cA = condest (apply, apply_t, solve, solve_t, N);
230 %! cA_test = norm (inv (A), 1) * norm (A, 1);
231 %! assert (cA, cA_test, -2^-6);
236 %! [rcondA, v] = condest (A);
238 %! assert (norm(x, inf), 0, eps);