1
|
1 // The rest of the tree classes. -*- C++ -*- |
|
2 /* |
|
3 |
|
4 Copyright (C) 1992, 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 #if !defined (_tree_const_h) |
|
25 #define _tree_const_h 1 |
|
26 |
|
27 #ifdef __GNUG__ |
|
28 #pragma interface |
|
29 #endif |
|
30 |
|
31 #include <stdlib.h> |
|
32 #include <String.h> |
|
33 |
|
34 #include "Range.h" |
|
35 #include "builtins.h" |
|
36 #include "Matrix.h" |
|
37 #include "idx-vector.h" |
|
38 #include "tree-base.h" |
|
39 |
|
40 /* |
|
41 * How about a few macros? |
|
42 */ |
|
43 |
|
44 #ifndef MAX |
|
45 #define MAX(a,b) ((a) > (b) ? (a) : (b)) |
|
46 #endif |
|
47 |
|
48 #ifndef MIN |
|
49 #define MIN(a,b) ((a) < (b) ? (a) : (b)) |
|
50 #endif |
|
51 |
|
52 #ifndef ABS |
|
53 #define ABS(x) (((x) < 0) ? (-x) : (x)) |
|
54 #endif |
|
55 |
|
56 #ifndef NULL_TREE |
|
57 #define NULL_TREE (tree *)NULL |
|
58 #endif |
|
59 |
|
60 #ifndef NULL_TREE_CONST |
|
61 #define NULL_TREE_CONST (tree_constant *)NULL |
|
62 #endif |
|
63 |
|
64 /* |
|
65 * The following are used by some of the functions in the |
|
66 * tree_constant_rep class that must deal with real and complex |
|
67 * matrices. This was not done with overloaded or virtual functions |
|
68 * from the Matrix class because there is no clean way to do that -- |
|
69 * the necessary functions (like elem) need to return values of |
|
70 * different types... |
|
71 */ |
|
72 |
|
73 // Given a tree_constant, and the names to be used for the real and |
|
74 // complex matrix and their dimensions, declare a real or complex |
|
75 // matrix, and initialize it from the tree_constant. Note that m, cm, |
|
76 // nr, and nc must not be previously declared, and they must not be |
|
77 // expressions. Since only one of the matrices will be defined after |
|
78 // this macro is used, only one set of dimesions is declared. |
|
79 |
|
80 // This macro only makes sense inside a friend or member function of |
|
81 // the tree_constant_rep class |
|
82 |
|
83 #define REP_RHS_MATRIX(tc,m,cm,nr,nc) \ |
|
84 int nr, nc; \ |
|
85 Matrix m; \ |
|
86 ComplexMatrix cm; \ |
|
87 if ((tc).const_type () == tree_constant_rep::complex_matrix_constant) \ |
|
88 { \ |
|
89 cm = (tc).complex_matrix_value (); \ |
|
90 nr = (cm).rows (); \ |
|
91 nc = (cm).columns (); \ |
|
92 } \ |
|
93 else \ |
|
94 { \ |
|
95 m = (tc).matrix_value (); \ |
|
96 nr = (m).rows (); \ |
|
97 nc = (m).columns (); \ |
|
98 } |
|
99 |
|
100 // Assign a real or complex value to a tree_constant. |
|
101 // |
|
102 // This macro only makes sense inside a friend or member function of |
|
103 // the tree_constant_rep class. |
|
104 |
|
105 #define REP_ELEM_ASSIGN(i,j,rval,cval,real_type) \ |
|
106 do \ |
|
107 { \ |
|
108 if (type_tag == tree_constant_rep::matrix_constant) \ |
|
109 { \ |
|
110 if (real_type) \ |
|
111 matrix->elem ((i), (j)) = (rval); \ |
|
112 else \ |
|
113 abort (); \ |
|
114 } \ |
|
115 else \ |
|
116 { \ |
|
117 if (real_type) \ |
|
118 complex_matrix->elem ((i), (j)) = (rval); \ |
|
119 else \ |
|
120 complex_matrix->elem ((i), (j)) = (cval); \ |
|
121 } \ |
|
122 } \ |
|
123 while (0) |
|
124 |
|
125 // Given a real and complex matrix and row and column dimensions, |
|
126 // declare both and size one of them. Only one of the matrices should |
|
127 // be used after this macro has been used. |
|
128 |
|
129 // This macro only makes sense inside a friend or member function of |
|
130 // the tree_constant_rep class. |
|
131 |
|
132 #define CRMATRIX(m,cm,nr,nc) \ |
|
133 Matrix m; \ |
|
134 ComplexMatrix cm; \ |
|
135 if (type_tag == tree_constant_rep::matrix_constant) \ |
|
136 { \ |
|
137 (m).resize ((nr), (nc)); \ |
|
138 } \ |
|
139 else if (type_tag == complex_matrix_constant) \ |
|
140 { \ |
|
141 (cm).resize ((nr), (nc)); \ |
|
142 } \ |
|
143 else \ |
|
144 { \ |
|
145 abort (); \ |
|
146 } |
|
147 |
|
148 // Assign a real or complex matrix to a tree constant. |
|
149 |
|
150 // This macro only makes sense inside a friend or member function of |
|
151 // the tree_constant_rep class. |
|
152 |
|
153 #define ASSIGN_CRMATRIX_TO(tc,m,cm) \ |
|
154 do \ |
|
155 { \ |
|
156 if (type_tag == matrix_constant) \ |
|
157 tc = tree_constant (m); \ |
|
158 else \ |
|
159 tc = tree_constant (cm); \ |
|
160 } \ |
|
161 while (0) |
|
162 |
|
163 // Assign an element of this tree_constant_rep's real or complex |
|
164 // matrix to another real or complex matrix. |
|
165 |
|
166 // This macro only makes sense inside a friend or member function of |
|
167 // the tree_constant_rep class. |
|
168 |
|
169 #define CRMATRIX_ASSIGN_REP_ELEM(m,cm,i1,j1,i2,j2) \ |
|
170 do \ |
|
171 { \ |
|
172 if (type_tag == matrix_constant) \ |
|
173 (m).elem ((i1), (j1)) = matrix->elem ((i2), (j2)); \ |
|
174 else \ |
|
175 (cm).elem ((i1), (j1)) = complex_matrix->elem ((i2), (j2)); \ |
|
176 } \ |
|
177 while (0) |
|
178 |
|
179 // Assign a value to an element of a real or complex matrix. Assumes |
|
180 // that the lhs and rhs are either both real or both complex types. |
|
181 |
|
182 #define CRMATRIX_ASSIGN_ELEM(m,cm,i,j,rval,cval,real_type) \ |
|
183 do \ |
|
184 { \ |
|
185 if (real_type) \ |
|
186 (m).elem ((i), (j)) = (rval); \ |
|
187 else \ |
|
188 (cm).elem ((i), (j)) = (cval); \ |
|
189 } \ |
|
190 while (0) |
|
191 |
|
192 |
|
193 /* |
|
194 * Forward class declarations. |
|
195 */ |
|
196 class tree; |
|
197 class tree_constant; |
|
198 |
|
199 #ifndef TREE_FCN_TYPEDEFS |
|
200 #define TREE_FCN_TYPEDEFS 1 |
|
201 |
|
202 typedef tree_constant (*Text_fcn)(int, char **); |
|
203 typedef tree_constant* (*General_fcn)(tree_constant *, int, int); |
|
204 |
|
205 #endif |
|
206 |
|
207 /* |
|
208 * The actual representation of the tree_constant. |
|
209 */ |
|
210 class |
|
211 tree_constant_rep |
|
212 { |
|
213 friend class tree_constant; |
|
214 |
|
215 enum force_orient |
|
216 { |
|
217 no_orient, |
|
218 row_orient, |
|
219 column_orient, |
|
220 }; |
|
221 |
|
222 public: |
|
223 enum constant_type |
|
224 { |
|
225 unknown_constant, |
|
226 scalar_constant, |
|
227 matrix_constant, |
|
228 complex_scalar_constant, |
|
229 complex_matrix_constant, |
|
230 string_constant, |
|
231 range_constant, |
|
232 magic_colon, |
|
233 }; |
|
234 |
|
235 tree_constant_rep (void); |
|
236 |
|
237 tree_constant_rep (double d); |
161
|
238 tree_constant_rep (const Matrix& m); |
|
239 tree_constant_rep (const DiagMatrix& d); |
|
240 tree_constant_rep (const RowVector& v); |
|
241 tree_constant_rep (const RowVector& v, int pcv); |
|
242 tree_constant_rep (const ColumnVector& v); |
|
243 tree_constant_rep (const ColumnVector& v, int pcv); |
1
|
244 |
161
|
245 tree_constant_rep (const Complex& c); |
|
246 tree_constant_rep (const ComplexMatrix& m); |
|
247 tree_constant_rep (const ComplexDiagMatrix& d); |
|
248 tree_constant_rep (const ComplexRowVector& v); |
|
249 tree_constant_rep (const ComplexRowVector& v, int pcv); |
|
250 tree_constant_rep (const ComplexColumnVector& v); |
|
251 tree_constant_rep (const ComplexColumnVector& v, int pcv); |
1
|
252 |
|
253 tree_constant_rep (const char *s); |
161
|
254 tree_constant_rep (const String& s); |
1
|
255 |
|
256 tree_constant_rep (double base, double limit, double inc); |
161
|
257 tree_constant_rep (const Range& r); |
1
|
258 |
|
259 tree_constant_rep (tree_constant_rep::constant_type t); |
|
260 |
161
|
261 tree_constant_rep (const tree_constant_rep& t); |
1
|
262 |
|
263 ~tree_constant_rep (void); |
|
264 |
|
265 #if defined (MDEBUG) |
|
266 void *operator new (size_t size); |
|
267 void operator delete (void *p, size_t size); |
|
268 #endif |
|
269 |
|
270 void resize (int i, int j); |
|
271 void resize (int i, int j, double val); |
|
272 |
|
273 void maybe_resize (int imax, force_orient fo = no_orient); |
|
274 void maybe_resize (int imax, int jmax); |
|
275 |
|
276 int valid_as_scalar_index (void); |
|
277 |
|
278 int is_defined (void) |
|
279 { return type_tag != tree_constant_rep::unknown_constant; } |
|
280 |
|
281 int is_undefined (void) |
|
282 { return type_tag == tree_constant_rep::unknown_constant; } |
|
283 |
|
284 int is_string_type (void) |
|
285 { return type_tag == tree_constant_rep::string_constant; } |
|
286 |
|
287 int is_scalar_type (void) |
|
288 { return type_tag == scalar_constant |
|
289 || type_tag == complex_scalar_constant; } |
|
290 |
|
291 int is_matrix_type (void) |
|
292 { return type_tag == matrix_constant |
|
293 || type_tag == complex_matrix_constant; } |
|
294 |
|
295 int is_real_type (void) |
|
296 { return type_tag == scalar_constant |
|
297 || type_tag == matrix_constant |
|
298 || type_tag == range_constant; } |
|
299 |
|
300 int is_complex_type (void) |
|
301 { return type_tag == complex_matrix_constant |
|
302 || type_tag == complex_scalar_constant; } |
|
303 |
|
304 |
|
305 int is_numeric_type (void) |
|
306 { return type_tag == scalar_constant |
|
307 || type_tag == matrix_constant |
|
308 || type_tag == complex_matrix_constant |
|
309 || type_tag == complex_scalar_constant; } |
|
310 |
|
311 int is_numeric_or_range_type (void) |
|
312 { return type_tag == scalar_constant |
|
313 || type_tag == matrix_constant |
|
314 || type_tag == complex_matrix_constant |
|
315 || type_tag == complex_scalar_constant |
|
316 || type_tag == range_constant; } |
|
317 |
|
318 double to_scalar (void); |
|
319 ColumnVector to_vector (void); |
|
320 Matrix to_matrix (void); |
|
321 |
|
322 tree_constant_rep::constant_type force_numeric (int force_str_conv = 0); |
|
323 tree_constant make_numeric (int force_str_conv = 0); |
|
324 |
|
325 friend tree_constant |
|
326 do_binary_op (tree_constant& a, tree_constant& b, tree::expression_type t); |
|
327 |
|
328 friend tree_constant |
|
329 do_unary_op (tree_constant& a, tree::expression_type t); |
|
330 |
|
331 void assign (tree_constant& rhs, tree_constant *args, int nargs); |
|
332 |
|
333 void do_scalar_assignment |
|
334 (tree_constant& rhs, tree_constant *args, int nargin); |
|
335 |
|
336 void do_matrix_assignment |
|
337 (tree_constant& rhs, tree_constant *args, int nargin); |
|
338 |
|
339 void do_matrix_assignment |
|
340 (tree_constant& rhs, tree_constant& i_arg); |
|
341 |
|
342 void do_matrix_assignment |
|
343 (tree_constant& rhs, tree_constant& i_arg, tree_constant& j_arg); |
|
344 |
|
345 void fortran_style_matrix_assignment (tree_constant& rhs, |
|
346 tree_constant& i_arg); |
|
347 |
|
348 void fortran_style_matrix_assignment (tree_constant& rhs, constant_type ci); |
|
349 |
|
350 void fortran_style_matrix_assignment (tree_constant& rhs, idx_vector& i); |
|
351 |
|
352 void vector_assignment (tree_constant& rhs, tree_constant& i_arg); |
|
353 |
|
354 void check_vector_assign (int rhs_nr, int rhs_nc, int ilen, char *rm); |
|
355 |
|
356 void do_vector_assign (tree_constant& rhs, int i); |
|
357 void do_vector_assign (tree_constant& rhs, idx_vector& i); |
|
358 void do_vector_assign (tree_constant& rhs, Range& i, int imax); |
|
359 |
|
360 void do_matrix_assignment |
|
361 (tree_constant& rhs, int i, tree_constant& j_arg); |
|
362 void do_matrix_assignment |
|
363 (tree_constant& rhs, idx_vector& i, tree_constant& j_arg); |
|
364 void do_matrix_assignment |
|
365 (tree_constant& rhs, Range& i, int imax, tree_constant& j_arg); |
|
366 void do_matrix_assignment |
|
367 (tree_constant& rhs, constant_type i, tree_constant& j_arg); |
|
368 |
|
369 void do_matrix_assignment (tree_constant& rhs, int i, int j); |
|
370 void do_matrix_assignment (tree_constant& rhs, int i, idx_vector& jv); |
|
371 void do_matrix_assignment (tree_constant& rhs, int i, Range& j); |
|
372 void do_matrix_assignment (tree_constant& rhs, int i, constant_type cj); |
|
373 |
|
374 void do_matrix_assignment (tree_constant& rhs, idx_vector& iv, int j); |
|
375 void do_matrix_assignment (tree_constant& rhs, idx_vector& iv, |
|
376 idx_vector& jv); |
|
377 void do_matrix_assignment (tree_constant& rhs, idx_vector& iv, Range& j); |
|
378 void do_matrix_assignment (tree_constant& rhs, idx_vector& iv, |
|
379 constant_type j); |
|
380 |
|
381 void do_matrix_assignment (tree_constant& rhs, Range& i, int j); |
|
382 void do_matrix_assignment (tree_constant& rhs, Range& i, idx_vector& jv); |
|
383 void do_matrix_assignment (tree_constant& rhs, Range& i, Range& j); |
|
384 void do_matrix_assignment (tree_constant& rhs, Range& i, constant_type j); |
|
385 |
|
386 void do_matrix_assignment (tree_constant& rhs, constant_type i, int j); |
|
387 void do_matrix_assignment (tree_constant& rhs, constant_type i, |
|
388 idx_vector& jv); |
|
389 void do_matrix_assignment (tree_constant& rhs, constant_type i, Range& j); |
|
390 void do_matrix_assignment (tree_constant& rhs, constant_type i, |
|
391 constant_type j); |
|
392 |
|
393 void bump_value (tree::expression_type); |
|
394 |
|
395 void eval (int print); |
|
396 |
|
397 tree_constant *eval (tree_constant *args, int n_in, int n_out, int print); |
|
398 |
|
399 tree_constant do_scalar_index (tree_constant *args, int nargin); |
|
400 |
|
401 tree_constant do_matrix_index (tree_constant *args, int nargin); |
|
402 |
|
403 tree_constant do_matrix_index (tree_constant& i_arg); |
|
404 |
|
405 tree_constant do_matrix_index (tree_constant& i_arg, tree_constant& j_arg); |
|
406 |
|
407 tree_constant do_matrix_index (constant_type i); |
|
408 |
|
409 tree_constant fortran_style_matrix_index (tree_constant& i_arg); |
|
410 tree_constant fortran_style_matrix_index (Matrix& mi); |
|
411 |
|
412 tree_constant do_vector_index (tree_constant& i_arg); |
|
413 |
|
414 tree_constant do_matrix_index (int i, tree_constant& i_arg); |
|
415 tree_constant do_matrix_index (idx_vector& i, tree_constant& i_arg); |
|
416 tree_constant do_matrix_index (Range& i, int imax, tree_constant& i_arg); |
|
417 tree_constant do_matrix_index (constant_type i, tree_constant& i_arg); |
|
418 |
|
419 tree_constant do_matrix_index (int i, int j); |
|
420 tree_constant do_matrix_index (int i, idx_vector& j); |
|
421 tree_constant do_matrix_index (int i, Range& j); |
|
422 tree_constant do_matrix_index (int i, constant_type cj); |
|
423 |
|
424 tree_constant do_matrix_index (idx_vector& i, int j); |
|
425 tree_constant do_matrix_index (idx_vector& i, idx_vector& j); |
|
426 tree_constant do_matrix_index (idx_vector& i, Range& j); |
|
427 tree_constant do_matrix_index (idx_vector& i, constant_type j); |
|
428 |
|
429 tree_constant do_matrix_index (Range& i, int j); |
|
430 tree_constant do_matrix_index (Range& i, idx_vector& j); |
|
431 tree_constant do_matrix_index (Range& i, Range& j); |
|
432 tree_constant do_matrix_index (Range& i, constant_type j); |
|
433 |
|
434 tree_constant do_matrix_index (constant_type i, int j); |
|
435 tree_constant do_matrix_index (constant_type i, idx_vector& j); |
|
436 tree_constant do_matrix_index (constant_type i, Range& j); |
|
437 tree_constant do_matrix_index (constant_type i, constant_type j); |
|
438 |
|
439 int save (ostream& os, int mark_as_global); |
|
440 int save_three_d (ostream& os, int parametric); |
|
441 int load (istream& is); |
|
442 constant_type load (istream& is, constant_type t); |
|
443 |
|
444 double double_value (void); |
|
445 Matrix matrix_value (void); |
|
446 Complex complex_value (void); |
|
447 ComplexMatrix complex_matrix_value (void); |
|
448 char *string_value (void); |
|
449 Range range_value (void); |
|
450 |
|
451 int rows (void); |
|
452 int columns (void); |
|
453 |
|
454 tree_constant all (void); |
|
455 tree_constant any (void); |
|
456 tree_constant isstr (void); |
|
457 |
|
458 tree_constant convert_to_str (void); |
|
459 |
|
460 tree_constant cumprod (void); |
|
461 tree_constant cumsum (void); |
|
462 tree_constant prod (void); |
|
463 tree_constant sum (void); |
|
464 tree_constant sumsq (void); |
|
465 |
|
466 tree_constant diag (void); |
|
467 tree_constant diag (tree_constant& a); |
|
468 |
|
469 friend tree_constant fill_matrix (tree_constant& a, double d, |
|
470 char *warn_for); |
|
471 friend tree_constant fill_matrix (tree_constant& a, tree_constant& b, |
|
472 double d, char *warn_for); |
|
473 |
|
474 friend tree_constant identity_matrix (tree_constant& a); |
|
475 friend tree_constant identity_matrix (tree_constant& a, tree_constant& b); |
|
476 |
|
477 friend tree_constant find_nonzero_elem_idx (tree_constant& a); |
|
478 |
|
479 friend tree_constant *matrix_log (tree_constant& a); |
|
480 friend tree_constant *matrix_sqrt (tree_constant& a); |
|
481 |
|
482 friend tree_constant *column_max (tree_constant *args, int nargin, |
|
483 int nargout); |
|
484 |
|
485 friend tree_constant *column_min (tree_constant *args, int nargin, |
|
486 int nargout); |
|
487 |
|
488 friend tree_constant *sort (tree_constant *args, int nargin, int nargout); |
|
489 |
|
490 friend tree_constant *feval (tree_constant *args, int nargin, int nargout); |
|
491 |
|
492 friend tree_constant eval_string (tree_constant& arg, int& parse_status); |
|
493 |
|
494 friend tree_constant get_user_input (tree_constant *args, int nargin, |
|
495 int nargout, int debug = 0); |
|
496 |
|
497 void print_if_string (ostream& os, int warn); |
|
498 |
|
499 constant_type const_type (void) { return type_tag; } |
|
500 |
|
501 tree_constant mapper (Mapper_fcn& m_fcn, int print); |
|
502 |
|
503 private: |
|
504 int count; |
|
505 constant_type type_tag; |
|
506 union |
|
507 { |
|
508 double scalar; // A real scalar constant. |
|
509 Matrix *matrix; // A real matrix constant. |
|
510 Complex *complex_scalar; // A real scalar constant. |
|
511 ComplexMatrix *complex_matrix; // A real matrix constant. |
|
512 char *string; // A character string constant. |
|
513 Range *range; // A set of evenly spaced values. |
|
514 }; |
|
515 }; |
|
516 |
|
517 /* |
|
518 * Constants. Nice -- No need to interpret them anymore. Logically, |
|
519 * this should be ahead of the tree_constant_rep class, but that |
|
520 * causes problems with my version of g++ (~2.2.2)... |
|
521 */ |
|
522 class tree_constant : public tree |
|
523 { |
|
524 friend class tree_constant_rep; |
|
525 |
|
526 public: |
|
527 tree_constant (void) |
|
528 { rep = new tree_constant_rep (); rep->count = 1; } |
|
529 |
|
530 tree_constant (double d) |
|
531 { rep = new tree_constant_rep (d); rep->count = 1; } |
161
|
532 tree_constant (const Matrix& m) |
1
|
533 { rep = new tree_constant_rep (m); rep->count = 1; } |
161
|
534 tree_constant (const DiagMatrix& d) |
1
|
535 { rep = new tree_constant_rep (d); rep->count = 1; } |
161
|
536 tree_constant (const RowVector& v) |
1
|
537 { rep = new tree_constant_rep (v); rep->count = 1; } |
161
|
538 tree_constant (const RowVector& v, int pcv) |
1
|
539 { rep = new tree_constant_rep (v, pcv); rep->count = 1; } |
161
|
540 tree_constant (const ColumnVector& v) |
1
|
541 { rep = new tree_constant_rep (v); rep->count = 1; } |
161
|
542 tree_constant (const ColumnVector& v, int pcv) |
1
|
543 { rep = new tree_constant_rep (v, pcv); rep->count = 1; } |
|
544 |
161
|
545 tree_constant (const Complex& c) |
1
|
546 { rep = new tree_constant_rep (c); rep->count = 1; } |
161
|
547 tree_constant (const ComplexMatrix& m) |
1
|
548 { rep = new tree_constant_rep (m); rep->count = 1; } |
161
|
549 tree_constant (const ComplexDiagMatrix& d) |
1
|
550 { rep = new tree_constant_rep (d); rep->count = 1; } |
161
|
551 tree_constant (const ComplexRowVector& v) |
1
|
552 { rep = new tree_constant_rep (v); rep->count = 1; } |
161
|
553 tree_constant (const ComplexRowVector& v, int pcv) |
1
|
554 { rep = new tree_constant_rep (v, pcv); rep->count = 1; } |
161
|
555 tree_constant (const ComplexColumnVector& v) |
1
|
556 { rep = new tree_constant_rep (v); rep->count = 1; } |
161
|
557 tree_constant (const ComplexColumnVector& v, int pcv) |
1
|
558 { rep = new tree_constant_rep (v, pcv); rep->count = 1; } |
|
559 |
|
560 tree_constant (const char *s) |
|
561 { rep = new tree_constant_rep (s); rep->count = 1; } |
161
|
562 tree_constant (const String& s) |
1
|
563 { rep = new tree_constant_rep (s); rep->count = 1; } |
|
564 |
|
565 tree_constant (double base, double limit, double inc) |
|
566 { rep = new tree_constant_rep (base, limit, inc); rep->count = 1; } |
161
|
567 tree_constant (const Range& r) |
1
|
568 { rep = new tree_constant_rep (r); rep->count = 1; } |
|
569 |
|
570 tree_constant (tree_constant_rep::constant_type t) |
|
571 { rep = new tree_constant_rep (t); rep->count = 1; } |
|
572 |
161
|
573 tree_constant (const tree_constant& a) |
1
|
574 { rep = a.rep; rep->count++; } |
|
575 tree_constant (tree_constant_rep& r) |
|
576 { rep = &r; rep->count++; } |
|
577 |
|
578 ~tree_constant (void); |
|
579 |
|
580 #if defined (MDEBUG) |
|
581 void *operator new (size_t size); |
|
582 void operator delete (void *p, size_t size); |
|
583 #endif |
|
584 |
|
585 tree_constant operator = (tree_constant& a) |
|
586 { |
|
587 if (--rep->count <= 0 && rep != a.rep) |
|
588 delete rep; |
|
589 |
|
590 rep = a.rep; |
|
591 rep->count++; |
|
592 return *this; |
|
593 } |
|
594 |
|
595 int is_constant (void) { return 1; } |
|
596 |
|
597 int is_scalar_type (void) { return rep->is_scalar_type (); } |
|
598 int is_matrix_type (void) { return rep->is_matrix_type (); } |
|
599 |
|
600 int is_real_type (void) { return rep->is_real_type (); } |
|
601 int is_complex_type (void) { return rep->is_complex_type (); } |
|
602 |
|
603 int is_numeric_type (void) { return rep->is_numeric_type (); } |
|
604 |
|
605 int is_numeric_or_range_type (void) |
|
606 { return rep->is_numeric_or_range_type (); } |
|
607 |
|
608 int is_string_type (void) { return rep->is_string_type (); } |
|
609 |
|
610 int valid_as_scalar_index (void) { return rep->valid_as_scalar_index (); } |
|
611 |
|
612 int is_defined (void) { return rep->is_defined (); } |
|
613 int is_undefined (void) { return rep->is_undefined (); } |
|
614 |
|
615 double to_scalar (void) { return rep->to_scalar (); } |
|
616 ColumnVector to_vector (void) { return rep->to_vector (); } |
|
617 Matrix to_matrix (void) { return rep->to_matrix (); } |
|
618 |
|
619 tree_constant_rep::constant_type force_numeric (int force_str_conv = 0) |
|
620 { return rep->force_numeric (force_str_conv); } |
|
621 |
|
622 tree_constant make_numeric (int force_str_conv = 0) |
|
623 { |
|
624 if (is_numeric_type ()) |
|
625 return *this; |
|
626 else |
|
627 return rep->make_numeric (force_str_conv); |
|
628 } |
|
629 |
|
630 tree_constant make_numeric_or_range (void) |
|
631 { |
|
632 if (is_numeric_type () |
|
633 || rep->type_tag == tree_constant_rep::range_constant) |
|
634 return *this; |
|
635 else |
|
636 return rep->make_numeric (); |
|
637 } |
|
638 |
|
639 tree_constant make_numeric_or_magic (void) |
|
640 { |
|
641 if (is_numeric_type () |
|
642 || rep->type_tag == tree_constant_rep::magic_colon) |
|
643 return *this; |
|
644 else |
|
645 return rep->make_numeric (); |
|
646 } |
|
647 |
|
648 tree_constant make_numeric_or_range_or_magic (void) |
|
649 { |
|
650 if (is_numeric_type () |
|
651 || rep->type_tag == tree_constant_rep::magic_colon |
|
652 || rep->type_tag == tree_constant_rep::range_constant) |
|
653 return *this; |
|
654 else |
|
655 return rep->make_numeric (); |
|
656 } |
|
657 |
|
658 tree_constant assign (tree_constant& rhs, tree_constant *args, int nargs) |
|
659 { |
|
660 if (rep->count > 1) |
|
661 { |
|
662 --rep->count; |
|
663 rep = new tree_constant_rep (*rep); |
|
664 rep->count = 1; |
|
665 } |
|
666 rep->assign (rhs, args, nargs); |
|
667 return *this; |
|
668 } |
|
669 |
|
670 int save (ostream& os, int mark_as_global = 0) |
|
671 { return rep->save (os, mark_as_global); } |
|
672 int save_three_d (ostream& os, int parametric = 0) |
|
673 { return rep->save_three_d (os, parametric); } |
|
674 |
|
675 int load (istream& is) { return rep->load (is); } |
|
676 tree_constant_rep::constant_type load |
|
677 (istream& is, tree_constant_rep::constant_type t) |
|
678 { return rep->load (is, t); } |
|
679 |
|
680 double double_value (void) { return rep->double_value (); } |
|
681 Matrix matrix_value (void) { return rep->matrix_value (); } |
|
682 Complex complex_value (void) { return rep->complex_value (); } |
|
683 ComplexMatrix complex_matrix_value (void) |
|
684 { return rep->complex_matrix_value (); } |
|
685 char *string_value (void) { return rep->string_value (); } |
|
686 Range range_value (void) { return rep->range_value (); } |
|
687 |
|
688 int rows (void) { return rep->rows (); } |
|
689 int columns (void) { return rep->columns (); } |
|
690 |
93
|
691 int is_empty (void) { return (rows () == 0 || columns () == 0); } |
|
692 |
1
|
693 tree_constant all (void) { return rep->all (); } |
|
694 tree_constant any (void) { return rep->any (); } |
|
695 tree_constant isstr (void) { return rep->isstr (); } |
|
696 |
|
697 tree_constant convert_to_str (void) { return rep->convert_to_str (); } |
|
698 |
|
699 tree_constant cumprod (void) { return rep->cumprod (); } |
|
700 tree_constant cumsum (void) { return rep->cumsum (); } |
|
701 tree_constant prod (void) { return rep->prod (); } |
|
702 tree_constant sum (void) { return rep->sum (); } |
|
703 tree_constant sumsq (void) { return rep->sumsq (); } |
|
704 |
|
705 tree_constant diag (void) { return rep->diag (); } |
|
706 tree_constant diag (tree_constant& a) { return rep->diag (a); } |
|
707 |
|
708 void print_if_string (ostream& os, int warn) |
|
709 { rep->print_if_string (os, warn); } |
|
710 |
|
711 tree_constant_rep::constant_type const_type (void) |
|
712 { return rep->const_type (); } |
|
713 |
|
714 tree_constant mapper (Mapper_fcn& m_fcn, int print) |
|
715 { return rep->mapper (m_fcn, print); } |
|
716 |
|
717 void bump_value (tree::expression_type et) |
|
718 { |
|
719 if (rep->count > 1) |
|
720 { |
|
721 --rep->count; |
|
722 rep = new tree_constant_rep (*rep); |
|
723 rep->count = 1; |
|
724 } |
|
725 rep->bump_value (et); |
|
726 } |
|
727 |
|
728 tree_constant eval (int print) |
|
729 { rep->eval (print); return *this; } |
|
730 |
|
731 // A tree constant can have one and only one value to return. |
|
732 tree_constant *eval (int print, int nargout) |
|
733 { |
|
734 rep->eval (print); |
|
735 tree_constant *retval = new tree_constant [2]; |
|
736 retval[0] = *this; |
|
737 return retval; |
|
738 } |
|
739 |
|
740 tree_constant *eval (tree_constant *args, int n_in, int n_out, int print) |
|
741 { return rep->eval (args, n_in, n_out, print); } |
|
742 |
|
743 private: |
|
744 tree_constant_rep *rep; |
|
745 }; |
|
746 |
94
|
747 /* |
|
748 * Here are some extra functions that are related to the tree_constant |
|
749 * class but that don't need to be class members or friends. |
|
750 */ |
|
751 |
97
|
752 extern tree_constant *vector_of_empties (int nargout, char *fcn_name); |
94
|
753 |
1
|
754 #endif |
|
755 |
|
756 /* |
|
757 ;;; Local Variables: *** |
|
758 ;;; mode: C++ *** |
|
759 ;;; page-delimiter: "^/\\*" *** |
|
760 ;;; End: *** |
|
761 */ |