]> Creatis software - CreaPhase.git/blob - octave_packages/optim-1.2.0/private/__null_optim__.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / optim-1.2.0 / private / __null_optim__.m
1 ## Copyright (C) 1994-2011 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 ## Adapted-By: Olaf Till <olaf.till@uni-jena.de>
38
39 ## This function has also been submitted to Octave (bug #33503).
40
41 function retval = __null_optim__ (A, tol)
42
43   if (isempty (A))
44     retval = [];
45   else
46     [U, S, V] = svd (A);
47
48     [rows, cols] = size (A);
49
50     [S_nr, S_nc] = size (S);
51
52     if (S_nr == 1 || S_nc == 1)
53       s = S(1);
54     else
55       s = diag (S);
56     endif
57
58     if (nargin == 1)
59       if (isa (A, "single"))
60         tol = max (size (A)) * (vtol = s (1) * (meps = eps ("single")));
61       else
62         tol = max (size (A)) * (vtol = s (1) * (meps = eps));
63       endif
64     elseif (nargin != 2)
65       print_usage ();
66     endif
67
68     rank = sum (s > tol);
69
70     if (rank < cols)
71       retval = V (:, rank+1:cols);
72
73       if (rows >= cols)
74         cb = columns (retval);
75
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.
81
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__;
87         endif
88         ebnd = vtol ./ (__disna__ ("R", s, rows, cols)(rank+1:cols));
89
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
94
95         ## norms of vectors made by all elements up to this
96         sb = sqrt (cumsum (sb));
97
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
103         ## bound.
104         zidx = sb <= repmat (ebnd, cols, 1);
105
106         ## set indexed elements to zero in original basis
107         zidx = zidx(idx);
108         retval(zidx) = 0;
109
110       else
111         ## no error bounds computable with LAPACK
112
113         retval(abs (retval) < meps) = 0;
114       endif
115     else
116         retval = zeros (cols, 0);
117     endif
118   endif
119
120 endfunction