1 ## Copyright (C) 1994-2011 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} {} null (@var{A})
21 ## @deftypefnx {Function File} {} null (@var{A}, @var{tol})
22 ## Return an orthonormal basis of the null space of @var{A}.
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
29 ## max (size (@var{A})) * max (svd (@var{A})) * eps
34 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
35 ## Created: 24 December 1993.
37 ## Adapted-By: Olaf Till <olaf.till@uni-jena.de>
39 ## This function has also been submitted to Octave (bug #33503).
41 function retval = __null_optim__ (A, tol)
48 [rows, cols] = size (A);
50 [S_nr, S_nc] = size (S);
52 if (S_nr == 1 || S_nc == 1)
59 if (isa (A, "single"))
60 tol = max (size (A)) * (vtol = s (1) * (meps = eps ("single")));
62 tol = max (size (A)) * (vtol = s (1) * (meps = eps));
71 retval = V (:, rank+1:cols);
74 cb = columns (retval);
76 ## Set those elements of each vector to zero whose absolute
77 ## values are smallest and which together could be zero without
78 ## making the angle to the originally computed vector larger
79 ## than given by the error bound. Do this in an approximative
80 ## but numerically feasible way.
82 ## error bounds of basis vectors in radians, see LAPACK user
83 ## guide, http://www.netlib.org/lapack/lug/node96.html
84 if (true) # test for Octave version once submitted patch is applied
85 # to Octave (bug #33503)
86 __disna__ = @ __disna_optim__;
88 ebnd = vtol ./ (__disna__ ("R", s, rows, cols)(rank+1:cols));
90 ## sort elements by magnitude
91 sb = conj (retval) .* retval;
92 [sb, idx] = sort (sb);
93 idx += repmat (0:cols:cols*(cb-1), cols, 1); # for un-sorting
95 ## norms of vectors made by all elements up to this
96 sb = sqrt (cumsum (sb));
98 ## The norm of the vectors made up by elements settable to zero
99 ## is small enough to be approximately equal to the angle
100 ## between the full vectors before and after setting these
101 ## elements to zero (considering the norms of the full vectors
102 ## being 1). Index of approximated angles not exceeding error
104 zidx = sb <= repmat (ebnd, cols, 1);
106 ## set indexed elements to zero in original basis
111 ## no error bounds computable with LAPACK
113 retval(abs (retval) < meps) = 0;
116 retval = zeros (cols, 0);