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