2928
|
1 /* |
|
2 |
7017
|
3 Copyright (C) 1996, 1997, 1999, 2000, 2001, 2002, 2004, 2005, 2006, |
|
4 2007 John W. Eaton |
2928
|
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 |
7016
|
10 Free Software Foundation; either version 3 of the License, or (at your |
|
11 option) any later version. |
2928
|
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 |
7016
|
19 along with Octave; see the file COPYING. If not, see |
|
20 <http://www.gnu.org/licenses/>. |
2928
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
|
28 #include "defun-dld.h" |
|
29 #include "error.h" |
|
30 #include "gripes.h" |
|
31 #include "oct-obj.h" |
|
32 #include "utils.h" |
|
33 |
3808
|
34 DEFUN_DLD (inv, args, nargout, |
3548
|
35 "-*- texinfo -*-\n\ |
3808
|
36 @deftypefn {Loadable Function} {[@var{x}, @var{rcond}] = } inv (@var{a})\n\ |
|
37 @deftypefnx {Loadable Function} {[@var{x}, @var{rcond}] = } inverse (@var{a})\n\ |
|
38 Compute the inverse of the square matrix @var{a}. Return an estimate\n\ |
|
39 of the reciprocal condition number if requested, otherwise warn of an\n\ |
|
40 ill-conditioned matrix if the reciprocal condition number is small.\n\ |
3372
|
41 @end deftypefn") |
2928
|
42 { |
|
43 octave_value_list retval; |
|
44 |
|
45 int nargin = args.length (); |
|
46 |
|
47 if (nargin != 1) |
|
48 { |
5823
|
49 print_usage (); |
2928
|
50 return retval; |
|
51 } |
|
52 |
|
53 octave_value arg = args(0); |
|
54 |
5275
|
55 octave_idx_type nr = arg.rows (); |
|
56 octave_idx_type nc = arg.columns (); |
2928
|
57 |
|
58 int arg_is_empty = empty_arg ("inverse", nr, nc); |
|
59 |
|
60 if (arg_is_empty < 0) |
|
61 return retval; |
|
62 else if (arg_is_empty > 0) |
4233
|
63 return octave_value (Matrix ()); |
2928
|
64 |
|
65 if (nr != nc) |
|
66 { |
|
67 gripe_square_matrix_required ("inverse"); |
|
68 return retval; |
|
69 } |
|
70 |
|
71 if (arg.is_real_type ()) |
|
72 { |
|
73 Matrix m = arg.matrix_value (); |
|
74 |
|
75 if (! error_state) |
|
76 { |
6207
|
77 MatrixType mattyp = args(0).matrix_type (); |
|
78 |
5275
|
79 octave_idx_type info; |
5064
|
80 double rcond = 0.0; |
2928
|
81 |
6207
|
82 Matrix result = m.inverse (mattyp, info, rcond, 1); |
|
83 |
|
84 args(0).matrix_type (mattyp); |
2928
|
85 |
3808
|
86 if (nargout > 1) |
|
87 retval(1) = rcond; |
|
88 |
|
89 retval(0) = result; |
|
90 |
5064
|
91 volatile double xrcond = rcond; |
|
92 xrcond += 1.0; |
|
93 if (nargout < 2 && (info == -1 || xrcond == 1.0)) |
2928
|
94 warning ("inverse: matrix singular to machine precision,\ |
|
95 rcond = %g", rcond); |
|
96 } |
|
97 } |
|
98 else if (arg.is_complex_type ()) |
|
99 { |
|
100 ComplexMatrix m = arg.complex_matrix_value (); |
|
101 |
|
102 if (! error_state) |
|
103 { |
6207
|
104 MatrixType mattyp = args(0).matrix_type (); |
|
105 |
5275
|
106 octave_idx_type info; |
5064
|
107 double rcond = 0.0; |
2928
|
108 |
6207
|
109 ComplexMatrix result = m.inverse (mattyp, info, rcond, 1); |
|
110 |
|
111 args(0).matrix_type (mattyp); |
2928
|
112 |
3808
|
113 if (nargout > 1) |
|
114 retval(1) = rcond; |
|
115 |
|
116 retval(0) = result; |
|
117 |
5064
|
118 volatile double xrcond = rcond; |
|
119 xrcond += 1.0; |
|
120 if (nargout < 2 && (info == -1 || xrcond == 1.0)) |
2928
|
121 warning ("inverse: matrix singular to machine precision,\ |
|
122 rcond = %g", rcond); |
|
123 } |
|
124 } |
|
125 else |
|
126 { |
|
127 gripe_wrong_type_arg ("inv", arg); |
|
128 } |
|
129 |
|
130 return retval; |
|
131 } |
|
132 |
5775
|
133 // FIXME -- this should really be done with an alias, but |
2928
|
134 // alias_builtin() won't do the right thing if we are actually using |
|
135 // dynamic linking. |
|
136 |
|
137 DEFUN_DLD (inverse, args, nargout, |
3458
|
138 "-*- texinfo -*-\n\ |
|
139 @deftypefn {Loadable Function} {} inverse (@var{a})\n\ |
|
140 See inv.\n\ |
|
141 @end deftypefn") |
2928
|
142 { |
|
143 return Finv (args, nargout); |
|
144 } |
|
145 |
|
146 /* |
|
147 ;;; Local Variables: *** |
|
148 ;;; mode: C++ *** |
|
149 ;;; End: *** |
|
150 */ |