Mercurial > hg > octave-nkf
annotate scripts/linear-algebra/rank.m @ 10023:73fc43e01f4c
allow issquare on arbitrary data
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Fri, 25 Dec 2009 22:20:33 +0100 |
parents | 16f53d29049f |
children | 95c3e38098bf |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1993, 1994, 1995, 1996, 1997, 1999, 2000, 2004, 2005, |
9245 | 2 ## 2006, 2007, 2008, 2009 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. | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
23 ## The rank is taken to be the number of singular values of @var{a} that |
3372 | 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 | |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
45 if (isa (A, "single")) |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
46 tolerance = max (size (A)) * sigma (1) * eps ("single"); |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
47 else |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
48 tolerance = max (size (A)) * sigma (1) * eps; |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
49 endif |
4706 | 50 endif |
4 | 51 elseif (nargin == 2) |
262 | 52 sigma = svd (A); |
4 | 53 tolerance = tol; |
54 else | |
6046 | 55 print_usage (); |
4 | 56 endif |
4706 | 57 |
4 | 58 retval = sum (sigma > tolerance); |
59 | |
60 endfunction |