]> Creatis software - CreaPhase.git/blob - octave_packages/m/linear-algebra/cond.m
update packages
[CreaPhase.git] / octave_packages / m / linear-algebra / cond.m
1 ## Copyright (C) 1993-2012 John W. Eaton
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} {} cond (@var{A})
21 ## @deftypefnx {Function File} {} cond (@var{A}, @var{p})
22 ## Compute the @var{p}-norm condition number of a matrix.
23 ##
24 ## @code{cond (@var{A})} is ## defined as
25 ## @tex
26 ## $ {\parallel A \parallel_p * \parallel A^{-1} \parallel_p .} $
27 ## @end tex
28 ## @ifnottex
29 ## @code{norm (@var{A}, @var{p}) * norm (inv (@var{A}), @var{p})}.
30 ## @end ifnottex
31 ##
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}
37 ## @end deftypefn
38
39 ## Author: jwe
40
41 function retval = cond (A, p)
42
43   if (nargin && nargin < 3)
44     if (ndims (A) > 2)
45       error ("cond: only valid on 2-D objects");
46     endif
47
48     if (nargin <2)
49       p = 2;
50     endif
51
52     if (! ischar (p) && p == 2)
53       [nr, nc] = size (A);
54       if (nr == 0 || nc == 0)
55         retval = 0.0;
56       elseif (any (any (isinf (A) | isnan (A))))
57         error ("cond: argument must not contain Inf or NaN values");
58       else
59         sigma   = svd (A);
60         sigma_1 = sigma(1);
61         sigma_n = sigma(end);
62         if (sigma_1 == 0 || sigma_n == 0)
63           retval = Inf;
64         else
65           retval = sigma_1 / sigma_n;
66         endif
67       endif
68     else
69       retval = norm (A, p) * norm (inv (A), p);
70     endif
71   else
72     print_usage ();
73   endif
74
75 endfunction
76
77 %!test
78 %! y= [7, 2, 3; 1, 3, 4; 6, 4, 5];
79 %! tol = 1e-6;
80 %! type = {1, 2, 'fro', 'inf', inf};
81 %! for n = 1:numel(type)
82 %!   rcondition(n) = 1 / cond (y, type{n});
83 %! endfor
84 %! assert (rcondition, [0.017460, 0.019597, 0.018714, 0.012022, 0.012022], tol);
85
86 %!assert (abs (cond ([1, 2; 2, 1]) - 3) < sqrt (eps));
87
88 %!assert (cond ([1, 2, 3; 4, 5, 6; 7, 8, 9]) > 1.0e+16);
89
90 %!error cond ();
91
92 %!error cond (1, 2, 3);
93