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