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 |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
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 { |
5275
|
77 octave_idx_type info; |
5064
|
78 double rcond = 0.0; |
2928
|
79 |
3808
|
80 Matrix result = m.inverse (info, rcond, 1); |
2928
|
81 |
3808
|
82 if (nargout > 1) |
|
83 retval(1) = rcond; |
|
84 |
|
85 retval(0) = result; |
|
86 |
5064
|
87 volatile double xrcond = rcond; |
|
88 xrcond += 1.0; |
|
89 if (nargout < 2 && (info == -1 || xrcond == 1.0)) |
2928
|
90 warning ("inverse: matrix singular to machine precision,\ |
|
91 rcond = %g", rcond); |
|
92 } |
|
93 } |
|
94 else if (arg.is_complex_type ()) |
|
95 { |
|
96 ComplexMatrix m = arg.complex_matrix_value (); |
|
97 |
|
98 if (! error_state) |
|
99 { |
5275
|
100 octave_idx_type info; |
5064
|
101 double rcond = 0.0; |
2928
|
102 |
3808
|
103 ComplexMatrix result = m.inverse (info, rcond, 1); |
2928
|
104 |
3808
|
105 if (nargout > 1) |
|
106 retval(1) = rcond; |
|
107 |
|
108 retval(0) = result; |
|
109 |
5064
|
110 volatile double xrcond = rcond; |
|
111 xrcond += 1.0; |
|
112 if (nargout < 2 && (info == -1 || xrcond == 1.0)) |
2928
|
113 warning ("inverse: matrix singular to machine precision,\ |
|
114 rcond = %g", rcond); |
|
115 } |
|
116 } |
|
117 else |
|
118 { |
|
119 gripe_wrong_type_arg ("inv", arg); |
|
120 } |
|
121 |
|
122 return retval; |
|
123 } |
|
124 |
5775
|
125 // FIXME -- this should really be done with an alias, but |
2928
|
126 // alias_builtin() won't do the right thing if we are actually using |
|
127 // dynamic linking. |
|
128 |
|
129 DEFUN_DLD (inverse, args, nargout, |
3458
|
130 "-*- texinfo -*-\n\ |
|
131 @deftypefn {Loadable Function} {} inverse (@var{a})\n\ |
|
132 See inv.\n\ |
|
133 @end deftypefn") |
2928
|
134 { |
|
135 return Finv (args, nargout); |
|
136 } |
|
137 |
|
138 /* |
|
139 ;;; Local Variables: *** |
|
140 ;;; mode: C++ *** |
|
141 ;;; End: *** |
|
142 */ |