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