]> Creatis software - CreaPhase.git/blob - octave_packages/m/linear-algebra/rank.m
update packages
[CreaPhase.git] / octave_packages / m / linear-algebra / rank.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} {} rank (@var{A})
21 ## @deftypefnx {Function File} {} rank (@var{A}, @var{tol})
22 ## Compute the rank of @var{A}, using the singular value decomposition.
23 ## The rank is taken to be the number of singular values of @var{A} that
24 ## are greater than the specified tolerance @var{tol}.  If the second
25 ## argument is omitted, it is taken to be
26 ##
27 ## @example
28 ## tol = max (size (@var{A})) * sigma(1) * eps;
29 ## @end example
30 ##
31 ## @noindent
32 ## where @code{eps} is machine precision and @code{sigma(1)} is the largest
33 ## singular value of @var{A}.
34 ## @end deftypefn
35
36 ## Author: jwe
37
38 function retval = rank (A, tol)
39
40   if (nargin == 1)
41     sigma = svd (A);
42     if (isempty (sigma))
43       tolerance = 0;
44     else
45       if (isa (A, "single"))
46         tolerance = max (size (A)) * sigma (1) * eps ("single");
47       else
48         tolerance = max (size (A)) * sigma (1) * eps;
49       endif
50     endif
51   elseif (nargin == 2)
52     sigma = svd (A);
53     tolerance = tol;
54   else
55     print_usage ();
56   endif
57
58   retval = sum (sigma > tolerance);
59
60 endfunction
61
62 %!test
63 %! A = [1 2 3 4 5 6 7;
64 %!      4 5 6 7 8 9 12;
65 %!      1 2 3.1 4 5 6 7;
66 %!      2 3 4 5 6 7 8;
67 %!      3 4 5 6 7 8 9;
68 %!      4 5 6 7 8 9 10;
69 %!      5 6 7 8 9 10 11];
70 %! assert(rank(A),4);
71
72 %!test
73 %! A = [1 2 3 4 5 6 7;
74 %!      4 5 6 7 8 9 12;
75 %!      1 2 3.0000001 4 5 6 7;
76 %!      4 5 6 7 8 9 12.00001;
77 %!      3 4 5 6 7 8 9;
78 %!      4 5 6 7 8 9 10;
79 %!      5 6 7 8 9 10 11];
80 %! assert(rank(A),4);
81
82 %!test
83 %! A = [1 2 3 4 5 6 7;
84 %!      4 5 6 7 8 9 12;
85 %!      1 2 3 4 5 6 7;
86 %!      4 5 6 7 8 9 12.00001;
87 %!      3 4 5 6 7 8 9;
88 %!      4 5 6 7 8 9 10;
89 %!      5 6 7 8 9 10 11];
90 %! assert(rank(A),3);
91
92 %!test
93 %! A = [1 2 3 4 5 6 7;
94 %!      4 5 6 7 8 9 12;
95 %!      1 2 3 4 5 6 7;
96 %!      4 5 6 7 8 9 12;
97 %!      3 4 5 6 7 8 9;
98 %!      4 5 6 7 8 9 10;
99 %!      5 6 7 8 9 10 11];
100 %! assert(rank(A),3);
101
102 %!test
103 %! A = eye(100);
104 %! assert(rank(A),100);
105
106 %!test
107 %! A = [1, 2, 3; 1, 2.001, 3; 1, 2, 3.0000001];
108 %! assert(rank(A),3)
109 %! assert(rank(A,0.0009),1)
110 %! assert(rank(A,0.0006),2)
111 %! assert(rank(A,0.00000002),3)