1 ## Copyright (C) 1993-2012 John W. Eaton
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} {} cond (@var{A})
21 ## @deftypefnx {Function File} {} cond (@var{A}, @var{p})
22 ## Compute the @var{p}-norm condition number of a matrix.
24 ## @code{cond (@var{A})} is ## defined as
26 ## $ {\parallel A \parallel_p * \parallel A^{-1} \parallel_p .} $
29 ## @code{norm (@var{A}, @var{p}) * norm (inv (@var{A}), @var{p})}.
32 ## By default @code{@var{p} = 2} is used which implies a (relatively slow)
33 ## singular value decomposition. Other possible selections are
34 ## @code{@var{p} = 1, Inf, "fro"} which are generally faster. See
35 ## @code{norm} for a full discussion of possible @var{p} values.
36 ## @seealso{condest, rcond, norm, svd}
41 function retval = cond (A, p)
43 if (nargin && nargin < 3)
45 error ("cond: only valid on 2-D objects");
52 if (! ischar (p) && p == 2)
54 if (nr == 0 || nc == 0)
56 elseif (any (any (isinf (A) | isnan (A))))
57 error ("cond: argument must not contain Inf or NaN values");
62 if (sigma_1 == 0 || sigma_n == 0)
65 retval = sigma_1 / sigma_n;
69 retval = norm (A, p) * norm (inv (A), p);
78 %! y= [7, 2, 3; 1, 3, 4; 6, 4, 5];
80 %! type = {1, 2, 'fro', 'inf', inf};
81 %! for n = 1:numel(type)
82 %! rcondition(n) = 1 / cond (y, type{n});
84 %! assert (rcondition, [0.017460, 0.019597, 0.018714, 0.012022, 0.012022], tol);
86 %!assert (abs (cond ([1, 2; 2, 1]) - 3) < sqrt (eps));
88 %!assert (cond ([1, 2, 3; 4, 5, 6; 7, 8, 9]) > 1.0e+16);
92 %!error cond (1, 2, 3);