7
|
1 // f-inv.cc -*- C++ -*- |
1
|
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" |
7
|
34 #include "f-inv.h" |
1
|
35 |
|
36 #ifdef WITH_DLD |
|
37 tree_constant * |
162
|
38 builtin_inv (const tree_constant *args, int nargin, int nargout) |
1
|
39 { |
|
40 tree_constant *retval = new tree_constant [2]; |
|
41 retval[0] = inverse (args[1]); |
|
42 return retval; |
|
43 } |
|
44 #endif |
|
45 |
|
46 tree_constant |
162
|
47 inverse (const tree_constant& a) |
1
|
48 { |
|
49 tree_constant retval; |
|
50 |
|
51 tree_constant tmp = a.make_numeric (); |
|
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) |
|
66 return tree_constant (mtmp); |
|
67 |
|
68 switch (tmp.const_type ()) |
|
69 { |
|
70 case tree_constant_rep::matrix_constant: |
|
71 { |
|
72 Matrix m = tmp.matrix_value (); |
|
73 if (m.rows () == m.columns ()) |
|
74 { |
|
75 int info; |
|
76 double rcond = 0.0; |
|
77 Matrix minv = m.inverse (info, rcond); |
|
78 if (info == -1) |
217
|
79 warning ("inverse: matrix singular to machine precision,\ |
|
80 rcond = %g", rcond); |
1
|
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) |
217
|
103 warning ("inverse: matrix singular to machine precision,\ |
|
104 rcond = %g", rcond); |
1
|
105 else |
|
106 retval = tree_constant (minv); |
|
107 } |
|
108 else |
|
109 gripe_square_matrix_required ("inverse"); |
|
110 } |
|
111 break; |
|
112 case tree_constant_rep::complex_scalar_constant: |
|
113 { |
|
114 Complex c = 1.0 / tmp.complex_value (); |
|
115 retval = tree_constant (c); |
|
116 } |
|
117 break; |
|
118 default: |
|
119 break; |
|
120 } |
|
121 return retval; |
|
122 } |
|
123 |
|
124 /* |
|
125 ;;; Local Variables: *** |
|
126 ;;; mode: C++ *** |
|
127 ;;; page-delimiter: "^/\\*" *** |
|
128 ;;; End: *** |
|
129 */ |