]> Creatis software - CreaPhase.git/blob - octave_packages/m/linear-algebra/null.m
update packages
[CreaPhase.git] / octave_packages / m / linear-algebra / null.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} {} null (@var{A})
21 ## @deftypefnx {Function File} {} null (@var{A}, @var{tol})
22 ## Return an orthonormal basis of the null space of @var{A}.
23 ##
24 ## The dimension of the null space is taken as the number of singular
25 ## values of @var{A} not greater than @var{tol}.  If the argument @var{tol}
26 ## is missing, it is computed as
27 ##
28 ## @example
29 ## max (size (@var{A})) * max (svd (@var{A})) * eps
30 ## @end example
31 ## @seealso{orth}
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 = null (A, tol)
39
40   if (isempty (A))
41     retval = [];
42   else
43     [U, S, V] = svd (A);
44
45     [rows, cols] = size (A);
46
47     [S_nr, S_nc] = size (S);
48
49     if (S_nr == 1 || S_nc == 1)
50       s = S(1);
51     else
52       s = diag (S);
53     endif
54
55     if (nargin == 1)
56       if (isa (A, "single"))
57         tol = max (size (A)) * s (1) * eps ("single");
58       else
59         tol = max (size (A)) * s (1) * eps;
60       endif
61     elseif (nargin != 2)
62       print_usage ();
63     endif
64
65     rank = sum (s > tol);
66
67     if (rank < cols)
68       retval = V (:, rank+1:cols);
69       if (isa (A, "single"))
70         retval(abs (retval) < eps ("single")) = 0;
71       else
72         retval(abs (retval) < eps) = 0;
73       endif
74     else
75       retval = zeros (cols, 0);
76     endif
77   endif
78
79 endfunction
80
81 %!test
82 %! A = 0;
83 %! assert(null(A), 1);
84
85 %!test
86 %! A = 1;
87 %! assert(null(A), zeros(1,0))
88
89 %!test
90 %! A = [1 0; 0 1];
91 %! assert(null(A), zeros(2,0));
92
93 %!test
94 %! A = [1 0; 1 0];
95 %! assert(null(A), [0 1]')
96
97 %!test
98 %! A = [1 1; 0 0];
99 %! assert(null(A), [-1/sqrt(2) 1/sqrt(2)]', eps)
100
101 %!test
102 %! tol = 1e-4;
103 %! A = [1 0; 0 tol-eps];
104 %! assert(null(A,tol), [0 1]')
105
106 %!test
107 %! tol = 1e-4;
108 %! A = [1 0; 0 tol+eps];
109 %! assert(null(A,tol), zeros(2,0));
110
111 %!error null()