7017
|
1 ## Copyright (C) 1993, 1994, 1995, 1996, 1997, 1999, 2000, 2003, 2004, |
|
2 ## 2005, 2006, 2007 John W. Eaton |
2313
|
3 ## |
|
4 ## This file is part of Octave. |
|
5 ## |
|
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 |
7016
|
8 ## the Free Software Foundation; either version 3 of the License, or (at |
|
9 ## your option) any later version. |
2313
|
10 ## |
|
11 ## Octave is distributed in the hope that it will be useful, but |
|
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 ## General Public License for more details. |
|
15 ## |
|
16 ## You should have received a copy of the GNU General Public License |
7016
|
17 ## along with Octave; see the file COPYING. If not, see |
|
18 ## <http://www.gnu.org/licenses/>. |
245
|
19 |
3372
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} cond (@var{a}) |
|
22 ## Compute the (two-norm) condition number of a matrix. @code{cond (a)} is |
|
23 ## defined as @code{norm (a) * norm (inv (a))}, and is computed via a |
|
24 ## singular value decomposition. |
5642
|
25 ## @seealso{norm, svd, rank} |
3372
|
26 ## @end deftypefn |
4
|
27 |
2314
|
28 ## Author: jwe |
|
29 |
2311
|
30 function retval = cond (a) |
4
|
31 |
|
32 if (nargin == 1) |
4891
|
33 if (ndims (a) > 2) |
4890
|
34 error ("cond: Only valid on 2-D objects") |
|
35 endif |
|
36 |
4
|
37 [nr, nc] = size (a); |
4478
|
38 if (nr == 0 || nc == 0) |
4
|
39 retval = 0.0; |
|
40 endif |
3250
|
41 if (any (any (isinf (a) | isnan (a)))) |
|
42 error ("cond: argument must not contain Inf or NaN values"); |
|
43 else |
|
44 sigma = svd (a); |
|
45 sigma_1 = sigma(1); |
|
46 sigma_n = sigma(length (sigma)); |
|
47 if (sigma_1 == 0 || sigma_n == 0) |
3426
|
48 retval = Inf; |
3250
|
49 else |
3426
|
50 retval = sigma_1 / sigma_n; |
3250
|
51 endif |
|
52 endif |
4
|
53 else |
6046
|
54 print_usage (); |
4
|
55 endif |
|
56 |
|
57 endfunction |