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