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-inv.cc < prev    next >
C/C++ Source or Header  |  1995-01-03  |  3KB  |  130 lines

  1. // f-inv.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 "dMatrix.h"
  29. #include "CMatrix.h"
  30.  
  31. #include "tree-const.h"
  32. #include "user-prefs.h"
  33. #include "gripes.h"
  34. #include "error.h"
  35. #include "utils.h"
  36. #include "help.h"
  37. #include "defun-dld.h"
  38.  
  39. DEFUN_DLD_BUILTIN ("inv", Finv, Sinv, 2, 1,
  40.   "inv (X): inverse of a square matrix")
  41. {
  42.   Octave_object retval;
  43.  
  44.   int nargin = args.length ();
  45.  
  46.   if (nargin != 1)
  47.     {
  48.       print_usage ("inv");
  49.       return retval;
  50.     }
  51.  
  52.   tree_constant arg = args(0);
  53.  
  54.   int nr = arg.rows ();
  55.   int nc = arg.columns ();
  56.  
  57.   int arg_is_empty = empty_arg ("inverse", nr, nc);
  58.  
  59.   if (arg_is_empty < 0)
  60.     return retval;
  61.   else if (arg_is_empty > 0)
  62.     return Matrix ();
  63.  
  64.   if (nr != nc)
  65.     {
  66.       gripe_square_matrix_required ("inverse");
  67.       return retval;
  68.     }
  69.  
  70.   if (arg.is_real_type ())
  71.     {
  72.       Matrix m = arg.matrix_value ();
  73.  
  74.       if (! error_state)
  75.     {
  76.       int info;
  77.       double rcond = 0.0;
  78.  
  79.       Matrix minv = m.inverse (info, rcond);
  80.  
  81.       if (info == -1)
  82.         warning ("inverse: matrix singular to machine precision,\
  83.  rcond = %g", rcond);
  84.       else
  85.         retval = minv;
  86.     }
  87.     }
  88.   else if (arg.is_complex_type ())
  89.     {
  90.       ComplexMatrix m = arg.complex_matrix_value ();
  91.  
  92.       if (! error_state)
  93.     {
  94.       int info;
  95.       double rcond = 0.0;
  96.  
  97.       ComplexMatrix minv = m.inverse (info, rcond);
  98.  
  99.       if (info == -1)
  100.         warning ("inverse: matrix singular to machine precision,\
  101.  rcond = %g", rcond);
  102.       else
  103.         retval = minv;
  104.     }
  105.     }
  106.   else
  107.     {
  108.       gripe_wrong_type_arg ("inv", arg);
  109.     }
  110.  
  111.   return retval;
  112. }
  113.  
  114. // XXX FIXME XXX -- this should really be done with an alias, but
  115. // alias_builtin() won't do the right thing if we are actually using
  116. // dynamic linking.
  117.  
  118. DEFUN_DLD_BUILTIN ("inverse", Finverse, Sinverse, 2, 1,
  119.   "inverse (X): inverse of a square matrix")
  120. {
  121.   return Finv (args, nargout);
  122. }
  123.  
  124. /*
  125. ;;; Local Variables: ***
  126. ;;; mode: C++ ***
  127. ;;; page-delimiter: "^/\\*" ***
  128. ;;; End: ***
  129. */
  130.