7017
|
1 ## Copyright (C) 1993, 1994, 1995, 1996, 1997, 1999, 2000, 2004, 2005, |
|
2 ## 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} {} 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 |
3426
|
26 ## |
3372
|
27 ## @example |
3600
|
28 ## tol = max (size (@var{a})) * sigma(1) * eps; |
3372
|
29 ## @end example |
3426
|
30 ## |
3372
|
31 ## @noindent |
3600
|
32 ## where @code{eps} is machine precision and @code{sigma(1)} is the largest |
3372
|
33 ## singular value of @var{a}. |
|
34 ## @end deftypefn |
4
|
35 |
2314
|
36 ## Author: jwe |
|
37 |
2311
|
38 function retval = rank (A, tol) |
4
|
39 |
|
40 if (nargin == 1) |
|
41 sigma = svd (A); |
4706
|
42 if (isempty (sigma)) |
|
43 tolerance = 0; |
|
44 else |
|
45 tolerance = max (size (A)) * sigma (1) * eps; |
|
46 endif |
4
|
47 elseif (nargin == 2) |
262
|
48 sigma = svd (A); |
4
|
49 tolerance = tol; |
|
50 else |
6046
|
51 print_usage (); |
4
|
52 endif |
4706
|
53 |
4
|
54 retval = sum (sigma > tolerance); |
|
55 |
|
56 endfunction |