1 ## Copyright (C) 1993-2012 John W. Eaton
3 ## This file is part of Octave.
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.
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.
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/>.
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
28 ## tol = max (size (@var{A})) * sigma(1) * eps;
32 ## where @code{eps} is machine precision and @code{sigma(1)} is the largest
33 ## singular value of @var{A}.
38 function retval = rank (A, tol)
45 if (isa (A, "single"))
46 tolerance = max (size (A)) * sigma (1) * eps ("single");
48 tolerance = max (size (A)) * sigma (1) * eps;
58 retval = sum (sigma > tolerance);
63 %! A = [1 2 3 4 5 6 7;
73 %! A = [1 2 3 4 5 6 7;
75 %! 1 2 3.0000001 4 5 6 7;
76 %! 4 5 6 7 8 9 12.00001;
83 %! A = [1 2 3 4 5 6 7;
86 %! 4 5 6 7 8 9 12.00001;
93 %! A = [1 2 3 4 5 6 7;
100 %! assert(rank(A),3);
104 %! assert(rank(A),100);
107 %! A = [1, 2, 3; 1, 2.001, 3; 1, 2, 3.0000001];
109 %! assert(rank(A,0.0009),1)
110 %! assert(rank(A,0.0006),2)
111 %! assert(rank(A,0.00000002),3)