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 |
|
33 #include "builtins.h" |
|
34 #include "tree-base.h" |
164
|
35 #include "Matrix.h" // Needed for some inline functions. |
|
36 #include "Range.h" // Ditto. |
|
37 |
|
38 class idx_vector; |
1
|
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); |
|
254 |
|
255 tree_constant_rep (double base, double limit, double inc); |
161
|
256 tree_constant_rep (const Range& r); |
1
|
257 |
|
258 tree_constant_rep (tree_constant_rep::constant_type t); |
|
259 |
161
|
260 tree_constant_rep (const tree_constant_rep& t); |
1
|
261 |
|
262 ~tree_constant_rep (void); |
|
263 |
|
264 #if defined (MDEBUG) |
|
265 void *operator new (size_t size); |
|
266 void operator delete (void *p, size_t size); |
|
267 #endif |
|
268 |
|
269 void resize (int i, int j); |
|
270 void resize (int i, int j, double val); |
|
271 |
|
272 void maybe_resize (int imax, force_orient fo = no_orient); |
|
273 void maybe_resize (int imax, int jmax); |
|
274 |
164
|
275 int valid_as_scalar_index (void) const; |
1
|
276 |
164
|
277 int is_defined (void) const |
1
|
278 { return type_tag != tree_constant_rep::unknown_constant; } |
|
279 |
164
|
280 int is_undefined (void) const |
1
|
281 { return type_tag == tree_constant_rep::unknown_constant; } |
|
282 |
164
|
283 int is_string_type (void) const |
1
|
284 { return type_tag == tree_constant_rep::string_constant; } |
|
285 |
164
|
286 int is_scalar_type (void) const |
1
|
287 { return type_tag == scalar_constant |
|
288 || type_tag == complex_scalar_constant; } |
|
289 |
164
|
290 int is_matrix_type (void) const |
1
|
291 { return type_tag == matrix_constant |
|
292 || type_tag == complex_matrix_constant; } |
|
293 |
164
|
294 int is_real_type (void) const |
1
|
295 { return type_tag == scalar_constant |
|
296 || type_tag == matrix_constant |
|
297 || type_tag == range_constant; } |
|
298 |
164
|
299 int is_complex_type (void) const |
1
|
300 { return type_tag == complex_matrix_constant |
|
301 || type_tag == complex_scalar_constant; } |
|
302 |
|
303 |
164
|
304 int is_numeric_type (void) const |
1
|
305 { return type_tag == scalar_constant |
|
306 || type_tag == matrix_constant |
|
307 || type_tag == complex_matrix_constant |
|
308 || type_tag == complex_scalar_constant; } |
|
309 |
164
|
310 int is_numeric_or_range_type (void) const |
1
|
311 { return type_tag == scalar_constant |
|
312 || type_tag == matrix_constant |
|
313 || type_tag == complex_matrix_constant |
|
314 || type_tag == complex_scalar_constant |
|
315 || type_tag == range_constant; } |
|
316 |
164
|
317 double to_scalar (void) const; |
|
318 ColumnVector to_vector (void) const; |
|
319 Matrix to_matrix (void) const; |
1
|
320 |
|
321 tree_constant_rep::constant_type force_numeric (int force_str_conv = 0); |
164
|
322 tree_constant make_numeric (int force_str_conv = 0) const; |
1
|
323 |
|
324 friend tree_constant |
|
325 do_binary_op (tree_constant& a, tree_constant& b, tree::expression_type t); |
|
326 |
|
327 friend tree_constant |
|
328 do_unary_op (tree_constant& a, tree::expression_type t); |
|
329 |
|
330 void assign (tree_constant& rhs, tree_constant *args, int nargs); |
|
331 |
|
332 void do_scalar_assignment |
|
333 (tree_constant& rhs, tree_constant *args, int nargin); |
|
334 |
|
335 void do_matrix_assignment |
|
336 (tree_constant& rhs, tree_constant *args, int nargin); |
|
337 |
|
338 void do_matrix_assignment |
|
339 (tree_constant& rhs, tree_constant& i_arg); |
|
340 |
|
341 void do_matrix_assignment |
|
342 (tree_constant& rhs, tree_constant& i_arg, tree_constant& j_arg); |
|
343 |
|
344 void fortran_style_matrix_assignment (tree_constant& rhs, |
|
345 tree_constant& i_arg); |
|
346 |
|
347 void fortran_style_matrix_assignment (tree_constant& rhs, constant_type ci); |
|
348 |
|
349 void fortran_style_matrix_assignment (tree_constant& rhs, idx_vector& i); |
|
350 |
|
351 void vector_assignment (tree_constant& rhs, tree_constant& i_arg); |
|
352 |
164
|
353 void check_vector_assign (int rhs_nr, int rhs_nc, int ilen, |
|
354 const char *rm); |
1
|
355 |
|
356 void do_vector_assign (tree_constant& rhs, int i); |
|
357 void do_vector_assign (tree_constant& rhs, idx_vector& i); |
212
|
358 void do_vector_assign (tree_constant& rhs, Range& i); |
1
|
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 |
212
|
365 (tree_constant& rhs, Range& i, tree_constant& j_arg); |
1
|
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 |
208
|
393 void delete_row (int); |
|
394 void delete_rows (idx_vector& i); |
|
395 void delete_rows (Range& i); |
|
396 |
|
397 void delete_column (int); |
|
398 void delete_columns (idx_vector& j); |
|
399 void delete_columns (Range& j); |
|
400 |
1
|
401 void bump_value (tree::expression_type); |
|
402 |
|
403 void eval (int print); |
|
404 |
164
|
405 tree_constant *eval (const tree_constant *args, int n_in, int n_out, |
|
406 int print); |
1
|
407 |
164
|
408 tree_constant do_scalar_index (const tree_constant *args, |
|
409 int nargin) const; |
1
|
410 |
164
|
411 tree_constant do_matrix_index (const tree_constant *args, int nargin) const; |
|
412 |
|
413 tree_constant do_matrix_index (const tree_constant& i_arg) const; |
1
|
414 |
164
|
415 tree_constant do_matrix_index (const tree_constant& i_arg, |
|
416 const tree_constant& j_arg) const; |
1
|
417 |
164
|
418 tree_constant do_matrix_index (constant_type i) const; |
1
|
419 |
164
|
420 tree_constant fortran_style_matrix_index (const tree_constant& i_arg) const; |
|
421 tree_constant fortran_style_matrix_index (const Matrix& mi) const; |
1
|
422 |
164
|
423 tree_constant do_vector_index (const tree_constant& i_arg) const; |
1
|
424 |
164
|
425 tree_constant do_matrix_index (int i, const tree_constant& i_arg) const; |
|
426 tree_constant do_matrix_index (const idx_vector& i, |
|
427 const tree_constant& i_arg) const; |
212
|
428 tree_constant do_matrix_index (const Range& i, |
164
|
429 const tree_constant& i_arg) const; |
|
430 tree_constant do_matrix_index (constant_type i, |
|
431 const tree_constant& i_arg) const; |
1
|
432 |
164
|
433 tree_constant do_matrix_index (int i, int j) const; |
|
434 tree_constant do_matrix_index (int i, const idx_vector& j) const; |
|
435 tree_constant do_matrix_index (int i, const Range& j) const; |
|
436 tree_constant do_matrix_index (int i, constant_type cj) const; |
1
|
437 |
164
|
438 tree_constant do_matrix_index (const idx_vector& i, int j) const; |
|
439 tree_constant do_matrix_index (const idx_vector& i, |
|
440 const idx_vector& j) const; |
|
441 tree_constant do_matrix_index (const idx_vector& i, const Range& j) const; |
|
442 tree_constant do_matrix_index (const idx_vector& i, constant_type j) const; |
1
|
443 |
164
|
444 tree_constant do_matrix_index (const Range& i, int j) const; |
|
445 tree_constant do_matrix_index (const Range& i, const idx_vector& j) const; |
|
446 tree_constant do_matrix_index (const Range& i, const Range& j) const; |
|
447 tree_constant do_matrix_index (const Range& i, constant_type j) const; |
1
|
448 |
164
|
449 tree_constant do_matrix_index (constant_type i, int j) const; |
|
450 tree_constant do_matrix_index (constant_type i, const idx_vector& j) const; |
|
451 tree_constant do_matrix_index (constant_type i, const Range& j) const; |
|
452 tree_constant do_matrix_index (constant_type i, constant_type j) const; |
1
|
453 |
|
454 int save (ostream& os, int mark_as_global); |
|
455 int save_three_d (ostream& os, int parametric); |
|
456 int load (istream& is); |
|
457 constant_type load (istream& is, constant_type t); |
|
458 |
164
|
459 double double_value (void) const; |
|
460 Matrix matrix_value (void) const; |
|
461 Complex complex_value (void) const; |
|
462 ComplexMatrix complex_matrix_value (void) const; |
|
463 char *string_value (void) const; |
|
464 Range range_value (void) const; |
1
|
465 |
164
|
466 int rows (void) const; |
|
467 int columns (void) const; |
1
|
468 |
164
|
469 tree_constant all (void) const; |
|
470 tree_constant any (void) const; |
|
471 tree_constant isstr (void) const; |
1
|
472 |
|
473 tree_constant convert_to_str (void); |
|
474 |
164
|
475 tree_constant cumprod (void) const; |
|
476 tree_constant cumsum (void) const; |
|
477 tree_constant prod (void) const; |
|
478 tree_constant sum (void) const; |
|
479 tree_constant sumsq (void) const; |
1
|
480 |
164
|
481 tree_constant diag (void) const; |
|
482 tree_constant diag (const tree_constant& a) const; |
1
|
483 |
164
|
484 friend tree_constant fill_matrix (const tree_constant& a, |
|
485 double d, const char *warn_for); |
|
486 friend tree_constant fill_matrix (const tree_constant& a, |
|
487 const tree_constant& b, |
|
488 double d, const char *warn_for); |
1
|
489 |
164
|
490 friend tree_constant identity_matrix (const tree_constant& a); |
|
491 friend tree_constant identity_matrix (const tree_constant& a, |
|
492 const tree_constant& b); |
1
|
493 |
164
|
494 friend tree_constant find_nonzero_elem_idx (const tree_constant& a); |
1
|
495 |
164
|
496 friend tree_constant *matrix_log (const tree_constant& a); |
|
497 friend tree_constant *matrix_sqrt (const tree_constant& a); |
|
498 |
|
499 friend tree_constant *column_max (const tree_constant *args, int nargin, |
1
|
500 int nargout); |
|
501 |
164
|
502 friend tree_constant *column_min (const tree_constant *args, int nargin, |
1
|
503 int nargout); |
|
504 |
164
|
505 friend tree_constant *sort (const tree_constant *args, int nargin, |
|
506 int nargout); |
1
|
507 |
164
|
508 friend tree_constant *feval (const tree_constant *args, int nargin, |
|
509 int nargout); |
1
|
510 |
164
|
511 friend tree_constant eval_string (const tree_constant& arg, int& |
|
512 parse_status); |
1
|
513 |
164
|
514 friend tree_constant get_user_input (const tree_constant *args, |
|
515 int nargin, int nargout, |
|
516 int debug = 0); |
1
|
517 |
164
|
518 constant_type const_type (void) const { return type_tag; } |
1
|
519 |
164
|
520 tree_constant mapper (Mapper_fcn& m_fcn, int print) const; |
1
|
521 |
|
522 private: |
|
523 int count; |
|
524 constant_type type_tag; |
|
525 union |
|
526 { |
|
527 double scalar; // A real scalar constant. |
|
528 Matrix *matrix; // A real matrix constant. |
|
529 Complex *complex_scalar; // A real scalar constant. |
|
530 ComplexMatrix *complex_matrix; // A real matrix constant. |
|
531 char *string; // A character string constant. |
|
532 Range *range; // A set of evenly spaced values. |
|
533 }; |
|
534 }; |
|
535 |
|
536 /* |
|
537 * Constants. Nice -- No need to interpret them anymore. Logically, |
|
538 * this should be ahead of the tree_constant_rep class, but that |
|
539 * causes problems with my version of g++ (~2.2.2)... |
|
540 */ |
|
541 class tree_constant : public tree |
|
542 { |
|
543 friend class tree_constant_rep; |
|
544 |
|
545 public: |
|
546 tree_constant (void) |
|
547 { rep = new tree_constant_rep (); rep->count = 1; } |
|
548 |
|
549 tree_constant (double d) |
|
550 { rep = new tree_constant_rep (d); rep->count = 1; } |
161
|
551 tree_constant (const Matrix& m) |
1
|
552 { rep = new tree_constant_rep (m); rep->count = 1; } |
161
|
553 tree_constant (const DiagMatrix& d) |
1
|
554 { rep = new tree_constant_rep (d); rep->count = 1; } |
161
|
555 tree_constant (const RowVector& v) |
1
|
556 { rep = new tree_constant_rep (v); rep->count = 1; } |
161
|
557 tree_constant (const RowVector& v, int pcv) |
1
|
558 { rep = new tree_constant_rep (v, pcv); rep->count = 1; } |
161
|
559 tree_constant (const ColumnVector& v) |
1
|
560 { rep = new tree_constant_rep (v); rep->count = 1; } |
161
|
561 tree_constant (const ColumnVector& v, int pcv) |
1
|
562 { rep = new tree_constant_rep (v, pcv); rep->count = 1; } |
|
563 |
161
|
564 tree_constant (const Complex& c) |
1
|
565 { rep = new tree_constant_rep (c); rep->count = 1; } |
161
|
566 tree_constant (const ComplexMatrix& m) |
1
|
567 { rep = new tree_constant_rep (m); rep->count = 1; } |
161
|
568 tree_constant (const ComplexDiagMatrix& d) |
1
|
569 { rep = new tree_constant_rep (d); rep->count = 1; } |
161
|
570 tree_constant (const ComplexRowVector& v) |
1
|
571 { rep = new tree_constant_rep (v); rep->count = 1; } |
161
|
572 tree_constant (const ComplexRowVector& v, int pcv) |
1
|
573 { rep = new tree_constant_rep (v, pcv); rep->count = 1; } |
161
|
574 tree_constant (const ComplexColumnVector& v) |
1
|
575 { rep = new tree_constant_rep (v); rep->count = 1; } |
161
|
576 tree_constant (const ComplexColumnVector& v, int pcv) |
1
|
577 { rep = new tree_constant_rep (v, pcv); rep->count = 1; } |
|
578 |
|
579 tree_constant (const char *s) |
|
580 { rep = new tree_constant_rep (s); rep->count = 1; } |
|
581 |
|
582 tree_constant (double base, double limit, double inc) |
|
583 { rep = new tree_constant_rep (base, limit, inc); rep->count = 1; } |
161
|
584 tree_constant (const Range& r) |
1
|
585 { rep = new tree_constant_rep (r); rep->count = 1; } |
|
586 |
|
587 tree_constant (tree_constant_rep::constant_type t) |
|
588 { rep = new tree_constant_rep (t); rep->count = 1; } |
|
589 |
161
|
590 tree_constant (const tree_constant& a) |
1
|
591 { rep = a.rep; rep->count++; } |
|
592 tree_constant (tree_constant_rep& r) |
|
593 { rep = &r; rep->count++; } |
|
594 |
|
595 ~tree_constant (void); |
|
596 |
|
597 #if defined (MDEBUG) |
|
598 void *operator new (size_t size); |
|
599 void operator delete (void *p, size_t size); |
|
600 #endif |
|
601 |
|
602 tree_constant operator = (tree_constant& a) |
|
603 { |
|
604 if (--rep->count <= 0 && rep != a.rep) |
|
605 delete rep; |
|
606 |
|
607 rep = a.rep; |
|
608 rep->count++; |
|
609 return *this; |
|
610 } |
|
611 |
164
|
612 int is_constant (void) const { return 1; } |
1
|
613 |
164
|
614 int is_scalar_type (void) const { return rep->is_scalar_type (); } |
|
615 int is_matrix_type (void) const { return rep->is_matrix_type (); } |
1
|
616 |
164
|
617 int is_real_type (void) const { return rep->is_real_type (); } |
|
618 int is_complex_type (void) const { return rep->is_complex_type (); } |
1
|
619 |
164
|
620 int is_numeric_type (void) const { return rep->is_numeric_type (); } |
1
|
621 |
164
|
622 int is_numeric_or_range_type (void) const |
1
|
623 { return rep->is_numeric_or_range_type (); } |
|
624 |
164
|
625 int is_string_type (void) const { return rep->is_string_type (); } |
1
|
626 |
164
|
627 int valid_as_scalar_index (void) const |
|
628 { return rep->valid_as_scalar_index (); } |
1
|
629 |
164
|
630 int is_defined (void) const { return rep->is_defined (); } |
|
631 int is_undefined (void) const { return rep->is_undefined (); } |
1
|
632 |
164
|
633 double to_scalar (void) const { return rep->to_scalar (); } |
|
634 ColumnVector to_vector (void) const { return rep->to_vector (); } |
|
635 Matrix to_matrix (void) const { return rep->to_matrix (); } |
1
|
636 |
|
637 tree_constant_rep::constant_type force_numeric (int force_str_conv = 0) |
|
638 { return rep->force_numeric (force_str_conv); } |
|
639 |
164
|
640 tree_constant make_numeric (int force_str_conv = 0) const |
1
|
641 { |
|
642 if (is_numeric_type ()) |
|
643 return *this; |
|
644 else |
|
645 return rep->make_numeric (force_str_conv); |
|
646 } |
|
647 |
164
|
648 tree_constant make_numeric_or_range (void) const |
1
|
649 { |
|
650 if (is_numeric_type () |
|
651 || rep->type_tag == tree_constant_rep::range_constant) |
|
652 return *this; |
|
653 else |
|
654 return rep->make_numeric (); |
|
655 } |
|
656 |
164
|
657 tree_constant make_numeric_or_magic (void) const |
1
|
658 { |
|
659 if (is_numeric_type () |
|
660 || rep->type_tag == tree_constant_rep::magic_colon) |
|
661 return *this; |
|
662 else |
|
663 return rep->make_numeric (); |
|
664 } |
|
665 |
164
|
666 tree_constant make_numeric_or_range_or_magic (void) const |
1
|
667 { |
|
668 if (is_numeric_type () |
|
669 || rep->type_tag == tree_constant_rep::magic_colon |
|
670 || rep->type_tag == tree_constant_rep::range_constant) |
|
671 return *this; |
|
672 else |
|
673 return rep->make_numeric (); |
|
674 } |
|
675 |
|
676 tree_constant assign (tree_constant& rhs, tree_constant *args, int nargs) |
|
677 { |
|
678 if (rep->count > 1) |
|
679 { |
|
680 --rep->count; |
|
681 rep = new tree_constant_rep (*rep); |
|
682 rep->count = 1; |
|
683 } |
|
684 rep->assign (rhs, args, nargs); |
|
685 return *this; |
|
686 } |
|
687 |
|
688 int save (ostream& os, int mark_as_global = 0) |
|
689 { return rep->save (os, mark_as_global); } |
|
690 int save_three_d (ostream& os, int parametric = 0) |
|
691 { return rep->save_three_d (os, parametric); } |
|
692 |
|
693 int load (istream& is) { return rep->load (is); } |
|
694 tree_constant_rep::constant_type load |
|
695 (istream& is, tree_constant_rep::constant_type t) |
|
696 { return rep->load (is, t); } |
|
697 |
164
|
698 double double_value (void) const { return rep->double_value (); } |
|
699 Matrix matrix_value (void) const { return rep->matrix_value (); } |
|
700 Complex complex_value (void) const { return rep->complex_value (); } |
|
701 ComplexMatrix complex_matrix_value (void) const |
1
|
702 { return rep->complex_matrix_value (); } |
164
|
703 char *string_value (void) const { return rep->string_value (); } |
|
704 Range range_value (void) const { return rep->range_value (); } |
1
|
705 |
164
|
706 int rows (void) const { return rep->rows (); } |
|
707 int columns (void) const { return rep->columns (); } |
1
|
708 |
208
|
709 int is_empty (void) const |
|
710 { |
|
711 return (rep->type_tag != tree_constant_rep::magic_colon |
|
712 && rep->type_tag != tree_constant_rep::unknown_constant |
|
713 && (rows () == 0 || columns () == 0)); |
|
714 } |
|
715 |
|
716 int is_zero_by_zero (void) const |
212
|
717 { |
|
718 return (rep->type_tag != tree_constant_rep::magic_colon |
|
719 && rep->type_tag != tree_constant_rep::unknown_constant |
|
720 && rows () == 0 |
|
721 && columns () == 0); |
|
722 } |
208
|
723 |
93
|
724 |
164
|
725 tree_constant all (void) const { return rep->all (); } |
|
726 tree_constant any (void) const { return rep->any (); } |
|
727 tree_constant isstr (void) const { return rep->isstr (); } |
1
|
728 |
|
729 tree_constant convert_to_str (void) { return rep->convert_to_str (); } |
|
730 |
164
|
731 tree_constant cumprod (void) const { return rep->cumprod (); } |
|
732 tree_constant cumsum (void) const { return rep->cumsum (); } |
|
733 tree_constant prod (void) const { return rep->prod (); } |
|
734 tree_constant sum (void) const { return rep->sum (); } |
|
735 tree_constant sumsq (void) const { return rep->sumsq (); } |
1
|
736 |
164
|
737 tree_constant diag (void) const { return rep->diag (); } |
|
738 tree_constant diag (const tree_constant& a) const { return rep->diag (a); } |
1
|
739 |
164
|
740 tree_constant_rep::constant_type const_type (void) const |
1
|
741 { return rep->const_type (); } |
|
742 |
164
|
743 tree_constant mapper (Mapper_fcn& m_fcn, int print) const |
1
|
744 { return rep->mapper (m_fcn, print); } |
|
745 |
|
746 void bump_value (tree::expression_type et) |
|
747 { |
|
748 if (rep->count > 1) |
|
749 { |
|
750 --rep->count; |
|
751 rep = new tree_constant_rep (*rep); |
|
752 rep->count = 1; |
|
753 } |
|
754 rep->bump_value (et); |
|
755 } |
|
756 |
|
757 tree_constant eval (int print) |
|
758 { rep->eval (print); return *this; } |
|
759 |
|
760 // A tree constant can have one and only one value to return. |
|
761 tree_constant *eval (int print, int nargout) |
|
762 { |
|
763 rep->eval (print); |
|
764 tree_constant *retval = new tree_constant [2]; |
|
765 retval[0] = *this; |
|
766 return retval; |
|
767 } |
|
768 |
164
|
769 tree_constant *eval (const tree_constant *args, int n_in, int n_out, |
|
770 int print) |
1
|
771 { return rep->eval (args, n_in, n_out, print); } |
|
772 |
|
773 private: |
|
774 tree_constant_rep *rep; |
|
775 }; |
|
776 |
94
|
777 /* |
|
778 * Here are some extra functions that are related to the tree_constant |
|
779 * class but that don't need to be class members or friends. |
|
780 */ |
|
781 |
164
|
782 extern tree_constant *vector_of_empties (int nargout, const char *fcn_name); |
94
|
783 |
1
|
784 #endif |
|
785 |
|
786 /* |
|
787 ;;; Local Variables: *** |
|
788 ;;; mode: C++ *** |
|
789 ;;; page-delimiter: "^/\\*" *** |
|
790 ;;; End: *** |
|
791 */ |