1
|
1 // xpow.cc -*- C++ -*- |
|
2 /* |
|
3 |
|
4 Copyright (C) 1992, 1993 John W. Eaton |
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef __GNUG__ |
|
25 #pragma implementation |
|
26 #endif |
|
27 |
|
28 #include <assert.h> |
|
29 #include "error.h" |
|
30 #include "xpow.h" |
|
31 |
|
32 // This function also appears in tree-const.cc. Maybe it should be a |
|
33 // member function of the Matrix class. |
|
34 |
|
35 static int |
|
36 any_element_is_negative (const Matrix& a) |
|
37 { |
|
38 int nr = a.rows (); |
|
39 int nc = a.columns (); |
|
40 for (int j = 0; j < nc; j++) |
|
41 for (int i = 0; i < nr; i++) |
|
42 if (a.elem (i, j) < 0.0) |
|
43 return 1; |
|
44 return 0; |
|
45 } |
|
46 |
|
47 /* |
|
48 * Safer pow functions. |
|
49 * |
|
50 * op2 \ op1: s m cs cm |
|
51 * +-- +---+---+----+----+ |
|
52 * scalar | | 1 | 5 | 7 | 11 | |
|
53 * +---+---+----+----+ |
|
54 * matrix | 2 | E | 8 | E | |
|
55 * +---+---+----+----+ |
|
56 * complex_scalar | 3 | 6 | 9 | 12 | |
|
57 * +---+---+----+----+ |
|
58 * complex_matrix | 4 | E | 10 | E | |
|
59 * +---+---+----+----+ |
|
60 * |
|
61 * E -> error, trapped in arith-ops.cc. |
|
62 */ |
|
63 |
|
64 tree_constant |
|
65 xpow (double a, double b) |
|
66 { |
|
67 if (a < 0.0 && (int) b != b) |
|
68 { |
|
69 Complex atmp (a); |
|
70 return tree_constant (pow (atmp, b)); |
|
71 } |
|
72 else |
|
73 return tree_constant (pow (a, b)); |
|
74 } |
|
75 |
|
76 tree_constant |
|
77 xpow (double a, Matrix& b) |
|
78 { |
|
79 tree_constant retval; |
|
80 |
|
81 int nr = b.rows (); |
|
82 int nc = b.columns (); |
|
83 |
|
84 if (nr == 0 || nc == 0 || nr != nc) |
|
85 error ("for x^A, A must be square"); |
|
86 else |
|
87 { |
|
88 EIG b_eig (b); |
|
89 ComplexColumnVector lambda (b_eig.eigenvalues ()); |
|
90 ComplexMatrix Q (b_eig.eigenvectors ()); |
|
91 |
|
92 for (int i = 0; i < nr; i++) |
|
93 { |
|
94 Complex elt = lambda.elem (i); |
|
95 if (imag (elt) == 0.0) |
|
96 lambda.elem (i) = pow (a, real (elt)); |
|
97 else |
|
98 lambda.elem (i) = pow (a, elt); |
|
99 } |
|
100 ComplexDiagMatrix D (lambda); |
|
101 |
|
102 ComplexMatrix result = Q * D * Q.inverse (); |
|
103 retval = tree_constant (result); |
|
104 } |
|
105 |
|
106 return retval; |
|
107 } |
|
108 |
|
109 tree_constant |
|
110 xpow (double a, Complex& b) |
|
111 { |
|
112 Complex result; |
|
113 Complex atmp (a); |
|
114 result = pow (atmp, b); |
|
115 return tree_constant (result); |
|
116 } |
|
117 |
|
118 tree_constant |
|
119 xpow (double a, ComplexMatrix& b) |
|
120 { |
|
121 tree_constant retval; |
|
122 |
|
123 int nr = b.rows (); |
|
124 int nc = b.columns (); |
|
125 |
|
126 if (nr == 0 || nc == 0 || nr != nc) |
|
127 error ("for x^A, A must be square"); |
|
128 else |
|
129 { |
|
130 EIG b_eig (b); |
|
131 ComplexColumnVector lambda (b_eig.eigenvalues ()); |
|
132 ComplexMatrix Q (b_eig.eigenvectors ()); |
|
133 |
|
134 for (int i = 0; i < nr; i++) |
|
135 { |
|
136 Complex elt = lambda.elem (i); |
|
137 if (imag (elt) == 0.0) |
|
138 lambda.elem (i) = pow (a, real (elt)); |
|
139 else |
|
140 lambda.elem (i) = pow (a, elt); |
|
141 } |
|
142 ComplexDiagMatrix D (lambda); |
|
143 |
|
144 ComplexMatrix result = Q * D * Q.inverse (); |
|
145 retval = tree_constant (result); |
|
146 } |
|
147 |
|
148 return retval; |
|
149 } |
|
150 |
|
151 tree_constant |
|
152 xpow (Matrix& a, double b) |
|
153 { |
|
154 tree_constant retval; |
|
155 |
|
156 int nr = a.rows (); |
|
157 int nc = a.columns (); |
|
158 |
|
159 if (nr == 0 || nc == 0 || nr != nc) |
|
160 { |
|
161 error ("for A^b, A must be square"); |
|
162 return retval; |
|
163 } |
|
164 |
|
165 if ((int) b == b) |
|
166 { |
|
167 int btmp = (int) b; |
|
168 if (btmp == 0) |
|
169 { |
|
170 DiagMatrix result (nr, nr, 1.0); |
|
171 retval = tree_constant (result); |
|
172 } |
|
173 else |
|
174 { |
|
175 // Too much copying? |
|
176 // XXX FIXME XXX -- we shouldn\'t do this if the exponent is large... |
|
177 Matrix atmp; |
|
178 if (btmp < 0) |
|
179 { |
|
180 btmp = -btmp; |
|
181 atmp = a.inverse (); |
|
182 } |
|
183 else |
|
184 atmp = a; |
|
185 |
|
186 Matrix result (atmp); |
|
187 for (int i = 1; i < btmp; i++) |
|
188 result = result * atmp; |
|
189 |
|
190 retval = tree_constant (result); |
|
191 } |
|
192 } |
|
193 else |
|
194 { |
|
195 EIG a_eig (a); |
|
196 ComplexColumnVector lambda (a_eig.eigenvalues ()); |
|
197 ComplexMatrix Q (a_eig.eigenvectors ()); |
|
198 |
|
199 for (int i = 0; i < nr; i++) |
|
200 lambda.elem (i) = pow (lambda.elem (i), b); |
|
201 |
|
202 ComplexDiagMatrix D (lambda); |
|
203 |
|
204 ComplexMatrix result = Q * D * Q.inverse (); |
|
205 retval = tree_constant (result); |
|
206 } |
|
207 |
|
208 return retval; |
|
209 } |
|
210 |
|
211 tree_constant |
|
212 xpow (Matrix& a, Complex& b) |
|
213 { |
|
214 int nr = a.rows (); |
|
215 int nc = a.columns (); |
|
216 |
|
217 if (nr == 0 || nc == 0 || nr != nc) |
|
218 { |
|
219 error ("for A^b, A must be square"); |
|
220 return tree_constant (); |
|
221 } |
|
222 |
|
223 EIG a_eig (a); |
|
224 ComplexColumnVector lambda (a_eig.eigenvalues ()); |
|
225 ComplexMatrix Q (a_eig.eigenvectors ()); |
|
226 |
|
227 for (int i = 0; i < nr; i++) |
|
228 lambda.elem (i) = pow (lambda.elem (i), b); |
|
229 |
|
230 ComplexDiagMatrix D (lambda); |
|
231 |
|
232 ComplexMatrix result = Q * D * Q.inverse (); |
|
233 |
|
234 return tree_constant (result); |
|
235 } |
|
236 |
|
237 tree_constant |
|
238 xpow (Complex& a, double b) |
|
239 { |
|
240 Complex result; |
|
241 result = pow (a, b); |
|
242 return tree_constant (result); |
|
243 } |
|
244 |
|
245 tree_constant |
|
246 xpow (Complex& a, Matrix& b) |
|
247 { |
|
248 tree_constant retval; |
|
249 |
|
250 int nr = b.rows (); |
|
251 int nc = b.columns (); |
|
252 |
|
253 if (nr == 0 || nc == 0 || nr != nc) |
|
254 { |
|
255 error ("for x^A, A must be square"); |
|
256 } |
|
257 else |
|
258 { |
|
259 EIG b_eig (b); |
|
260 ComplexColumnVector lambda (b_eig.eigenvalues ()); |
|
261 ComplexMatrix Q (b_eig.eigenvectors ()); |
|
262 |
|
263 for (int i = 0; i < nr; i++) |
|
264 { |
|
265 Complex elt = lambda.elem (i); |
|
266 if (imag (elt) == 0.0) |
|
267 lambda.elem (i) = pow (a, real (elt)); |
|
268 else |
|
269 lambda.elem (i) = pow (a, elt); |
|
270 } |
|
271 ComplexDiagMatrix D (lambda); |
|
272 |
|
273 ComplexMatrix result = Q * D * Q.inverse (); |
|
274 retval = tree_constant (result); |
|
275 } |
|
276 |
|
277 return retval; |
|
278 } |
|
279 |
|
280 tree_constant |
|
281 xpow (Complex& a, Complex& b) |
|
282 { |
|
283 Complex result; |
|
284 result = pow (a, b); |
|
285 return tree_constant (result); |
|
286 } |
|
287 |
|
288 tree_constant |
|
289 xpow (Complex& a, ComplexMatrix& b) |
|
290 { |
|
291 tree_constant retval; |
|
292 |
|
293 int nr = b.rows (); |
|
294 int nc = b.columns (); |
|
295 |
|
296 if (nr == 0 || nc == 0 || nr != nc) |
|
297 error ("for x^A, A must be square"); |
|
298 else |
|
299 { |
|
300 EIG b_eig (b); |
|
301 ComplexColumnVector lambda (b_eig.eigenvalues ()); |
|
302 ComplexMatrix Q (b_eig.eigenvectors ()); |
|
303 |
|
304 for (int i = 0; i < nr; i++) |
|
305 { |
|
306 Complex elt = lambda.elem (i); |
|
307 if (imag (elt) == 0.0) |
|
308 lambda.elem (i) = pow (a, real (elt)); |
|
309 else |
|
310 lambda.elem (i) = pow (a, elt); |
|
311 } |
|
312 ComplexDiagMatrix D (lambda); |
|
313 |
|
314 ComplexMatrix result = Q * D * Q.inverse (); |
|
315 retval = tree_constant (result); |
|
316 } |
|
317 |
|
318 return retval; |
|
319 } |
|
320 |
|
321 tree_constant |
|
322 xpow (ComplexMatrix& a, double b) |
|
323 { |
|
324 tree_constant retval; |
|
325 |
|
326 int nr = a.rows (); |
|
327 int nc = a.columns (); |
|
328 |
|
329 if (nr == 0 || nc == 0 || nr != nc) |
|
330 { |
|
331 error ("for A^b, A must be square"); |
|
332 return retval; |
|
333 } |
|
334 |
|
335 if ((int) b == b) |
|
336 { |
|
337 int btmp = (int) b; |
|
338 if (btmp == 0) |
|
339 { |
|
340 DiagMatrix result (nr, nr, 1.0); |
|
341 retval = tree_constant (result); |
|
342 } |
|
343 else |
|
344 { |
|
345 // Too much copying? |
|
346 // XXX FIXME XXX -- we shouldn\'t do this if the exponent is large... |
|
347 ComplexMatrix atmp; |
|
348 if (btmp < 0) |
|
349 { |
|
350 btmp = -btmp; |
|
351 atmp = a.inverse (); |
|
352 } |
|
353 else |
|
354 atmp = a; |
|
355 |
|
356 ComplexMatrix result (atmp); |
|
357 for (int i = 1; i < btmp; i++) |
|
358 result = result * atmp; |
|
359 |
|
360 retval = tree_constant (result); |
|
361 } |
|
362 } |
|
363 else |
|
364 { |
|
365 EIG a_eig (a); |
|
366 ComplexColumnVector lambda (a_eig.eigenvalues ()); |
|
367 ComplexMatrix Q (a_eig.eigenvectors ()); |
|
368 |
|
369 for (int i = 0; i < nr; i++) |
|
370 lambda.elem (i) = pow (lambda.elem (i), b); |
|
371 |
|
372 ComplexDiagMatrix D (lambda); |
|
373 |
|
374 ComplexMatrix result = Q * D * Q.inverse (); |
|
375 retval = tree_constant (result); |
|
376 } |
|
377 |
|
378 return retval; |
|
379 } |
|
380 |
|
381 tree_constant |
|
382 xpow (ComplexMatrix& a, Complex& b) |
|
383 { |
|
384 int nr = a.rows (); |
|
385 int nc = a.columns (); |
|
386 |
|
387 if (nr == 0 || nc == 0 || nr != nc) |
|
388 { |
|
389 error ("for A^b, A must be square"); |
|
390 return tree_constant (); |
|
391 } |
|
392 |
|
393 EIG a_eig (a); |
|
394 ComplexColumnVector lambda (a_eig.eigenvalues ()); |
|
395 ComplexMatrix Q (a_eig.eigenvectors ()); |
|
396 |
|
397 for (int i = 0; i < nr; i++) |
|
398 lambda.elem (i) = pow (lambda.elem (i), b); |
|
399 |
|
400 ComplexDiagMatrix D (lambda); |
|
401 |
|
402 ComplexMatrix result = Q * D * Q.inverse (); |
|
403 |
|
404 return tree_constant (result); |
|
405 } |
|
406 |
|
407 /* |
|
408 * Safer pow functions that work elementwise for matrices. |
|
409 * |
|
410 * op2 \ op1: s m cs cm |
|
411 * +-- +---+---+----+----+ |
|
412 * scalar | | * | 3 | * | 9 | |
|
413 * +---+---+----+----+ |
|
414 * matrix | 1 | 4 | 7 | 10 | |
|
415 * +---+---+----+----+ |
|
416 * complex_scalar | * | 5 | * | 11 | |
|
417 * +---+---+----+----+ |
|
418 * complex_matrix | 2 | 6 | 8 | 12 | |
|
419 * +---+---+----+----+ |
|
420 * |
|
421 * * -> not needed. |
|
422 */ |
|
423 |
|
424 tree_constant |
|
425 elem_xpow (double a, Matrix& b) |
|
426 { |
|
427 tree_constant retval; |
|
428 |
|
429 int nr = b.rows (); |
|
430 int nc = b.columns (); |
|
431 |
|
432 // For now, assume the worst. |
|
433 if (a < 0.0) |
|
434 { |
|
435 Complex atmp (a); |
|
436 ComplexMatrix result (nr, nc); |
|
437 for (int j = 0; j < nc; j++) |
|
438 for (int i = 0; i < nr; i++) |
|
439 result.elem (i, j) = pow (atmp, b.elem (i, j)); |
|
440 |
|
441 retval = tree_constant (result); |
|
442 } |
|
443 else |
|
444 { |
|
445 Matrix result (nr, nc); |
|
446 for (int j = 0; j < nc; j++) |
|
447 for (int i = 0; i < nr; i++) |
|
448 result.elem (i, j) = pow (a, b.elem (i, j)); |
|
449 |
|
450 retval = tree_constant (result); |
|
451 } |
|
452 |
|
453 return retval; |
|
454 } |
|
455 |
|
456 tree_constant |
|
457 elem_xpow (double a, ComplexMatrix& b) |
|
458 { |
|
459 int nr = b.rows (); |
|
460 int nc = b.columns (); |
|
461 |
|
462 ComplexMatrix result (nr, nc); |
|
463 for (int j = 0; j < nc; j++) |
|
464 for (int i = 0; i < nr; i++) |
|
465 result.elem (i, j) = pow (a, b.elem (i, j)); |
|
466 |
|
467 return tree_constant (result); |
|
468 } |
|
469 |
|
470 tree_constant |
|
471 elem_xpow (Matrix& a, double b) |
|
472 { |
|
473 tree_constant retval; |
|
474 |
|
475 int nr = a.rows (); |
|
476 int nc = a.columns (); |
|
477 |
|
478 if ((int) b != b && any_element_is_negative (a)) |
|
479 { |
|
480 ComplexMatrix result (nr, nc); |
|
481 for (int j = 0; j < nc; j++) |
|
482 for (int i = 0; i < nr; i++) |
|
483 { |
|
484 Complex atmp (a.elem (i, j)); |
|
485 result.elem (i, j) = pow (atmp, b); |
|
486 } |
|
487 |
|
488 retval = tree_constant (result); |
|
489 } |
|
490 else |
|
491 { |
|
492 Matrix result (nr, nc); |
|
493 for (int j = 0; j < nc; j++) |
|
494 for (int i = 0; i < nr; i++) |
|
495 result.elem (i, j) = pow (a.elem (i, j), b); |
|
496 |
|
497 retval = tree_constant (result); |
|
498 } |
|
499 |
|
500 return retval; |
|
501 } |
|
502 |
|
503 tree_constant |
|
504 elem_xpow (Matrix& a, Matrix& b) |
|
505 { |
|
506 int nr = a.rows (); |
|
507 int nc = a.columns (); |
|
508 |
|
509 assert (nr == b.rows () && nc == b.columns ()); |
|
510 |
|
511 int convert_to_complex = 0; |
|
512 int i; |
|
513 for (int j = 0; j < nc; j++) |
|
514 for (i = 0; i < nr; i++) |
|
515 { |
|
516 double atmp = a.elem (i, j); |
|
517 double btmp = b.elem (i, j); |
|
518 if (atmp < 0.0 && (int) btmp != btmp) |
|
519 { |
|
520 convert_to_complex = 1; |
|
521 goto done; |
|
522 } |
|
523 } |
|
524 |
|
525 done: |
|
526 |
|
527 if (convert_to_complex) |
|
528 { |
|
529 ComplexMatrix complex_result (nr, nc); |
|
530 |
|
531 for (j = 0; j < nc; j++) |
|
532 for (i = 0; i < nr; i++) |
|
533 { |
|
534 Complex atmp (a.elem (i, j)); |
|
535 Complex btmp (b.elem (i, j)); |
|
536 complex_result.elem (i, j) = pow (atmp, btmp); |
|
537 } |
|
538 return tree_constant (complex_result); |
|
539 } |
|
540 else |
|
541 { |
|
542 Matrix result (nr, nc); |
|
543 |
|
544 for (j = 0; j < nc; j++) |
|
545 for (i = 0; i < nr; i++) |
|
546 result.elem (i, j) = pow (a.elem (i, j), b.elem (i, j)); |
|
547 |
|
548 return tree_constant (result); |
|
549 } |
|
550 } |
|
551 |
|
552 tree_constant |
|
553 elem_xpow (Matrix& a, Complex& b) |
|
554 { |
|
555 int nr = a.rows (); |
|
556 int nc = a.columns (); |
|
557 |
|
558 ComplexMatrix result (nr, nc); |
|
559 for (int j = 0; j < nc; j++) |
|
560 for (int i = 0; i < nr; i++) |
|
561 result.elem (i, j) = pow (a.elem (i, j), b); |
|
562 |
|
563 return tree_constant (result); |
|
564 } |
|
565 |
|
566 tree_constant |
|
567 elem_xpow (Matrix& a, ComplexMatrix& b) |
|
568 { |
|
569 int nr = a.rows (); |
|
570 int nc = a.columns (); |
|
571 |
|
572 assert (nr == b.rows () && nc == b.columns ()); |
|
573 |
|
574 ComplexMatrix result (nr, nc); |
|
575 for (int j = 0; j < nc; j++) |
|
576 for (int i = 0; i < nr; i++) |
|
577 result.elem (i, j) = pow (a.elem (i, j), b.elem (i, j)); |
|
578 |
|
579 return tree_constant (result); |
|
580 } |
|
581 |
|
582 tree_constant |
|
583 elem_xpow (Complex& a, Matrix& b) |
|
584 { |
|
585 int nr = b.rows (); |
|
586 int nc = b.columns (); |
|
587 |
|
588 ComplexMatrix result (nr, nc); |
|
589 for (int j = 0; j < nc; j++) |
|
590 for (int i = 0; i < nr; i++) |
|
591 result.elem (i, j) = pow (a, b.elem (i, j)); |
|
592 |
|
593 return tree_constant (result); |
|
594 } |
|
595 |
|
596 tree_constant |
|
597 elem_xpow (Complex& a, ComplexMatrix& b) |
|
598 { |
|
599 int nr = b.rows (); |
|
600 int nc = b.columns (); |
|
601 |
|
602 ComplexMatrix result (nr, nc); |
|
603 for (int j = 0; j < nc; j++) |
|
604 for (int i = 0; i < nr; i++) |
|
605 result.elem (i, j) = pow (a, b.elem (i, j)); |
|
606 |
|
607 return tree_constant (result); |
|
608 } |
|
609 |
|
610 tree_constant |
|
611 elem_xpow (ComplexMatrix& a, double b) |
|
612 { |
|
613 int nr = a.rows (); |
|
614 int nc = a.columns (); |
|
615 |
|
616 ComplexMatrix result (nr, nc); |
|
617 for (int j = 0; j < nc; j++) |
|
618 for (int i = 0; i < nr; i++) |
|
619 result.elem (i, j) = pow (a.elem (i, j), b); |
|
620 |
|
621 return tree_constant (result); |
|
622 } |
|
623 |
|
624 tree_constant |
|
625 elem_xpow (ComplexMatrix& a, Matrix& b) |
|
626 { |
|
627 int nr = a.rows (); |
|
628 int nc = a.columns (); |
|
629 |
|
630 assert (nr == b.rows () && nc == b.columns ()); |
|
631 |
|
632 ComplexMatrix result (nr, nc); |
|
633 for (int j = 0; j < nc; j++) |
|
634 for (int i = 0; i < nr; i++) |
|
635 result.elem (i, j) = pow (a.elem (i, j), b.elem (i, j)); |
|
636 |
|
637 return tree_constant (result); |
|
638 } |
|
639 |
|
640 tree_constant |
|
641 elem_xpow (ComplexMatrix& a, Complex& b) |
|
642 { |
|
643 int nr = a.rows (); |
|
644 int nc = a.columns (); |
|
645 |
|
646 ComplexMatrix result (nr, nc); |
|
647 for (int j = 0; j < nc; j++) |
|
648 for (int i = 0; i < nr; i++) |
|
649 result.elem (i, j) = pow (a.elem (i, j), b); |
|
650 |
|
651 return tree_constant (result); |
|
652 } |
|
653 |
|
654 tree_constant |
|
655 elem_xpow (ComplexMatrix& a, ComplexMatrix& b) |
|
656 { |
|
657 int nr = a.rows (); |
|
658 int nc = a.columns (); |
|
659 |
|
660 ComplexMatrix result (nr, nc); |
|
661 |
|
662 for (int j = 0; j < nc; j++) |
|
663 for (int i = 0; i < nr; i++) |
|
664 result.elem (i, j) = pow (a.elem (i, j), b.elem (i, j)); |
|
665 |
|
666 return tree_constant (result); |
|
667 } |
|
668 |
|
669 /* |
|
670 ;;; Local Variables: *** |
|
671 ;;; mode: C++ *** |
|
672 ;;; page-delimiter: "^/\\*" *** |
|
673 ;;; End: *** |
|
674 */ |