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