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