7
|
1 // f-inv.cc -*- C++ -*- |
1
|
2 /* |
|
3 |
277
|
4 Copyright (C) 1993, 1994 John W. Eaton |
1
|
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 |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
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 |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
240
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include "config.h" |
1
|
26 #endif |
|
27 |
453
|
28 #include "dMatrix.h" |
|
29 #include "CMatrix.h" |
1
|
30 |
|
31 #include "tree-const.h" |
|
32 #include "user-prefs.h" |
|
33 #include "gripes.h" |
|
34 #include "error.h" |
544
|
35 #include "help.h" |
519
|
36 #include "defun-dld.h" |
1
|
37 |
519
|
38 DEFUN_DLD ("inv", Finv, Sinv, 2, 1, |
|
39 "inv (X): inverse of a square matrix") |
1
|
40 { |
519
|
41 Octave_object retval; |
|
42 |
|
43 int nargin = args.length (); |
1
|
44 |
519
|
45 if (nargin != 2) |
|
46 { |
|
47 print_usage ("inv"); |
|
48 return retval; |
|
49 } |
1
|
50 |
519
|
51 tree_constant tmp = args(1).make_numeric (); |
1
|
52 |
|
53 int nr = tmp.rows (); |
|
54 int nc = tmp.columns (); |
|
55 if (nr == 0 || nc == 0) |
|
56 { |
|
57 int flag = user_pref.propagate_empty_matrices; |
|
58 if (flag < 0) |
|
59 gripe_empty_arg ("inverse", 0); |
|
60 else if (flag == 0) |
|
61 gripe_empty_arg ("inverse", 1); |
|
62 } |
|
63 |
|
64 Matrix mtmp; |
|
65 if (nr == 0 && nc == 0) |
516
|
66 return mtmp; |
1
|
67 |
620
|
68 if (tmp.is_real_matrix ()) |
1
|
69 { |
620
|
70 Matrix m = tmp.matrix_value (); |
|
71 if (m.rows () == m.columns ()) |
|
72 { |
|
73 int info; |
|
74 double rcond = 0.0; |
|
75 Matrix minv = m.inverse (info, rcond); |
|
76 if (info == -1) |
|
77 warning ("inverse: matrix singular to machine precision,\ |
217
|
78 rcond = %g", rcond); |
620
|
79 else |
|
80 retval = minv; |
|
81 } |
|
82 else |
|
83 gripe_square_matrix_required ("inverse"); |
|
84 } |
|
85 else if (tmp.is_real_scalar ()) |
|
86 { |
|
87 double d = 1.0 / tmp.double_value (); |
|
88 retval = d; |
|
89 } |
|
90 else if (tmp.is_complex_matrix ()) |
|
91 { |
|
92 ComplexMatrix m = tmp.complex_matrix_value (); |
|
93 if (m.rows () == m.columns ()) |
|
94 { |
|
95 int info; |
|
96 double rcond = 0.0; |
|
97 ComplexMatrix minv = m.inverse (info, rcond); |
|
98 if (info == -1) |
|
99 warning ("inverse: matrix singular to machine precision,\ |
217
|
100 rcond = %g", rcond); |
620
|
101 else |
|
102 retval = minv; |
|
103 } |
|
104 else |
|
105 gripe_square_matrix_required ("inverse"); |
1
|
106 } |
620
|
107 else if (tmp.is_complex_scalar ()) |
|
108 { |
|
109 Complex c = 1.0 / tmp.complex_value (); |
|
110 retval = c; |
|
111 } |
|
112 else |
|
113 { |
|
114 gripe_wrong_type_arg ("inv", tmp); |
|
115 } |
|
116 |
1
|
117 return retval; |
|
118 } |
|
119 |
|
120 /* |
|
121 ;;; Local Variables: *** |
|
122 ;;; mode: C++ *** |
|
123 ;;; page-delimiter: "^/\\*" *** |
|
124 ;;; End: *** |
|
125 */ |