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 |
|
50 #include "tc-inlines.cc" |
|
51 |
745
|
52 // And still some more handy helper functions. |
492
|
53 |
|
54 static int |
|
55 any_element_less_than (const Matrix& a, double val) |
|
56 { |
|
57 int nr = a.rows (); |
|
58 int nc = a.columns (); |
|
59 for (int j = 0; j < nc; j++) |
|
60 for (int i = 0; i < nr; i++) |
|
61 if (a.elem (i, j) < val) |
|
62 return 1; |
|
63 return 0; |
|
64 } |
|
65 |
|
66 static int |
|
67 any_element_greater_than (const Matrix& a, double val) |
|
68 { |
|
69 int nr = a.rows (); |
|
70 int nc = a.columns (); |
|
71 for (int j = 0; j < nc; j++) |
|
72 for (int i = 0; i < nr; i++) |
|
73 if (a.elem (i, j) > val) |
|
74 return 1; |
|
75 return 0; |
|
76 } |
|
77 |
|
78 static int |
|
79 any_element_is_complex (const ComplexMatrix& a) |
|
80 { |
|
81 int nr = a.rows (); |
|
82 int nc = a.columns (); |
|
83 for (int j = 0; j < nc; j++) |
|
84 for (int i = 0; i < nr; i++) |
|
85 if (imag (a.elem (i, j)) != 0.0) |
|
86 return 1; |
|
87 return 0; |
|
88 } |
|
89 |
581
|
90 // The real representation of constants. |
|
91 |
631
|
92 TC_REP::tree_constant_rep (void) |
492
|
93 { |
|
94 type_tag = unknown_constant; |
581
|
95 orig_text = 0; |
492
|
96 } |
|
97 |
631
|
98 TC_REP::tree_constant_rep (double d) |
492
|
99 { |
|
100 scalar = d; |
|
101 type_tag = scalar_constant; |
581
|
102 orig_text = 0; |
492
|
103 } |
|
104 |
631
|
105 TC_REP::tree_constant_rep (const Matrix& m) |
492
|
106 { |
|
107 if (m.rows () == 1 && m.columns () == 1) |
|
108 { |
|
109 scalar = m.elem (0, 0); |
|
110 type_tag = scalar_constant; |
|
111 } |
|
112 else |
|
113 { |
|
114 matrix = new Matrix (m); |
|
115 type_tag = matrix_constant; |
|
116 } |
581
|
117 orig_text = 0; |
492
|
118 } |
|
119 |
631
|
120 TC_REP::tree_constant_rep (const DiagMatrix& d) |
492
|
121 { |
|
122 if (d.rows () == 1 && d.columns () == 1) |
|
123 { |
|
124 scalar = d.elem (0, 0); |
|
125 type_tag = scalar_constant; |
|
126 } |
|
127 else |
|
128 { |
|
129 matrix = new Matrix (d); |
|
130 type_tag = matrix_constant; |
|
131 } |
581
|
132 orig_text = 0; |
492
|
133 } |
|
134 |
631
|
135 TC_REP::tree_constant_rep (const RowVector& v, int prefer_column_vector) |
492
|
136 { |
|
137 int len = v.capacity (); |
|
138 if (len == 1) |
|
139 { |
|
140 scalar = v.elem (0); |
|
141 type_tag = scalar_constant; |
|
142 } |
|
143 else |
|
144 { |
|
145 int pcv = (prefer_column_vector < 0) |
|
146 ? user_pref.prefer_column_vectors |
|
147 : prefer_column_vector; |
|
148 |
|
149 if (pcv) |
|
150 { |
|
151 Matrix m (len, 1); |
|
152 for (int i = 0; i < len; i++) |
|
153 m.elem (i, 0) = v.elem (i); |
|
154 matrix = new Matrix (m); |
|
155 type_tag = matrix_constant; |
|
156 } |
|
157 else |
|
158 { |
|
159 Matrix m (1, len); |
|
160 for (int i = 0; i < len; i++) |
|
161 m.elem (0, i) = v.elem (i); |
|
162 matrix = new Matrix (m); |
|
163 type_tag = matrix_constant; |
|
164 } |
|
165 } |
581
|
166 orig_text = 0; |
492
|
167 } |
|
168 |
631
|
169 TC_REP::tree_constant_rep (const ColumnVector& v, int prefer_column_vector) |
492
|
170 { |
|
171 int len = v.capacity (); |
|
172 if (len == 1) |
|
173 { |
|
174 scalar = v.elem (0); |
|
175 type_tag = scalar_constant; |
|
176 } |
|
177 else |
|
178 { |
|
179 int pcv = (prefer_column_vector < 0) |
|
180 ? user_pref.prefer_column_vectors |
|
181 : prefer_column_vector; |
|
182 |
|
183 if (pcv) |
|
184 { |
|
185 Matrix m (len, 1); |
|
186 for (int i = 0; i < len; i++) |
|
187 m.elem (i, 0) = v.elem (i); |
|
188 matrix = new Matrix (m); |
|
189 type_tag = matrix_constant; |
|
190 } |
|
191 else |
|
192 { |
|
193 Matrix m (1, len); |
|
194 for (int i = 0; i < len; i++) |
|
195 m.elem (0, i) = v.elem (i); |
|
196 matrix = new Matrix (m); |
|
197 type_tag = matrix_constant; |
|
198 } |
|
199 } |
581
|
200 orig_text = 0; |
492
|
201 } |
|
202 |
631
|
203 TC_REP::tree_constant_rep (const Complex& c) |
492
|
204 { |
|
205 complex_scalar = new Complex (c); |
|
206 type_tag = complex_scalar_constant; |
581
|
207 orig_text = 0; |
492
|
208 } |
|
209 |
631
|
210 TC_REP::tree_constant_rep (const ComplexMatrix& m) |
492
|
211 { |
|
212 if (m.rows () == 1 && m.columns () == 1) |
|
213 { |
|
214 complex_scalar = new Complex (m.elem (0, 0)); |
|
215 type_tag = complex_scalar_constant; |
|
216 } |
|
217 else |
|
218 { |
|
219 complex_matrix = new ComplexMatrix (m); |
|
220 type_tag = complex_matrix_constant; |
|
221 } |
581
|
222 orig_text = 0; |
492
|
223 } |
|
224 |
631
|
225 TC_REP::tree_constant_rep (const ComplexDiagMatrix& d) |
492
|
226 { |
|
227 if (d.rows () == 1 && d.columns () == 1) |
|
228 { |
|
229 complex_scalar = new Complex (d.elem (0, 0)); |
|
230 type_tag = complex_scalar_constant; |
|
231 } |
|
232 else |
|
233 { |
|
234 complex_matrix = new ComplexMatrix (d); |
|
235 type_tag = complex_matrix_constant; |
|
236 } |
581
|
237 orig_text = 0; |
492
|
238 } |
|
239 |
631
|
240 TC_REP::tree_constant_rep (const ComplexRowVector& v, |
|
241 int prefer_column_vector) |
492
|
242 { |
|
243 int len = v.capacity (); |
|
244 if (len == 1) |
|
245 { |
|
246 complex_scalar = new Complex (v.elem (0)); |
|
247 type_tag = complex_scalar_constant; |
|
248 } |
|
249 else |
|
250 { |
|
251 int pcv = (prefer_column_vector < 0) |
|
252 ? user_pref.prefer_column_vectors |
|
253 : prefer_column_vector; |
|
254 |
|
255 if (pcv) |
|
256 { |
|
257 ComplexMatrix m (len, 1); |
|
258 for (int i = 0; i < len; i++) |
|
259 m.elem (i, 0) = v.elem (i); |
|
260 complex_matrix = new ComplexMatrix (m); |
|
261 type_tag = complex_matrix_constant; |
|
262 } |
|
263 else |
|
264 { |
|
265 ComplexMatrix m (1, len); |
|
266 for (int i = 0; i < len; i++) |
|
267 m.elem (0, i) = v.elem (i); |
|
268 complex_matrix = new ComplexMatrix (m); |
|
269 type_tag = complex_matrix_constant; |
|
270 } |
|
271 } |
581
|
272 orig_text = 0; |
492
|
273 } |
|
274 |
631
|
275 TC_REP::tree_constant_rep (const ComplexColumnVector& v, int |
|
276 prefer_column_vector) |
492
|
277 { |
|
278 int len = v.capacity (); |
|
279 if (len == 1) |
|
280 { |
|
281 complex_scalar = new Complex (v.elem (0)); |
|
282 type_tag = complex_scalar_constant; |
|
283 } |
|
284 else |
|
285 { |
|
286 int pcv = (prefer_column_vector < 0) |
|
287 ? user_pref.prefer_column_vectors |
|
288 : prefer_column_vector; |
|
289 |
|
290 if (pcv) |
|
291 { |
|
292 ComplexMatrix m (len, 1); |
|
293 for (int i = 0; i < len; i++) |
|
294 m.elem (i, 0) = v.elem (i); |
|
295 complex_matrix = new ComplexMatrix (m); |
|
296 type_tag = complex_matrix_constant; |
|
297 } |
|
298 else |
|
299 { |
|
300 ComplexMatrix m (1, len); |
|
301 for (int i = 0; i < len; i++) |
|
302 m.elem (0, i) = v.elem (i); |
|
303 complex_matrix = new ComplexMatrix (m); |
|
304 type_tag = complex_matrix_constant; |
|
305 } |
|
306 } |
581
|
307 orig_text = 0; |
492
|
308 } |
|
309 |
631
|
310 TC_REP::tree_constant_rep (const char *s) |
492
|
311 { |
|
312 string = strsave (s); |
|
313 type_tag = string_constant; |
581
|
314 orig_text = 0; |
492
|
315 } |
|
316 |
631
|
317 TC_REP::tree_constant_rep (double b, double l, double i) |
492
|
318 { |
|
319 range = new Range (b, l, i); |
|
320 int nel = range->nelem (); |
|
321 if (nel < 0) |
|
322 { |
|
323 delete range; |
|
324 type_tag = unknown_constant; |
|
325 if (nel == -1) |
|
326 ::error ("number of elements in range exceeds INT_MAX"); |
|
327 else |
|
328 ::error ("invalid range"); |
|
329 } |
|
330 else if (nel > 1) |
|
331 type_tag = range_constant; |
|
332 else |
|
333 { |
|
334 delete range; |
|
335 if (nel == 1) |
|
336 { |
|
337 scalar = b; |
|
338 type_tag = scalar_constant; |
|
339 } |
|
340 else if (nel == 0) |
|
341 { |
|
342 matrix = new Matrix (); |
|
343 type_tag = matrix_constant; |
|
344 } |
|
345 else |
|
346 panic_impossible (); |
|
347 } |
581
|
348 orig_text = 0; |
492
|
349 } |
|
350 |
631
|
351 TC_REP::tree_constant_rep (const Range& r) |
492
|
352 { |
|
353 if (r.nelem () > 1) |
|
354 { |
|
355 range = new Range (r); |
|
356 type_tag = range_constant; |
|
357 } |
|
358 else if (r.nelem () == 1) |
|
359 { |
|
360 scalar = r.base (); |
|
361 type_tag = scalar_constant; |
|
362 } |
|
363 else if (r.nelem () == 0) |
|
364 { |
|
365 matrix = new Matrix (); |
|
366 type_tag = matrix_constant; |
|
367 } |
|
368 else |
|
369 panic_impossible (); |
581
|
370 |
|
371 orig_text = 0; |
492
|
372 } |
|
373 |
745
|
374 TC_REP::tree_constant_rep (const Octave_map& m) |
|
375 { |
|
376 a_map = new Octave_map (m); |
|
377 type_tag = map_constant; |
|
378 orig_text = 0; |
|
379 } |
|
380 |
631
|
381 TC_REP::tree_constant_rep (TC_REP::constant_type t) |
492
|
382 { |
|
383 assert (t == magic_colon); |
|
384 type_tag = magic_colon; |
581
|
385 orig_text = 0; |
492
|
386 } |
|
387 |
631
|
388 TC_REP::tree_constant_rep (const tree_constant_rep& t) |
492
|
389 { |
|
390 type_tag = t.type_tag; |
|
391 |
|
392 switch (t.type_tag) |
|
393 { |
|
394 case unknown_constant: |
|
395 break; |
631
|
396 |
492
|
397 case scalar_constant: |
|
398 scalar = t.scalar; |
|
399 break; |
631
|
400 |
492
|
401 case matrix_constant: |
|
402 matrix = new Matrix (*(t.matrix)); |
|
403 break; |
631
|
404 |
492
|
405 case string_constant: |
|
406 string = strsave (t.string); |
|
407 break; |
631
|
408 |
492
|
409 case complex_matrix_constant: |
|
410 complex_matrix = new ComplexMatrix (*(t.complex_matrix)); |
|
411 break; |
631
|
412 |
492
|
413 case complex_scalar_constant: |
|
414 complex_scalar = new Complex (*(t.complex_scalar)); |
|
415 break; |
631
|
416 |
492
|
417 case range_constant: |
|
418 range = new Range (*(t.range)); |
|
419 break; |
631
|
420 |
745
|
421 case map_constant: |
|
422 a_map = new Octave_map (*(t.a_map)); |
|
423 break; |
|
424 |
492
|
425 case magic_colon: |
|
426 break; |
631
|
427 |
492
|
428 default: |
|
429 panic_impossible (); |
|
430 break; |
|
431 } |
581
|
432 |
|
433 orig_text = strsave (t.orig_text); |
492
|
434 } |
|
435 |
631
|
436 TC_REP::~tree_constant_rep (void) |
492
|
437 { |
|
438 switch (type_tag) |
|
439 { |
|
440 case matrix_constant: |
|
441 delete matrix; |
|
442 break; |
631
|
443 |
492
|
444 case complex_scalar_constant: |
|
445 delete complex_scalar; |
|
446 break; |
631
|
447 |
492
|
448 case complex_matrix_constant: |
|
449 delete complex_matrix; |
|
450 break; |
631
|
451 |
492
|
452 case string_constant: |
|
453 delete [] string; |
|
454 break; |
631
|
455 |
492
|
456 case range_constant: |
|
457 delete range; |
|
458 break; |
631
|
459 |
745
|
460 case map_constant: |
|
461 delete a_map; |
|
462 break; |
|
463 |
492
|
464 default: |
|
465 break; |
|
466 } |
581
|
467 |
|
468 delete [] orig_text; |
492
|
469 } |
|
470 |
|
471 #if defined (MDEBUG) |
|
472 void * |
631
|
473 TC_REP::operator new (size_t size) |
492
|
474 { |
|
475 tree_constant_rep *p = ::new tree_constant_rep; |
631
|
476 cerr << "TC_REP::new(): " << p << "\n"; |
492
|
477 return p; |
|
478 } |
|
479 |
|
480 void |
631
|
481 TC_REP::operator delete (void *p, size_t size) |
492
|
482 { |
631
|
483 cerr << "TC_REP::delete(): " << p << "\n"; |
492
|
484 ::delete p; |
|
485 } |
|
486 #endif |
|
487 |
631
|
488 int |
|
489 TC_REP::rows (void) const |
492
|
490 { |
631
|
491 int retval = -1; |
581
|
492 |
|
493 switch (type_tag) |
|
494 { |
|
495 case scalar_constant: |
|
496 case complex_scalar_constant: |
631
|
497 retval = 1; |
|
498 break; |
|
499 |
581
|
500 case string_constant: |
|
501 case range_constant: |
631
|
502 retval = (columns () > 0); |
|
503 break; |
|
504 |
|
505 case matrix_constant: |
|
506 retval = matrix->rows (); |
|
507 break; |
|
508 |
|
509 case complex_matrix_constant: |
|
510 retval = complex_matrix->rows (); |
|
511 break; |
|
512 |
581
|
513 case magic_colon: |
631
|
514 ::error ("invalid use of colon operator"); |
|
515 break; |
|
516 |
|
517 case unknown_constant: |
745
|
518 case map_constant: |
|
519 retval = -1; |
631
|
520 break; |
|
521 |
492
|
522 default: |
|
523 panic_impossible (); |
|
524 break; |
|
525 } |
|
526 |
|
527 return retval; |
|
528 } |
|
529 |
|
530 int |
631
|
531 TC_REP::columns (void) const |
492
|
532 { |
|
533 int retval = -1; |
631
|
534 |
492
|
535 switch (type_tag) |
|
536 { |
|
537 case scalar_constant: |
|
538 case complex_scalar_constant: |
|
539 retval = 1; |
|
540 break; |
631
|
541 |
492
|
542 case matrix_constant: |
631
|
543 retval = matrix->columns (); |
|
544 break; |
|
545 |
492
|
546 case complex_matrix_constant: |
631
|
547 retval = complex_matrix->columns (); |
|
548 break; |
|
549 |
|
550 case string_constant: |
|
551 retval = strlen (string); |
|
552 break; |
|
553 |
|
554 case range_constant: |
|
555 retval = range->nelem (); |
|
556 break; |
|
557 |
492
|
558 case magic_colon: |
|
559 ::error ("invalid use of colon operator"); |
|
560 break; |
631
|
561 |
492
|
562 case unknown_constant: |
745
|
563 case map_constant: |
|
564 retval = -1; |
492
|
565 break; |
631
|
566 |
492
|
567 default: |
|
568 panic_impossible (); |
|
569 break; |
|
570 } |
631
|
571 |
492
|
572 return retval; |
|
573 } |
|
574 |
|
575 tree_constant |
631
|
576 TC_REP::all (void) const |
492
|
577 { |
|
578 if (type_tag == string_constant || type_tag == range_constant) |
|
579 { |
|
580 tree_constant tmp = make_numeric (); |
|
581 return tmp.all (); |
|
582 } |
|
583 |
|
584 tree_constant retval; |
631
|
585 |
492
|
586 switch (type_tag) |
|
587 { |
|
588 case scalar_constant: |
|
589 { |
|
590 double status = (scalar != 0.0); |
|
591 retval = tree_constant (status); |
|
592 } |
|
593 break; |
631
|
594 |
492
|
595 case matrix_constant: |
|
596 { |
|
597 Matrix m = matrix->all (); |
|
598 retval = tree_constant (m); |
|
599 } |
|
600 break; |
631
|
601 |
492
|
602 case complex_scalar_constant: |
|
603 { |
|
604 double status = (*complex_scalar != 0.0); |
|
605 retval = tree_constant (status); |
|
606 } |
|
607 break; |
631
|
608 |
492
|
609 case complex_matrix_constant: |
|
610 { |
|
611 Matrix m = complex_matrix->all (); |
|
612 retval = tree_constant (m); |
|
613 } |
|
614 break; |
631
|
615 |
492
|
616 case string_constant: |
|
617 case range_constant: |
|
618 case magic_colon: |
|
619 default: |
|
620 panic_impossible (); |
|
621 break; |
|
622 } |
631
|
623 |
492
|
624 return retval; |
|
625 } |
|
626 |
|
627 tree_constant |
631
|
628 TC_REP::any (void) const |
492
|
629 { |
|
630 if (type_tag == string_constant || type_tag == range_constant) |
|
631 { |
|
632 tree_constant tmp = make_numeric (); |
|
633 return tmp.any (); |
|
634 } |
|
635 |
|
636 tree_constant retval; |
631
|
637 |
492
|
638 switch (type_tag) |
|
639 { |
|
640 case scalar_constant: |
|
641 { |
|
642 double status = (scalar != 0.0); |
|
643 retval = tree_constant (status); |
|
644 } |
|
645 break; |
631
|
646 |
492
|
647 case matrix_constant: |
|
648 { |
|
649 Matrix m = matrix->any (); |
|
650 retval = tree_constant (m); |
|
651 } |
|
652 break; |
631
|
653 |
492
|
654 case complex_scalar_constant: |
|
655 { |
|
656 double status = (*complex_scalar != 0.0); |
|
657 retval = tree_constant (status); |
|
658 } |
|
659 break; |
631
|
660 |
492
|
661 case complex_matrix_constant: |
|
662 { |
|
663 Matrix m = complex_matrix->any (); |
|
664 retval = tree_constant (m); |
|
665 } |
|
666 break; |
631
|
667 |
|
668 case string_constant: |
|
669 case range_constant: |
|
670 case magic_colon: |
|
671 default: |
|
672 panic_impossible (); |
|
673 break; |
|
674 } |
|
675 |
|
676 return retval; |
|
677 } |
|
678 |
|
679 int |
|
680 TC_REP::valid_as_scalar_index (void) const |
|
681 { |
|
682 return (type_tag == magic_colon |
|
683 || (type_tag == scalar_constant && NINT (scalar) == 1) |
|
684 || (type_tag == range_constant |
|
685 && range->nelem () == 1 && NINT (range->base ()) == 1)); |
|
686 } |
|
687 |
|
688 int |
|
689 TC_REP::is_true (void) const |
|
690 { |
|
691 if (type_tag == string_constant || type_tag == range_constant) |
|
692 { |
|
693 tree_constant tmp = make_numeric (); |
|
694 return tmp.is_true (); |
|
695 } |
|
696 |
|
697 int retval; |
|
698 |
|
699 switch (type_tag) |
|
700 { |
|
701 case scalar_constant: |
|
702 retval = (scalar != 0.0); |
|
703 break; |
|
704 |
|
705 case matrix_constant: |
|
706 { |
|
707 Matrix m = (matrix->all ()) . all (); |
|
708 retval = (m.rows () == 1 |
|
709 && m.columns () == 1 |
|
710 && m.elem (0, 0) != 0.0); |
|
711 } |
|
712 break; |
|
713 |
|
714 case complex_scalar_constant: |
|
715 retval = (*complex_scalar != 0.0); |
|
716 break; |
|
717 |
|
718 case complex_matrix_constant: |
|
719 { |
|
720 Matrix m = (complex_matrix->all ()) . all (); |
|
721 retval = (m.rows () == 1 |
|
722 && m.columns () == 1 |
|
723 && m.elem (0, 0) != 0.0); |
|
724 } |
|
725 break; |
|
726 |
492
|
727 case string_constant: |
|
728 case range_constant: |
|
729 case magic_colon: |
|
730 default: |
|
731 panic_impossible (); |
|
732 break; |
|
733 } |
631
|
734 |
|
735 return retval; |
|
736 } |
|
737 |
|
738 static void |
|
739 warn_implicit_conversion (const char *from, const char *to) |
|
740 { |
|
741 warning ("implicit conversion from %s to %s", from, to); |
|
742 } |
|
743 |
|
744 double |
|
745 TC_REP::double_value (int force_string_conversion) const |
|
746 { |
|
747 double retval = octave_NaN; |
|
748 |
|
749 switch (type_tag) |
|
750 { |
|
751 case scalar_constant: |
|
752 retval = scalar; |
|
753 break; |
|
754 |
|
755 case matrix_constant: |
|
756 { |
|
757 if (user_pref.do_fortran_indexing && rows () > 0 && columns () > 0) |
|
758 retval = matrix->elem (0, 0); |
|
759 else |
|
760 gripe_invalid_conversion ("real matrix", "real scalar"); |
|
761 } |
|
762 break; |
|
763 |
|
764 case complex_matrix_constant: |
|
765 case complex_scalar_constant: |
|
766 { |
|
767 int flag = user_pref.ok_to_lose_imaginary_part; |
|
768 |
|
769 if (flag < 0) |
|
770 warn_implicit_conversion ("complex scalar", "real scalar"); |
|
771 |
|
772 if (flag) |
|
773 { |
|
774 if (type_tag == complex_scalar_constant) |
|
775 retval = ::real (*complex_scalar); |
|
776 else if (type_tag == complex_matrix_constant) |
|
777 { |
|
778 if (user_pref.do_fortran_indexing |
|
779 && rows () > 0 && columns () > 0) |
|
780 retval = ::real (complex_matrix->elem (0, 0)); |
|
781 else |
|
782 gripe_invalid_conversion ("complex matrix", "real scalar"); |
|
783 } |
|
784 else |
|
785 panic_impossible (); |
|
786 } |
|
787 else |
|
788 gripe_invalid_conversion ("complex scalar", "real scalar"); |
|
789 } |
|
790 break; |
|
791 |
|
792 case string_constant: |
|
793 { |
|
794 int flag = force_string_conversion; |
|
795 if (! flag) |
|
796 flag = user_pref.implicit_str_to_num_ok; |
|
797 |
|
798 if (flag < 0) |
|
799 warn_implicit_conversion ("string", "real scalar"); |
|
800 |
|
801 int len = strlen (string); |
|
802 if (flag && (len == 1 || (len > 1 && user_pref.do_fortran_indexing))) |
|
803 retval = toascii ((int) string[0]); |
|
804 else |
|
805 gripe_invalid_conversion ("string", "real scalar"); |
|
806 } |
|
807 break; |
|
808 |
|
809 case range_constant: |
|
810 { |
|
811 int nel = range->nelem (); |
|
812 if (nel == 1 || (nel > 1 && user_pref.do_fortran_indexing)) |
|
813 retval = range->base (); |
|
814 else |
|
815 gripe_invalid_conversion ("range", "real scalar"); |
|
816 } |
|
817 break; |
|
818 |
|
819 default: |
|
820 gripe_invalid_conversion (type_as_string (), "real scalar"); |
|
821 break; |
|
822 } |
|
823 |
|
824 return retval; |
|
825 } |
|
826 |
|
827 Matrix |
|
828 TC_REP::matrix_value (int force_string_conversion) const |
|
829 { |
|
830 Matrix retval; |
|
831 |
|
832 switch (type_tag) |
|
833 { |
|
834 case scalar_constant: |
|
835 retval = Matrix (1, 1, scalar); |
|
836 break; |
|
837 |
|
838 case matrix_constant: |
|
839 retval = *matrix; |
|
840 break; |
|
841 |
|
842 case complex_scalar_constant: |
|
843 case complex_matrix_constant: |
|
844 { |
|
845 int flag = user_pref.ok_to_lose_imaginary_part; |
|
846 if (flag < 0) |
|
847 warn_implicit_conversion ("complex matrix", "real matrix"); |
|
848 |
|
849 if (flag) |
|
850 { |
|
851 if (type_tag == complex_scalar_constant) |
|
852 retval = Matrix (1, 1, ::real (*complex_scalar)); |
|
853 else if (type_tag == complex_matrix_constant) |
|
854 retval = ::real (*complex_matrix); |
|
855 else |
|
856 panic_impossible (); |
|
857 } |
|
858 else |
|
859 gripe_invalid_conversion ("complex matrix", "real matrix"); |
|
860 } |
|
861 break; |
|
862 |
|
863 case string_constant: |
|
864 { |
|
865 int flag = force_string_conversion; |
|
866 if (! flag) |
|
867 flag = user_pref.implicit_str_to_num_ok; |
|
868 |
|
869 if (flag < 0) |
|
870 warn_implicit_conversion ("string", "real matrix"); |
|
871 |
|
872 if (flag) |
|
873 { |
|
874 int len = strlen (string); |
|
875 |
|
876 retval.resize (1, len); |
|
877 |
|
878 if (len > 1) |
|
879 { |
|
880 for (int i = 0; i < len; i++) |
|
881 retval.elem (0, i) = toascii ((int) string[i]); |
|
882 } |
|
883 else if (len == 1) |
|
884 retval.elem (0, 0) = toascii ((int) string[0]); |
|
885 else |
|
886 panic_impossible (); |
|
887 } |
|
888 else |
|
889 gripe_invalid_conversion ("string", "real matrix"); |
|
890 } |
|
891 break; |
|
892 |
|
893 case range_constant: |
|
894 retval = range->matrix_value (); |
|
895 break; |
|
896 |
|
897 default: |
|
898 gripe_invalid_conversion (type_as_string (), "real matrix"); |
|
899 break; |
|
900 } |
|
901 |
|
902 return retval; |
|
903 } |
|
904 |
|
905 Complex |
|
906 TC_REP::complex_value (int force_string_conversion) const |
|
907 { |
636
|
908 Complex retval (octave_NaN, octave_NaN); |
631
|
909 |
|
910 switch (type_tag) |
|
911 { |
|
912 case complex_scalar_constant: |
|
913 retval = *complex_scalar; |
|
914 break; |
|
915 |
|
916 case scalar_constant: |
|
917 retval = scalar; |
|
918 break; |
|
919 |
|
920 case complex_matrix_constant: |
|
921 case matrix_constant: |
|
922 { |
|
923 if (user_pref.do_fortran_indexing && rows () > 0 && columns () > 0) |
|
924 { |
|
925 if (type_tag == complex_matrix_constant) |
|
926 retval = complex_matrix->elem (0, 0); |
|
927 else |
|
928 retval = matrix->elem (0, 0); |
|
929 } |
|
930 else |
|
931 gripe_invalid_conversion ("real matrix", "real scalar"); |
|
932 } |
|
933 break; |
|
934 |
|
935 case string_constant: |
|
936 { |
|
937 int flag = force_string_conversion; |
|
938 if (! flag) |
|
939 flag = user_pref.implicit_str_to_num_ok; |
|
940 |
|
941 if (flag < 0) |
|
942 warn_implicit_conversion ("string", "complex scalar"); |
|
943 |
|
944 int len = strlen (string); |
|
945 if (flag && (len == 1 || (len > 1 && user_pref.do_fortran_indexing))) |
|
946 retval = toascii ((int) string[0]); |
|
947 else |
|
948 gripe_invalid_conversion ("string", "complex scalar"); |
|
949 } |
|
950 break; |
|
951 |
|
952 case range_constant: |
|
953 { |
|
954 int nel = range->nelem (); |
|
955 if (nel == 1 || (nel > 1 && user_pref.do_fortran_indexing)) |
|
956 retval = range->base (); |
|
957 else |
|
958 gripe_invalid_conversion ("range", "complex scalar"); |
|
959 } |
|
960 break; |
|
961 |
|
962 default: |
|
963 gripe_invalid_conversion (type_as_string (), "complex scalar"); |
|
964 break; |
|
965 } |
|
966 |
|
967 return retval; |
|
968 } |
|
969 |
|
970 ComplexMatrix |
|
971 TC_REP::complex_matrix_value (int force_string_conversion) const |
|
972 { |
|
973 ComplexMatrix retval; |
|
974 |
|
975 switch (type_tag) |
|
976 { |
|
977 case scalar_constant: |
|
978 retval = ComplexMatrix (1, 1, Complex (scalar)); |
|
979 break; |
|
980 |
|
981 case complex_scalar_constant: |
|
982 retval = ComplexMatrix (1, 1, *complex_scalar); |
|
983 break; |
|
984 |
|
985 case matrix_constant: |
|
986 retval = ComplexMatrix (*matrix); |
|
987 break; |
|
988 |
|
989 case complex_matrix_constant: |
|
990 retval = *complex_matrix; |
|
991 break; |
|
992 |
|
993 case string_constant: |
|
994 { |
|
995 int flag = force_string_conversion; |
|
996 if (! flag) |
|
997 flag = user_pref.implicit_str_to_num_ok; |
|
998 |
|
999 if (flag < 0) |
|
1000 warn_implicit_conversion ("string", "complex matrix"); |
|
1001 |
|
1002 if (flag) |
|
1003 { |
|
1004 int len = strlen (string); |
|
1005 |
|
1006 retval.resize (1, len); |
|
1007 |
|
1008 if (len > 1) |
|
1009 { |
|
1010 for (int i = 0; i < len; i++) |
|
1011 retval.elem (0, i) = toascii ((int) string[i]); |
|
1012 } |
|
1013 else if (len == 1) |
|
1014 retval.elem (0, 0) = toascii ((int) string[0]); |
|
1015 else |
|
1016 panic_impossible (); |
|
1017 } |
|
1018 else |
|
1019 gripe_invalid_conversion ("string", "real matrix"); |
|
1020 } |
|
1021 break; |
|
1022 |
|
1023 case range_constant: |
|
1024 retval = range->matrix_value (); |
|
1025 break; |
|
1026 |
|
1027 default: |
|
1028 gripe_invalid_conversion (type_as_string (), "complex matrix"); |
|
1029 break; |
|
1030 } |
|
1031 |
|
1032 return retval; |
|
1033 } |
|
1034 |
|
1035 char * |
|
1036 TC_REP::string_value (void) const |
|
1037 { |
636
|
1038 if (type_tag == string_constant) |
|
1039 return string; |
|
1040 else |
|
1041 { |
|
1042 gripe_invalid_conversion (type_as_string (), "string"); |
|
1043 return 0; |
|
1044 } |
631
|
1045 } |
|
1046 |
|
1047 Range |
|
1048 TC_REP::range_value (void) const |
|
1049 { |
|
1050 assert (type_tag == range_constant); |
|
1051 return *range; |
|
1052 } |
|
1053 |
745
|
1054 Octave_map |
|
1055 TC_REP::map_value (void) const |
|
1056 { |
|
1057 assert (type_tag == map_constant); |
|
1058 return *a_map; |
|
1059 } |
|
1060 |
|
1061 tree_constant& |
|
1062 TC_REP::lookup_map_element (const char *name, int insert) |
|
1063 { |
|
1064 static tree_constant retval; |
|
1065 |
|
1066 if (type_tag == map_constant) |
|
1067 { |
|
1068 Pix idx = a_map->seek (name); |
|
1069 |
|
1070 if (idx) |
|
1071 return a_map->contents (idx); |
|
1072 else if (insert) |
|
1073 return (*a_map) [name]; |
|
1074 else |
|
1075 error ("structure has no member `%s'", name); |
|
1076 } |
|
1077 else |
|
1078 error ("invalid structure access attempted"); |
|
1079 |
|
1080 return retval; |
|
1081 } |
|
1082 |
631
|
1083 // This could be made more efficient by doing all the work here rather |
|
1084 // than relying on matrix_value() to do any possible type conversions. |
|
1085 |
|
1086 ColumnVector |
|
1087 TC_REP::vector_value (int force_string_conversion, |
|
1088 int force_vector_conversion) const |
|
1089 { |
|
1090 ColumnVector retval; |
|
1091 |
|
1092 Matrix m = matrix_value (force_string_conversion); |
|
1093 |
|
1094 if (error_state) |
|
1095 return retval; |
|
1096 |
|
1097 int nr = m.rows (); |
|
1098 int nc = m.columns (); |
|
1099 if (nr == 1) |
|
1100 { |
|
1101 retval.resize (nc); |
|
1102 for (int i = 0; i < nc; i++) |
|
1103 retval.elem (i) = m (0, i); |
|
1104 } |
|
1105 else if (nc == 1) |
|
1106 { |
|
1107 retval.resize (nr); |
|
1108 for (int i = 0; i < nr; i++) |
|
1109 retval.elem (i) = m.elem (i, 0); |
|
1110 } |
|
1111 else if (nr > 0 && nc > 0 |
|
1112 && (user_pref.do_fortran_indexing || force_vector_conversion)) |
|
1113 { |
|
1114 retval.resize (nr * nc); |
|
1115 int k = 0; |
|
1116 for (int j = 0; j < nc; j++) |
|
1117 for (int i = 0; i < nr; i++) |
|
1118 retval.elem (k++) = m.elem (i, j); |
|
1119 } |
|
1120 else |
|
1121 gripe_invalid_conversion ("real matrix", "real vector"); |
|
1122 |
|
1123 return retval; |
|
1124 } |
|
1125 |
|
1126 // This could be made more efficient by doing all the work here rather |
|
1127 // than relying on complex_matrix_value() to do any possible type |
|
1128 // conversions. |
|
1129 |
|
1130 ComplexColumnVector |
|
1131 TC_REP::complex_vector_value (int force_string_conversion, |
|
1132 int force_vector_conversion) const |
|
1133 { |
|
1134 ComplexColumnVector retval; |
|
1135 |
|
1136 ComplexMatrix m = complex_matrix_value (force_string_conversion); |
|
1137 |
|
1138 if (error_state) |
|
1139 return retval; |
|
1140 |
|
1141 int nr = m.rows (); |
|
1142 int nc = m.columns (); |
|
1143 if (nr == 1) |
|
1144 { |
|
1145 retval.resize (nc); |
|
1146 for (int i = 0; i < nc; i++) |
|
1147 retval.elem (i) = m (0, i); |
|
1148 } |
|
1149 else if (nc == 1) |
|
1150 { |
|
1151 retval.resize (nr); |
|
1152 for (int i = 0; i < nr; i++) |
|
1153 retval.elem (i) = m.elem (i, 0); |
|
1154 } |
|
1155 else if (nr > 0 && nc > 0 |
|
1156 && (user_pref.do_fortran_indexing || force_vector_conversion)) |
|
1157 { |
|
1158 retval.resize (nr * nc); |
|
1159 int k = 0; |
|
1160 for (int j = 0; j < nc; j++) |
|
1161 for (int i = 0; i < nr; i++) |
|
1162 retval.elem (k++) = m.elem (i, j); |
|
1163 } |
|
1164 else |
|
1165 gripe_invalid_conversion ("complex matrix", "complex vector"); |
|
1166 |
492
|
1167 return retval; |
|
1168 } |
|
1169 |
|
1170 tree_constant |
745
|
1171 TC_REP::convert_to_str (void) const |
492
|
1172 { |
|
1173 tree_constant retval; |
|
1174 |
|
1175 switch (type_tag) |
|
1176 { |
|
1177 case complex_scalar_constant: |
|
1178 case scalar_constant: |
|
1179 { |
|
1180 double d = double_value (); |
|
1181 int i = NINT (d); |
|
1182 // Warn about out of range conversions? |
|
1183 char s[2]; |
|
1184 s[0] = (char) i; |
|
1185 s[1] = '\0'; |
|
1186 retval = tree_constant (s); |
|
1187 } |
|
1188 break; |
631
|
1189 |
492
|
1190 case complex_matrix_constant: |
|
1191 case matrix_constant: |
|
1192 { |
631
|
1193 ColumnVector v = vector_value (); |
492
|
1194 int len = v.length (); |
|
1195 if (len == 0) |
|
1196 ::error ("can only convert vectors and scalars to strings"); |
|
1197 else |
|
1198 { |
|
1199 char *s = new char [len+1]; |
|
1200 s[len] = '\0'; |
|
1201 for (int i = 0; i < len; i++) |
|
1202 { |
|
1203 double d = v.elem (i); |
|
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 } |
|
1212 break; |
631
|
1213 |
492
|
1214 case range_constant: |
|
1215 { |
|
1216 Range r = range_value (); |
|
1217 double b = r.base (); |
|
1218 double incr = r.inc (); |
|
1219 int nel = r.nelem (); |
|
1220 char *s = new char [nel+1]; |
|
1221 s[nel] = '\0'; |
|
1222 for (int i = 0; i < nel; i++) |
|
1223 { |
|
1224 double d = b + i * incr; |
|
1225 int ival = NINT (d); |
|
1226 // Warn about out of range conversions? |
|
1227 s[i] = (char) ival; |
|
1228 } |
|
1229 retval = tree_constant (s); |
|
1230 delete [] s; |
|
1231 } |
|
1232 break; |
631
|
1233 |
492
|
1234 case string_constant: |
581
|
1235 retval = string; |
492
|
1236 break; |
631
|
1237 |
492
|
1238 case magic_colon: |
|
1239 default: |
|
1240 panic_impossible (); |
|
1241 break; |
|
1242 } |
631
|
1243 |
492
|
1244 return retval; |
|
1245 } |
|
1246 |
|
1247 void |
631
|
1248 TC_REP::convert_to_row_or_column_vector (void) |
492
|
1249 { |
|
1250 assert (type_tag == matrix_constant || type_tag == complex_matrix_constant); |
|
1251 |
|
1252 int nr = rows (); |
|
1253 int nc = columns (); |
|
1254 |
509
|
1255 if (nr == 1 || nc == 1) |
|
1256 return; |
|
1257 |
492
|
1258 int len = nr * nc; |
|
1259 |
|
1260 assert (len > 0); |
|
1261 |
|
1262 int new_nr = 1; |
|
1263 int new_nc = 1; |
|
1264 |
|
1265 if (user_pref.prefer_column_vectors) |
|
1266 new_nr = len; |
|
1267 else |
|
1268 new_nc = len; |
|
1269 |
|
1270 if (type_tag == matrix_constant) |
|
1271 { |
|
1272 Matrix *m = new Matrix (new_nr, new_nc); |
|
1273 |
|
1274 double *cop_out = matrix->fortran_vec (); |
|
1275 |
|
1276 for (int i = 0; i < len; i++) |
|
1277 { |
|
1278 if (new_nr == 1) |
|
1279 m->elem (0, i) = *cop_out++; |
|
1280 else |
|
1281 m->elem (i, 0) = *cop_out++; |
|
1282 } |
|
1283 |
|
1284 delete matrix; |
|
1285 matrix = m; |
|
1286 } |
|
1287 else |
|
1288 { |
|
1289 ComplexMatrix *cm = new ComplexMatrix (new_nr, new_nc); |
|
1290 |
|
1291 Complex *cop_out = complex_matrix->fortran_vec (); |
|
1292 |
|
1293 for (int i = 0; i < len; i++) |
|
1294 { |
|
1295 if (new_nr == 1) |
|
1296 cm->elem (0, i) = *cop_out++; |
|
1297 else |
|
1298 cm->elem (i, 0) = *cop_out++; |
|
1299 } |
|
1300 |
|
1301 delete complex_matrix; |
|
1302 complex_matrix = cm; |
|
1303 } |
|
1304 } |
|
1305 |
631
|
1306 void |
|
1307 TC_REP::force_numeric (int force_str_conv) |
492
|
1308 { |
|
1309 switch (type_tag) |
|
1310 { |
|
1311 case scalar_constant: |
|
1312 case matrix_constant: |
|
1313 case complex_scalar_constant: |
|
1314 case complex_matrix_constant: |
631
|
1315 break; |
|
1316 |
|
1317 case string_constant: |
492
|
1318 { |
631
|
1319 if (! force_str_conv && ! user_pref.implicit_str_to_num_ok) |
|
1320 { |
|
1321 ::error ("failed to convert `%s' to a numeric type --", string); |
|
1322 ::error ("default conversion turned off"); |
|
1323 // Abort! |
|
1324 jump_to_top_level (); |
|
1325 } |
|
1326 |
|
1327 int len = strlen (string); |
|
1328 if (len > 1) |
|
1329 { |
|
1330 type_tag = matrix_constant; |
|
1331 Matrix *tm = new Matrix (1, len); |
|
1332 for (int i = 0; i < len; i++) |
|
1333 tm->elem (0, i) = toascii ((int) string[i]); |
|
1334 matrix = tm; |
|
1335 } |
|
1336 else if (len == 1) |
|
1337 { |
|
1338 type_tag = scalar_constant; |
|
1339 scalar = toascii ((int) string[0]); |
|
1340 } |
|
1341 else if (len == 0) |
|
1342 { |
|
1343 type_tag = matrix_constant; |
|
1344 matrix = new Matrix (0, 0); |
|
1345 } |
|
1346 else |
|
1347 panic_impossible (); |
492
|
1348 } |
|
1349 break; |
631
|
1350 |
492
|
1351 case range_constant: |
631
|
1352 { |
|
1353 int len = range->nelem (); |
|
1354 if (len > 1) |
|
1355 { |
|
1356 type_tag = matrix_constant; |
|
1357 Matrix *tm = new Matrix (1, len); |
|
1358 double b = range->base (); |
|
1359 double increment = range->inc (); |
|
1360 for (int i = 0; i < len; i++) |
|
1361 tm->elem (0, i) = b + i * increment; |
|
1362 matrix = tm; |
|
1363 } |
|
1364 else if (len == 1) |
|
1365 { |
|
1366 type_tag = scalar_constant; |
|
1367 scalar = range->base (); |
|
1368 } |
|
1369 } |
|
1370 break; |
|
1371 |
492
|
1372 case magic_colon: |
|
1373 default: |
|
1374 panic_impossible (); |
|
1375 break; |
|
1376 } |
|
1377 } |
|
1378 |
|
1379 tree_constant |
631
|
1380 TC_REP::make_numeric (int force_str_conv) const |
492
|
1381 { |
|
1382 tree_constant retval; |
631
|
1383 |
492
|
1384 switch (type_tag) |
|
1385 { |
|
1386 case scalar_constant: |
|
1387 retval = tree_constant (scalar); |
|
1388 break; |
631
|
1389 |
492
|
1390 case matrix_constant: |
631
|
1391 retval = tree_constant (*matrix); |
|
1392 break; |
|
1393 |
492
|
1394 case complex_scalar_constant: |
|
1395 retval = tree_constant (*complex_scalar); |
|
1396 break; |
631
|
1397 |
492
|
1398 case complex_matrix_constant: |
631
|
1399 retval = tree_constant (*complex_matrix); |
|
1400 break; |
|
1401 |
492
|
1402 case string_constant: |
631
|
1403 retval = tree_constant (string); |
|
1404 retval.force_numeric (force_str_conv); |
|
1405 break; |
|
1406 |
492
|
1407 case range_constant: |
631
|
1408 retval = tree_constant (*range); |
|
1409 retval.force_numeric (force_str_conv); |
|
1410 break; |
|
1411 |
492
|
1412 case magic_colon: |
|
1413 default: |
|
1414 panic_impossible (); |
|
1415 break; |
|
1416 } |
631
|
1417 |
|
1418 return retval; |
|
1419 } |
|
1420 |
|
1421 void |
|
1422 TC_REP::bump_value (tree_expression::type etype) |
|
1423 { |
|
1424 switch (etype) |
|
1425 { |
|
1426 case tree_expression::increment: |
|
1427 switch (type_tag) |
|
1428 { |
|
1429 case scalar_constant: |
|
1430 scalar++; |
|
1431 break; |
|
1432 |
|
1433 case matrix_constant: |
|
1434 *matrix = *matrix + 1.0; |
|
1435 break; |
|
1436 |
|
1437 case complex_scalar_constant: |
|
1438 *complex_scalar = *complex_scalar + 1.0; |
|
1439 break; |
|
1440 |
|
1441 case complex_matrix_constant: |
|
1442 *complex_matrix = *complex_matrix + 1.0; |
|
1443 break; |
|
1444 |
|
1445 case string_constant: |
|
1446 ::error ("string++ and ++string not implemented yet, ok?"); |
|
1447 break; |
|
1448 |
|
1449 case range_constant: |
|
1450 range->set_base (range->base () + 1.0); |
|
1451 range->set_limit (range->limit () + 1.0); |
|
1452 break; |
|
1453 |
|
1454 case magic_colon: |
|
1455 default: |
|
1456 panic_impossible (); |
|
1457 break; |
|
1458 } |
|
1459 break; |
|
1460 |
|
1461 case tree_expression::decrement: |
|
1462 switch (type_tag) |
|
1463 { |
|
1464 case scalar_constant: |
|
1465 scalar--; |
|
1466 break; |
|
1467 |
|
1468 case matrix_constant: |
|
1469 *matrix = *matrix - 1.0; |
|
1470 break; |
|
1471 |
|
1472 case string_constant: |
|
1473 ::error ("string-- and -- string not implemented yet, ok?"); |
|
1474 break; |
|
1475 |
|
1476 case range_constant: |
|
1477 range->set_base (range->base () - 1.0); |
|
1478 range->set_limit (range->limit () - 1.0); |
|
1479 break; |
|
1480 |
|
1481 case magic_colon: |
|
1482 default: |
|
1483 panic_impossible (); |
|
1484 break; |
|
1485 } |
|
1486 break; |
|
1487 |
|
1488 default: |
|
1489 panic_impossible (); |
|
1490 break; |
|
1491 } |
|
1492 } |
|
1493 |
|
1494 void |
|
1495 TC_REP::resize (int i, int j) |
|
1496 { |
|
1497 switch (type_tag) |
|
1498 { |
|
1499 case matrix_constant: |
|
1500 matrix->resize (i, j); |
|
1501 break; |
|
1502 |
|
1503 case complex_matrix_constant: |
|
1504 complex_matrix->resize (i, j); |
|
1505 break; |
|
1506 |
|
1507 default: |
|
1508 panic_impossible (); |
|
1509 break; |
|
1510 } |
|
1511 } |
|
1512 |
|
1513 void |
|
1514 TC_REP::resize (int i, int j, double val) |
|
1515 { |
|
1516 switch (type_tag) |
|
1517 { |
|
1518 case matrix_constant: |
|
1519 matrix->resize (i, j, val); |
|
1520 break; |
|
1521 |
|
1522 case complex_matrix_constant: |
|
1523 complex_matrix->resize (i, j, val); |
|
1524 break; |
|
1525 |
|
1526 default: |
|
1527 panic_impossible (); |
|
1528 break; |
|
1529 } |
|
1530 } |
|
1531 |
|
1532 void |
|
1533 TC_REP::maybe_resize (int i, int j) |
|
1534 { |
|
1535 int nr = rows (); |
|
1536 int nc = columns (); |
|
1537 |
|
1538 i++; |
|
1539 j++; |
|
1540 |
|
1541 assert (i > 0 && j > 0); |
|
1542 |
|
1543 if (i > nr || j > nc) |
|
1544 { |
|
1545 if (user_pref.resize_on_range_error) |
|
1546 resize (MAX (i, nr), MAX (j, nc), 0.0); |
|
1547 else |
|
1548 { |
|
1549 if (i > nr) |
|
1550 ::error ("row index = %d exceeds max row dimension = %d", i, nr); |
|
1551 |
|
1552 if (j > nc) |
|
1553 ::error ("column index = %d exceeds max column dimension = %d", |
|
1554 j, nc); |
|
1555 } |
|
1556 } |
|
1557 } |
|
1558 |
|
1559 void |
|
1560 TC_REP::maybe_resize (int i, force_orient f_orient) |
|
1561 { |
|
1562 int nr = rows (); |
|
1563 int nc = columns (); |
|
1564 |
|
1565 i++; |
|
1566 |
|
1567 assert (i >= 0 && (nr <= 1 || nc <= 1)); |
|
1568 |
|
1569 // This function never reduces the size of a vector, and all vectors |
|
1570 // have dimensions of at least 0x0. If i is 0, it is either because |
|
1571 // a vector has been indexed with a vector of all zeros (in which case |
|
1572 // the index vector is empty and nothing will happen) or a vector has |
|
1573 // been indexed with 0 (an error which will be caught elsewhere). |
|
1574 if (i == 0) |
|
1575 return; |
|
1576 |
|
1577 if (nr <= 1 && nc <= 1 && i >= 1) |
|
1578 { |
|
1579 if (user_pref.resize_on_range_error) |
|
1580 { |
|
1581 if (f_orient == row_orient) |
|
1582 resize (1, i, 0.0); |
|
1583 else if (f_orient == column_orient) |
|
1584 resize (i, 1, 0.0); |
|
1585 else if (user_pref.prefer_column_vectors) |
|
1586 resize (i, 1, 0.0); |
|
1587 else |
|
1588 resize (1, i, 0.0); |
|
1589 } |
|
1590 else |
|
1591 ::error ("matrix index = %d exceeds max dimension = %d", i, nc); |
|
1592 } |
|
1593 else if (nr == 1 && i > nc) |
|
1594 { |
|
1595 if (user_pref.resize_on_range_error) |
|
1596 resize (1, i, 0.0); |
|
1597 else |
|
1598 ::error ("matrix index = %d exceeds max dimension = %d", i, nc); |
|
1599 } |
|
1600 else if (nc == 1 && i > nr) |
|
1601 { |
|
1602 if (user_pref.resize_on_range_error) |
|
1603 resize (i, 1, 0.0); |
|
1604 else |
|
1605 ::error ("matrix index = %d exceeds max dimension = ", i, nc); |
|
1606 } |
|
1607 } |
|
1608 |
|
1609 void |
|
1610 TC_REP::stash_original_text (char *s) |
|
1611 { |
|
1612 orig_text = strsave (s); |
|
1613 } |
|
1614 |
|
1615 void |
|
1616 TC_REP::maybe_mutate (void) |
492
|
1617 { |
631
|
1618 if (error_state) |
|
1619 return; |
581
|
1620 |
|
1621 switch (type_tag) |
|
1622 { |
|
1623 case complex_scalar_constant: |
631
|
1624 if (::imag (*complex_scalar) == 0.0) |
|
1625 { |
|
1626 double d = ::real (*complex_scalar); |
|
1627 delete complex_scalar; |
|
1628 scalar = d; |
|
1629 type_tag = scalar_constant; |
|
1630 } |
|
1631 break; |
|
1632 |
|
1633 case complex_matrix_constant: |
|
1634 if (! any_element_is_complex (*complex_matrix)) |
|
1635 { |
|
1636 Matrix *m = new Matrix (::real (*complex_matrix)); |
|
1637 delete complex_matrix; |
|
1638 matrix = m; |
|
1639 type_tag = matrix_constant; |
|
1640 } |
|
1641 break; |
|
1642 |
581
|
1643 case scalar_constant: |
631
|
1644 case matrix_constant: |
|
1645 case string_constant: |
|
1646 case range_constant: |
745
|
1647 case map_constant: |
631
|
1648 case magic_colon: |
|
1649 break; |
|
1650 |
|
1651 default: |
|
1652 panic_impossible (); |
|
1653 break; |
|
1654 } |
|
1655 |
|
1656 // Avoid calling rows() and columns() for things like magic_colon. |
|
1657 |
|
1658 int nr = 1; |
|
1659 int nc = 1; |
|
1660 if (type_tag == matrix_constant |
|
1661 || type_tag == complex_matrix_constant |
|
1662 || type_tag == range_constant) |
|
1663 { |
|
1664 nr = rows (); |
|
1665 nc = columns (); |
|
1666 } |
|
1667 |
|
1668 switch (type_tag) |
|
1669 { |
|
1670 case matrix_constant: |
|
1671 if (nr == 1 && nc == 1) |
|
1672 { |
|
1673 double d = matrix->elem (0, 0); |
|
1674 delete matrix; |
|
1675 scalar = d; |
|
1676 type_tag = scalar_constant; |
|
1677 } |
|
1678 break; |
|
1679 |
581
|
1680 case complex_matrix_constant: |
631
|
1681 if (nr == 1 && nc == 1) |
|
1682 { |
|
1683 Complex c = complex_matrix->elem (0, 0); |
|
1684 delete complex_matrix; |
|
1685 complex_scalar = new Complex (c); |
|
1686 type_tag = complex_scalar_constant; |
|
1687 } |
|
1688 break; |
|
1689 |
|
1690 case range_constant: |
|
1691 if (nr == 1 && nc == 1) |
|
1692 { |
|
1693 double d = range->base (); |
|
1694 delete range; |
|
1695 scalar = d; |
|
1696 type_tag = scalar_constant; |
|
1697 } |
|
1698 break; |
|
1699 |
|
1700 default: |
|
1701 break; |
|
1702 } |
|
1703 } |
|
1704 |
|
1705 void |
|
1706 TC_REP::print (void) |
|
1707 { |
|
1708 if (error_state) |
|
1709 return; |
|
1710 |
|
1711 if (print) |
|
1712 { |
|
1713 ostrstream output_buf; |
|
1714 |
|
1715 switch (type_tag) |
|
1716 { |
|
1717 case scalar_constant: |
|
1718 octave_print_internal (output_buf, scalar); |
|
1719 break; |
|
1720 |
|
1721 case matrix_constant: |
|
1722 octave_print_internal (output_buf, *matrix); |
|
1723 break; |
|
1724 |
|
1725 case complex_scalar_constant: |
|
1726 octave_print_internal (output_buf, *complex_scalar); |
|
1727 break; |
|
1728 |
|
1729 case complex_matrix_constant: |
|
1730 octave_print_internal (output_buf, *complex_matrix); |
|
1731 break; |
|
1732 |
|
1733 case string_constant: |
|
1734 output_buf << string << "\n"; |
|
1735 break; |
|
1736 |
|
1737 case range_constant: |
|
1738 octave_print_internal (output_buf, *range); |
|
1739 break; |
|
1740 |
745
|
1741 case map_constant: |
|
1742 { |
|
1743 output_buf << "<structure"; |
|
1744 int first = 1; |
|
1745 for (Pix p = a_map->first (); p != 0; a_map->next (p)) |
|
1746 { |
|
1747 if (first) |
|
1748 { |
|
1749 output_buf << ":"; |
|
1750 first = 0; |
|
1751 } |
|
1752 output_buf << " " << a_map->key (p); |
|
1753 } |
|
1754 output_buf << ">\n"; |
|
1755 } |
|
1756 break; |
|
1757 |
631
|
1758 case magic_colon: |
|
1759 default: |
|
1760 panic_impossible (); |
|
1761 break; |
|
1762 } |
|
1763 |
|
1764 output_buf << ends; |
|
1765 maybe_page_output (output_buf); |
|
1766 } |
|
1767 } |
|
1768 |
|
1769 static char * |
|
1770 undo_string_escapes (char c) |
|
1771 { |
|
1772 static char retval[2]; |
|
1773 retval[1] = '\0'; |
|
1774 |
|
1775 if (! c) |
|
1776 return 0; |
|
1777 |
|
1778 switch (c) |
|
1779 { |
|
1780 case '\a': |
|
1781 return "\\a"; |
|
1782 |
|
1783 case '\b': // backspace |
|
1784 return "\\b"; |
|
1785 |
|
1786 case '\f': // formfeed |
|
1787 return "\\f"; |
|
1788 |
|
1789 case '\n': // newline |
|
1790 return "\\n"; |
|
1791 |
|
1792 case '\r': // carriage return |
|
1793 return "\\r"; |
|
1794 |
|
1795 case '\t': // horizontal tab |
|
1796 return "\\t"; |
|
1797 |
|
1798 case '\v': // vertical tab |
|
1799 return "\\v"; |
|
1800 |
|
1801 case '\\': // backslash |
|
1802 return "\\\\"; |
|
1803 |
|
1804 case '"': // double quote |
|
1805 return "\\\""; |
|
1806 |
|
1807 default: |
|
1808 retval[0] = c; |
|
1809 return retval; |
|
1810 } |
|
1811 } |
|
1812 |
|
1813 void |
|
1814 TC_REP::print_code (ostream& os) |
|
1815 { |
|
1816 switch (type_tag) |
|
1817 { |
|
1818 case scalar_constant: |
|
1819 if (orig_text) |
|
1820 os << orig_text; |
|
1821 else |
|
1822 octave_print_internal (os, scalar, 1); |
|
1823 break; |
|
1824 |
|
1825 case matrix_constant: |
|
1826 octave_print_internal (os, *matrix, 1); |
|
1827 break; |
|
1828 |
|
1829 case complex_scalar_constant: |
|
1830 { |
|
1831 double re = complex_scalar->real (); |
|
1832 double im = complex_scalar->imag (); |
|
1833 |
|
1834 // If we have the original text and a pure imaginary, just print the |
|
1835 // original text, because this must be a constant that was parsed as |
|
1836 // part of a function. |
|
1837 |
|
1838 if (orig_text && re == 0.0 && im > 0.0) |
|
1839 os << orig_text; |
|
1840 else |
|
1841 octave_print_internal (os, *complex_scalar, 1); |
|
1842 } |
|
1843 break; |
|
1844 |
|
1845 case complex_matrix_constant: |
|
1846 octave_print_internal (os, *complex_matrix, 1); |
|
1847 break; |
|
1848 |
|
1849 case string_constant: |
|
1850 { |
|
1851 os << "\""; |
|
1852 char *s, *t = string; |
|
1853 while (s = undo_string_escapes (*t++)) |
|
1854 os << s; |
|
1855 os << "\""; |
|
1856 } |
|
1857 break; |
|
1858 |
|
1859 case range_constant: |
|
1860 octave_print_internal (os, *range, 1); |
|
1861 break; |
|
1862 |
|
1863 case magic_colon: |
|
1864 os << ":"; |
|
1865 break; |
|
1866 |
|
1867 default: |
|
1868 panic_impossible (); |
|
1869 break; |
|
1870 } |
|
1871 } |
|
1872 |
|
1873 char * |
|
1874 TC_REP::type_as_string (void) const |
|
1875 { |
|
1876 switch (type_tag) |
|
1877 { |
|
1878 case scalar_constant: |
|
1879 return "real scalar"; |
|
1880 |
581
|
1881 case matrix_constant: |
631
|
1882 return "real matrix"; |
|
1883 |
|
1884 case complex_scalar_constant: |
|
1885 return "complex scalar"; |
|
1886 |
|
1887 case complex_matrix_constant: |
|
1888 return "complex matrix"; |
|
1889 |
|
1890 case string_constant: |
|
1891 return "string"; |
|
1892 |
581
|
1893 case range_constant: |
631
|
1894 return "range"; |
|
1895 |
|
1896 default: |
|
1897 return "<unknown type>"; |
|
1898 } |
|
1899 } |
|
1900 |
|
1901 tree_constant |
|
1902 do_binary_op (tree_constant& a, tree_constant& b, tree_expression::type t) |
|
1903 { |
|
1904 tree_constant ans; |
|
1905 |
|
1906 int first_empty = (a.rows () == 0 || a.columns () == 0); |
|
1907 int second_empty = (b.rows () == 0 || b.columns () == 0); |
|
1908 |
|
1909 if (first_empty || second_empty) |
|
1910 { |
|
1911 int flag = user_pref.propagate_empty_matrices; |
|
1912 if (flag < 0) |
|
1913 warning ("binary operation on empty matrix"); |
|
1914 else if (flag == 0) |
|
1915 { |
|
1916 ::error ("invalid binary operation on empty matrix"); |
|
1917 return ans; |
|
1918 } |
|
1919 } |
|
1920 |
|
1921 tree_constant tmp_a = a.make_numeric (); |
|
1922 tree_constant tmp_b = b.make_numeric (); |
|
1923 |
|
1924 TC_REP::constant_type a_type = tmp_a.const_type (); |
|
1925 TC_REP::constant_type b_type = tmp_b.const_type (); |
|
1926 |
|
1927 double d1, d2; |
|
1928 Matrix m1, m2; |
|
1929 Complex c1, c2; |
|
1930 ComplexMatrix cm1, cm2; |
|
1931 |
|
1932 switch (a_type) |
|
1933 { |
|
1934 case TC_REP::scalar_constant: |
|
1935 |
|
1936 d1 = tmp_a.double_value (); |
|
1937 |
|
1938 switch (b_type) |
|
1939 { |
|
1940 case TC_REP::scalar_constant: |
|
1941 d2 = tmp_b.double_value (); |
|
1942 ans = do_binary_op (d1, d2, t); |
|
1943 break; |
|
1944 |
|
1945 case TC_REP::matrix_constant: |
|
1946 m2 = tmp_b.matrix_value (); |
|
1947 ans = do_binary_op (d1, m2, t); |
|
1948 break; |
|
1949 |
|
1950 case TC_REP::complex_scalar_constant: |
|
1951 c2 = tmp_b.complex_value (); |
|
1952 ans = do_binary_op (d1, c2, t); |
|
1953 break; |
|
1954 |
|
1955 case TC_REP::complex_matrix_constant: |
|
1956 cm2 = tmp_b.complex_matrix_value (); |
|
1957 ans = do_binary_op (d1, cm2, t); |
|
1958 break; |
|
1959 |
|
1960 case TC_REP::magic_colon: |
|
1961 default: |
|
1962 panic_impossible (); |
|
1963 break; |
|
1964 } |
|
1965 break; |
|
1966 |
|
1967 case TC_REP::matrix_constant: |
|
1968 |
|
1969 m1 = tmp_a.matrix_value (); |
|
1970 |
|
1971 switch (b_type) |
|
1972 { |
|
1973 case TC_REP::scalar_constant: |
|
1974 d2 = tmp_b.double_value (); |
|
1975 ans = do_binary_op (m1, d2, t); |
|
1976 break; |
|
1977 |
|
1978 case TC_REP::matrix_constant: |
|
1979 m2 = tmp_b.matrix_value (); |
|
1980 ans = do_binary_op (m1, m2, t); |
|
1981 break; |
|
1982 |
|
1983 case TC_REP::complex_scalar_constant: |
|
1984 c2 = tmp_b.complex_value (); |
|
1985 ans = do_binary_op (m1, c2, t); |
|
1986 break; |
|
1987 |
|
1988 case TC_REP::complex_matrix_constant: |
|
1989 cm2 = tmp_b.complex_matrix_value (); |
|
1990 ans = do_binary_op (m1, cm2, t); |
|
1991 break; |
|
1992 |
|
1993 case TC_REP::magic_colon: |
|
1994 default: |
|
1995 panic_impossible (); |
|
1996 break; |
|
1997 } |
|
1998 break; |
|
1999 |
|
2000 case TC_REP::complex_scalar_constant: |
|
2001 |
|
2002 c1 = tmp_a.complex_value (); |
|
2003 |
|
2004 switch (b_type) |
|
2005 { |
|
2006 case TC_REP::scalar_constant: |
|
2007 d2 = tmp_b.double_value (); |
|
2008 ans = do_binary_op (c1, d2, t); |
|
2009 break; |
|
2010 |
|
2011 case TC_REP::matrix_constant: |
|
2012 m2 = tmp_b.matrix_value (); |
|
2013 ans = do_binary_op (c1, m2, t); |
|
2014 break; |
|
2015 |
|
2016 case TC_REP::complex_scalar_constant: |
|
2017 c2 = tmp_b.complex_value (); |
|
2018 ans = do_binary_op (c1, c2, t); |
|
2019 break; |
|
2020 |
|
2021 case TC_REP::complex_matrix_constant: |
|
2022 cm2 = tmp_b.complex_matrix_value (); |
|
2023 ans = do_binary_op (c1, cm2, t); |
|
2024 break; |
|
2025 |
|
2026 case TC_REP::magic_colon: |
|
2027 default: |
|
2028 panic_impossible (); |
|
2029 break; |
|
2030 } |
|
2031 break; |
|
2032 |
|
2033 case TC_REP::complex_matrix_constant: |
|
2034 |
|
2035 cm1 = tmp_a.complex_matrix_value (); |
|
2036 |
|
2037 switch (b_type) |
|
2038 { |
|
2039 case TC_REP::scalar_constant: |
|
2040 d2 = tmp_b.double_value (); |
|
2041 ans = do_binary_op (cm1, d2, t); |
|
2042 break; |
|
2043 |
|
2044 case TC_REP::matrix_constant: |
|
2045 m2 = tmp_b.matrix_value (); |
|
2046 ans = do_binary_op (cm1, m2, t); |
|
2047 break; |
|
2048 |
|
2049 case TC_REP::complex_scalar_constant: |
|
2050 c2 = tmp_b.complex_value (); |
|
2051 ans = do_binary_op (cm1, c2, t); |
|
2052 break; |
|
2053 |
|
2054 case TC_REP::complex_matrix_constant: |
|
2055 cm2 = tmp_b.complex_matrix_value (); |
|
2056 ans = do_binary_op (cm1, cm2, t); |
|
2057 break; |
|
2058 |
|
2059 case TC_REP::magic_colon: |
|
2060 default: |
|
2061 panic_impossible (); |
|
2062 break; |
|
2063 } |
|
2064 break; |
|
2065 |
|
2066 case TC_REP::magic_colon: |
|
2067 default: |
|
2068 panic_impossible (); |
|
2069 break; |
|
2070 } |
|
2071 |
|
2072 return ans; |
|
2073 } |
|
2074 |
|
2075 tree_constant |
|
2076 do_unary_op (tree_constant& a, tree_expression::type t) |
|
2077 { |
|
2078 tree_constant ans; |
|
2079 |
|
2080 if (a.rows () == 0 || a.columns () == 0) |
|
2081 { |
|
2082 int flag = user_pref.propagate_empty_matrices; |
|
2083 if (flag < 0) |
|
2084 warning ("unary operation on empty matrix"); |
|
2085 else if (flag == 0) |
|
2086 { |
|
2087 ::error ("invalid unary operation on empty matrix"); |
|
2088 return ans; |
|
2089 } |
|
2090 } |
|
2091 |
|
2092 tree_constant tmp_a = a.make_numeric (); |
|
2093 |
|
2094 switch (tmp_a.const_type ()) |
|
2095 { |
|
2096 case TC_REP::scalar_constant: |
|
2097 ans = do_unary_op (tmp_a.double_value (), t); |
|
2098 break; |
|
2099 |
|
2100 case TC_REP::matrix_constant: |
|
2101 { |
|
2102 Matrix m = tmp_a.matrix_value (); |
|
2103 ans = do_unary_op (m, t); |
|
2104 } |
|
2105 break; |
|
2106 |
|
2107 case TC_REP::complex_scalar_constant: |
|
2108 ans = do_unary_op (tmp_a.complex_value (), t); |
|
2109 break; |
|
2110 |
|
2111 case TC_REP::complex_matrix_constant: |
|
2112 { |
|
2113 ComplexMatrix m = tmp_a.complex_matrix_value (); |
|
2114 ans = do_unary_op (m, t); |
|
2115 } |
|
2116 break; |
|
2117 |
|
2118 case TC_REP::magic_colon: |
|
2119 default: |
|
2120 panic_impossible (); |
|
2121 break; |
|
2122 } |
|
2123 |
|
2124 return ans; |
|
2125 } |
|
2126 |
|
2127 tree_constant |
|
2128 TC_REP::cumprod (void) const |
|
2129 { |
|
2130 if (type_tag == string_constant || type_tag == range_constant) |
|
2131 { |
|
2132 tree_constant tmp = make_numeric (); |
|
2133 return tmp.cumprod (); |
|
2134 } |
|
2135 |
|
2136 tree_constant retval; |
|
2137 |
|
2138 switch (type_tag) |
|
2139 { |
|
2140 case scalar_constant: |
|
2141 retval = tree_constant (scalar); |
|
2142 break; |
|
2143 |
|
2144 case matrix_constant: |
|
2145 { |
|
2146 Matrix m = matrix->cumprod (); |
|
2147 retval = tree_constant (m); |
|
2148 } |
|
2149 break; |
|
2150 |
|
2151 case complex_scalar_constant: |
|
2152 retval = tree_constant (*complex_scalar); |
|
2153 break; |
|
2154 |
|
2155 case complex_matrix_constant: |
|
2156 { |
|
2157 ComplexMatrix m = complex_matrix->cumprod (); |
|
2158 retval = tree_constant (m); |
|
2159 } |
|
2160 break; |
|
2161 |
581
|
2162 case string_constant: |
631
|
2163 case range_constant: |
581
|
2164 case magic_colon: |
|
2165 default: |
|
2166 panic_impossible (); |
|
2167 break; |
|
2168 } |
|
2169 |
529
|
2170 return retval; |
492
|
2171 } |
|
2172 |
|
2173 tree_constant |
631
|
2174 TC_REP::cumsum (void) const |
|
2175 { |
|
2176 if (type_tag == string_constant || type_tag == range_constant) |
|
2177 { |
|
2178 tree_constant tmp = make_numeric (); |
|
2179 return tmp.cumsum (); |
|
2180 } |
|
2181 |
|
2182 tree_constant retval; |
|
2183 |
|
2184 switch (type_tag) |
|
2185 { |
|
2186 case scalar_constant: |
|
2187 retval = tree_constant (scalar); |
|
2188 break; |
|
2189 |
|
2190 case matrix_constant: |
|
2191 { |
|
2192 Matrix m = matrix->cumsum (); |
|
2193 retval = tree_constant (m); |
|
2194 } |
|
2195 break; |
|
2196 |
|
2197 case complex_scalar_constant: |
|
2198 retval = tree_constant (*complex_scalar); |
|
2199 break; |
|
2200 |
|
2201 case complex_matrix_constant: |
|
2202 { |
|
2203 ComplexMatrix m = complex_matrix->cumsum (); |
|
2204 retval = tree_constant (m); |
|
2205 } |
|
2206 break; |
|
2207 |
|
2208 case string_constant: |
|
2209 case range_constant: |
|
2210 case magic_colon: |
|
2211 default: |
|
2212 panic_impossible (); |
|
2213 break; |
|
2214 } |
|
2215 |
|
2216 return retval; |
|
2217 } |
|
2218 |
|
2219 tree_constant |
|
2220 TC_REP::prod (void) const |
|
2221 { |
|
2222 if (type_tag == string_constant || type_tag == range_constant) |
|
2223 { |
|
2224 tree_constant tmp = make_numeric (); |
|
2225 return tmp.prod (); |
|
2226 } |
|
2227 |
|
2228 tree_constant retval; |
|
2229 |
|
2230 switch (type_tag) |
|
2231 { |
|
2232 case scalar_constant: |
|
2233 retval = tree_constant (scalar); |
|
2234 break; |
|
2235 |
|
2236 case matrix_constant: |
|
2237 { |
|
2238 Matrix m = matrix->prod (); |
|
2239 retval = tree_constant (m); |
|
2240 } |
|
2241 break; |
|
2242 |
|
2243 case complex_scalar_constant: |
|
2244 retval = tree_constant (*complex_scalar); |
|
2245 break; |
|
2246 |
|
2247 case complex_matrix_constant: |
|
2248 { |
|
2249 ComplexMatrix m = complex_matrix->prod (); |
|
2250 retval = tree_constant (m); |
|
2251 } |
|
2252 break; |
|
2253 |
|
2254 case string_constant: |
|
2255 case range_constant: |
|
2256 case magic_colon: |
|
2257 default: |
|
2258 panic_impossible (); |
|
2259 break; |
|
2260 } |
|
2261 |
|
2262 return retval; |
|
2263 } |
|
2264 |
|
2265 tree_constant |
|
2266 TC_REP::sum (void) const |
492
|
2267 { |
631
|
2268 if (type_tag == string_constant || type_tag == range_constant) |
|
2269 { |
|
2270 tree_constant tmp = make_numeric (); |
|
2271 return tmp.sum (); |
|
2272 } |
|
2273 |
|
2274 tree_constant retval; |
|
2275 |
|
2276 switch (type_tag) |
|
2277 { |
|
2278 case scalar_constant: |
|
2279 retval = tree_constant (scalar); |
|
2280 break; |
|
2281 |
|
2282 case matrix_constant: |
|
2283 { |
|
2284 Matrix m = matrix->sum (); |
|
2285 retval = tree_constant (m); |
|
2286 } |
|
2287 break; |
|
2288 |
|
2289 case complex_scalar_constant: |
|
2290 retval = tree_constant (*complex_scalar); |
|
2291 break; |
|
2292 |
|
2293 case complex_matrix_constant: |
|
2294 { |
|
2295 ComplexMatrix m = complex_matrix->sum (); |
|
2296 retval = tree_constant (m); |
|
2297 } |
|
2298 break; |
|
2299 |
|
2300 case string_constant: |
|
2301 case range_constant: |
|
2302 case magic_colon: |
|
2303 default: |
|
2304 panic_impossible (); |
|
2305 break; |
|
2306 } |
|
2307 |
|
2308 return retval; |
|
2309 } |
|
2310 |
|
2311 tree_constant |
|
2312 TC_REP::sumsq (void) const |
|
2313 { |
|
2314 if (type_tag == string_constant || type_tag == range_constant) |
|
2315 { |
|
2316 tree_constant tmp = make_numeric (); |
|
2317 return tmp.sumsq (); |
|
2318 } |
|
2319 |
|
2320 tree_constant retval; |
|
2321 |
|
2322 switch (type_tag) |
|
2323 { |
|
2324 case scalar_constant: |
|
2325 retval = tree_constant (scalar * scalar); |
|
2326 break; |
|
2327 |
|
2328 case matrix_constant: |
|
2329 { |
|
2330 Matrix m = matrix->sumsq (); |
|
2331 retval = tree_constant (m); |
|
2332 } |
|
2333 break; |
|
2334 |
|
2335 case complex_scalar_constant: |
|
2336 { |
|
2337 Complex c (*complex_scalar); |
|
2338 retval = tree_constant (c * c); |
|
2339 } |
|
2340 break; |
|
2341 |
|
2342 case complex_matrix_constant: |
|
2343 { |
|
2344 ComplexMatrix m = complex_matrix->sumsq (); |
|
2345 retval = tree_constant (m); |
|
2346 } |
|
2347 break; |
|
2348 |
|
2349 case string_constant: |
|
2350 case range_constant: |
|
2351 case magic_colon: |
|
2352 default: |
|
2353 panic_impossible (); |
|
2354 break; |
|
2355 } |
|
2356 |
|
2357 return retval; |
|
2358 } |
|
2359 |
|
2360 static tree_constant |
|
2361 make_diag (const Matrix& v, int k) |
|
2362 { |
|
2363 int nr = v.rows (); |
|
2364 int nc = v.columns (); |
|
2365 assert (nc == 1 || nr == 1); |
|
2366 |
|
2367 tree_constant retval; |
|
2368 |
|
2369 int roff = 0; |
|
2370 int coff = 0; |
|
2371 if (k > 0) |
|
2372 { |
|
2373 roff = 0; |
|
2374 coff = k; |
|
2375 } |
|
2376 else if (k < 0) |
|
2377 { |
|
2378 roff = -k; |
|
2379 coff = 0; |
|
2380 } |
|
2381 |
|
2382 if (nr == 1) |
|
2383 { |
|
2384 int n = nc + ABS (k); |
|
2385 Matrix m (n, n, 0.0); |
|
2386 for (int i = 0; i < nc; i++) |
|
2387 m.elem (i+roff, i+coff) = v.elem (0, i); |
|
2388 retval = tree_constant (m); |
|
2389 } |
|
2390 else |
|
2391 { |
|
2392 int n = nr + ABS (k); |
|
2393 Matrix m (n, n, 0.0); |
|
2394 for (int i = 0; i < nr; i++) |
|
2395 m.elem (i+roff, i+coff) = v.elem (i, 0); |
|
2396 retval = tree_constant (m); |
|
2397 } |
|
2398 |
|
2399 return retval; |
|
2400 } |
|
2401 |
|
2402 static tree_constant |
|
2403 make_diag (const ComplexMatrix& v, int k) |
|
2404 { |
|
2405 int nr = v.rows (); |
|
2406 int nc = v.columns (); |
|
2407 assert (nc == 1 || nr == 1); |
|
2408 |
|
2409 tree_constant retval; |
|
2410 |
|
2411 int roff = 0; |
|
2412 int coff = 0; |
|
2413 if (k > 0) |
|
2414 { |
|
2415 roff = 0; |
|
2416 coff = k; |
|
2417 } |
|
2418 else if (k < 0) |
|
2419 { |
|
2420 roff = -k; |
|
2421 coff = 0; |
|
2422 } |
|
2423 |
|
2424 if (nr == 1) |
|
2425 { |
|
2426 int n = nc + ABS (k); |
|
2427 ComplexMatrix m (n, n, 0.0); |
|
2428 for (int i = 0; i < nc; i++) |
|
2429 m.elem (i+roff, i+coff) = v.elem (0, i); |
|
2430 retval = tree_constant (m); |
|
2431 } |
|
2432 else |
|
2433 { |
|
2434 int n = nr + ABS (k); |
|
2435 ComplexMatrix m (n, n, 0.0); |
|
2436 for (int i = 0; i < nr; i++) |
|
2437 m.elem (i+roff, i+coff) = v.elem (i, 0); |
|
2438 retval = tree_constant (m); |
|
2439 } |
|
2440 |
|
2441 return retval; |
|
2442 } |
|
2443 |
|
2444 tree_constant |
|
2445 TC_REP::diag (void) const |
|
2446 { |
|
2447 if (type_tag == string_constant || type_tag == range_constant) |
|
2448 { |
|
2449 tree_constant tmp = make_numeric (); |
|
2450 return tmp.diag (); |
|
2451 } |
492
|
2452 |
|
2453 tree_constant retval; |
631
|
2454 |
|
2455 switch (type_tag) |
|
2456 { |
|
2457 case scalar_constant: |
|
2458 retval = tree_constant (scalar); |
|
2459 break; |
|
2460 |
|
2461 case matrix_constant: |
|
2462 { |
|
2463 int nr = rows (); |
|
2464 int nc = columns (); |
|
2465 if (nr == 0 || nc == 0) |
|
2466 { |
|
2467 Matrix mtmp; |
|
2468 retval = tree_constant (mtmp); |
|
2469 } |
|
2470 else if (nr == 1 || nc == 1) |
|
2471 retval = make_diag (matrix_value (), 0); |
|
2472 else |
|
2473 { |
|
2474 ColumnVector v = matrix->diag (); |
|
2475 if (v.capacity () > 0) |
|
2476 retval = tree_constant (v); |
|
2477 } |
|
2478 } |
|
2479 break; |
|
2480 |
|
2481 case complex_scalar_constant: |
|
2482 retval = tree_constant (*complex_scalar); |
|
2483 break; |
|
2484 |
|
2485 case complex_matrix_constant: |
|
2486 { |
|
2487 int nr = rows (); |
|
2488 int nc = columns (); |
|
2489 if (nr == 0 || nc == 0) |
|
2490 { |
|
2491 Matrix mtmp; |
|
2492 retval = tree_constant (mtmp); |
|
2493 } |
|
2494 else if (nr == 1 || nc == 1) |
|
2495 retval = make_diag (complex_matrix_value (), 0); |
|
2496 else |
492
|
2497 { |
631
|
2498 ComplexColumnVector v = complex_matrix->diag (); |
|
2499 if (v.capacity () > 0) |
|
2500 retval = tree_constant (v); |
492
|
2501 } |
631
|
2502 } |
|
2503 break; |
|
2504 |
|
2505 case string_constant: |
|
2506 case range_constant: |
|
2507 case magic_colon: |
|
2508 default: |
|
2509 panic_impossible (); |
|
2510 break; |
|
2511 } |
|
2512 |
|
2513 return retval; |
|
2514 } |
|
2515 |
|
2516 tree_constant |
|
2517 TC_REP::diag (const tree_constant& a) const |
|
2518 { |
|
2519 if (type_tag == string_constant || type_tag == range_constant) |
|
2520 { |
|
2521 tree_constant tmp = make_numeric (); |
|
2522 return tmp.diag (a); |
|
2523 } |
|
2524 |
|
2525 tree_constant tmp_a = a.make_numeric (); |
|
2526 |
|
2527 TC_REP::constant_type a_type = tmp_a.const_type (); |
|
2528 |
|
2529 tree_constant retval; |
|
2530 |
|
2531 switch (type_tag) |
|
2532 { |
|
2533 case scalar_constant: |
|
2534 if (a_type == scalar_constant) |
|
2535 { |
|
2536 int k = NINT (tmp_a.double_value ()); |
|
2537 int n = ABS (k) + 1; |
|
2538 if (k == 0) |
|
2539 retval = tree_constant (scalar); |
|
2540 else if (k > 0) |
|
2541 { |
|
2542 Matrix m (n, n, 0.0); |
|
2543 m.elem (0, k) = scalar; |
|
2544 retval = tree_constant (m); |
|
2545 } |
|
2546 else if (k < 0) |
|
2547 { |
|
2548 Matrix m (n, n, 0.0); |
|
2549 m.elem (-k, 0) = scalar; |
|
2550 retval = tree_constant (m); |
|
2551 } |
|
2552 } |
|
2553 break; |
|
2554 |
|
2555 case matrix_constant: |
|
2556 if (a_type == scalar_constant) |
|
2557 { |
|
2558 int k = NINT (tmp_a.double_value ()); |
|
2559 int nr = rows (); |
|
2560 int nc = columns (); |
|
2561 if (nr == 0 || nc == 0) |
|
2562 { |
|
2563 Matrix mtmp; |
|
2564 retval = tree_constant (mtmp); |
|
2565 } |
|
2566 else if (nr == 1 || nc == 1) |
|
2567 retval = make_diag (matrix_value (), k); |
|
2568 else |
|
2569 { |
|
2570 ColumnVector d = matrix->diag (k); |
|
2571 retval = tree_constant (d); |
|
2572 } |
|
2573 } |
|
2574 else |
|
2575 ::error ("diag: invalid second argument"); |
|
2576 |
|
2577 break; |
|
2578 |
|
2579 case complex_scalar_constant: |
|
2580 if (a_type == scalar_constant) |
|
2581 { |
|
2582 int k = NINT (tmp_a.double_value ()); |
|
2583 int n = ABS (k) + 1; |
|
2584 if (k == 0) |
|
2585 retval = tree_constant (*complex_scalar); |
|
2586 else if (k > 0) |
|
2587 { |
|
2588 ComplexMatrix m (n, n, 0.0); |
|
2589 m.elem (0, k) = *complex_scalar; |
|
2590 retval = tree_constant (m); |
|
2591 } |
|
2592 else if (k < 0) |
|
2593 { |
|
2594 ComplexMatrix m (n, n, 0.0); |
|
2595 m.elem (-k, 0) = *complex_scalar; |
|
2596 retval = tree_constant (m); |
|
2597 } |
|
2598 } |
|
2599 break; |
|
2600 |
|
2601 case complex_matrix_constant: |
|
2602 if (a_type == scalar_constant) |
|
2603 { |
|
2604 int k = NINT (tmp_a.double_value ()); |
|
2605 int nr = rows (); |
|
2606 int nc = columns (); |
|
2607 if (nr == 0 || nc == 0) |
|
2608 { |
|
2609 Matrix mtmp; |
|
2610 retval = tree_constant (mtmp); |
|
2611 } |
|
2612 else if (nr == 1 || nc == 1) |
|
2613 retval = make_diag (complex_matrix_value (), k); |
|
2614 else |
|
2615 { |
|
2616 ComplexColumnVector d = complex_matrix->diag (k); |
|
2617 retval = tree_constant (d); |
|
2618 } |
|
2619 } |
|
2620 else |
|
2621 ::error ("diag: invalid second argument"); |
|
2622 |
|
2623 break; |
|
2624 |
|
2625 case string_constant: |
|
2626 case range_constant: |
|
2627 case magic_colon: |
|
2628 default: |
|
2629 panic_impossible (); |
|
2630 break; |
|
2631 } |
|
2632 |
|
2633 return retval; |
|
2634 } |
|
2635 |
|
2636 // XXX FIXME XXX -- this can probably be rewritten efficiently as a |
|
2637 // nonmember function... |
|
2638 |
|
2639 tree_constant |
|
2640 TC_REP::mapper (Mapper_fcn& m_fcn, int print) const |
|
2641 { |
|
2642 tree_constant retval; |
|
2643 |
|
2644 if (type_tag == string_constant || type_tag == range_constant) |
|
2645 { |
|
2646 tree_constant tmp = make_numeric (); |
|
2647 return tmp.mapper (m_fcn, print); |
|
2648 } |
|
2649 |
|
2650 switch (type_tag) |
|
2651 { |
|
2652 case scalar_constant: |
|
2653 if (m_fcn.can_return_complex_for_real_arg |
|
2654 && (scalar < m_fcn.lower_limit |
|
2655 || scalar > m_fcn.upper_limit)) |
|
2656 { |
|
2657 if (m_fcn.c_c_mapper) |
|
2658 { |
|
2659 Complex c = m_fcn.c_c_mapper (Complex (scalar)); |
|
2660 retval = tree_constant (c); |
|
2661 } |
|
2662 else |
|
2663 ::error ("%s: unable to handle real arguments", m_fcn.name); |
|
2664 } |
|
2665 else |
|
2666 { |
|
2667 if (m_fcn.d_d_mapper) |
|
2668 { |
|
2669 double d = m_fcn.d_d_mapper (scalar); |
|
2670 retval = tree_constant (d); |
|
2671 } |
|
2672 else |
|
2673 ::error ("%s: unable to handle real arguments", m_fcn.name); |
|
2674 } |
|
2675 break; |
|
2676 |
|
2677 case matrix_constant: |
|
2678 if (m_fcn.can_return_complex_for_real_arg |
|
2679 && (any_element_less_than (*matrix, m_fcn.lower_limit) |
|
2680 || any_element_greater_than (*matrix, m_fcn.upper_limit))) |
|
2681 { |
|
2682 if (m_fcn.c_c_mapper) |
|
2683 { |
|
2684 ComplexMatrix cm = map (m_fcn.c_c_mapper, |
|
2685 ComplexMatrix (*matrix)); |
|
2686 retval = tree_constant (cm); |
|
2687 } |
|
2688 else |
|
2689 ::error ("%s: unable to handle real arguments", m_fcn.name); |
|
2690 } |
|
2691 else |
|
2692 { |
|
2693 if (m_fcn.d_d_mapper) |
|
2694 { |
|
2695 Matrix m = map (m_fcn.d_d_mapper, *matrix); |
|
2696 retval = tree_constant (m); |
|
2697 } |
|
2698 else |
|
2699 ::error ("%s: unable to handle real arguments", m_fcn.name); |
|
2700 } |
|
2701 break; |
|
2702 |
|
2703 case complex_scalar_constant: |
|
2704 if (m_fcn.d_c_mapper) |
|
2705 { |
|
2706 double d; |
|
2707 d = m_fcn.d_c_mapper (*complex_scalar); |
|
2708 retval = tree_constant (d); |
|
2709 } |
|
2710 else if (m_fcn.c_c_mapper) |
|
2711 { |
|
2712 Complex c; |
|
2713 c = m_fcn.c_c_mapper (*complex_scalar); |
|
2714 retval = tree_constant (c); |
|
2715 } |
|
2716 else |
|
2717 ::error ("%s: unable to handle complex arguments", m_fcn.name); |
|
2718 break; |
|
2719 |
|
2720 case complex_matrix_constant: |
|
2721 if (m_fcn.d_c_mapper) |
|
2722 { |
|
2723 Matrix m; |
|
2724 m = map (m_fcn.d_c_mapper, *complex_matrix); |
|
2725 retval = tree_constant (m); |
|
2726 } |
|
2727 else if (m_fcn.c_c_mapper) |
|
2728 { |
|
2729 ComplexMatrix cm; |
|
2730 cm = map (m_fcn.c_c_mapper, *complex_matrix); |
|
2731 retval = tree_constant (cm); |
|
2732 } |
|
2733 else |
|
2734 ::error ("%s: unable to handle complex arguments", m_fcn.name); |
|
2735 break; |
|
2736 |
|
2737 case string_constant: |
|
2738 case range_constant: |
|
2739 case magic_colon: |
|
2740 default: |
|
2741 panic_impossible (); |
|
2742 break; |
|
2743 } |
|
2744 |
492
|
2745 return retval; |
|
2746 } |
|
2747 |
|
2748 /* |
|
2749 ;;; Local Variables: *** |
|
2750 ;;; mode: C++ *** |
|
2751 ;;; page-delimiter: "^/\\*" *** |
|
2752 ;;; End: *** |
|
2753 */ |