529
|
1 // tc-rep.cc -*- C++ -*- |
492
|
2 /* |
|
3 |
1009
|
4 Copyright (C) 1992, 1993, 1994, 1995 John W. Eaton |
492
|
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 #ifdef HAVE_CONFIG_H |
|
25 #include "config.h" |
|
26 #endif |
|
27 |
|
28 #include <ctype.h> |
|
29 #include <string.h> |
|
30 #include <fstream.h> |
|
31 #include <iostream.h> |
|
32 #include <strstream.h> |
|
33 |
|
34 #include "mx-base.h" |
|
35 #include "Range.h" |
|
36 |
504
|
37 #include "arith-ops.h" |
492
|
38 #include "variables.h" |
631
|
39 #include "sysdep.h" |
492
|
40 #include "error.h" |
|
41 #include "gripes.h" |
|
42 #include "user-prefs.h" |
|
43 #include "utils.h" |
|
44 #include "pager.h" |
|
45 #include "pr-output.h" |
|
46 #include "tree-const.h" |
|
47 #include "idx-vector.h" |
745
|
48 #include "oct-map.h" |
492
|
49 |
759
|
50 #include "tc-inlines.h" |
492
|
51 |
1168
|
52 // The following three variables could be made static members of the |
|
53 // TC_REP class. |
|
54 |
|
55 // Pointer to the blocks of memory we manage. |
|
56 static TC_REP *newlist; |
|
57 |
|
58 // Multiplier for allocating new blocks. |
|
59 static const int newlist_grow_size = 128; |
|
60 |
|
61 // Pointer to the last element of the last block allocated. |
|
62 static TC_REP *newlist_tail = 0; |
|
63 |
492
|
64 static int |
|
65 any_element_is_complex (const ComplexMatrix& a) |
|
66 { |
|
67 int nr = a.rows (); |
|
68 int nc = a.columns (); |
|
69 for (int j = 0; j < nc; j++) |
|
70 for (int i = 0; i < nr; i++) |
|
71 if (imag (a.elem (i, j)) != 0.0) |
|
72 return 1; |
|
73 return 0; |
|
74 } |
|
75 |
581
|
76 // The real representation of constants. |
|
77 |
631
|
78 TC_REP::tree_constant_rep (void) |
492
|
79 { |
|
80 type_tag = unknown_constant; |
581
|
81 orig_text = 0; |
492
|
82 } |
|
83 |
631
|
84 TC_REP::tree_constant_rep (double d) |
492
|
85 { |
|
86 scalar = d; |
|
87 type_tag = scalar_constant; |
581
|
88 orig_text = 0; |
492
|
89 } |
|
90 |
631
|
91 TC_REP::tree_constant_rep (const Matrix& m) |
492
|
92 { |
|
93 if (m.rows () == 1 && m.columns () == 1) |
|
94 { |
|
95 scalar = m.elem (0, 0); |
|
96 type_tag = scalar_constant; |
|
97 } |
|
98 else |
|
99 { |
|
100 matrix = new Matrix (m); |
|
101 type_tag = matrix_constant; |
|
102 } |
581
|
103 orig_text = 0; |
492
|
104 } |
|
105 |
631
|
106 TC_REP::tree_constant_rep (const DiagMatrix& d) |
492
|
107 { |
|
108 if (d.rows () == 1 && d.columns () == 1) |
|
109 { |
|
110 scalar = d.elem (0, 0); |
|
111 type_tag = scalar_constant; |
|
112 } |
|
113 else |
|
114 { |
|
115 matrix = new Matrix (d); |
|
116 type_tag = matrix_constant; |
|
117 } |
581
|
118 orig_text = 0; |
492
|
119 } |
|
120 |
631
|
121 TC_REP::tree_constant_rep (const RowVector& v, int prefer_column_vector) |
492
|
122 { |
|
123 int len = v.capacity (); |
|
124 if (len == 1) |
|
125 { |
|
126 scalar = v.elem (0); |
|
127 type_tag = scalar_constant; |
|
128 } |
|
129 else |
|
130 { |
|
131 int pcv = (prefer_column_vector < 0) |
|
132 ? user_pref.prefer_column_vectors |
|
133 : prefer_column_vector; |
|
134 |
|
135 if (pcv) |
|
136 { |
|
137 Matrix m (len, 1); |
|
138 for (int i = 0; i < len; i++) |
|
139 m.elem (i, 0) = v.elem (i); |
|
140 matrix = new Matrix (m); |
|
141 type_tag = matrix_constant; |
|
142 } |
|
143 else |
|
144 { |
|
145 Matrix m (1, len); |
|
146 for (int i = 0; i < len; i++) |
|
147 m.elem (0, i) = v.elem (i); |
|
148 matrix = new Matrix (m); |
|
149 type_tag = matrix_constant; |
|
150 } |
|
151 } |
581
|
152 orig_text = 0; |
492
|
153 } |
|
154 |
631
|
155 TC_REP::tree_constant_rep (const ColumnVector& v, int prefer_column_vector) |
492
|
156 { |
|
157 int len = v.capacity (); |
|
158 if (len == 1) |
|
159 { |
|
160 scalar = v.elem (0); |
|
161 type_tag = scalar_constant; |
|
162 } |
|
163 else |
|
164 { |
|
165 int pcv = (prefer_column_vector < 0) |
|
166 ? user_pref.prefer_column_vectors |
|
167 : prefer_column_vector; |
|
168 |
|
169 if (pcv) |
|
170 { |
|
171 Matrix m (len, 1); |
|
172 for (int i = 0; i < len; i++) |
|
173 m.elem (i, 0) = v.elem (i); |
|
174 matrix = new Matrix (m); |
|
175 type_tag = matrix_constant; |
|
176 } |
|
177 else |
|
178 { |
|
179 Matrix m (1, len); |
|
180 for (int i = 0; i < len; i++) |
|
181 m.elem (0, i) = v.elem (i); |
|
182 matrix = new Matrix (m); |
|
183 type_tag = matrix_constant; |
|
184 } |
|
185 } |
581
|
186 orig_text = 0; |
492
|
187 } |
|
188 |
631
|
189 TC_REP::tree_constant_rep (const Complex& c) |
492
|
190 { |
|
191 complex_scalar = new Complex (c); |
|
192 type_tag = complex_scalar_constant; |
581
|
193 orig_text = 0; |
492
|
194 } |
|
195 |
631
|
196 TC_REP::tree_constant_rep (const ComplexMatrix& m) |
492
|
197 { |
|
198 if (m.rows () == 1 && m.columns () == 1) |
|
199 { |
|
200 complex_scalar = new Complex (m.elem (0, 0)); |
|
201 type_tag = complex_scalar_constant; |
|
202 } |
|
203 else |
|
204 { |
|
205 complex_matrix = new ComplexMatrix (m); |
|
206 type_tag = complex_matrix_constant; |
|
207 } |
581
|
208 orig_text = 0; |
492
|
209 } |
|
210 |
631
|
211 TC_REP::tree_constant_rep (const ComplexDiagMatrix& d) |
492
|
212 { |
|
213 if (d.rows () == 1 && d.columns () == 1) |
|
214 { |
|
215 complex_scalar = new Complex (d.elem (0, 0)); |
|
216 type_tag = complex_scalar_constant; |
|
217 } |
|
218 else |
|
219 { |
|
220 complex_matrix = new ComplexMatrix (d); |
|
221 type_tag = complex_matrix_constant; |
|
222 } |
581
|
223 orig_text = 0; |
492
|
224 } |
|
225 |
631
|
226 TC_REP::tree_constant_rep (const ComplexRowVector& v, |
|
227 int prefer_column_vector) |
492
|
228 { |
|
229 int len = v.capacity (); |
|
230 if (len == 1) |
|
231 { |
|
232 complex_scalar = new Complex (v.elem (0)); |
|
233 type_tag = complex_scalar_constant; |
|
234 } |
|
235 else |
|
236 { |
|
237 int pcv = (prefer_column_vector < 0) |
|
238 ? user_pref.prefer_column_vectors |
|
239 : prefer_column_vector; |
|
240 |
|
241 if (pcv) |
|
242 { |
|
243 ComplexMatrix m (len, 1); |
|
244 for (int i = 0; i < len; i++) |
|
245 m.elem (i, 0) = v.elem (i); |
|
246 complex_matrix = new ComplexMatrix (m); |
|
247 type_tag = complex_matrix_constant; |
|
248 } |
|
249 else |
|
250 { |
|
251 ComplexMatrix m (1, len); |
|
252 for (int i = 0; i < len; i++) |
|
253 m.elem (0, i) = v.elem (i); |
|
254 complex_matrix = new ComplexMatrix (m); |
|
255 type_tag = complex_matrix_constant; |
|
256 } |
|
257 } |
581
|
258 orig_text = 0; |
492
|
259 } |
|
260 |
631
|
261 TC_REP::tree_constant_rep (const ComplexColumnVector& v, int |
|
262 prefer_column_vector) |
492
|
263 { |
|
264 int len = v.capacity (); |
|
265 if (len == 1) |
|
266 { |
|
267 complex_scalar = new Complex (v.elem (0)); |
|
268 type_tag = complex_scalar_constant; |
|
269 } |
|
270 else |
|
271 { |
|
272 int pcv = (prefer_column_vector < 0) |
|
273 ? user_pref.prefer_column_vectors |
|
274 : prefer_column_vector; |
|
275 |
|
276 if (pcv) |
|
277 { |
|
278 ComplexMatrix m (len, 1); |
|
279 for (int i = 0; i < len; i++) |
|
280 m.elem (i, 0) = v.elem (i); |
|
281 complex_matrix = new ComplexMatrix (m); |
|
282 type_tag = complex_matrix_constant; |
|
283 } |
|
284 else |
|
285 { |
|
286 ComplexMatrix m (1, len); |
|
287 for (int i = 0; i < len; i++) |
|
288 m.elem (0, i) = v.elem (i); |
|
289 complex_matrix = new ComplexMatrix (m); |
|
290 type_tag = complex_matrix_constant; |
|
291 } |
|
292 } |
581
|
293 orig_text = 0; |
492
|
294 } |
|
295 |
631
|
296 TC_REP::tree_constant_rep (const char *s) |
492
|
297 { |
|
298 string = strsave (s); |
|
299 type_tag = string_constant; |
581
|
300 orig_text = 0; |
492
|
301 } |
|
302 |
631
|
303 TC_REP::tree_constant_rep (double b, double l, double i) |
492
|
304 { |
|
305 range = new Range (b, l, i); |
|
306 int nel = range->nelem (); |
777
|
307 if (nel > 1) |
492
|
308 type_tag = range_constant; |
|
309 else |
|
310 { |
|
311 delete range; |
|
312 if (nel == 1) |
|
313 { |
|
314 scalar = b; |
|
315 type_tag = scalar_constant; |
|
316 } |
|
317 else if (nel == 0) |
|
318 { |
|
319 matrix = new Matrix (); |
|
320 type_tag = matrix_constant; |
|
321 } |
|
322 else |
777
|
323 { |
|
324 type_tag = unknown_constant; |
|
325 if (nel == -1) |
|
326 ::error ("number of elements in range exceeds INT_MAX"); |
|
327 else |
|
328 ::error ("invalid range"); |
|
329 } |
492
|
330 } |
581
|
331 orig_text = 0; |
492
|
332 } |
|
333 |
631
|
334 TC_REP::tree_constant_rep (const Range& r) |
492
|
335 { |
777
|
336 int nel = r.nelem (); |
|
337 if (nel > 1) |
492
|
338 { |
|
339 range = new Range (r); |
|
340 type_tag = range_constant; |
|
341 } |
777
|
342 else if (nel == 1) |
492
|
343 { |
|
344 scalar = r.base (); |
|
345 type_tag = scalar_constant; |
|
346 } |
777
|
347 else if (nel == 0) |
492
|
348 { |
|
349 matrix = new Matrix (); |
|
350 type_tag = matrix_constant; |
|
351 } |
|
352 else |
777
|
353 { |
|
354 type_tag = unknown_constant; |
|
355 if (nel == -1) |
|
356 ::error ("number of elements in range exceeds INT_MAX"); |
|
357 else |
|
358 ::error ("invalid range"); |
|
359 } |
581
|
360 |
|
361 orig_text = 0; |
492
|
362 } |
|
363 |
745
|
364 TC_REP::tree_constant_rep (const Octave_map& m) |
|
365 { |
|
366 a_map = new Octave_map (m); |
|
367 type_tag = map_constant; |
|
368 orig_text = 0; |
|
369 } |
|
370 |
631
|
371 TC_REP::tree_constant_rep (TC_REP::constant_type t) |
492
|
372 { |
922
|
373 assert (t == magic_colon || t == all_va_args); |
|
374 type_tag = t; |
581
|
375 orig_text = 0; |
492
|
376 } |
|
377 |
631
|
378 TC_REP::tree_constant_rep (const tree_constant_rep& t) |
492
|
379 { |
|
380 type_tag = t.type_tag; |
|
381 |
|
382 switch (t.type_tag) |
|
383 { |
|
384 case unknown_constant: |
|
385 break; |
631
|
386 |
492
|
387 case scalar_constant: |
|
388 scalar = t.scalar; |
|
389 break; |
631
|
390 |
492
|
391 case matrix_constant: |
|
392 matrix = new Matrix (*(t.matrix)); |
|
393 break; |
631
|
394 |
492
|
395 case string_constant: |
|
396 string = strsave (t.string); |
|
397 break; |
631
|
398 |
492
|
399 case complex_matrix_constant: |
|
400 complex_matrix = new ComplexMatrix (*(t.complex_matrix)); |
|
401 break; |
631
|
402 |
492
|
403 case complex_scalar_constant: |
|
404 complex_scalar = new Complex (*(t.complex_scalar)); |
|
405 break; |
631
|
406 |
492
|
407 case range_constant: |
|
408 range = new Range (*(t.range)); |
|
409 break; |
631
|
410 |
745
|
411 case map_constant: |
|
412 a_map = new Octave_map (*(t.a_map)); |
|
413 break; |
|
414 |
492
|
415 case magic_colon: |
922
|
416 case all_va_args: |
492
|
417 break; |
|
418 } |
581
|
419 |
|
420 orig_text = strsave (t.orig_text); |
492
|
421 } |
|
422 |
631
|
423 TC_REP::~tree_constant_rep (void) |
492
|
424 { |
|
425 switch (type_tag) |
|
426 { |
|
427 case matrix_constant: |
|
428 delete matrix; |
|
429 break; |
631
|
430 |
492
|
431 case complex_scalar_constant: |
|
432 delete complex_scalar; |
|
433 break; |
631
|
434 |
492
|
435 case complex_matrix_constant: |
|
436 delete complex_matrix; |
|
437 break; |
631
|
438 |
492
|
439 case string_constant: |
|
440 delete [] string; |
|
441 break; |
631
|
442 |
492
|
443 case range_constant: |
|
444 delete range; |
|
445 break; |
631
|
446 |
745
|
447 case map_constant: |
|
448 delete a_map; |
|
449 break; |
|
450 |
777
|
451 case unknown_constant: |
|
452 case scalar_constant: |
|
453 case magic_colon: |
922
|
454 case all_va_args: |
492
|
455 break; |
|
456 } |
581
|
457 |
|
458 delete [] orig_text; |
492
|
459 } |
|
460 |
|
461 void * |
631
|
462 TC_REP::operator new (size_t size) |
492
|
463 { |
1168
|
464 assert (size == sizeof (TC_REP)); |
|
465 |
|
466 if (! newlist) |
|
467 { |
|
468 int block_size = newlist_grow_size * sizeof (TC_REP); |
|
469 newlist = (TC_REP *) new char [block_size]; |
|
470 |
|
471 for (int i = 0; i < newlist_grow_size - 1; i++) |
|
472 newlist[i].freeptr = &newlist[i+1]; |
|
473 |
|
474 newlist[i].freeptr = 0; |
|
475 |
|
476 if (newlist_tail) |
|
477 newlist_tail->freeptr = newlist; |
|
478 |
|
479 newlist_tail = &newlist[i]; |
|
480 } |
|
481 |
|
482 TC_REP *tmp = newlist; |
|
483 newlist = newlist->freeptr; |
|
484 return tmp; |
492
|
485 } |
|
486 |
|
487 void |
631
|
488 TC_REP::operator delete (void *p, size_t size) |
492
|
489 { |
1168
|
490 TC_REP *tmp = (TC_REP *) p; |
|
491 tmp->freeptr = newlist; |
|
492 newlist = tmp; |
492
|
493 } |
|
494 |
631
|
495 int |
|
496 TC_REP::rows (void) const |
492
|
497 { |
631
|
498 int retval = -1; |
581
|
499 |
|
500 switch (type_tag) |
|
501 { |
|
502 case scalar_constant: |
|
503 case complex_scalar_constant: |
631
|
504 retval = 1; |
|
505 break; |
|
506 |
581
|
507 case string_constant: |
|
508 case range_constant: |
631
|
509 retval = (columns () > 0); |
|
510 break; |
|
511 |
|
512 case matrix_constant: |
|
513 retval = matrix->rows (); |
|
514 break; |
|
515 |
|
516 case complex_matrix_constant: |
|
517 retval = complex_matrix->rows (); |
|
518 break; |
|
519 |
777
|
520 default: |
631
|
521 break; |
492
|
522 } |
|
523 |
|
524 return retval; |
|
525 } |
|
526 |
|
527 int |
631
|
528 TC_REP::columns (void) const |
492
|
529 { |
|
530 int retval = -1; |
631
|
531 |
492
|
532 switch (type_tag) |
|
533 { |
|
534 case scalar_constant: |
|
535 case complex_scalar_constant: |
|
536 retval = 1; |
|
537 break; |
631
|
538 |
492
|
539 case matrix_constant: |
631
|
540 retval = matrix->columns (); |
|
541 break; |
|
542 |
492
|
543 case complex_matrix_constant: |
631
|
544 retval = complex_matrix->columns (); |
|
545 break; |
|
546 |
|
547 case string_constant: |
|
548 retval = strlen (string); |
|
549 break; |
|
550 |
|
551 case range_constant: |
|
552 retval = range->nelem (); |
|
553 break; |
|
554 |
777
|
555 default: |
492
|
556 break; |
|
557 } |
631
|
558 |
492
|
559 return retval; |
|
560 } |
|
561 |
|
562 tree_constant |
631
|
563 TC_REP::all (void) const |
492
|
564 { |
777
|
565 tree_constant retval; |
|
566 |
|
567 if (error_state) |
|
568 return retval; |
|
569 |
|
570 if (! is_numeric_type ()) |
492
|
571 { |
|
572 tree_constant tmp = make_numeric (); |
915
|
573 |
|
574 if (error_state) |
|
575 return retval; |
|
576 |
492
|
577 return tmp.all (); |
|
578 } |
|
579 |
|
580 switch (type_tag) |
|
581 { |
|
582 case scalar_constant: |
|
583 { |
|
584 double status = (scalar != 0.0); |
|
585 retval = tree_constant (status); |
|
586 } |
|
587 break; |
631
|
588 |
492
|
589 case matrix_constant: |
|
590 { |
|
591 Matrix m = matrix->all (); |
|
592 retval = tree_constant (m); |
|
593 } |
|
594 break; |
631
|
595 |
492
|
596 case complex_scalar_constant: |
|
597 { |
|
598 double status = (*complex_scalar != 0.0); |
|
599 retval = tree_constant (status); |
|
600 } |
|
601 break; |
631
|
602 |
492
|
603 case complex_matrix_constant: |
|
604 { |
|
605 Matrix m = complex_matrix->all (); |
|
606 retval = tree_constant (m); |
|
607 } |
|
608 break; |
631
|
609 |
492
|
610 default: |
777
|
611 gripe_wrong_type_arg ("all", *this); |
492
|
612 break; |
|
613 } |
631
|
614 |
492
|
615 return retval; |
|
616 } |
|
617 |
|
618 tree_constant |
631
|
619 TC_REP::any (void) const |
492
|
620 { |
777
|
621 tree_constant retval; |
|
622 |
|
623 if (error_state) |
|
624 return retval; |
|
625 |
|
626 if (! is_numeric_type ()) |
492
|
627 { |
|
628 tree_constant tmp = make_numeric (); |
915
|
629 |
|
630 if (error_state) |
|
631 return retval; |
|
632 |
492
|
633 return tmp.any (); |
|
634 } |
|
635 |
|
636 switch (type_tag) |
|
637 { |
|
638 case scalar_constant: |
|
639 { |
|
640 double status = (scalar != 0.0); |
|
641 retval = tree_constant (status); |
|
642 } |
|
643 break; |
631
|
644 |
492
|
645 case matrix_constant: |
|
646 { |
|
647 Matrix m = matrix->any (); |
|
648 retval = tree_constant (m); |
|
649 } |
|
650 break; |
631
|
651 |
492
|
652 case complex_scalar_constant: |
|
653 { |
|
654 double status = (*complex_scalar != 0.0); |
|
655 retval = tree_constant (status); |
|
656 } |
|
657 break; |
631
|
658 |
492
|
659 case complex_matrix_constant: |
|
660 { |
|
661 Matrix m = complex_matrix->any (); |
|
662 retval = tree_constant (m); |
|
663 } |
|
664 break; |
631
|
665 |
|
666 default: |
777
|
667 gripe_wrong_type_arg ("any", *this); |
631
|
668 break; |
|
669 } |
|
670 |
|
671 return retval; |
|
672 } |
|
673 |
|
674 int |
|
675 TC_REP::valid_as_scalar_index (void) const |
|
676 { |
|
677 return (type_tag == magic_colon |
1086
|
678 || (type_tag == scalar_constant |
|
679 && ! xisnan (scalar) |
|
680 && NINT (scalar) == 1) |
631
|
681 || (type_tag == range_constant |
1086
|
682 && range->nelem () == 1 |
|
683 && ! xisnan (range->base ()) |
|
684 && NINT (range->base ()) == 1)); |
631
|
685 } |
|
686 |
|
687 int |
1041
|
688 TC_REP::valid_as_zero_index (void) const |
|
689 { |
1086
|
690 return ((type_tag == scalar_constant |
|
691 && ! xisnan (scalar) |
|
692 && NINT (scalar) == 0) |
1041
|
693 || (type_tag == matrix_constant |
1086
|
694 && matrix->rows () == 0 |
|
695 && matrix->columns () == 0) |
1041
|
696 || (type_tag == range_constant |
1086
|
697 && range->nelem () == 1 |
|
698 && ! xisnan (range->base ()) |
|
699 && NINT (range->base ()) == 0)); |
1041
|
700 } |
|
701 |
|
702 int |
631
|
703 TC_REP::is_true (void) const |
|
704 { |
777
|
705 int retval = 0; |
|
706 |
|
707 if (error_state) |
|
708 return retval; |
|
709 |
|
710 if (! is_numeric_type ()) |
631
|
711 { |
|
712 tree_constant tmp = make_numeric (); |
915
|
713 |
|
714 if (error_state) |
|
715 return retval; |
|
716 |
631
|
717 return tmp.is_true (); |
|
718 } |
|
719 |
|
720 switch (type_tag) |
|
721 { |
|
722 case scalar_constant: |
|
723 retval = (scalar != 0.0); |
|
724 break; |
|
725 |
|
726 case matrix_constant: |
|
727 { |
|
728 Matrix m = (matrix->all ()) . all (); |
|
729 retval = (m.rows () == 1 |
|
730 && m.columns () == 1 |
|
731 && m.elem (0, 0) != 0.0); |
|
732 } |
|
733 break; |
|
734 |
|
735 case complex_scalar_constant: |
|
736 retval = (*complex_scalar != 0.0); |
|
737 break; |
|
738 |
|
739 case complex_matrix_constant: |
|
740 { |
|
741 Matrix m = (complex_matrix->all ()) . all (); |
|
742 retval = (m.rows () == 1 |
|
743 && m.columns () == 1 |
|
744 && m.elem (0, 0) != 0.0); |
|
745 } |
|
746 break; |
|
747 |
492
|
748 default: |
777
|
749 gripe_wrong_type_arg (0, *this); |
492
|
750 break; |
|
751 } |
631
|
752 |
|
753 return retval; |
|
754 } |
|
755 |
|
756 static void |
|
757 warn_implicit_conversion (const char *from, const char *to) |
|
758 { |
|
759 warning ("implicit conversion from %s to %s", from, to); |
|
760 } |
|
761 |
|
762 double |
|
763 TC_REP::double_value (int force_string_conversion) const |
|
764 { |
|
765 double retval = octave_NaN; |
|
766 |
|
767 switch (type_tag) |
|
768 { |
|
769 case scalar_constant: |
|
770 retval = scalar; |
|
771 break; |
|
772 |
|
773 case matrix_constant: |
|
774 { |
|
775 if (user_pref.do_fortran_indexing && rows () > 0 && columns () > 0) |
|
776 retval = matrix->elem (0, 0); |
|
777 else |
|
778 gripe_invalid_conversion ("real matrix", "real scalar"); |
|
779 } |
|
780 break; |
|
781 |
|
782 case complex_matrix_constant: |
|
783 case complex_scalar_constant: |
|
784 { |
|
785 int flag = user_pref.ok_to_lose_imaginary_part; |
|
786 |
|
787 if (flag < 0) |
|
788 warn_implicit_conversion ("complex scalar", "real scalar"); |
|
789 |
|
790 if (flag) |
|
791 { |
|
792 if (type_tag == complex_scalar_constant) |
|
793 retval = ::real (*complex_scalar); |
|
794 else if (type_tag == complex_matrix_constant) |
|
795 { |
|
796 if (user_pref.do_fortran_indexing |
|
797 && rows () > 0 && columns () > 0) |
|
798 retval = ::real (complex_matrix->elem (0, 0)); |
|
799 else |
|
800 gripe_invalid_conversion ("complex matrix", "real scalar"); |
|
801 } |
|
802 else |
|
803 panic_impossible (); |
|
804 } |
|
805 else |
|
806 gripe_invalid_conversion ("complex scalar", "real scalar"); |
|
807 } |
|
808 break; |
|
809 |
|
810 case string_constant: |
|
811 { |
|
812 int flag = force_string_conversion; |
|
813 if (! flag) |
|
814 flag = user_pref.implicit_str_to_num_ok; |
|
815 |
|
816 if (flag < 0) |
|
817 warn_implicit_conversion ("string", "real scalar"); |
|
818 |
|
819 int len = strlen (string); |
|
820 if (flag && (len == 1 || (len > 1 && user_pref.do_fortran_indexing))) |
|
821 retval = toascii ((int) string[0]); |
|
822 else |
|
823 gripe_invalid_conversion ("string", "real scalar"); |
|
824 } |
|
825 break; |
|
826 |
|
827 case range_constant: |
|
828 { |
|
829 int nel = range->nelem (); |
|
830 if (nel == 1 || (nel > 1 && user_pref.do_fortran_indexing)) |
|
831 retval = range->base (); |
|
832 else |
|
833 gripe_invalid_conversion ("range", "real scalar"); |
|
834 } |
|
835 break; |
|
836 |
|
837 default: |
|
838 gripe_invalid_conversion (type_as_string (), "real scalar"); |
|
839 break; |
|
840 } |
|
841 |
|
842 return retval; |
|
843 } |
|
844 |
|
845 Matrix |
|
846 TC_REP::matrix_value (int force_string_conversion) const |
|
847 { |
|
848 Matrix retval; |
|
849 |
|
850 switch (type_tag) |
|
851 { |
|
852 case scalar_constant: |
|
853 retval = Matrix (1, 1, scalar); |
|
854 break; |
|
855 |
|
856 case matrix_constant: |
|
857 retval = *matrix; |
|
858 break; |
|
859 |
|
860 case complex_scalar_constant: |
|
861 case complex_matrix_constant: |
|
862 { |
|
863 int flag = user_pref.ok_to_lose_imaginary_part; |
|
864 if (flag < 0) |
|
865 warn_implicit_conversion ("complex matrix", "real matrix"); |
|
866 |
|
867 if (flag) |
|
868 { |
|
869 if (type_tag == complex_scalar_constant) |
|
870 retval = Matrix (1, 1, ::real (*complex_scalar)); |
|
871 else if (type_tag == complex_matrix_constant) |
|
872 retval = ::real (*complex_matrix); |
|
873 else |
|
874 panic_impossible (); |
|
875 } |
|
876 else |
|
877 gripe_invalid_conversion ("complex matrix", "real matrix"); |
|
878 } |
|
879 break; |
|
880 |
|
881 case string_constant: |
|
882 { |
|
883 int flag = force_string_conversion; |
|
884 if (! flag) |
|
885 flag = user_pref.implicit_str_to_num_ok; |
|
886 |
|
887 if (flag < 0) |
|
888 warn_implicit_conversion ("string", "real matrix"); |
|
889 |
|
890 if (flag) |
|
891 { |
|
892 int len = strlen (string); |
|
893 |
983
|
894 if (len > 0) |
|
895 { |
|
896 retval.resize (1, len); |
631
|
897 |
|
898 for (int i = 0; i < len; i++) |
|
899 retval.elem (0, i) = toascii ((int) string[i]); |
|
900 } |
|
901 else |
983
|
902 retval = Matrix (); |
631
|
903 } |
|
904 else |
|
905 gripe_invalid_conversion ("string", "real matrix"); |
|
906 } |
|
907 break; |
|
908 |
|
909 case range_constant: |
|
910 retval = range->matrix_value (); |
|
911 break; |
|
912 |
|
913 default: |
|
914 gripe_invalid_conversion (type_as_string (), "real matrix"); |
|
915 break; |
|
916 } |
|
917 |
|
918 return retval; |
|
919 } |
|
920 |
|
921 Complex |
|
922 TC_REP::complex_value (int force_string_conversion) const |
|
923 { |
636
|
924 Complex retval (octave_NaN, octave_NaN); |
631
|
925 |
|
926 switch (type_tag) |
|
927 { |
|
928 case complex_scalar_constant: |
|
929 retval = *complex_scalar; |
|
930 break; |
|
931 |
|
932 case scalar_constant: |
|
933 retval = scalar; |
|
934 break; |
|
935 |
|
936 case complex_matrix_constant: |
|
937 case matrix_constant: |
|
938 { |
|
939 if (user_pref.do_fortran_indexing && rows () > 0 && columns () > 0) |
|
940 { |
|
941 if (type_tag == complex_matrix_constant) |
|
942 retval = complex_matrix->elem (0, 0); |
|
943 else |
|
944 retval = matrix->elem (0, 0); |
|
945 } |
|
946 else |
|
947 gripe_invalid_conversion ("real matrix", "real scalar"); |
|
948 } |
|
949 break; |
|
950 |
|
951 case string_constant: |
|
952 { |
|
953 int flag = force_string_conversion; |
|
954 if (! flag) |
|
955 flag = user_pref.implicit_str_to_num_ok; |
|
956 |
|
957 if (flag < 0) |
|
958 warn_implicit_conversion ("string", "complex scalar"); |
|
959 |
|
960 int len = strlen (string); |
|
961 if (flag && (len == 1 || (len > 1 && user_pref.do_fortran_indexing))) |
|
962 retval = toascii ((int) string[0]); |
|
963 else |
|
964 gripe_invalid_conversion ("string", "complex scalar"); |
|
965 } |
|
966 break; |
|
967 |
|
968 case range_constant: |
|
969 { |
|
970 int nel = range->nelem (); |
|
971 if (nel == 1 || (nel > 1 && user_pref.do_fortran_indexing)) |
|
972 retval = range->base (); |
|
973 else |
|
974 gripe_invalid_conversion ("range", "complex scalar"); |
|
975 } |
|
976 break; |
|
977 |
|
978 default: |
|
979 gripe_invalid_conversion (type_as_string (), "complex scalar"); |
|
980 break; |
|
981 } |
|
982 |
|
983 return retval; |
|
984 } |
|
985 |
|
986 ComplexMatrix |
|
987 TC_REP::complex_matrix_value (int force_string_conversion) const |
|
988 { |
|
989 ComplexMatrix retval; |
|
990 |
|
991 switch (type_tag) |
|
992 { |
|
993 case scalar_constant: |
|
994 retval = ComplexMatrix (1, 1, Complex (scalar)); |
|
995 break; |
|
996 |
|
997 case complex_scalar_constant: |
|
998 retval = ComplexMatrix (1, 1, *complex_scalar); |
|
999 break; |
|
1000 |
|
1001 case matrix_constant: |
|
1002 retval = ComplexMatrix (*matrix); |
|
1003 break; |
|
1004 |
|
1005 case complex_matrix_constant: |
|
1006 retval = *complex_matrix; |
|
1007 break; |
|
1008 |
|
1009 case string_constant: |
|
1010 { |
|
1011 int flag = force_string_conversion; |
|
1012 if (! flag) |
|
1013 flag = user_pref.implicit_str_to_num_ok; |
|
1014 |
|
1015 if (flag < 0) |
|
1016 warn_implicit_conversion ("string", "complex matrix"); |
|
1017 |
|
1018 if (flag) |
|
1019 { |
|
1020 int len = strlen (string); |
|
1021 |
|
1022 retval.resize (1, len); |
|
1023 |
|
1024 if (len > 1) |
|
1025 { |
|
1026 for (int i = 0; i < len; i++) |
|
1027 retval.elem (0, i) = toascii ((int) string[i]); |
|
1028 } |
|
1029 else if (len == 1) |
|
1030 retval.elem (0, 0) = toascii ((int) string[0]); |
|
1031 else |
|
1032 panic_impossible (); |
|
1033 } |
|
1034 else |
|
1035 gripe_invalid_conversion ("string", "real matrix"); |
|
1036 } |
|
1037 break; |
|
1038 |
|
1039 case range_constant: |
|
1040 retval = range->matrix_value (); |
|
1041 break; |
|
1042 |
|
1043 default: |
|
1044 gripe_invalid_conversion (type_as_string (), "complex matrix"); |
|
1045 break; |
|
1046 } |
|
1047 |
|
1048 return retval; |
|
1049 } |
|
1050 |
|
1051 char * |
|
1052 TC_REP::string_value (void) const |
|
1053 { |
636
|
1054 if (type_tag == string_constant) |
|
1055 return string; |
|
1056 else |
|
1057 { |
|
1058 gripe_invalid_conversion (type_as_string (), "string"); |
|
1059 return 0; |
|
1060 } |
631
|
1061 } |
|
1062 |
|
1063 Range |
|
1064 TC_REP::range_value (void) const |
|
1065 { |
|
1066 assert (type_tag == range_constant); |
|
1067 return *range; |
|
1068 } |
|
1069 |
745
|
1070 Octave_map |
|
1071 TC_REP::map_value (void) const |
|
1072 { |
|
1073 assert (type_tag == map_constant); |
|
1074 return *a_map; |
|
1075 } |
|
1076 |
|
1077 tree_constant& |
|
1078 TC_REP::lookup_map_element (const char *name, int insert) |
|
1079 { |
|
1080 static tree_constant retval; |
|
1081 |
|
1082 if (type_tag == map_constant) |
|
1083 { |
|
1084 Pix idx = a_map->seek (name); |
|
1085 |
|
1086 if (idx) |
|
1087 return a_map->contents (idx); |
|
1088 else if (insert) |
|
1089 return (*a_map) [name]; |
|
1090 else |
|
1091 error ("structure has no member `%s'", name); |
|
1092 } |
|
1093 else |
|
1094 error ("invalid structure access attempted"); |
|
1095 |
|
1096 return retval; |
|
1097 } |
|
1098 |
631
|
1099 // This could be made more efficient by doing all the work here rather |
|
1100 // than relying on matrix_value() to do any possible type conversions. |
|
1101 |
|
1102 ColumnVector |
|
1103 TC_REP::vector_value (int force_string_conversion, |
|
1104 int force_vector_conversion) const |
|
1105 { |
|
1106 ColumnVector retval; |
|
1107 |
|
1108 Matrix m = matrix_value (force_string_conversion); |
|
1109 |
|
1110 if (error_state) |
|
1111 return retval; |
|
1112 |
|
1113 int nr = m.rows (); |
|
1114 int nc = m.columns (); |
|
1115 if (nr == 1) |
|
1116 { |
|
1117 retval.resize (nc); |
|
1118 for (int i = 0; i < nc; i++) |
|
1119 retval.elem (i) = m (0, i); |
|
1120 } |
|
1121 else if (nc == 1) |
|
1122 { |
|
1123 retval.resize (nr); |
|
1124 for (int i = 0; i < nr; i++) |
|
1125 retval.elem (i) = m.elem (i, 0); |
|
1126 } |
|
1127 else if (nr > 0 && nc > 0 |
|
1128 && (user_pref.do_fortran_indexing || force_vector_conversion)) |
|
1129 { |
|
1130 retval.resize (nr * nc); |
|
1131 int k = 0; |
|
1132 for (int j = 0; j < nc; j++) |
|
1133 for (int i = 0; i < nr; i++) |
|
1134 retval.elem (k++) = m.elem (i, j); |
|
1135 } |
|
1136 else |
|
1137 gripe_invalid_conversion ("real matrix", "real vector"); |
|
1138 |
|
1139 return retval; |
|
1140 } |
|
1141 |
|
1142 // This could be made more efficient by doing all the work here rather |
|
1143 // than relying on complex_matrix_value() to do any possible type |
|
1144 // conversions. |
|
1145 |
|
1146 ComplexColumnVector |
|
1147 TC_REP::complex_vector_value (int force_string_conversion, |
|
1148 int force_vector_conversion) const |
|
1149 { |
|
1150 ComplexColumnVector retval; |
|
1151 |
|
1152 ComplexMatrix m = complex_matrix_value (force_string_conversion); |
|
1153 |
|
1154 if (error_state) |
|
1155 return retval; |
|
1156 |
|
1157 int nr = m.rows (); |
|
1158 int nc = m.columns (); |
|
1159 if (nr == 1) |
|
1160 { |
|
1161 retval.resize (nc); |
|
1162 for (int i = 0; i < nc; i++) |
|
1163 retval.elem (i) = m (0, i); |
|
1164 } |
|
1165 else if (nc == 1) |
|
1166 { |
|
1167 retval.resize (nr); |
|
1168 for (int i = 0; i < nr; i++) |
|
1169 retval.elem (i) = m.elem (i, 0); |
|
1170 } |
|
1171 else if (nr > 0 && nc > 0 |
|
1172 && (user_pref.do_fortran_indexing || force_vector_conversion)) |
|
1173 { |
|
1174 retval.resize (nr * nc); |
|
1175 int k = 0; |
|
1176 for (int j = 0; j < nc; j++) |
|
1177 for (int i = 0; i < nr; i++) |
|
1178 retval.elem (k++) = m.elem (i, j); |
|
1179 } |
|
1180 else |
|
1181 gripe_invalid_conversion ("complex matrix", "complex vector"); |
|
1182 |
492
|
1183 return retval; |
|
1184 } |
|
1185 |
|
1186 tree_constant |
745
|
1187 TC_REP::convert_to_str (void) const |
492
|
1188 { |
|
1189 tree_constant retval; |
|
1190 |
|
1191 switch (type_tag) |
|
1192 { |
|
1193 case complex_scalar_constant: |
|
1194 case scalar_constant: |
|
1195 { |
|
1196 double d = double_value (); |
1086
|
1197 |
|
1198 if (xisnan (d)) |
|
1199 { |
|
1200 ::error ("invalid conversion from NaN to character"); |
|
1201 return retval; |
|
1202 } |
|
1203 else |
|
1204 { |
|
1205 int i = NINT (d); |
492
|
1206 // Warn about out of range conversions? |
1086
|
1207 char s[2]; |
|
1208 s[0] = (char) i; |
|
1209 s[1] = '\0'; |
|
1210 retval = tree_constant (s); |
|
1211 } |
492
|
1212 } |
|
1213 break; |
631
|
1214 |
492
|
1215 case complex_matrix_constant: |
|
1216 case matrix_constant: |
|
1217 { |
767
|
1218 if (rows () == 0 && columns () == 0) |
|
1219 { |
|
1220 char s = '\0'; |
|
1221 retval = tree_constant (&s); |
|
1222 } |
492
|
1223 else |
|
1224 { |
767
|
1225 ColumnVector v = vector_value (); |
|
1226 int len = v.length (); |
|
1227 if (len == 0) |
|
1228 { |
|
1229 char s = '\0'; |
|
1230 retval = tree_constant (&s); |
|
1231 } |
|
1232 else |
492
|
1233 { |
767
|
1234 char *s = new char [len+1]; |
|
1235 s[len] = '\0'; |
|
1236 for (int i = 0; i < len; i++) |
|
1237 { |
|
1238 double d = v.elem (i); |
1086
|
1239 |
|
1240 if (xisnan (d)) |
|
1241 { |
|
1242 ::error ("invalid conversion from NaN to character"); |
|
1243 delete [] s; |
|
1244 return retval; |
|
1245 } |
|
1246 else |
|
1247 { |
|
1248 int ival = NINT (d); |
492
|
1249 // Warn about out of range conversions? |
1086
|
1250 s[i] = (char) ival; |
|
1251 } |
767
|
1252 } |
|
1253 retval = tree_constant (s); |
|
1254 delete [] s; |
492
|
1255 } |
|
1256 } |
|
1257 } |
|
1258 break; |
631
|
1259 |
492
|
1260 case range_constant: |
|
1261 { |
|
1262 Range r = range_value (); |
|
1263 double b = r.base (); |
|
1264 double incr = r.inc (); |
|
1265 int nel = r.nelem (); |
|
1266 char *s = new char [nel+1]; |
|
1267 s[nel] = '\0'; |
|
1268 for (int i = 0; i < nel; i++) |
|
1269 { |
|
1270 double d = b + i * incr; |
1086
|
1271 |
|
1272 if (xisnan (d)) |
|
1273 { |
|
1274 ::error ("invalid conversion from NaN to character"); |
|
1275 delete [] s; |
|
1276 return retval; |
|
1277 } |
|
1278 else |
|
1279 { |
|
1280 int ival = NINT (d); |
492
|
1281 // Warn about out of range conversions? |
1086
|
1282 s[i] = (char) ival; |
|
1283 } |
492
|
1284 } |
|
1285 retval = tree_constant (s); |
|
1286 delete [] s; |
|
1287 } |
|
1288 break; |
631
|
1289 |
492
|
1290 case string_constant: |
581
|
1291 retval = string; |
492
|
1292 break; |
631
|
1293 |
492
|
1294 default: |
777
|
1295 gripe_invalid_conversion (type_as_string (), "string"); |
492
|
1296 break; |
|
1297 } |
631
|
1298 |
492
|
1299 return retval; |
|
1300 } |
|
1301 |
|
1302 void |
631
|
1303 TC_REP::convert_to_row_or_column_vector (void) |
492
|
1304 { |
|
1305 assert (type_tag == matrix_constant || type_tag == complex_matrix_constant); |
|
1306 |
|
1307 int nr = rows (); |
|
1308 int nc = columns (); |
|
1309 |
509
|
1310 if (nr == 1 || nc == 1) |
|
1311 return; |
|
1312 |
492
|
1313 int len = nr * nc; |
|
1314 |
|
1315 assert (len > 0); |
|
1316 |
|
1317 int new_nr = 1; |
|
1318 int new_nc = 1; |
|
1319 |
|
1320 if (user_pref.prefer_column_vectors) |
|
1321 new_nr = len; |
|
1322 else |
|
1323 new_nc = len; |
|
1324 |
|
1325 if (type_tag == matrix_constant) |
|
1326 { |
|
1327 Matrix *m = new Matrix (new_nr, new_nc); |
|
1328 |
|
1329 double *cop_out = matrix->fortran_vec (); |
|
1330 |
|
1331 for (int i = 0; i < len; i++) |
|
1332 { |
|
1333 if (new_nr == 1) |
|
1334 m->elem (0, i) = *cop_out++; |
|
1335 else |
|
1336 m->elem (i, 0) = *cop_out++; |
|
1337 } |
|
1338 |
|
1339 delete matrix; |
|
1340 matrix = m; |
|
1341 } |
|
1342 else |
|
1343 { |
|
1344 ComplexMatrix *cm = new ComplexMatrix (new_nr, new_nc); |
|
1345 |
|
1346 Complex *cop_out = complex_matrix->fortran_vec (); |
|
1347 |
|
1348 for (int i = 0; i < len; i++) |
|
1349 { |
|
1350 if (new_nr == 1) |
|
1351 cm->elem (0, i) = *cop_out++; |
|
1352 else |
|
1353 cm->elem (i, 0) = *cop_out++; |
|
1354 } |
|
1355 |
|
1356 delete complex_matrix; |
|
1357 complex_matrix = cm; |
|
1358 } |
|
1359 } |
|
1360 |
631
|
1361 void |
|
1362 TC_REP::force_numeric (int force_str_conv) |
492
|
1363 { |
|
1364 switch (type_tag) |
|
1365 { |
|
1366 case scalar_constant: |
|
1367 case matrix_constant: |
|
1368 case complex_scalar_constant: |
|
1369 case complex_matrix_constant: |
631
|
1370 break; |
|
1371 |
|
1372 case string_constant: |
492
|
1373 { |
631
|
1374 if (! force_str_conv && ! user_pref.implicit_str_to_num_ok) |
|
1375 { |
|
1376 ::error ("failed to convert `%s' to a numeric type --", string); |
|
1377 ::error ("default conversion turned off"); |
915
|
1378 |
|
1379 return; |
631
|
1380 } |
|
1381 |
|
1382 int len = strlen (string); |
|
1383 if (len > 1) |
|
1384 { |
|
1385 type_tag = matrix_constant; |
|
1386 Matrix *tm = new Matrix (1, len); |
|
1387 for (int i = 0; i < len; i++) |
|
1388 tm->elem (0, i) = toascii ((int) string[i]); |
|
1389 matrix = tm; |
|
1390 } |
|
1391 else if (len == 1) |
|
1392 { |
|
1393 type_tag = scalar_constant; |
|
1394 scalar = toascii ((int) string[0]); |
|
1395 } |
|
1396 else if (len == 0) |
|
1397 { |
|
1398 type_tag = matrix_constant; |
|
1399 matrix = new Matrix (0, 0); |
|
1400 } |
|
1401 else |
|
1402 panic_impossible (); |
492
|
1403 } |
|
1404 break; |
631
|
1405 |
492
|
1406 case range_constant: |
631
|
1407 { |
|
1408 int len = range->nelem (); |
|
1409 if (len > 1) |
|
1410 { |
|
1411 type_tag = matrix_constant; |
|
1412 Matrix *tm = new Matrix (1, len); |
|
1413 double b = range->base (); |
|
1414 double increment = range->inc (); |
|
1415 for (int i = 0; i < len; i++) |
|
1416 tm->elem (0, i) = b + i * increment; |
|
1417 matrix = tm; |
|
1418 } |
|
1419 else if (len == 1) |
|
1420 { |
|
1421 type_tag = scalar_constant; |
|
1422 scalar = range->base (); |
|
1423 } |
|
1424 } |
|
1425 break; |
|
1426 |
492
|
1427 default: |
777
|
1428 gripe_invalid_conversion (type_as_string (), "numeric type"); |
492
|
1429 break; |
|
1430 } |
|
1431 } |
|
1432 |
|
1433 tree_constant |
631
|
1434 TC_REP::make_numeric (int force_str_conv) const |
492
|
1435 { |
|
1436 tree_constant retval; |
631
|
1437 |
492
|
1438 switch (type_tag) |
|
1439 { |
|
1440 case scalar_constant: |
|
1441 retval = tree_constant (scalar); |
|
1442 break; |
631
|
1443 |
492
|
1444 case matrix_constant: |
631
|
1445 retval = tree_constant (*matrix); |
|
1446 break; |
|
1447 |
492
|
1448 case complex_scalar_constant: |
|
1449 retval = tree_constant (*complex_scalar); |
|
1450 break; |
631
|
1451 |
492
|
1452 case complex_matrix_constant: |
631
|
1453 retval = tree_constant (*complex_matrix); |
|
1454 break; |
|
1455 |
492
|
1456 case string_constant: |
631
|
1457 retval = tree_constant (string); |
|
1458 retval.force_numeric (force_str_conv); |
|
1459 break; |
|
1460 |
492
|
1461 case range_constant: |
631
|
1462 retval = tree_constant (*range); |
|
1463 retval.force_numeric (force_str_conv); |
|
1464 break; |
|
1465 |
492
|
1466 default: |
777
|
1467 gripe_invalid_conversion (type_as_string (), "numeric value"); |
492
|
1468 break; |
|
1469 } |
631
|
1470 |
|
1471 return retval; |
|
1472 } |
|
1473 |
|
1474 void |
|
1475 TC_REP::bump_value (tree_expression::type etype) |
|
1476 { |
|
1477 switch (etype) |
|
1478 { |
|
1479 case tree_expression::increment: |
|
1480 switch (type_tag) |
|
1481 { |
|
1482 case scalar_constant: |
|
1483 scalar++; |
|
1484 break; |
|
1485 |
|
1486 case matrix_constant: |
|
1487 *matrix = *matrix + 1.0; |
|
1488 break; |
|
1489 |
|
1490 case complex_scalar_constant: |
|
1491 *complex_scalar = *complex_scalar + 1.0; |
|
1492 break; |
|
1493 |
|
1494 case complex_matrix_constant: |
|
1495 *complex_matrix = *complex_matrix + 1.0; |
|
1496 break; |
|
1497 |
|
1498 case range_constant: |
|
1499 range->set_base (range->base () + 1.0); |
|
1500 range->set_limit (range->limit () + 1.0); |
|
1501 break; |
|
1502 |
|
1503 default: |
777
|
1504 gripe_wrong_type_arg ("operator ++", type_as_string ()); |
631
|
1505 break; |
|
1506 } |
|
1507 break; |
|
1508 |
|
1509 case tree_expression::decrement: |
|
1510 switch (type_tag) |
|
1511 { |
|
1512 case scalar_constant: |
|
1513 scalar--; |
|
1514 break; |
|
1515 |
|
1516 case matrix_constant: |
|
1517 *matrix = *matrix - 1.0; |
|
1518 break; |
|
1519 |
|
1520 case range_constant: |
|
1521 range->set_base (range->base () - 1.0); |
|
1522 range->set_limit (range->limit () - 1.0); |
|
1523 break; |
|
1524 |
|
1525 default: |
777
|
1526 gripe_wrong_type_arg ("operator --", type_as_string ()); |
631
|
1527 break; |
|
1528 } |
|
1529 break; |
|
1530 |
|
1531 default: |
|
1532 panic_impossible (); |
|
1533 break; |
|
1534 } |
|
1535 } |
|
1536 |
|
1537 void |
|
1538 TC_REP::resize (int i, int j) |
|
1539 { |
|
1540 switch (type_tag) |
|
1541 { |
|
1542 case matrix_constant: |
|
1543 matrix->resize (i, j); |
|
1544 break; |
|
1545 |
|
1546 case complex_matrix_constant: |
|
1547 complex_matrix->resize (i, j); |
|
1548 break; |
|
1549 |
|
1550 default: |
777
|
1551 gripe_wrong_type_arg ("resize", type_as_string ()); |
631
|
1552 break; |
|
1553 } |
|
1554 } |
|
1555 |
|
1556 void |
|
1557 TC_REP::resize (int i, int j, double val) |
|
1558 { |
|
1559 switch (type_tag) |
|
1560 { |
|
1561 case matrix_constant: |
|
1562 matrix->resize (i, j, val); |
|
1563 break; |
|
1564 |
|
1565 case complex_matrix_constant: |
|
1566 complex_matrix->resize (i, j, val); |
|
1567 break; |
|
1568 |
|
1569 default: |
777
|
1570 gripe_wrong_type_arg ("resize", type_as_string ()); |
631
|
1571 break; |
|
1572 } |
|
1573 } |
|
1574 |
|
1575 void |
|
1576 TC_REP::maybe_resize (int i, int j) |
|
1577 { |
|
1578 int nr = rows (); |
|
1579 int nc = columns (); |
|
1580 |
|
1581 i++; |
|
1582 j++; |
|
1583 |
|
1584 assert (i > 0 && j > 0); |
|
1585 |
|
1586 if (i > nr || j > nc) |
|
1587 { |
|
1588 if (user_pref.resize_on_range_error) |
|
1589 resize (MAX (i, nr), MAX (j, nc), 0.0); |
|
1590 else |
|
1591 { |
|
1592 if (i > nr) |
|
1593 ::error ("row index = %d exceeds max row dimension = %d", i, nr); |
|
1594 |
|
1595 if (j > nc) |
|
1596 ::error ("column index = %d exceeds max column dimension = %d", |
|
1597 j, nc); |
|
1598 } |
|
1599 } |
|
1600 } |
|
1601 |
|
1602 void |
|
1603 TC_REP::maybe_resize (int i, force_orient f_orient) |
|
1604 { |
|
1605 int nr = rows (); |
|
1606 int nc = columns (); |
|
1607 |
|
1608 i++; |
|
1609 |
|
1610 assert (i >= 0 && (nr <= 1 || nc <= 1)); |
|
1611 |
|
1612 // This function never reduces the size of a vector, and all vectors |
|
1613 // have dimensions of at least 0x0. If i is 0, it is either because |
|
1614 // a vector has been indexed with a vector of all zeros (in which case |
|
1615 // the index vector is empty and nothing will happen) or a vector has |
|
1616 // been indexed with 0 (an error which will be caught elsewhere). |
|
1617 if (i == 0) |
|
1618 return; |
|
1619 |
|
1620 if (nr <= 1 && nc <= 1 && i >= 1) |
|
1621 { |
|
1622 if (user_pref.resize_on_range_error) |
|
1623 { |
|
1624 if (f_orient == row_orient) |
|
1625 resize (1, i, 0.0); |
|
1626 else if (f_orient == column_orient) |
|
1627 resize (i, 1, 0.0); |
|
1628 else if (user_pref.prefer_column_vectors) |
|
1629 resize (i, 1, 0.0); |
|
1630 else |
|
1631 resize (1, i, 0.0); |
|
1632 } |
|
1633 else |
|
1634 ::error ("matrix index = %d exceeds max dimension = %d", i, nc); |
|
1635 } |
|
1636 else if (nr == 1 && i > nc) |
|
1637 { |
|
1638 if (user_pref.resize_on_range_error) |
|
1639 resize (1, i, 0.0); |
|
1640 else |
|
1641 ::error ("matrix index = %d exceeds max dimension = %d", i, nc); |
|
1642 } |
|
1643 else if (nc == 1 && i > nr) |
|
1644 { |
|
1645 if (user_pref.resize_on_range_error) |
|
1646 resize (i, 1, 0.0); |
|
1647 else |
|
1648 ::error ("matrix index = %d exceeds max dimension = ", i, nc); |
|
1649 } |
|
1650 } |
|
1651 |
|
1652 void |
|
1653 TC_REP::stash_original_text (char *s) |
|
1654 { |
|
1655 orig_text = strsave (s); |
|
1656 } |
|
1657 |
|
1658 void |
|
1659 TC_REP::maybe_mutate (void) |
492
|
1660 { |
631
|
1661 if (error_state) |
|
1662 return; |
581
|
1663 |
|
1664 switch (type_tag) |
|
1665 { |
|
1666 case complex_scalar_constant: |
631
|
1667 if (::imag (*complex_scalar) == 0.0) |
|
1668 { |
|
1669 double d = ::real (*complex_scalar); |
|
1670 delete complex_scalar; |
|
1671 scalar = d; |
|
1672 type_tag = scalar_constant; |
|
1673 } |
|
1674 break; |
|
1675 |
|
1676 case complex_matrix_constant: |
|
1677 if (! any_element_is_complex (*complex_matrix)) |
|
1678 { |
|
1679 Matrix *m = new Matrix (::real (*complex_matrix)); |
|
1680 delete complex_matrix; |
|
1681 matrix = m; |
|
1682 type_tag = matrix_constant; |
|
1683 } |
|
1684 break; |
|
1685 |
|
1686 default: |
|
1687 break; |
|
1688 } |
|
1689 |
|
1690 // Avoid calling rows() and columns() for things like magic_colon. |
|
1691 |
|
1692 int nr = 1; |
|
1693 int nc = 1; |
|
1694 if (type_tag == matrix_constant |
|
1695 || type_tag == complex_matrix_constant |
|
1696 || type_tag == range_constant) |
|
1697 { |
|
1698 nr = rows (); |
|
1699 nc = columns (); |
|
1700 } |
|
1701 |
|
1702 switch (type_tag) |
|
1703 { |
|
1704 case matrix_constant: |
|
1705 if (nr == 1 && nc == 1) |
|
1706 { |
|
1707 double d = matrix->elem (0, 0); |
|
1708 delete matrix; |
|
1709 scalar = d; |
|
1710 type_tag = scalar_constant; |
|
1711 } |
|
1712 break; |
|
1713 |
581
|
1714 case complex_matrix_constant: |
631
|
1715 if (nr == 1 && nc == 1) |
|
1716 { |
|
1717 Complex c = complex_matrix->elem (0, 0); |
|
1718 delete complex_matrix; |
|
1719 complex_scalar = new Complex (c); |
|
1720 type_tag = complex_scalar_constant; |
|
1721 } |
|
1722 break; |
|
1723 |
|
1724 case range_constant: |
|
1725 if (nr == 1 && nc == 1) |
|
1726 { |
|
1727 double d = range->base (); |
|
1728 delete range; |
|
1729 scalar = d; |
|
1730 type_tag = scalar_constant; |
|
1731 } |
|
1732 break; |
|
1733 |
|
1734 default: |
|
1735 break; |
|
1736 } |
|
1737 } |
|
1738 |
|
1739 void |
|
1740 TC_REP::print (void) |
|
1741 { |
|
1742 if (error_state) |
|
1743 return; |
|
1744 |
961
|
1745 ostrstream output_buf; |
631
|
1746 |
961
|
1747 switch (type_tag) |
|
1748 { |
|
1749 case scalar_constant: |
|
1750 octave_print_internal (output_buf, scalar); |
|
1751 break; |
|
1752 |
|
1753 case matrix_constant: |
|
1754 octave_print_internal (output_buf, *matrix); |
|
1755 break; |
631
|
1756 |
961
|
1757 case complex_scalar_constant: |
|
1758 octave_print_internal (output_buf, *complex_scalar); |
|
1759 break; |
631
|
1760 |
961
|
1761 case complex_matrix_constant: |
|
1762 octave_print_internal (output_buf, *complex_matrix); |
|
1763 break; |
631
|
1764 |
961
|
1765 case string_constant: |
|
1766 output_buf << string << "\n"; |
|
1767 break; |
631
|
1768 |
961
|
1769 case range_constant: |
|
1770 octave_print_internal (output_buf, *range); |
|
1771 break; |
631
|
1772 |
961
|
1773 case map_constant: |
|
1774 { |
|
1775 output_buf << "<structure"; |
|
1776 int first = 1; |
|
1777 for (Pix p = a_map->first (); p != 0; a_map->next (p)) |
745
|
1778 { |
961
|
1779 if (first) |
745
|
1780 { |
961
|
1781 output_buf << ":"; |
|
1782 first = 0; |
745
|
1783 } |
961
|
1784 output_buf << " " << a_map->key (p); |
745
|
1785 } |
961
|
1786 output_buf << ">\n"; |
|
1787 } |
|
1788 break; |
745
|
1789 |
961
|
1790 case unknown_constant: |
|
1791 case magic_colon: |
|
1792 case all_va_args: |
|
1793 panic_impossible (); |
|
1794 break; |
|
1795 } |
631
|
1796 |
961
|
1797 output_buf << ends; |
|
1798 maybe_page_output (output_buf); |
631
|
1799 } |
|
1800 |
|
1801 void |
|
1802 TC_REP::print_code (ostream& os) |
|
1803 { |
|
1804 switch (type_tag) |
|
1805 { |
|
1806 case scalar_constant: |
|
1807 if (orig_text) |
|
1808 os << orig_text; |
|
1809 else |
|
1810 octave_print_internal (os, scalar, 1); |
|
1811 break; |
|
1812 |
|
1813 case matrix_constant: |
|
1814 octave_print_internal (os, *matrix, 1); |
|
1815 break; |
|
1816 |
|
1817 case complex_scalar_constant: |
|
1818 { |
|
1819 double re = complex_scalar->real (); |
|
1820 double im = complex_scalar->imag (); |
|
1821 |
|
1822 // If we have the original text and a pure imaginary, just print the |
|
1823 // original text, because this must be a constant that was parsed as |
|
1824 // part of a function. |
|
1825 |
|
1826 if (orig_text && re == 0.0 && im > 0.0) |
|
1827 os << orig_text; |
|
1828 else |
|
1829 octave_print_internal (os, *complex_scalar, 1); |
|
1830 } |
|
1831 break; |
|
1832 |
|
1833 case complex_matrix_constant: |
|
1834 octave_print_internal (os, *complex_matrix, 1); |
|
1835 break; |
|
1836 |
|
1837 case string_constant: |
|
1838 { |
|
1839 os << "\""; |
|
1840 char *s, *t = string; |
801
|
1841 while (s = undo_string_escape (*t++)) |
631
|
1842 os << s; |
|
1843 os << "\""; |
|
1844 } |
|
1845 break; |
|
1846 |
|
1847 case range_constant: |
|
1848 octave_print_internal (os, *range, 1); |
|
1849 break; |
|
1850 |
|
1851 case magic_colon: |
|
1852 os << ":"; |
|
1853 break; |
|
1854 |
922
|
1855 case all_va_args: |
|
1856 os << "all_va_args"; |
|
1857 break; |
|
1858 |
777
|
1859 case map_constant: |
|
1860 case unknown_constant: |
631
|
1861 panic_impossible (); |
|
1862 break; |
|
1863 } |
|
1864 } |
|
1865 |
777
|
1866 void |
|
1867 TC_REP::gripe_wrong_type_arg (const char *name, |
|
1868 const tree_constant_rep& tcr) const |
|
1869 { |
|
1870 if (name) |
|
1871 ::error ("%s: wrong type argument `%s'", name, tcr.type_as_string ()); |
|
1872 else |
|
1873 ::error ("wrong type argument `%s'", name, tcr.type_as_string ()); |
|
1874 } |
|
1875 |
631
|
1876 char * |
|
1877 TC_REP::type_as_string (void) const |
|
1878 { |
|
1879 switch (type_tag) |
|
1880 { |
|
1881 case scalar_constant: |
|
1882 return "real scalar"; |
|
1883 |
581
|
1884 case matrix_constant: |
631
|
1885 return "real matrix"; |
|
1886 |
|
1887 case complex_scalar_constant: |
|
1888 return "complex scalar"; |
|
1889 |
|
1890 case complex_matrix_constant: |
|
1891 return "complex matrix"; |
|
1892 |
|
1893 case string_constant: |
|
1894 return "string"; |
|
1895 |
581
|
1896 case range_constant: |
631
|
1897 return "range"; |
|
1898 |
777
|
1899 case map_constant: |
|
1900 return "structure"; |
|
1901 |
631
|
1902 default: |
|
1903 return "<unknown type>"; |
|
1904 } |
|
1905 } |
|
1906 |
|
1907 tree_constant |
|
1908 do_binary_op (tree_constant& a, tree_constant& b, tree_expression::type t) |
|
1909 { |
777
|
1910 tree_constant retval; |
631
|
1911 |
|
1912 int first_empty = (a.rows () == 0 || a.columns () == 0); |
|
1913 int second_empty = (b.rows () == 0 || b.columns () == 0); |
|
1914 |
|
1915 if (first_empty || second_empty) |
|
1916 { |
|
1917 int flag = user_pref.propagate_empty_matrices; |
|
1918 if (flag < 0) |
|
1919 warning ("binary operation on empty matrix"); |
|
1920 else if (flag == 0) |
|
1921 { |
|
1922 ::error ("invalid binary operation on empty matrix"); |
777
|
1923 return retval; |
631
|
1924 } |
|
1925 } |
|
1926 |
|
1927 tree_constant tmp_a = a.make_numeric (); |
777
|
1928 |
|
1929 if (error_state) |
|
1930 return retval; |
|
1931 |
631
|
1932 tree_constant tmp_b = b.make_numeric (); |
|
1933 |
777
|
1934 if (error_state) |
|
1935 return retval; |
|
1936 |
631
|
1937 TC_REP::constant_type a_type = tmp_a.const_type (); |
|
1938 TC_REP::constant_type b_type = tmp_b.const_type (); |
|
1939 |
|
1940 double d1, d2; |
|
1941 Matrix m1, m2; |
|
1942 Complex c1, c2; |
|
1943 ComplexMatrix cm1, cm2; |
|
1944 |
|
1945 switch (a_type) |
|
1946 { |
|
1947 case TC_REP::scalar_constant: |
|
1948 |
|
1949 d1 = tmp_a.double_value (); |
|
1950 |
|
1951 switch (b_type) |
|
1952 { |
|
1953 case TC_REP::scalar_constant: |
|
1954 d2 = tmp_b.double_value (); |
777
|
1955 retval = do_binary_op (d1, d2, t); |
631
|
1956 break; |
|
1957 |
|
1958 case TC_REP::matrix_constant: |
|
1959 m2 = tmp_b.matrix_value (); |
777
|
1960 retval = do_binary_op (d1, m2, t); |
631
|
1961 break; |
|
1962 |
|
1963 case TC_REP::complex_scalar_constant: |
|
1964 c2 = tmp_b.complex_value (); |
777
|
1965 retval = do_binary_op (d1, c2, t); |
631
|
1966 break; |
|
1967 |
|
1968 case TC_REP::complex_matrix_constant: |
|
1969 cm2 = tmp_b.complex_matrix_value (); |
777
|
1970 retval = do_binary_op (d1, cm2, t); |
631
|
1971 break; |
|
1972 |
|
1973 default: |
777
|
1974 gripe_wrong_type_arg_for_binary_op (tmp_b); |
631
|
1975 break; |
|
1976 } |
|
1977 break; |
|
1978 |
|
1979 case TC_REP::matrix_constant: |
|
1980 |
|
1981 m1 = tmp_a.matrix_value (); |
|
1982 |
|
1983 switch (b_type) |
|
1984 { |
|
1985 case TC_REP::scalar_constant: |
|
1986 d2 = tmp_b.double_value (); |
777
|
1987 retval = do_binary_op (m1, d2, t); |
631
|
1988 break; |
|
1989 |
|
1990 case TC_REP::matrix_constant: |
|
1991 m2 = tmp_b.matrix_value (); |
777
|
1992 retval = do_binary_op (m1, m2, t); |
631
|
1993 break; |
|
1994 |
|
1995 case TC_REP::complex_scalar_constant: |
|
1996 c2 = tmp_b.complex_value (); |
777
|
1997 retval = do_binary_op (m1, c2, t); |
631
|
1998 break; |
|
1999 |
|
2000 case TC_REP::complex_matrix_constant: |
|
2001 cm2 = tmp_b.complex_matrix_value (); |
777
|
2002 retval = do_binary_op (m1, cm2, t); |
631
|
2003 break; |
|
2004 |
|
2005 default: |
777
|
2006 gripe_wrong_type_arg_for_binary_op (tmp_b); |
631
|
2007 break; |
|
2008 } |
|
2009 break; |
|
2010 |
|
2011 case TC_REP::complex_scalar_constant: |
|
2012 |
|
2013 c1 = tmp_a.complex_value (); |
|
2014 |
|
2015 switch (b_type) |
|
2016 { |
|
2017 case TC_REP::scalar_constant: |
|
2018 d2 = tmp_b.double_value (); |
777
|
2019 retval = do_binary_op (c1, d2, t); |
631
|
2020 break; |
|
2021 |
|
2022 case TC_REP::matrix_constant: |
|
2023 m2 = tmp_b.matrix_value (); |
777
|
2024 retval = do_binary_op (c1, m2, t); |
631
|
2025 break; |
|
2026 |
|
2027 case TC_REP::complex_scalar_constant: |
|
2028 c2 = tmp_b.complex_value (); |
777
|
2029 retval = do_binary_op (c1, c2, t); |
631
|
2030 break; |
|
2031 |
|
2032 case TC_REP::complex_matrix_constant: |
|
2033 cm2 = tmp_b.complex_matrix_value (); |
777
|
2034 retval = do_binary_op (c1, cm2, t); |
631
|
2035 break; |
|
2036 |
|
2037 default: |
777
|
2038 gripe_wrong_type_arg_for_binary_op (tmp_b); |
631
|
2039 break; |
|
2040 } |
|
2041 break; |
|
2042 |
|
2043 case TC_REP::complex_matrix_constant: |
|
2044 |
|
2045 cm1 = tmp_a.complex_matrix_value (); |
|
2046 |
|
2047 switch (b_type) |
|
2048 { |
|
2049 case TC_REP::scalar_constant: |
|
2050 d2 = tmp_b.double_value (); |
777
|
2051 retval = do_binary_op (cm1, d2, t); |
631
|
2052 break; |
|
2053 |
|
2054 case TC_REP::matrix_constant: |
|
2055 m2 = tmp_b.matrix_value (); |
777
|
2056 retval = do_binary_op (cm1, m2, t); |
631
|
2057 break; |
|
2058 |
|
2059 case TC_REP::complex_scalar_constant: |
|
2060 c2 = tmp_b.complex_value (); |
777
|
2061 retval = do_binary_op (cm1, c2, t); |
631
|
2062 break; |
|
2063 |
|
2064 case TC_REP::complex_matrix_constant: |
|
2065 cm2 = tmp_b.complex_matrix_value (); |
777
|
2066 retval = do_binary_op (cm1, cm2, t); |
631
|
2067 break; |
|
2068 |
|
2069 default: |
777
|
2070 gripe_wrong_type_arg_for_binary_op (tmp_b); |
631
|
2071 break; |
|
2072 } |
|
2073 break; |
|
2074 |
|
2075 default: |
777
|
2076 gripe_wrong_type_arg_for_binary_op (tmp_a); |
631
|
2077 break; |
|
2078 } |
|
2079 |
777
|
2080 return retval; |
631
|
2081 } |
|
2082 |
|
2083 tree_constant |
|
2084 do_unary_op (tree_constant& a, tree_expression::type t) |
|
2085 { |
777
|
2086 tree_constant retval; |
631
|
2087 |
|
2088 if (a.rows () == 0 || a.columns () == 0) |
|
2089 { |
|
2090 int flag = user_pref.propagate_empty_matrices; |
|
2091 if (flag < 0) |
|
2092 warning ("unary operation on empty matrix"); |
|
2093 else if (flag == 0) |
|
2094 { |
|
2095 ::error ("invalid unary operation on empty matrix"); |
777
|
2096 return retval; |
631
|
2097 } |
|
2098 } |
|
2099 |
|
2100 tree_constant tmp_a = a.make_numeric (); |
|
2101 |
777
|
2102 if (error_state) |
|
2103 return retval; |
|
2104 |
631
|
2105 switch (tmp_a.const_type ()) |
|
2106 { |
|
2107 case TC_REP::scalar_constant: |
777
|
2108 retval = do_unary_op (tmp_a.double_value (), t); |
631
|
2109 break; |
|
2110 |
|
2111 case TC_REP::matrix_constant: |
|
2112 { |
|
2113 Matrix m = tmp_a.matrix_value (); |
777
|
2114 retval = do_unary_op (m, t); |
631
|
2115 } |
|
2116 break; |
|
2117 |
|
2118 case TC_REP::complex_scalar_constant: |
777
|
2119 retval = do_unary_op (tmp_a.complex_value (), t); |
631
|
2120 break; |
|
2121 |
|
2122 case TC_REP::complex_matrix_constant: |
|
2123 { |
|
2124 ComplexMatrix m = tmp_a.complex_matrix_value (); |
777
|
2125 retval = do_unary_op (m, t); |
631
|
2126 } |
|
2127 break; |
|
2128 |
|
2129 default: |
777
|
2130 gripe_wrong_type_arg_for_unary_op (tmp_a); |
631
|
2131 break; |
|
2132 } |
|
2133 |
777
|
2134 return retval; |
631
|
2135 } |
|
2136 |
492
|
2137 /* |
|
2138 ;;; Local Variables: *** |
|
2139 ;;; mode: C++ *** |
|
2140 ;;; page-delimiter: "^/\\*" *** |
|
2141 ;;; End: *** |
|
2142 */ |