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