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 |
3808
|
33 DEFUN_DLD (inv, args, nargout, |
3548
|
34 "-*- texinfo -*-\n\ |
3808
|
35 @deftypefn {Loadable Function} {[@var{x}, @var{rcond}] = } inv (@var{a})\n\ |
|
36 @deftypefnx {Loadable Function} {[@var{x}, @var{rcond}] = } inverse (@var{a})\n\ |
|
37 Compute the inverse of the square matrix @var{a}. Return an estimate\n\ |
|
38 of the reciprocal condition number if requested, otherwise warn of an\n\ |
|
39 ill-conditioned matrix if the reciprocal condition number is small.\n\ |
3372
|
40 @end deftypefn") |
2928
|
41 { |
|
42 octave_value_list retval; |
|
43 |
|
44 int nargin = args.length (); |
|
45 |
|
46 if (nargin != 1) |
|
47 { |
|
48 print_usage ("inv"); |
|
49 return retval; |
|
50 } |
|
51 |
|
52 octave_value 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) |
4233
|
62 return octave_value (Matrix ()); |
2928
|
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; |
5064
|
77 double rcond = 0.0; |
2928
|
78 |
3808
|
79 Matrix result = m.inverse (info, rcond, 1); |
2928
|
80 |
3808
|
81 if (nargout > 1) |
|
82 retval(1) = rcond; |
|
83 |
|
84 retval(0) = result; |
|
85 |
5064
|
86 volatile double xrcond = rcond; |
|
87 xrcond += 1.0; |
|
88 if (nargout < 2 && (info == -1 || xrcond == 1.0)) |
2928
|
89 warning ("inverse: matrix singular to machine precision,\ |
|
90 rcond = %g", rcond); |
|
91 } |
|
92 } |
|
93 else if (arg.is_complex_type ()) |
|
94 { |
|
95 ComplexMatrix m = arg.complex_matrix_value (); |
|
96 |
|
97 if (! error_state) |
|
98 { |
|
99 int info; |
5064
|
100 double rcond = 0.0; |
2928
|
101 |
3808
|
102 ComplexMatrix result = m.inverse (info, rcond, 1); |
2928
|
103 |
3808
|
104 if (nargout > 1) |
|
105 retval(1) = rcond; |
|
106 |
|
107 retval(0) = result; |
|
108 |
5064
|
109 volatile double xrcond = rcond; |
|
110 xrcond += 1.0; |
|
111 if (nargout < 2 && (info == -1 || xrcond == 1.0)) |
2928
|
112 warning ("inverse: matrix singular to machine precision,\ |
|
113 rcond = %g", rcond); |
|
114 } |
|
115 } |
|
116 else |
|
117 { |
|
118 gripe_wrong_type_arg ("inv", arg); |
|
119 } |
|
120 |
|
121 return retval; |
|
122 } |
|
123 |
|
124 // XXX FIXME XXX -- this should really be done with an alias, but |
|
125 // alias_builtin() won't do the right thing if we are actually using |
|
126 // dynamic linking. |
|
127 |
|
128 DEFUN_DLD (inverse, args, nargout, |
3458
|
129 "-*- texinfo -*-\n\ |
|
130 @deftypefn {Loadable Function} {} inverse (@var{a})\n\ |
|
131 See inv.\n\ |
|
132 @end deftypefn") |
2928
|
133 { |
|
134 return Finv (args, nargout); |
|
135 } |
|
136 |
|
137 /* |
|
138 ;;; Local Variables: *** |
|
139 ;;; mode: C++ *** |
|
140 ;;; End: *** |
|
141 */ |