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