Mercurial > matrix-functions
comparison toolbox/cond.m @ 2:c124219d7bfa draft
Re-add the 1995 toolbox after noticing the statement in the ~higham/mctoolbox/ webpage.
author | Antonio Pino Robles <data.script93@gmail.com> |
---|---|
date | Thu, 07 May 2015 18:36:24 +0200 |
parents | 8f23314345f4 |
children |
comparison
equal
deleted
inserted
replaced
1:e471a92d17be | 2:c124219d7bfa |
---|---|
1 function y = cond(A, p) | |
2 %COND Matrix condition number in 1, 2, Frobenius, or infinity norm. | |
3 % For p = 1, 2, 'fro', inf, COND(A,p) = NORM(A,p) * NORM(INV(A),p). | |
4 % If p is omitted then p = 2 is used. | |
5 % A may be a rectangular matrix if p = 2; in this case COND(A) | |
6 % is the ratio of the largest singular value of A to the smallest | |
7 % (and hence is infinite if A is rank deficient). | |
8 | |
9 % See also RCOND, NORM, CONDEST, NORMEST. | |
10 % Generalises and incorporates MATFUN/COND.M from Matlab 4. | |
11 | |
12 if length(A) == 0 % Handle null matrix. | |
13 y = NaN; | |
14 return | |
15 end | |
16 if issparse(A) | |
17 error('Matrix must be non-sparse.') | |
18 end | |
19 | |
20 if nargin == 1, p = 2; end | |
21 | |
22 [m, n] = size(A); | |
23 if m ~= n & p ~= 2 | |
24 error('A is rectangular. Use the 2 norm.') | |
25 end | |
26 | |
27 if p == 2 | |
28 s = svd(A); | |
29 if any(s == 0) % Handle singular matrix | |
30 disp('Condition is infinite') | |
31 y = Inf; | |
32 return | |
33 end | |
34 y = max(s)./min(s); | |
35 else | |
36 % We'll let NORM pick up any invalid p argument. | |
37 y = norm(A, p) * norm(inv(A), p); | |
38 end |