home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts / linear-algebra / pinv.m < prev    next >
Text File  |  1998-11-10  |  2KB  |  56 lines

  1. ## Copyright (C) 1996, 1997 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. ## usage: pinv (X, tol)
  20. ##
  21. ## Returns the pseudoinverse of X; singular values less than tol are
  22. ## ignored.
  23. ##
  24. ## If the second argument is omitted, it is assumed that
  25. ##
  26. ##   tol = max (size (X)) * sigma_max (X) * eps,
  27. ##
  28. ## where sigma_max(X) is the maximal singular value of X.
  29.  
  30. ## Author: Kurt Hornik (hornik@neuro.tuwien.ac.at>
  31. ## Created: March 1993.
  32. ## Adapted-By: jwe
  33.  
  34. function retval = pinv (X, tol)
  35.  
  36.   if (nargin < 1 || nargin > 2)
  37.     error ("usage: pinv (X [, tol])");
  38.   endif
  39.  
  40.   [U, S, V] = svd(X);
  41.   s = diag(S);
  42.  
  43.   if (nargin == 1)
  44.     tol = max (size (X)) * s (1) * eps;
  45.   endif
  46.  
  47.   r = sum (s > tol);
  48.   if (r == 0)
  49.     retval = zeros (X');
  50.   else
  51.     D = diag (ones (r, 1) ./ s (1:r));
  52.     retval = V (:, 1:r) * D * U (:, 1:r)';
  53.   endif
  54.  
  55. endfunction
  56.