1
|
1 // tc-inv.cc -*- C++ -*- |
|
2 /* |
|
3 |
|
4 Copyright (C) 1993 John W. Eaton |
|
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 |
|
24 #ifdef __GNUG__ |
|
25 #pragma implementation |
|
26 #endif |
|
27 |
|
28 #include "Matrix.h" |
|
29 |
|
30 #include "tree-const.h" |
|
31 #include "user-prefs.h" |
|
32 #include "gripes.h" |
|
33 #include "error.h" |
|
34 |
|
35 #ifdef WITH_DLD |
|
36 tree_constant * |
|
37 builtin_inv (tree_constant *args, int nargin, int nargout) |
|
38 { |
|
39 tree_constant *retval = new tree_constant [2]; |
|
40 retval[0] = inverse (args[1]); |
|
41 return retval; |
|
42 } |
|
43 #endif |
|
44 |
|
45 tree_constant |
|
46 inverse (tree_constant& a) |
|
47 { |
|
48 tree_constant retval; |
|
49 |
|
50 tree_constant tmp = a.make_numeric (); |
|
51 |
|
52 int nr = tmp.rows (); |
|
53 int nc = tmp.columns (); |
|
54 if (nr == 0 || nc == 0) |
|
55 { |
|
56 int flag = user_pref.propagate_empty_matrices; |
|
57 if (flag < 0) |
|
58 gripe_empty_arg ("inverse", 0); |
|
59 else if (flag == 0) |
|
60 gripe_empty_arg ("inverse", 1); |
|
61 } |
|
62 |
|
63 Matrix mtmp; |
|
64 if (nr == 0 && nc == 0) |
|
65 return tree_constant (mtmp); |
|
66 |
|
67 switch (tmp.const_type ()) |
|
68 { |
|
69 case tree_constant_rep::matrix_constant: |
|
70 { |
|
71 Matrix m = tmp.matrix_value (); |
|
72 if (m.rows () == m.columns ()) |
|
73 { |
|
74 int info; |
|
75 double rcond = 0.0; |
|
76 Matrix minv = m.inverse (info, rcond); |
|
77 if (info == -1) |
|
78 message ("inverse", |
|
79 "matrix singular to machine precision, rcond = %g", |
|
80 rcond); |
|
81 else |
|
82 retval = tree_constant (minv); |
|
83 } |
|
84 else |
|
85 gripe_square_matrix_required ("inverse"); |
|
86 } |
|
87 break; |
|
88 case tree_constant_rep::scalar_constant: |
|
89 { |
|
90 double d = 1.0 / tmp.double_value (); |
|
91 retval = tree_constant (d); |
|
92 } |
|
93 break; |
|
94 case tree_constant_rep::complex_matrix_constant: |
|
95 { |
|
96 ComplexMatrix m = tmp.complex_matrix_value (); |
|
97 if (m.rows () == m.columns ()) |
|
98 { |
|
99 int info; |
|
100 double rcond = 0.0; |
|
101 ComplexMatrix minv = m.inverse (info, rcond); |
|
102 if (info == -1) |
|
103 message ("inverse", |
|
104 "matrix singular to machine precision, rcond = %g", |
|
105 rcond); |
|
106 else |
|
107 retval = tree_constant (minv); |
|
108 } |
|
109 else |
|
110 gripe_square_matrix_required ("inverse"); |
|
111 } |
|
112 break; |
|
113 case tree_constant_rep::complex_scalar_constant: |
|
114 { |
|
115 Complex c = 1.0 / tmp.complex_value (); |
|
116 retval = tree_constant (c); |
|
117 } |
|
118 break; |
|
119 default: |
|
120 break; |
|
121 } |
|
122 return retval; |
|
123 } |
|
124 |
|
125 /* |
|
126 ;;; Local Variables: *** |
|
127 ;;; mode: C++ *** |
|
128 ;;; page-delimiter: "^/\\*" *** |
|
129 ;;; End: *** |
|
130 */ |