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