2928
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include "defun-dld.h" |
|
28 #include "error.h" |
|
29 #include "gripes.h" |
|
30 #include "oct-obj.h" |
|
31 #include "utils.h" |
|
32 |
|
33 DEFUN_DLD (inv, args, , |
3548
|
34 "-*- texinfo -*-\n\ |
3372
|
35 @deftypefn {Loadable Function} {} inv (@var{a})\n\ |
|
36 @deftypefnx {Loadable Function} {} inverse (@var{a})\n\ |
|
37 Compute the inverse of the square matrix @var{a}.\n\ |
|
38 @end deftypefn") |
2928
|
39 { |
|
40 octave_value_list retval; |
|
41 |
|
42 int nargin = args.length (); |
|
43 |
|
44 if (nargin != 1) |
|
45 { |
|
46 print_usage ("inv"); |
|
47 return retval; |
|
48 } |
|
49 |
|
50 octave_value arg = args(0); |
|
51 |
|
52 int nr = arg.rows (); |
|
53 int nc = arg.columns (); |
|
54 |
|
55 int arg_is_empty = empty_arg ("inverse", nr, nc); |
|
56 |
|
57 if (arg_is_empty < 0) |
|
58 return retval; |
|
59 else if (arg_is_empty > 0) |
|
60 return Matrix (); |
|
61 |
|
62 if (nr != nc) |
|
63 { |
|
64 gripe_square_matrix_required ("inverse"); |
|
65 return retval; |
|
66 } |
|
67 |
|
68 if (arg.is_real_type ()) |
|
69 { |
|
70 Matrix m = arg.matrix_value (); |
|
71 |
|
72 if (! error_state) |
|
73 { |
|
74 int info; |
|
75 double rcond = 0.0; |
|
76 |
|
77 retval = m.inverse (info, rcond, 1); |
|
78 |
|
79 if (info == -1) |
|
80 warning ("inverse: matrix singular to machine precision,\ |
|
81 rcond = %g", rcond); |
|
82 } |
|
83 } |
|
84 else if (arg.is_complex_type ()) |
|
85 { |
|
86 ComplexMatrix m = arg.complex_matrix_value (); |
|
87 |
|
88 if (! error_state) |
|
89 { |
|
90 int info; |
|
91 double rcond = 0.0; |
|
92 |
|
93 retval = m.inverse (info, rcond, 1); |
|
94 |
|
95 if (info == -1) |
|
96 warning ("inverse: matrix singular to machine precision,\ |
|
97 rcond = %g", rcond); |
|
98 } |
|
99 } |
|
100 else |
|
101 { |
|
102 gripe_wrong_type_arg ("inv", arg); |
|
103 } |
|
104 |
|
105 return retval; |
|
106 } |
|
107 |
|
108 // XXX FIXME XXX -- this should really be done with an alias, but |
|
109 // alias_builtin() won't do the right thing if we are actually using |
|
110 // dynamic linking. |
|
111 |
|
112 DEFUN_DLD (inverse, args, nargout, |
3458
|
113 "-*- texinfo -*-\n\ |
|
114 @deftypefn {Loadable Function} {} inverse (@var{a})\n\ |
|
115 See inv.\n\ |
|
116 @end deftypefn") |
2928
|
117 { |
|
118 return Finv (args, nargout); |
|
119 } |
|
120 |
|
121 /* |
|
122 ;;; Local Variables: *** |
|
123 ;;; mode: C++ *** |
|
124 ;;; End: *** |
|
125 */ |