Mercurial > hg > octave-nkf
annotate scripts/linear-algebra/cond.m @ 11939:a24565131108 release-3-0-x
fix invalid matrix dimensioning in graphics.cc
author | Marco Caliari <marco.caliari@univr.it> |
---|---|
date | Wed, 25 Feb 2009 08:40:31 +0100 |
parents | c5d9aaeb306a |
children |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1993, 1994, 1995, 1996, 1997, 1999, 2000, 2003, 2004, |
11649 | 2 ## 2005, 2006, 2007, 2008 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 -*- |
11649 | 21 ## @deftypefn {Function File} {} cond (@var{a},@var{p}) |
22 ## Compute the @var{p}-norm condition number of a matrix. @code{cond (@var{a})} is | |
23 ## defined as @code{norm (@var{a}, @var{p}) * norm (inv (@var{a}), @var{p})}. | |
24 ## By default @code{@var{p}=2} is used which implies a (relatively slow) | |
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} | |
3372 | 28 ## @end deftypefn |
4 | 29 |
2314 | 30 ## Author: jwe |
31 | |
11649 | 32 function retval = cond (a, p) |
4 | 33 |
11649 | 34 if (nargin && nargin < 3) |
4891 | 35 if (ndims (a) > 2) |
11649 | 36 error ("cond: only valid on 2-D objects") |
37 endif | |
38 | |
39 if (nargin <2) | |
40 p = 2; | |
4890 | 41 endif |
42 | |
11777
c5d9aaeb306a
use ischar instead of isstr
John W. Eaton <jwe@octave.org>
parents:
11649
diff
changeset
|
43 if (! ischar (p) && p == 2) |
11649 | 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 | |
3250 | 59 else |
11649 | 60 retval = norm (a, p) * norm (inv (a), p); |
3250 | 61 endif |
4 | 62 else |
6046 | 63 print_usage (); |
4 | 64 endif |
65 | |
66 endfunction | |
11649 | 67 |
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); | |
76 | |
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); | |
80 | |
81 %!error cond (); | |
82 | |
83 %!error cond (1, 2, 3); |