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, , |
|
34 "inv (X): inverse of a square matrix") |
|
35 { |
|
36 octave_value_list retval; |
|
37 |
|
38 int nargin = args.length (); |
|
39 |
|
40 if (nargin != 1) |
|
41 { |
|
42 print_usage ("inv"); |
|
43 return retval; |
|
44 } |
|
45 |
|
46 octave_value arg = args(0); |
|
47 |
|
48 int nr = arg.rows (); |
|
49 int nc = arg.columns (); |
|
50 |
|
51 int arg_is_empty = empty_arg ("inverse", nr, nc); |
|
52 |
|
53 if (arg_is_empty < 0) |
|
54 return retval; |
|
55 else if (arg_is_empty > 0) |
|
56 return Matrix (); |
|
57 |
|
58 if (nr != nc) |
|
59 { |
|
60 gripe_square_matrix_required ("inverse"); |
|
61 return retval; |
|
62 } |
|
63 |
|
64 if (arg.is_real_type ()) |
|
65 { |
|
66 Matrix m = arg.matrix_value (); |
|
67 |
|
68 if (! error_state) |
|
69 { |
|
70 int info; |
|
71 double rcond = 0.0; |
|
72 |
|
73 retval = m.inverse (info, rcond, 1); |
|
74 |
|
75 if (info == -1) |
|
76 warning ("inverse: matrix singular to machine precision,\ |
|
77 rcond = %g", rcond); |
|
78 } |
|
79 } |
|
80 else if (arg.is_complex_type ()) |
|
81 { |
|
82 ComplexMatrix m = arg.complex_matrix_value (); |
|
83 |
|
84 if (! error_state) |
|
85 { |
|
86 int info; |
|
87 double rcond = 0.0; |
|
88 |
|
89 retval = m.inverse (info, rcond, 1); |
|
90 |
|
91 if (info == -1) |
|
92 warning ("inverse: matrix singular to machine precision,\ |
|
93 rcond = %g", rcond); |
|
94 } |
|
95 } |
|
96 else |
|
97 { |
|
98 gripe_wrong_type_arg ("inv", arg); |
|
99 } |
|
100 |
|
101 return retval; |
|
102 } |
|
103 |
|
104 // XXX FIXME XXX -- this should really be done with an alias, but |
|
105 // alias_builtin() won't do the right thing if we are actually using |
|
106 // dynamic linking. |
|
107 |
|
108 DEFUN_DLD (inverse, args, nargout, |
|
109 "inverse (X): inverse of a square matrix") |
|
110 { |
|
111 return Finv (args, nargout); |
|
112 } |
|
113 |
|
114 /* |
|
115 ;;; Local Variables: *** |
|
116 ;;; mode: C++ *** |
|
117 ;;; End: *** |
|
118 */ |