1
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
1
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
20 |
|
21 */ |
|
22 |
240
|
23 #ifdef HAVE_CONFIG_H |
1192
|
24 #include <config.h> |
1
|
25 #endif |
|
26 |
1343
|
27 #include <cassert> |
1580
|
28 #include <climits> |
1343
|
29 |
1352
|
30 #include "CColVector.h" |
453
|
31 #include "CDiagMatrix.h" |
1352
|
32 #include "CMatrix.h" |
453
|
33 #include "EIG.h" |
1352
|
34 #include "dDiagMatrix.h" |
|
35 #include "dMatrix.h" |
1651
|
36 #include "oct-cmplx.h" |
1352
|
37 |
|
38 #include "error.h" |
2365
|
39 #include "ov.h" |
1567
|
40 #include "utils.h" |
1352
|
41 #include "xpow.h" |
1
|
42 |
1567
|
43 static inline int |
|
44 xisint (double x) |
|
45 { |
|
46 return (D_NINT (x) == x |
|
47 && ((x >= 0 && x < INT_MAX) |
|
48 || (x <= 0 && x > INT_MIN))); |
|
49 } |
|
50 |
767
|
51 // Safer pow functions. |
|
52 // |
|
53 // op2 \ op1: s m cs cm |
|
54 // +-- +---+---+----+----+ |
|
55 // scalar | | 1 | 5 | 7 | 11 | |
|
56 // +---+---+----+----+ |
2365
|
57 // matrix | 2 | * | 8 | * | |
767
|
58 // +---+---+----+----+ |
|
59 // complex_scalar | 3 | 6 | 9 | 12 | |
|
60 // +---+---+----+----+ |
2365
|
61 // complex_matrix | 4 | * | 10 | * | |
767
|
62 // +---+---+----+----+ |
1
|
63 |
767
|
64 // -*- 1 -*- |
2086
|
65 octave_value |
1
|
66 xpow (double a, double b) |
|
67 { |
2800
|
68 if (a < 0.0 && static_cast<int> (b) != b) |
1
|
69 { |
|
70 Complex atmp (a); |
1567
|
71 return pow (atmp, b); |
1
|
72 } |
|
73 else |
1567
|
74 return pow (a, b); |
1
|
75 } |
|
76 |
767
|
77 // -*- 2 -*- |
2086
|
78 octave_value |
164
|
79 xpow (double a, const Matrix& b) |
1
|
80 { |
2086
|
81 octave_value retval; |
1
|
82 |
|
83 int nr = b.rows (); |
2365
|
84 int nc = b.cols (); |
1
|
85 |
|
86 if (nr == 0 || nc == 0 || nr != nc) |
|
87 error ("for x^A, A must be square"); |
|
88 else |
|
89 { |
|
90 EIG b_eig (b); |
|
91 ComplexColumnVector lambda (b_eig.eigenvalues ()); |
|
92 ComplexMatrix Q (b_eig.eigenvectors ()); |
|
93 |
|
94 for (int i = 0; i < nr; i++) |
|
95 { |
2305
|
96 Complex elt = lambda (i); |
1
|
97 if (imag (elt) == 0.0) |
2305
|
98 lambda (i) = pow (a, real (elt)); |
1
|
99 else |
2305
|
100 lambda (i) = pow (a, elt); |
1
|
101 } |
|
102 ComplexDiagMatrix D (lambda); |
|
103 |
1567
|
104 retval = ComplexMatrix (Q * D * Q.inverse ()); |
1
|
105 } |
|
106 |
|
107 return retval; |
|
108 } |
|
109 |
767
|
110 // -*- 3 -*- |
2086
|
111 octave_value |
164
|
112 xpow (double a, const Complex& b) |
1
|
113 { |
|
114 Complex result; |
|
115 Complex atmp (a); |
|
116 result = pow (atmp, b); |
1567
|
117 return result; |
1
|
118 } |
|
119 |
767
|
120 // -*- 4 -*- |
2086
|
121 octave_value |
164
|
122 xpow (double a, const ComplexMatrix& b) |
1
|
123 { |
2086
|
124 octave_value retval; |
1
|
125 |
|
126 int nr = b.rows (); |
2365
|
127 int nc = b.cols (); |
1
|
128 |
|
129 if (nr == 0 || nc == 0 || nr != nc) |
2365
|
130 error ("for x^A, A must be square"); |
1
|
131 else |
|
132 { |
|
133 EIG b_eig (b); |
|
134 ComplexColumnVector lambda (b_eig.eigenvalues ()); |
|
135 ComplexMatrix Q (b_eig.eigenvectors ()); |
|
136 |
|
137 for (int i = 0; i < nr; i++) |
|
138 { |
2305
|
139 Complex elt = lambda (i); |
1
|
140 if (imag (elt) == 0.0) |
2305
|
141 lambda (i) = pow (a, real (elt)); |
1
|
142 else |
2305
|
143 lambda (i) = pow (a, elt); |
1
|
144 } |
|
145 ComplexDiagMatrix D (lambda); |
|
146 |
1567
|
147 retval = ComplexMatrix (Q * D * Q.inverse ()); |
1
|
148 } |
|
149 |
|
150 return retval; |
|
151 } |
|
152 |
767
|
153 // -*- 5 -*- |
2086
|
154 octave_value |
164
|
155 xpow (const Matrix& a, double b) |
1
|
156 { |
2086
|
157 octave_value retval; |
1
|
158 |
|
159 int nr = a.rows (); |
2365
|
160 int nc = a.cols (); |
1
|
161 |
|
162 if (nr == 0 || nc == 0 || nr != nc) |
2365
|
163 error ("for A^b, A must be square"); |
1567
|
164 else |
1
|
165 { |
2800
|
166 if (static_cast<int> (b) == b) |
1
|
167 { |
2804
|
168 int btmp = static_cast<int> (b); |
1567
|
169 if (btmp == 0) |
|
170 { |
|
171 retval = DiagMatrix (nr, nr, 1.0); |
|
172 } |
|
173 else |
|
174 { |
|
175 // Too much copying? |
|
176 // XXX FIXME XXX -- we shouldn't do this if the exponent is |
|
177 // large... |
|
178 |
|
179 Matrix atmp; |
|
180 if (btmp < 0) |
|
181 { |
|
182 btmp = -btmp; |
1655
|
183 |
|
184 int info; |
|
185 double rcond = 0.0; |
|
186 |
1656
|
187 atmp = a.inverse (info, rcond, 1); |
1655
|
188 |
|
189 if (info == -1) |
|
190 warning ("inverse: matrix singular to machine\ |
|
191 precision, rcond = %g", rcond); |
1567
|
192 } |
|
193 else |
|
194 atmp = a; |
|
195 |
|
196 Matrix result (atmp); |
3178
|
197 |
|
198 btmp--; |
|
199 |
|
200 while (btmp > 0) |
|
201 { |
|
202 if (btmp & 1) |
|
203 result = result * atmp; |
|
204 |
|
205 btmp >>= 1; |
|
206 |
|
207 if (btmp > 0) |
|
208 atmp = atmp * atmp; |
|
209 } |
1567
|
210 |
|
211 retval = result; |
|
212 } |
1
|
213 } |
|
214 else |
|
215 { |
1567
|
216 EIG a_eig (a); |
|
217 ComplexColumnVector lambda (a_eig.eigenvalues ()); |
|
218 ComplexMatrix Q (a_eig.eigenvectors ()); |
|
219 |
|
220 for (int i = 0; i < nr; i++) |
2305
|
221 lambda (i) = pow (lambda (i), b); |
1567
|
222 |
|
223 ComplexDiagMatrix D (lambda); |
|
224 |
|
225 retval = ComplexMatrix (Q * D * Q.inverse ()); |
|
226 } |
|
227 } |
1358
|
228 |
1567
|
229 return retval; |
|
230 } |
1
|
231 |
1567
|
232 // -*- 6 -*- |
2086
|
233 octave_value |
1567
|
234 xpow (const Matrix& a, const Complex& b) |
|
235 { |
2086
|
236 octave_value retval; |
1
|
237 |
1567
|
238 int nr = a.rows (); |
2365
|
239 int nc = a.cols (); |
1567
|
240 |
|
241 if (nr == 0 || nc == 0 || nr != nc) |
2365
|
242 error ("for A^b, A must be square"); |
1
|
243 else |
|
244 { |
|
245 EIG a_eig (a); |
|
246 ComplexColumnVector lambda (a_eig.eigenvalues ()); |
|
247 ComplexMatrix Q (a_eig.eigenvectors ()); |
|
248 |
|
249 for (int i = 0; i < nr; i++) |
2305
|
250 lambda (i) = pow (lambda (i), b); |
1
|
251 |
|
252 ComplexDiagMatrix D (lambda); |
|
253 |
1567
|
254 retval = ComplexMatrix (Q * D * Q.inverse ()); |
1
|
255 } |
|
256 |
|
257 return retval; |
|
258 } |
|
259 |
767
|
260 // -*- 7 -*- |
2086
|
261 octave_value |
164
|
262 xpow (const Complex& a, double b) |
1
|
263 { |
|
264 Complex result; |
1567
|
265 |
|
266 if (xisint (b)) |
2800
|
267 result = pow (a, static_cast<int> (b)); |
1567
|
268 else |
|
269 result = pow (a, b); |
|
270 |
|
271 return result; |
1
|
272 } |
|
273 |
767
|
274 // -*- 8 -*- |
2086
|
275 octave_value |
164
|
276 xpow (const Complex& a, const Matrix& b) |
1
|
277 { |
2086
|
278 octave_value retval; |
1
|
279 |
|
280 int nr = b.rows (); |
2365
|
281 int nc = b.cols (); |
1
|
282 |
|
283 if (nr == 0 || nc == 0 || nr != nc) |
2365
|
284 error ("for x^A, A must be square"); |
1
|
285 else |
|
286 { |
|
287 EIG b_eig (b); |
|
288 ComplexColumnVector lambda (b_eig.eigenvalues ()); |
|
289 ComplexMatrix Q (b_eig.eigenvectors ()); |
|
290 |
|
291 for (int i = 0; i < nr; i++) |
|
292 { |
2305
|
293 Complex elt = lambda (i); |
1
|
294 if (imag (elt) == 0.0) |
2305
|
295 lambda (i) = pow (a, real (elt)); |
1
|
296 else |
2305
|
297 lambda (i) = pow (a, elt); |
1
|
298 } |
|
299 ComplexDiagMatrix D (lambda); |
|
300 |
1567
|
301 retval = ComplexMatrix (Q * D * Q.inverse ()); |
1
|
302 } |
|
303 |
|
304 return retval; |
|
305 } |
|
306 |
767
|
307 // -*- 9 -*- |
2086
|
308 octave_value |
164
|
309 xpow (const Complex& a, const Complex& b) |
1
|
310 { |
|
311 Complex result; |
|
312 result = pow (a, b); |
1567
|
313 return result; |
1
|
314 } |
|
315 |
767
|
316 // -*- 10 -*- |
2086
|
317 octave_value |
164
|
318 xpow (const Complex& a, const ComplexMatrix& b) |
1
|
319 { |
2086
|
320 octave_value retval; |
1
|
321 |
|
322 int nr = b.rows (); |
2365
|
323 int nc = b.cols (); |
1
|
324 |
|
325 if (nr == 0 || nc == 0 || nr != nc) |
2365
|
326 error ("for x^A, A must be square"); |
1
|
327 else |
|
328 { |
|
329 EIG b_eig (b); |
|
330 ComplexColumnVector lambda (b_eig.eigenvalues ()); |
|
331 ComplexMatrix Q (b_eig.eigenvectors ()); |
|
332 |
|
333 for (int i = 0; i < nr; i++) |
|
334 { |
2305
|
335 Complex elt = lambda (i); |
1
|
336 if (imag (elt) == 0.0) |
2305
|
337 lambda (i) = pow (a, real (elt)); |
1
|
338 else |
2305
|
339 lambda (i) = pow (a, elt); |
1
|
340 } |
|
341 ComplexDiagMatrix D (lambda); |
|
342 |
1567
|
343 retval = ComplexMatrix (Q * D * Q.inverse ()); |
1
|
344 } |
|
345 |
|
346 return retval; |
|
347 } |
|
348 |
767
|
349 // -*- 11 -*- |
2086
|
350 octave_value |
164
|
351 xpow (const ComplexMatrix& a, double b) |
1
|
352 { |
2086
|
353 octave_value retval; |
1
|
354 |
|
355 int nr = a.rows (); |
2365
|
356 int nc = a.cols (); |
1
|
357 |
|
358 if (nr == 0 || nc == 0 || nr != nc) |
2365
|
359 error ("for A^b, A must be square"); |
1567
|
360 else |
1
|
361 { |
2800
|
362 if (static_cast<int> (b) == b) |
1
|
363 { |
2804
|
364 int btmp = static_cast<int> (b); |
1567
|
365 if (btmp == 0) |
|
366 { |
|
367 retval = DiagMatrix (nr, nr, 1.0); |
|
368 } |
|
369 else |
|
370 { |
|
371 // Too much copying? |
|
372 // XXX FIXME XXX -- we shouldn't do this if the exponent is |
|
373 // large... |
|
374 |
|
375 ComplexMatrix atmp; |
|
376 if (btmp < 0) |
|
377 { |
|
378 btmp = -btmp; |
1655
|
379 |
|
380 int info; |
|
381 double rcond = 0.0; |
|
382 |
1656
|
383 atmp = a.inverse (info, rcond, 1); |
1655
|
384 |
|
385 if (info == -1) |
|
386 warning ("inverse: matrix singular to machine\ |
|
387 precision, rcond = %g", rcond); |
1567
|
388 } |
|
389 else |
|
390 atmp = a; |
|
391 |
|
392 ComplexMatrix result (atmp); |
3178
|
393 |
|
394 btmp--; |
|
395 |
|
396 while (btmp > 0) |
|
397 { |
|
398 if (btmp & 1) |
|
399 result = result * atmp; |
|
400 |
|
401 btmp >>= 1; |
|
402 |
|
403 if (btmp > 0) |
|
404 atmp = atmp * atmp; |
|
405 } |
1567
|
406 |
|
407 retval = result; |
|
408 } |
1
|
409 } |
|
410 else |
|
411 { |
1567
|
412 EIG a_eig (a); |
|
413 ComplexColumnVector lambda (a_eig.eigenvalues ()); |
|
414 ComplexMatrix Q (a_eig.eigenvectors ()); |
|
415 |
|
416 for (int i = 0; i < nr; i++) |
2305
|
417 lambda (i) = pow (lambda (i), b); |
1567
|
418 |
|
419 ComplexDiagMatrix D (lambda); |
|
420 |
|
421 retval = ComplexMatrix (Q * D * Q.inverse ()); |
|
422 } |
|
423 } |
1358
|
424 |
1567
|
425 return retval; |
|
426 } |
1
|
427 |
1567
|
428 // -*- 12 -*- |
2086
|
429 octave_value |
1567
|
430 xpow (const ComplexMatrix& a, const Complex& b) |
|
431 { |
2086
|
432 octave_value retval; |
1
|
433 |
1567
|
434 int nr = a.rows (); |
2365
|
435 int nc = a.cols (); |
1567
|
436 |
|
437 if (nr == 0 || nc == 0 || nr != nc) |
2365
|
438 error ("for A^b, A must be square"); |
1
|
439 else |
|
440 { |
|
441 EIG a_eig (a); |
|
442 ComplexColumnVector lambda (a_eig.eigenvalues ()); |
|
443 ComplexMatrix Q (a_eig.eigenvectors ()); |
|
444 |
|
445 for (int i = 0; i < nr; i++) |
2305
|
446 lambda (i) = pow (lambda (i), b); |
1
|
447 |
|
448 ComplexDiagMatrix D (lambda); |
|
449 |
1567
|
450 retval = ComplexMatrix (Q * D * Q.inverse ()); |
1
|
451 } |
|
452 |
|
453 return retval; |
|
454 } |
|
455 |
767
|
456 // Safer pow functions that work elementwise for matrices. |
|
457 // |
|
458 // op2 \ op1: s m cs cm |
|
459 // +-- +---+---+----+----+ |
|
460 // scalar | | * | 3 | * | 9 | |
|
461 // +---+---+----+----+ |
|
462 // matrix | 1 | 4 | 7 | 10 | |
|
463 // +---+---+----+----+ |
|
464 // complex_scalar | * | 5 | * | 11 | |
|
465 // +---+---+----+----+ |
|
466 // complex_matrix | 2 | 6 | 8 | 12 | |
|
467 // +---+---+----+----+ |
|
468 // |
|
469 // * -> not needed. |
1
|
470 |
3162
|
471 // XXX FIXME XXX -- these functions need to be fixed so that things |
|
472 // like |
|
473 // |
|
474 // a = -1; b = [ 0, 0.5, 1 ]; r = a .^ b |
|
475 // |
|
476 // and |
|
477 // |
|
478 // a = -1; b = [ 0, 0.5, 1 ]; for i = 1:3, r(i) = a .^ b(i), end |
|
479 // |
|
480 // produce identical results. Also, it would be nice if -1^0.5 |
|
481 // produced a pure imaginary result instead of a complex number with a |
|
482 // small real part. But perhaps that's really a problem with the math |
|
483 // library... |
|
484 |
767
|
485 // -*- 1 -*- |
2086
|
486 octave_value |
164
|
487 elem_xpow (double a, const Matrix& b) |
1
|
488 { |
2086
|
489 octave_value retval; |
1
|
490 |
|
491 int nr = b.rows (); |
2365
|
492 int nc = b.cols (); |
1
|
493 |
3162
|
494 double d1, d2; |
1358
|
495 |
3162
|
496 if (a < 0.0 && ! b.all_integers (d1, d2)) |
1
|
497 { |
|
498 Complex atmp (a); |
|
499 ComplexMatrix result (nr, nc); |
|
500 for (int j = 0; j < nc; j++) |
|
501 for (int i = 0; i < nr; i++) |
2305
|
502 result (i, j) = pow (atmp, b (i, j)); |
1
|
503 |
1567
|
504 retval = result; |
1
|
505 } |
|
506 else |
|
507 { |
|
508 Matrix result (nr, nc); |
|
509 for (int j = 0; j < nc; j++) |
|
510 for (int i = 0; i < nr; i++) |
2305
|
511 result (i, j) = pow (a, b (i, j)); |
1
|
512 |
1567
|
513 retval = result; |
1
|
514 } |
|
515 |
|
516 return retval; |
|
517 } |
|
518 |
767
|
519 // -*- 2 -*- |
2086
|
520 octave_value |
164
|
521 elem_xpow (double a, const ComplexMatrix& b) |
1
|
522 { |
|
523 int nr = b.rows (); |
2365
|
524 int nc = b.cols (); |
1
|
525 |
|
526 ComplexMatrix result (nr, nc); |
3125
|
527 Complex atmp (a); |
1
|
528 for (int j = 0; j < nc; j++) |
|
529 for (int i = 0; i < nr; i++) |
3125
|
530 result (i, j) = pow (atmp, b (i, j)); |
1
|
531 |
1567
|
532 return result; |
1
|
533 } |
|
534 |
767
|
535 // -*- 3 -*- |
2086
|
536 octave_value |
164
|
537 elem_xpow (const Matrix& a, double b) |
1
|
538 { |
2086
|
539 octave_value retval; |
1
|
540 |
|
541 int nr = a.rows (); |
2365
|
542 int nc = a.cols (); |
1
|
543 |
2800
|
544 if (static_cast<int> (b) != b && a.any_element_is_negative ()) |
1
|
545 { |
|
546 ComplexMatrix result (nr, nc); |
|
547 for (int j = 0; j < nc; j++) |
|
548 for (int i = 0; i < nr; i++) |
|
549 { |
2305
|
550 Complex atmp (a (i, j)); |
|
551 result (i, j) = pow (atmp, b); |
1
|
552 } |
|
553 |
1567
|
554 retval = result; |
1
|
555 } |
|
556 else |
|
557 { |
|
558 Matrix result (nr, nc); |
|
559 for (int j = 0; j < nc; j++) |
|
560 for (int i = 0; i < nr; i++) |
2305
|
561 result (i, j) = pow (a (i, j), b); |
1
|
562 |
1567
|
563 retval = result; |
1
|
564 } |
|
565 |
|
566 return retval; |
|
567 } |
|
568 |
767
|
569 // -*- 4 -*- |
2086
|
570 octave_value |
164
|
571 elem_xpow (const Matrix& a, const Matrix& b) |
1
|
572 { |
2086
|
573 octave_value retval; |
1567
|
574 |
1
|
575 int nr = a.rows (); |
2365
|
576 int nc = a.cols (); |
|
577 |
|
578 int b_nr = b.rows (); |
|
579 int b_nc = b.cols (); |
1
|
580 |
2365
|
581 if (nr != b_nr || nc != b_nc) |
|
582 { |
|
583 gripe_nonconformant ("operator .^", nr, nc, b_nr, b_nc); |
|
584 return octave_value (); |
|
585 } |
1
|
586 |
|
587 int convert_to_complex = 0; |
|
588 for (int j = 0; j < nc; j++) |
1321
|
589 for (int i = 0; i < nr; i++) |
1
|
590 { |
2305
|
591 double atmp = a (i, j); |
|
592 double btmp = b (i, j); |
2800
|
593 if (atmp < 0.0 && static_cast<int> (btmp) != btmp) |
1
|
594 { |
|
595 convert_to_complex = 1; |
|
596 goto done; |
|
597 } |
|
598 } |
|
599 |
2365
|
600 done: |
1
|
601 |
|
602 if (convert_to_complex) |
|
603 { |
|
604 ComplexMatrix complex_result (nr, nc); |
|
605 |
1321
|
606 for (int j = 0; j < nc; j++) |
|
607 for (int i = 0; i < nr; i++) |
1
|
608 { |
2305
|
609 Complex atmp (a (i, j)); |
|
610 Complex btmp (b (i, j)); |
|
611 complex_result (i, j) = pow (atmp, btmp); |
1
|
612 } |
1567
|
613 |
|
614 retval = complex_result; |
1
|
615 } |
|
616 else |
|
617 { |
|
618 Matrix result (nr, nc); |
|
619 |
1321
|
620 for (int j = 0; j < nc; j++) |
|
621 for (int i = 0; i < nr; i++) |
2305
|
622 result (i, j) = pow (a (i, j), b (i, j)); |
1
|
623 |
1567
|
624 retval = result; |
1
|
625 } |
1567
|
626 |
|
627 return retval; |
1
|
628 } |
|
629 |
767
|
630 // -*- 5 -*- |
2086
|
631 octave_value |
164
|
632 elem_xpow (const Matrix& a, const Complex& b) |
1
|
633 { |
|
634 int nr = a.rows (); |
2365
|
635 int nc = a.cols (); |
1
|
636 |
|
637 ComplexMatrix result (nr, nc); |
|
638 for (int j = 0; j < nc; j++) |
|
639 for (int i = 0; i < nr; i++) |
3125
|
640 result (i, j) = pow (Complex (a (i, j)), b); |
1
|
641 |
1567
|
642 return result; |
1
|
643 } |
|
644 |
767
|
645 // -*- 6 -*- |
2086
|
646 octave_value |
164
|
647 elem_xpow (const Matrix& a, const ComplexMatrix& b) |
1
|
648 { |
|
649 int nr = a.rows (); |
2365
|
650 int nc = a.cols (); |
|
651 |
|
652 int b_nr = b.rows (); |
|
653 int b_nc = b.cols (); |
1
|
654 |
2365
|
655 if (nr != b_nr || nc != b_nc) |
|
656 { |
|
657 gripe_nonconformant ("operator .^", nr, nc, b_nr, b_nc); |
|
658 return octave_value (); |
|
659 } |
1
|
660 |
|
661 ComplexMatrix result (nr, nc); |
|
662 for (int j = 0; j < nc; j++) |
|
663 for (int i = 0; i < nr; i++) |
3125
|
664 result (i, j) = pow (Complex (a (i, j)), b (i, j)); |
1
|
665 |
1567
|
666 return result; |
1
|
667 } |
|
668 |
767
|
669 // -*- 7 -*- |
2086
|
670 octave_value |
164
|
671 elem_xpow (const Complex& a, const Matrix& b) |
1
|
672 { |
|
673 int nr = b.rows (); |
2365
|
674 int nc = b.cols (); |
1
|
675 |
|
676 ComplexMatrix result (nr, nc); |
|
677 for (int j = 0; j < nc; j++) |
|
678 for (int i = 0; i < nr; i++) |
1567
|
679 { |
2305
|
680 double btmp = b (i, j); |
1567
|
681 if (xisint (btmp)) |
2800
|
682 result (i, j) = pow (a, static_cast<int> (btmp)); |
1567
|
683 else |
2305
|
684 result (i, j) = pow (a, btmp); |
1567
|
685 } |
1
|
686 |
1567
|
687 return result; |
1
|
688 } |
|
689 |
767
|
690 // -*- 8 -*- |
2086
|
691 octave_value |
164
|
692 elem_xpow (const Complex& a, const ComplexMatrix& b) |
1
|
693 { |
|
694 int nr = b.rows (); |
2365
|
695 int nc = b.cols (); |
1
|
696 |
|
697 ComplexMatrix result (nr, nc); |
|
698 for (int j = 0; j < nc; j++) |
|
699 for (int i = 0; i < nr; i++) |
2305
|
700 result (i, j) = pow (a, b (i, j)); |
1
|
701 |
1567
|
702 return result; |
1
|
703 } |
|
704 |
767
|
705 // -*- 9 -*- |
2086
|
706 octave_value |
164
|
707 elem_xpow (const ComplexMatrix& a, double b) |
1
|
708 { |
|
709 int nr = a.rows (); |
2365
|
710 int nc = a.cols (); |
1
|
711 |
|
712 ComplexMatrix result (nr, nc); |
|
713 |
1567
|
714 if (xisint (b)) |
|
715 { |
|
716 for (int j = 0; j < nc; j++) |
|
717 for (int i = 0; i < nr; i++) |
2800
|
718 result (i, j) = pow (a (i, j), static_cast<int> (b)); |
1567
|
719 } |
|
720 else |
|
721 { |
|
722 for (int j = 0; j < nc; j++) |
|
723 for (int i = 0; i < nr; i++) |
2305
|
724 result (i, j) = pow (a (i, j), b); |
1567
|
725 } |
|
726 |
|
727 return result; |
1
|
728 } |
|
729 |
767
|
730 // -*- 10 -*- |
2086
|
731 octave_value |
164
|
732 elem_xpow (const ComplexMatrix& a, const Matrix& b) |
1
|
733 { |
|
734 int nr = a.rows (); |
2365
|
735 int nc = a.cols (); |
|
736 |
|
737 int b_nr = b.rows (); |
|
738 int b_nc = b.cols (); |
1
|
739 |
2365
|
740 if (nr != b_nr || nc != b_nc) |
|
741 { |
|
742 gripe_nonconformant ("operator .^", nr, nc, b_nr, b_nc); |
|
743 return octave_value (); |
|
744 } |
1
|
745 |
|
746 ComplexMatrix result (nr, nc); |
|
747 for (int j = 0; j < nc; j++) |
|
748 for (int i = 0; i < nr; i++) |
1567
|
749 { |
2305
|
750 double btmp = b (i, j); |
1567
|
751 if (xisint (btmp)) |
2800
|
752 result (i, j) = pow (a (i, j), static_cast<int> (btmp)); |
1567
|
753 else |
2305
|
754 result (i, j) = pow (a (i, j), btmp); |
1567
|
755 } |
1
|
756 |
1567
|
757 return result; |
1
|
758 } |
|
759 |
767
|
760 // -*- 11 -*- |
2086
|
761 octave_value |
164
|
762 elem_xpow (const ComplexMatrix& a, const Complex& b) |
1
|
763 { |
|
764 int nr = a.rows (); |
2365
|
765 int nc = a.cols (); |
1
|
766 |
|
767 ComplexMatrix result (nr, nc); |
|
768 for (int j = 0; j < nc; j++) |
|
769 for (int i = 0; i < nr; i++) |
2305
|
770 result (i, j) = pow (a (i, j), b); |
1
|
771 |
1567
|
772 return result; |
1
|
773 } |
|
774 |
767
|
775 // -*- 12 -*- |
2086
|
776 octave_value |
164
|
777 elem_xpow (const ComplexMatrix& a, const ComplexMatrix& b) |
1
|
778 { |
|
779 int nr = a.rows (); |
2365
|
780 int nc = a.cols (); |
|
781 |
|
782 int b_nr = b.rows (); |
|
783 int b_nc = b.cols (); |
|
784 |
|
785 if (nr != b_nr || nc != b_nc) |
|
786 { |
|
787 gripe_nonconformant ("operator .^", nr, nc, b_nr, b_nc); |
|
788 return octave_value (); |
|
789 } |
1
|
790 |
|
791 ComplexMatrix result (nr, nc); |
|
792 for (int j = 0; j < nc; j++) |
|
793 for (int i = 0; i < nr; i++) |
2305
|
794 result (i, j) = pow (a (i, j), b (i, j)); |
1
|
795 |
1567
|
796 return result; |
1
|
797 } |
|
798 |
|
799 /* |
|
800 ;;; Local Variables: *** |
|
801 ;;; mode: C++ *** |
|
802 ;;; End: *** |
|
803 */ |