1
|
1 // The rest of the tree classes. -*- C++ -*- |
|
2 /* |
|
3 |
296
|
4 Copyright (C) 1992, 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 |
383
|
24 #if !defined (octave_tree_const_h) |
|
25 #define octave_tree_const_h 1 |
1
|
26 |
455
|
27 #if defined (__GNUG__) |
|
28 #pragma interface |
|
29 #endif |
|
30 |
1
|
31 #include <stdlib.h> |
|
32 |
500
|
33 #include "mx-base.h" |
|
34 #include "Range.h" |
|
35 |
1
|
36 #include "builtins.h" |
|
37 #include "tree-base.h" |
495
|
38 #include "tree-expr.h" |
|
39 #include "tc-rep.h" |
500
|
40 #include "oct-obj.h" |
164
|
41 |
|
42 class idx_vector; |
1
|
43 |
|
44 /* |
495
|
45 * Constants. |
1
|
46 */ |
|
47 class |
495
|
48 tree_constant : public tree_fvc |
1
|
49 { |
|
50 friend class tree_constant_rep; |
|
51 |
|
52 public: |
|
53 tree_constant (void) |
|
54 { rep = new tree_constant_rep (); rep->count = 1; } |
|
55 |
|
56 tree_constant (double d) |
|
57 { rep = new tree_constant_rep (d); rep->count = 1; } |
161
|
58 tree_constant (const Matrix& m) |
1
|
59 { rep = new tree_constant_rep (m); rep->count = 1; } |
161
|
60 tree_constant (const DiagMatrix& d) |
1
|
61 { rep = new tree_constant_rep (d); rep->count = 1; } |
455
|
62 tree_constant (const RowVector& v, int pcv = -1) |
1
|
63 { rep = new tree_constant_rep (v, pcv); rep->count = 1; } |
455
|
64 tree_constant (const ColumnVector& v, int pcv = -1) |
1
|
65 { rep = new tree_constant_rep (v, pcv); rep->count = 1; } |
|
66 |
161
|
67 tree_constant (const Complex& c) |
1
|
68 { rep = new tree_constant_rep (c); rep->count = 1; } |
161
|
69 tree_constant (const ComplexMatrix& m) |
1
|
70 { rep = new tree_constant_rep (m); rep->count = 1; } |
161
|
71 tree_constant (const ComplexDiagMatrix& d) |
1
|
72 { rep = new tree_constant_rep (d); rep->count = 1; } |
455
|
73 tree_constant (const ComplexRowVector& v, int pcv = -1) |
1
|
74 { rep = new tree_constant_rep (v, pcv); rep->count = 1; } |
455
|
75 tree_constant (const ComplexColumnVector& v, int pcv = -1) |
1
|
76 { rep = new tree_constant_rep (v, pcv); rep->count = 1; } |
|
77 |
|
78 tree_constant (const char *s) |
|
79 { rep = new tree_constant_rep (s); rep->count = 1; } |
|
80 |
|
81 tree_constant (double base, double limit, double inc) |
|
82 { rep = new tree_constant_rep (base, limit, inc); rep->count = 1; } |
161
|
83 tree_constant (const Range& r) |
1
|
84 { rep = new tree_constant_rep (r); rep->count = 1; } |
|
85 |
|
86 tree_constant (tree_constant_rep::constant_type t) |
|
87 { rep = new tree_constant_rep (t); rep->count = 1; } |
|
88 |
161
|
89 tree_constant (const tree_constant& a) |
1
|
90 { rep = a.rep; rep->count++; } |
|
91 tree_constant (tree_constant_rep& r) |
|
92 { rep = &r; rep->count++; } |
|
93 |
|
94 ~tree_constant (void); |
|
95 |
|
96 #if defined (MDEBUG) |
|
97 void *operator new (size_t size); |
|
98 void operator delete (void *p, size_t size); |
|
99 #endif |
|
100 |
|
101 tree_constant operator = (tree_constant& a) |
|
102 { |
|
103 if (--rep->count <= 0 && rep != a.rep) |
|
104 delete rep; |
|
105 |
|
106 rep = a.rep; |
|
107 rep->count++; |
|
108 return *this; |
|
109 } |
|
110 |
164
|
111 int is_constant (void) const { return 1; } |
1
|
112 |
164
|
113 int is_scalar_type (void) const { return rep->is_scalar_type (); } |
|
114 int is_matrix_type (void) const { return rep->is_matrix_type (); } |
1
|
115 |
164
|
116 int is_real_type (void) const { return rep->is_real_type (); } |
|
117 int is_complex_type (void) const { return rep->is_complex_type (); } |
1
|
118 |
164
|
119 int is_numeric_type (void) const { return rep->is_numeric_type (); } |
1
|
120 |
164
|
121 int is_numeric_or_range_type (void) const |
1
|
122 { return rep->is_numeric_or_range_type (); } |
|
123 |
164
|
124 int is_string_type (void) const { return rep->is_string_type (); } |
1
|
125 |
164
|
126 int valid_as_scalar_index (void) const |
|
127 { return rep->valid_as_scalar_index (); } |
1
|
128 |
164
|
129 int is_defined (void) const { return rep->is_defined (); } |
|
130 int is_undefined (void) const { return rep->is_undefined (); } |
1
|
131 |
164
|
132 double to_scalar (void) const { return rep->to_scalar (); } |
|
133 ColumnVector to_vector (void) const { return rep->to_vector (); } |
|
134 Matrix to_matrix (void) const { return rep->to_matrix (); } |
1
|
135 |
|
136 tree_constant_rep::constant_type force_numeric (int force_str_conv = 0) |
|
137 { return rep->force_numeric (force_str_conv); } |
|
138 |
164
|
139 tree_constant make_numeric (int force_str_conv = 0) const |
1
|
140 { |
|
141 if (is_numeric_type ()) |
|
142 return *this; |
|
143 else |
|
144 return rep->make_numeric (force_str_conv); |
|
145 } |
|
146 |
164
|
147 tree_constant make_numeric_or_range (void) const |
1
|
148 { |
|
149 if (is_numeric_type () |
|
150 || rep->type_tag == tree_constant_rep::range_constant) |
|
151 return *this; |
|
152 else |
|
153 return rep->make_numeric (); |
|
154 } |
|
155 |
164
|
156 tree_constant make_numeric_or_magic (void) const |
1
|
157 { |
|
158 if (is_numeric_type () |
|
159 || rep->type_tag == tree_constant_rep::magic_colon) |
|
160 return *this; |
|
161 else |
|
162 return rep->make_numeric (); |
|
163 } |
|
164 |
164
|
165 tree_constant make_numeric_or_range_or_magic (void) const |
1
|
166 { |
|
167 if (is_numeric_type () |
|
168 || rep->type_tag == tree_constant_rep::magic_colon |
|
169 || rep->type_tag == tree_constant_rep::range_constant) |
|
170 return *this; |
|
171 else |
|
172 return rep->make_numeric (); |
|
173 } |
|
174 |
506
|
175 tree_constant assign (tree_constant& rhs, const Octave_object& args) |
1
|
176 { |
|
177 if (rep->count > 1) |
|
178 { |
|
179 --rep->count; |
|
180 rep = new tree_constant_rep (*rep); |
|
181 rep->count = 1; |
|
182 } |
506
|
183 rep->assign (rhs, args); |
1
|
184 return *this; |
|
185 } |
|
186 |
276
|
187 int save (ostream& os, int mark_as_global = 0, int precision = 17) |
|
188 { return rep->save (os, mark_as_global, precision); } |
1
|
189 int save_three_d (ostream& os, int parametric = 0) |
|
190 { return rep->save_three_d (os, parametric); } |
|
191 |
|
192 int load (istream& is) { return rep->load (is); } |
|
193 tree_constant_rep::constant_type load |
|
194 (istream& is, tree_constant_rep::constant_type t) |
|
195 { return rep->load (is, t); } |
|
196 |
164
|
197 double double_value (void) const { return rep->double_value (); } |
|
198 Matrix matrix_value (void) const { return rep->matrix_value (); } |
|
199 Complex complex_value (void) const { return rep->complex_value (); } |
|
200 ComplexMatrix complex_matrix_value (void) const |
1
|
201 { return rep->complex_matrix_value (); } |
164
|
202 char *string_value (void) const { return rep->string_value (); } |
|
203 Range range_value (void) const { return rep->range_value (); } |
1
|
204 |
164
|
205 int rows (void) const { return rep->rows (); } |
|
206 int columns (void) const { return rep->columns (); } |
1
|
207 |
208
|
208 int is_empty (void) const |
|
209 { |
|
210 return (rep->type_tag != tree_constant_rep::magic_colon |
|
211 && rep->type_tag != tree_constant_rep::unknown_constant |
|
212 && (rows () == 0 || columns () == 0)); |
|
213 } |
|
214 |
|
215 int is_zero_by_zero (void) const |
212
|
216 { |
|
217 return (rep->type_tag != tree_constant_rep::magic_colon |
|
218 && rep->type_tag != tree_constant_rep::unknown_constant |
|
219 && rows () == 0 |
|
220 && columns () == 0); |
|
221 } |
208
|
222 |
93
|
223 |
164
|
224 tree_constant all (void) const { return rep->all (); } |
|
225 tree_constant any (void) const { return rep->any (); } |
|
226 tree_constant isstr (void) const { return rep->isstr (); } |
1
|
227 |
|
228 tree_constant convert_to_str (void) { return rep->convert_to_str (); } |
|
229 |
435
|
230 void convert_to_row_or_column_vector (void) |
455
|
231 { rep->convert_to_row_or_column_vector (); } |
435
|
232 |
428
|
233 int is_true (void) const { return rep->is_true (); } |
|
234 |
164
|
235 tree_constant cumprod (void) const { return rep->cumprod (); } |
|
236 tree_constant cumsum (void) const { return rep->cumsum (); } |
|
237 tree_constant prod (void) const { return rep->prod (); } |
|
238 tree_constant sum (void) const { return rep->sum (); } |
|
239 tree_constant sumsq (void) const { return rep->sumsq (); } |
1
|
240 |
164
|
241 tree_constant diag (void) const { return rep->diag (); } |
|
242 tree_constant diag (const tree_constant& a) const { return rep->diag (a); } |
1
|
243 |
164
|
244 tree_constant_rep::constant_type const_type (void) const |
1
|
245 { return rep->const_type (); } |
|
246 |
164
|
247 tree_constant mapper (Mapper_fcn& m_fcn, int print) const |
1
|
248 { return rep->mapper (m_fcn, print); } |
|
249 |
|
250 void bump_value (tree::expression_type et) |
|
251 { |
|
252 if (rep->count > 1) |
|
253 { |
|
254 --rep->count; |
|
255 rep = new tree_constant_rep (*rep); |
|
256 rep->count = 1; |
|
257 } |
|
258 rep->bump_value (et); |
|
259 } |
|
260 |
|
261 tree_constant eval (int print) |
495
|
262 { |
|
263 rep->maybe_mutate (); |
|
264 if (print) |
|
265 rep->print (); |
|
266 return *this; |
|
267 } |
1
|
268 |
506
|
269 Octave_object eval (int print, int nargout, const Octave_object& args) |
1
|
270 { |
500
|
271 Octave_object retval (1); |
495
|
272 |
506
|
273 // XXX FIXME XXX -- make it safe to call do_index() with |
|
274 // args.length () == 0 |
|
275 if (args.length () > 0) |
|
276 retval(0) = rep->do_index (args); |
495
|
277 else |
500
|
278 retval(0) = *this; |
495
|
279 |
500
|
280 if (retval(0).is_defined ()) |
|
281 retval(0).eval (print); |
1
|
282 return retval; |
|
283 } |
|
284 |
|
285 private: |
|
286 tree_constant_rep *rep; |
|
287 }; |
|
288 |
94
|
289 /* |
|
290 * Here are some extra functions that are related to the tree_constant |
|
291 * class but that don't need to be class members or friends. |
|
292 */ |
|
293 |
500
|
294 extern Octave_object vector_of_empties (int nargout, const char *fcn_name); |
94
|
295 |
495
|
296 extern tree_constant fill_matrix (const tree_constant& a, |
|
297 double d, const char *warn_for); |
|
298 extern tree_constant fill_matrix (const tree_constant& a, |
|
299 const tree_constant& b, |
|
300 double d, const char *warn_for); |
|
301 |
|
302 extern tree_constant identity_matrix (const tree_constant& a); |
|
303 extern tree_constant identity_matrix (const tree_constant& a, |
|
304 const tree_constant& b); |
|
305 |
|
306 extern tree_constant find_nonzero_elem_idx (const tree_constant& a); |
|
307 |
500
|
308 extern Octave_object matrix_log (const tree_constant& a); |
|
309 extern Octave_object matrix_sqrt (const tree_constant& a); |
495
|
310 |
506
|
311 extern Octave_object column_max (const Octave_object& args, int nargout); |
|
312 extern Octave_object column_min (const Octave_object& args, int nargout); |
495
|
313 |
506
|
314 extern Octave_object sort (const Octave_object& args, int nargout); |
495
|
315 |
506
|
316 extern Octave_object feval (const Octave_object& args, int nargout); |
495
|
317 |
|
318 extern tree_constant eval_string (const tree_constant& arg, int& |
|
319 parse_status); |
|
320 |
500
|
321 extern tree_constant get_user_input (const Octave_object& args, |
506
|
322 int nargout, int debug = 0); |
495
|
323 |
1
|
324 #endif |
|
325 |
|
326 /* |
|
327 ;;; Local Variables: *** |
|
328 ;;; mode: C++ *** |
|
329 ;;; page-delimiter: "^/\\*" *** |
|
330 ;;; End: *** |
|
331 */ |