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" |
7
|
35 #include "f-inv.h" |
1
|
36 |
|
37 #ifdef WITH_DLD |
|
38 tree_constant * |
277
|
39 builtin_inv_2 (const tree_constant *args, int nargin, int nargout) |
1
|
40 { |
|
41 tree_constant *retval = new tree_constant [2]; |
|
42 retval[0] = inverse (args[1]); |
|
43 return retval; |
|
44 } |
|
45 #endif |
|
46 |
|
47 tree_constant |
162
|
48 inverse (const tree_constant& a) |
1
|
49 { |
|
50 tree_constant retval; |
|
51 |
|
52 tree_constant tmp = a.make_numeric (); |
|
53 |
|
54 int nr = tmp.rows (); |
|
55 int nc = tmp.columns (); |
|
56 if (nr == 0 || nc == 0) |
|
57 { |
|
58 int flag = user_pref.propagate_empty_matrices; |
|
59 if (flag < 0) |
|
60 gripe_empty_arg ("inverse", 0); |
|
61 else if (flag == 0) |
|
62 gripe_empty_arg ("inverse", 1); |
|
63 } |
|
64 |
|
65 Matrix mtmp; |
|
66 if (nr == 0 && nc == 0) |
|
67 return tree_constant (mtmp); |
|
68 |
|
69 switch (tmp.const_type ()) |
|
70 { |
|
71 case tree_constant_rep::matrix_constant: |
|
72 { |
|
73 Matrix m = tmp.matrix_value (); |
|
74 if (m.rows () == m.columns ()) |
|
75 { |
|
76 int info; |
|
77 double rcond = 0.0; |
|
78 Matrix minv = m.inverse (info, rcond); |
|
79 if (info == -1) |
217
|
80 warning ("inverse: matrix singular to machine precision,\ |
|
81 rcond = %g", rcond); |
1
|
82 else |
|
83 retval = tree_constant (minv); |
|
84 } |
|
85 else |
|
86 gripe_square_matrix_required ("inverse"); |
|
87 } |
|
88 break; |
|
89 case tree_constant_rep::scalar_constant: |
|
90 { |
|
91 double d = 1.0 / tmp.double_value (); |
|
92 retval = tree_constant (d); |
|
93 } |
|
94 break; |
|
95 case tree_constant_rep::complex_matrix_constant: |
|
96 { |
|
97 ComplexMatrix m = tmp.complex_matrix_value (); |
|
98 if (m.rows () == m.columns ()) |
|
99 { |
|
100 int info; |
|
101 double rcond = 0.0; |
|
102 ComplexMatrix minv = m.inverse (info, rcond); |
|
103 if (info == -1) |
217
|
104 warning ("inverse: matrix singular to machine precision,\ |
|
105 rcond = %g", rcond); |
1
|
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 */ |