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 * |
|
38 builtin_inv (tree_constant *args, int nargin, int nargout) |
|
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 |
|
47 inverse (tree_constant& a) |
|
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) |
|
79 message ("inverse", |
|
80 "matrix singular to machine precision, rcond = %g", |
|
81 rcond); |
|
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) |
|
104 message ("inverse", |
|
105 "matrix singular to machine precision, rcond = %g", |
|
106 rcond); |
|
107 else |
|
108 retval = tree_constant (minv); |
|
109 } |
|
110 else |
|
111 gripe_square_matrix_required ("inverse"); |
|
112 } |
|
113 break; |
|
114 case tree_constant_rep::complex_scalar_constant: |
|
115 { |
|
116 Complex c = 1.0 / tmp.complex_value (); |
|
117 retval = tree_constant (c); |
|
118 } |
|
119 break; |
|
120 default: |
|
121 break; |
|
122 } |
|
123 return retval; |
|
124 } |
|
125 |
|
126 /* |
|
127 ;;; Local Variables: *** |
|
128 ;;; mode: C++ *** |
|
129 ;;; page-delimiter: "^/\\*" *** |
|
130 ;;; End: *** |
|
131 */ |