home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts.fat / control / pinv.m < prev    next >
Text File  |  1999-04-29  |  2KB  |  55 lines

  1. # Copyright (C) 1994 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 the
  7. # Free Software Foundation; either version 2, or (at your option) any
  8. # later version.
  9. #
  10. # Octave is distributed in the hope that it will be useful, but WITHOUT
  11. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13. # 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, write to the Free
  17. # Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19. function retval = pinv (X, tol)
  20.  
  21. # usage: pinv (X, tol)
  22. #
  23. # Returns the pseudoinverse of X; singular values less than tol are
  24. # ignored.
  25. #
  26. # If the second argument is omitted, it is assumed that
  27. #
  28. #   tol = max (size (X)) * sigma_max (X) * eps,
  29. #
  30. # where sigma_max(X) is the maximal singular value of X.
  31.  
  32. # Written by Kurt Hornik (hornik@neuro.tuwien.ac.at) March 1993.
  33. # Dept of Probability Theory and Statistics TU Wien, Austria.
  34.  
  35.   if (nargin < 1 || nargin > 2)
  36.     error ("usage: pinv (X [, tol])");
  37.   endif
  38.  
  39.   [U, S, V] = svd(X);
  40.   s = diag(S);
  41.  
  42.   if (nargin == 1)
  43.     tol = max (size (X)) * s (1) * eps;
  44.   endif
  45.  
  46.   r = sum (s > tol);
  47.   if (r == 0)
  48.     retval = zeros (X');
  49.   else
  50.     D = diag (ones (r, 1) ./ s (1:r));
  51.     retval = V (:, 1:r) * D * U (:, 1:r)';
  52.   endif
  53.  
  54. endfunction
  55.