]> Creatis software - CreaPhase.git/blob - octave_packages/m/linear-algebra/orth.m
update packages
[CreaPhase.git] / octave_packages / m / linear-algebra / orth.m
1 ## Copyright (C) 1994-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} {} orth (@var{A})
21 ## @deftypefnx {Function File} {} orth (@var{A}, @var{tol})
22 ## Return an orthonormal basis of the range space of @var{A}.
23 ##
24 ## The dimension of the range space is taken as the number of singular
25 ## values of @var{A} greater than @var{tol}.  If the argument @var{tol} is
26 ## missing, it is computed as
27 ##
28 ## @example
29 ## max (size (@var{A})) * max (svd (@var{A})) * eps
30 ## @end example
31 ## @seealso{null}
32 ## @end deftypefn
33
34 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
35 ## Created: 24 December 1993.
36 ## Adapted-By: jwe
37
38 function retval = orth (A, tol)
39
40   if (nargin == 1 || nargin == 2)
41
42     if (isempty (A))
43       retval = [];
44       return;
45     endif
46
47     [U, S, V] = svd (A);
48
49     [rows, cols] = size (A);
50
51     [S_nr, S_nc] = size (S);
52
53     if (S_nr == 1 || S_nc == 1)
54       s = S(1);
55     else
56       s = diag (S);
57     endif
58
59     if (nargin == 1)
60       if (isa (A, "single"))
61         tol = max (size (A)) * s (1) * eps ("single");
62       else
63         tol = max (size (A)) * s (1) * eps;
64       endif
65     endif
66
67     rank = sum (s > tol);
68
69     if (rank > 0)
70       retval = -U (:, 1:rank);
71     else
72       retval = zeros (rows, 0);
73     endif
74
75   else
76
77     print_usage ();
78
79   endif
80
81 endfunction
82
83 %!test
84 %! for ii=1:20
85 %!   A = rand (10, 10);
86 %!   V = orth (A);
87 %!   if (det (A) != 0)
88 %!     assert (V'*V, eye (10), 100*eps)
89 %!   endif
90 %! endfor