comparison scripts/linear-algebra/cond.m @ 7484:e7485946272b

new norm arg for cond
author John W. Eaton <jwe@octave.org>
date Fri, 15 Feb 2008 16:35:59 -0500
parents 83a8781b529d
children a2d9f325b65a
comparison
equal deleted inserted replaced
7483:fb66330b2608 7484:e7485946272b
1 ## Copyright (C) 1993, 1994, 1995, 1996, 1997, 1999, 2000, 2003, 2004, 1 ## Copyright (C) 1993, 1994, 1995, 1996, 1997, 1999, 2000, 2003, 2004,
2 ## 2005, 2006, 2007 John W. Eaton 2 ## 2005, 2006, 2007, 2008 John W. Eaton
3 ## 3 ##
4 ## This file is part of Octave. 4 ## This file is part of Octave.
5 ## 5 ##
6 ## Octave is free software; you can redistribute it and/or modify it 6 ## Octave is free software; you can redistribute it and/or modify it
7 ## under the terms of the GNU General Public License as published by 7 ## under the terms of the GNU General Public License as published by
16 ## You should have received a copy of the GNU General Public License 16 ## You should have received a copy of the GNU General Public License
17 ## along with Octave; see the file COPYING. If not, see 17 ## along with Octave; see the file COPYING. If not, see
18 ## <http://www.gnu.org/licenses/>. 18 ## <http://www.gnu.org/licenses/>.
19 19
20 ## -*- texinfo -*- 20 ## -*- texinfo -*-
21 ## @deftypefn {Function File} {} cond (@var{a}) 21 ## @deftypefn {Function File} {} cond (@var{a},@var{p})
22 ## Compute the (two-norm) condition number of a matrix. @code{cond (a)} is 22 ## Compute the @var{p}-norm condition number of a matrix. @code{cond (@var{a})} is
23 ## defined as @code{norm (a) * norm (inv (a))}, and is computed via a 23 ## defined as @code{norm (@var{a}, @var{p}) * norm (inv (@var{a}), @var{p})}.
24 ## singular value decomposition. 24 ## By default @code{@var{p}=2} is used which implies a (relatively slow)
25 ## @seealso{norm, svd, rank} 25 ## singular value decomposition. Other possible selections are
26 ## @code{@var{p}= 1, Inf, inf, 'Inf', 'fro'} which are generally faster.
27 ## @seealso{norm, inv, det, svd, rank}
26 ## @end deftypefn 28 ## @end deftypefn
27 29
28 ## Author: jwe 30 ## Author: jwe
29 31
30 function retval = cond (a) 32 function retval = cond (a, p)
31 33
32 if (nargin == 1) 34 if (nargin && nargin < 3)
33 if (ndims (a) > 2) 35 if (ndims (a) > 2)
34 error ("cond: Only valid on 2-D objects") 36 error ("cond: only valid on 2-D objects")
35 endif 37 endif
36 38
37 [nr, nc] = size (a); 39 if (nargin <2)
38 if (nr == 0 || nc == 0) 40 p = 2;
39 retval = 0.0;
40 endif 41 endif
41 if (any (any (isinf (a) | isnan (a)))) 42
42 error ("cond: argument must not contain Inf or NaN values"); 43 if (! isstr (p) && p == 2)
44 [nr, nc] = size (a);
45 if (nr == 0 || nc == 0)
46 retval = 0.0;
47 elseif (any (any (isinf (a) | isnan (a))))
48 error ("cond: argument must not contain Inf or NaN values");
49 else
50 sigma = svd (a);
51 sigma_1 = sigma(1);
52 sigma_n = sigma(end);
53 if (sigma_1 == 0 || sigma_n == 0)
54 retval = Inf;
55 else
56 retval = sigma_1 / sigma_n;
57 endif
58 endif
43 else 59 else
44 sigma = svd (a); 60 retval = norm (a, p) * norm (inv (a), p);
45 sigma_1 = sigma(1);
46 sigma_n = sigma(length (sigma));
47 if (sigma_1 == 0 || sigma_n == 0)
48 retval = Inf;
49 else
50 retval = sigma_1 / sigma_n;
51 endif
52 endif 61 endif
53 else 62 else
54 print_usage (); 63 print_usage ();
55 endif 64 endif
56 65
57 endfunction 66 endfunction
58 67
59 %!assert(abs (cond ([1, 2; 2, 1]) - 3) < sqrt (eps)); 68 %!test
69 %! y= [7, 2, 3; 1, 3, 4; 6, 4, 5];
70 %! tol = 1e-6;
71 %! type = {1, 2, 'fro', 'inf', inf};
72 %! for n = 1:numel(type)
73 %! rcondition(n) = 1 / cond (y, type{n});
74 %! endfor
75 %! assert (rcondition, [0.017460, 0.019597, 0.018714, 0.012022, 0.012022], tol);
60 76
61 %!assert(cond ([1, 2, 3; 4, 5, 6; 7, 8, 9]) > 1.0e+16); 77 %!assert (abs (cond ([1, 2; 2, 1]) - 3) < sqrt (eps));
78
79 %!assert (cond ([1, 2, 3; 4, 5, 6; 7, 8, 9]) > 1.0e+16);
62 80
63 %!error cond (); 81 %!error cond ();
64 82
65 %!error cond (1, 2); 83 %!error cond (1, 2, 3);
66 84