home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / octave-1.1.1p1-base.tgz / octave-1.1.1p1-base.tar / fsf / octave / src / f-eig.cc < prev    next >
C/C++ Source or Header  |  1995-01-03  |  2KB  |  119 lines

  1. // f-eig.cc                                           -*- C++ -*-
  2. /*
  3.  
  4. Copyright (C) 1993, 1994, 1995 John W. Eaton
  5.  
  6. This file is part of Octave.
  7.  
  8. Octave is free software; you can redistribute it and/or modify it
  9. under the terms of the GNU General Public License as published by the
  10. Free Software Foundation; either version 2, or (at your option) any
  11. later version.
  12.  
  13. Octave is distributed in the hope that it will be useful, but WITHOUT
  14. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  16. for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with Octave; see the file COPYING.  If not, write to the Free
  20. Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22. */
  23.  
  24. #ifdef HAVE_CONFIG_H
  25. #include "config.h"
  26. #endif
  27.  
  28. #include "EIG.h"
  29.  
  30. #include "tree-const.h"
  31. #include "user-prefs.h"
  32. #include "gripes.h"
  33. #include "error.h"
  34. #include "utils.h"
  35. #include "help.h"
  36. #include "defun-dld.h"
  37.  
  38. DEFUN_DLD_BUILTIN ("eig", Feig, Seig, 2, 1,
  39.   "eig (X) or [V, D] = eig (X): compute eigenvalues and eigenvectors of X")
  40. {
  41.   Octave_object retval;
  42.  
  43.   int nargin = args.length ();
  44.  
  45.   if (nargin != 1 || nargout > 2)
  46.     {
  47.       print_usage ("eig");
  48.       return retval;
  49.     }
  50.  
  51.   tree_constant arg = args(0);
  52.  
  53.   int nr = arg.rows ();
  54.   int nc = arg.columns ();
  55.  
  56.   int arg_is_empty = empty_arg ("eig", nr, nc);
  57.   if (arg_is_empty < 0)
  58.     return retval;
  59.   else if (arg_is_empty > 0)
  60.     return Octave_object (2, Matrix ());
  61.  
  62.   if (nr != nc)
  63.     {
  64.       gripe_square_matrix_required ("eig");
  65.       return retval;
  66.     }
  67.  
  68.   Matrix tmp;
  69.   ComplexMatrix ctmp;
  70.   EIG result;
  71.  
  72.   if (arg.is_real_type ())
  73.     {
  74.       tmp = arg.matrix_value ();
  75.  
  76.       if (error_state)
  77.     return retval;
  78.       else
  79.     result = EIG (tmp);
  80.     }
  81.   else if (arg.is_complex_type ())
  82.     {
  83.       ctmp = arg.complex_matrix_value ();
  84.  
  85.       if (error_state)
  86.     return retval;
  87.       else
  88.     result = EIG (ctmp);
  89.     }
  90.   else
  91.     {
  92.       gripe_wrong_type_arg ("eig", tmp);
  93.       return retval;
  94.     }
  95.  
  96.   if (nargout == 0 || nargout == 1)
  97.     {
  98.       retval(0) = result.eigenvalues (), 1;
  99.     }
  100.   else
  101.     {
  102. // Blame it on Matlab.
  103.  
  104.       ComplexDiagMatrix d (result.eigenvalues ());
  105.  
  106.       retval(1) = d;
  107.       retval(0) = result.eigenvectors ();
  108.     }
  109.  
  110.   return retval;
  111. }
  112.  
  113. /*
  114. ;;; Local Variables: ***
  115. ;;; mode: C++ ***
  116. ;;; page-delimiter: "^/\\*" ***
  117. ;;; End: ***
  118. */
  119.