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" |
3585
|
36 #include "mx-cm-cdm.h" |
1651
|
37 #include "oct-cmplx.h" |
4153
|
38 #include "quit.h" |
1352
|
39 |
|
40 #include "error.h" |
4055
|
41 #include "oct-obj.h" |
1567
|
42 #include "utils.h" |
1352
|
43 #include "xpow.h" |
1
|
44 |
1567
|
45 static inline int |
|
46 xisint (double x) |
|
47 { |
|
48 return (D_NINT (x) == x |
|
49 && ((x >= 0 && x < INT_MAX) |
|
50 || (x <= 0 && x > INT_MIN))); |
|
51 } |
|
52 |
767
|
53 // Safer pow functions. |
|
54 // |
|
55 // op2 \ op1: s m cs cm |
|
56 // +-- +---+---+----+----+ |
|
57 // scalar | | 1 | 5 | 7 | 11 | |
|
58 // +---+---+----+----+ |
2365
|
59 // matrix | 2 | * | 8 | * | |
767
|
60 // +---+---+----+----+ |
|
61 // complex_scalar | 3 | 6 | 9 | 12 | |
|
62 // +---+---+----+----+ |
2365
|
63 // complex_matrix | 4 | * | 10 | * | |
767
|
64 // +---+---+----+----+ |
1
|
65 |
767
|
66 // -*- 1 -*- |
2086
|
67 octave_value |
1
|
68 xpow (double a, double b) |
|
69 { |
2800
|
70 if (a < 0.0 && static_cast<int> (b) != b) |
1
|
71 { |
|
72 Complex atmp (a); |
1567
|
73 return pow (atmp, b); |
1
|
74 } |
|
75 else |
1567
|
76 return pow (a, b); |
1
|
77 } |
|
78 |
767
|
79 // -*- 2 -*- |
2086
|
80 octave_value |
164
|
81 xpow (double a, const Matrix& b) |
1
|
82 { |
2086
|
83 octave_value retval; |
1
|
84 |
|
85 int nr = b.rows (); |
2365
|
86 int nc = b.cols (); |
1
|
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 { |
2305
|
98 Complex elt = lambda (i); |
1
|
99 if (imag (elt) == 0.0) |
2305
|
100 lambda (i) = pow (a, real (elt)); |
1
|
101 else |
2305
|
102 lambda (i) = pow (a, elt); |
1
|
103 } |
|
104 ComplexDiagMatrix D (lambda); |
|
105 |
1567
|
106 retval = ComplexMatrix (Q * D * Q.inverse ()); |
1
|
107 } |
|
108 |
|
109 return retval; |
|
110 } |
|
111 |
767
|
112 // -*- 3 -*- |
2086
|
113 octave_value |
164
|
114 xpow (double a, const Complex& b) |
1
|
115 { |
|
116 Complex result; |
|
117 Complex atmp (a); |
|
118 result = pow (atmp, b); |
1567
|
119 return result; |
1
|
120 } |
|
121 |
767
|
122 // -*- 4 -*- |
2086
|
123 octave_value |
164
|
124 xpow (double a, const ComplexMatrix& b) |
1
|
125 { |
2086
|
126 octave_value retval; |
1
|
127 |
|
128 int nr = b.rows (); |
2365
|
129 int nc = b.cols (); |
1
|
130 |
|
131 if (nr == 0 || nc == 0 || nr != nc) |
2365
|
132 error ("for x^A, A must be square"); |
1
|
133 else |
|
134 { |
|
135 EIG b_eig (b); |
|
136 ComplexColumnVector lambda (b_eig.eigenvalues ()); |
|
137 ComplexMatrix Q (b_eig.eigenvectors ()); |
|
138 |
|
139 for (int i = 0; i < nr; i++) |
|
140 { |
2305
|
141 Complex elt = lambda (i); |
1
|
142 if (imag (elt) == 0.0) |
2305
|
143 lambda (i) = pow (a, real (elt)); |
1
|
144 else |
2305
|
145 lambda (i) = pow (a, elt); |
1
|
146 } |
|
147 ComplexDiagMatrix D (lambda); |
|
148 |
1567
|
149 retval = ComplexMatrix (Q * D * Q.inverse ()); |
1
|
150 } |
|
151 |
|
152 return retval; |
|
153 } |
|
154 |
767
|
155 // -*- 5 -*- |
2086
|
156 octave_value |
164
|
157 xpow (const Matrix& a, double b) |
1
|
158 { |
2086
|
159 octave_value retval; |
1
|
160 |
|
161 int nr = a.rows (); |
2365
|
162 int nc = a.cols (); |
1
|
163 |
|
164 if (nr == 0 || nc == 0 || nr != nc) |
2365
|
165 error ("for A^b, A must be square"); |
1567
|
166 else |
1
|
167 { |
2800
|
168 if (static_cast<int> (b) == b) |
1
|
169 { |
2804
|
170 int btmp = static_cast<int> (b); |
1567
|
171 if (btmp == 0) |
|
172 { |
|
173 retval = DiagMatrix (nr, nr, 1.0); |
|
174 } |
|
175 else |
|
176 { |
|
177 // Too much copying? |
|
178 // XXX FIXME XXX -- we shouldn't do this if the exponent is |
|
179 // large... |
|
180 |
|
181 Matrix atmp; |
|
182 if (btmp < 0) |
|
183 { |
|
184 btmp = -btmp; |
1655
|
185 |
|
186 int info; |
|
187 double rcond = 0.0; |
|
188 |
1656
|
189 atmp = a.inverse (info, rcond, 1); |
1655
|
190 |
|
191 if (info == -1) |
|
192 warning ("inverse: matrix singular to machine\ |
|
193 precision, rcond = %g", rcond); |
1567
|
194 } |
|
195 else |
|
196 atmp = a; |
|
197 |
|
198 Matrix result (atmp); |
3178
|
199 |
|
200 btmp--; |
|
201 |
|
202 while (btmp > 0) |
|
203 { |
|
204 if (btmp & 1) |
|
205 result = result * atmp; |
|
206 |
|
207 btmp >>= 1; |
|
208 |
|
209 if (btmp > 0) |
|
210 atmp = atmp * atmp; |
|
211 } |
1567
|
212 |
|
213 retval = result; |
|
214 } |
1
|
215 } |
|
216 else |
|
217 { |
1567
|
218 EIG a_eig (a); |
|
219 ComplexColumnVector lambda (a_eig.eigenvalues ()); |
|
220 ComplexMatrix Q (a_eig.eigenvectors ()); |
|
221 |
|
222 for (int i = 0; i < nr; i++) |
2305
|
223 lambda (i) = pow (lambda (i), b); |
1567
|
224 |
|
225 ComplexDiagMatrix D (lambda); |
|
226 |
|
227 retval = ComplexMatrix (Q * D * Q.inverse ()); |
|
228 } |
|
229 } |
1358
|
230 |
1567
|
231 return retval; |
|
232 } |
1
|
233 |
1567
|
234 // -*- 6 -*- |
2086
|
235 octave_value |
1567
|
236 xpow (const Matrix& a, const Complex& b) |
|
237 { |
2086
|
238 octave_value retval; |
1
|
239 |
1567
|
240 int nr = a.rows (); |
2365
|
241 int nc = a.cols (); |
1567
|
242 |
|
243 if (nr == 0 || nc == 0 || nr != nc) |
2365
|
244 error ("for A^b, A must be square"); |
1
|
245 else |
|
246 { |
|
247 EIG a_eig (a); |
|
248 ComplexColumnVector lambda (a_eig.eigenvalues ()); |
|
249 ComplexMatrix Q (a_eig.eigenvectors ()); |
|
250 |
|
251 for (int i = 0; i < nr; i++) |
2305
|
252 lambda (i) = pow (lambda (i), b); |
1
|
253 |
|
254 ComplexDiagMatrix D (lambda); |
|
255 |
1567
|
256 retval = ComplexMatrix (Q * D * Q.inverse ()); |
1
|
257 } |
|
258 |
|
259 return retval; |
|
260 } |
|
261 |
767
|
262 // -*- 7 -*- |
2086
|
263 octave_value |
164
|
264 xpow (const Complex& a, double b) |
1
|
265 { |
|
266 Complex result; |
1567
|
267 |
|
268 if (xisint (b)) |
2800
|
269 result = pow (a, static_cast<int> (b)); |
1567
|
270 else |
|
271 result = pow (a, b); |
|
272 |
|
273 return result; |
1
|
274 } |
|
275 |
767
|
276 // -*- 8 -*- |
2086
|
277 octave_value |
164
|
278 xpow (const Complex& a, const Matrix& b) |
1
|
279 { |
2086
|
280 octave_value retval; |
1
|
281 |
|
282 int nr = b.rows (); |
2365
|
283 int nc = b.cols (); |
1
|
284 |
|
285 if (nr == 0 || nc == 0 || nr != nc) |
2365
|
286 error ("for x^A, A must be square"); |
1
|
287 else |
|
288 { |
|
289 EIG b_eig (b); |
|
290 ComplexColumnVector lambda (b_eig.eigenvalues ()); |
|
291 ComplexMatrix Q (b_eig.eigenvectors ()); |
|
292 |
|
293 for (int i = 0; i < nr; i++) |
|
294 { |
2305
|
295 Complex elt = lambda (i); |
1
|
296 if (imag (elt) == 0.0) |
2305
|
297 lambda (i) = pow (a, real (elt)); |
1
|
298 else |
2305
|
299 lambda (i) = pow (a, elt); |
1
|
300 } |
|
301 ComplexDiagMatrix D (lambda); |
|
302 |
1567
|
303 retval = ComplexMatrix (Q * D * Q.inverse ()); |
1
|
304 } |
|
305 |
|
306 return retval; |
|
307 } |
|
308 |
767
|
309 // -*- 9 -*- |
2086
|
310 octave_value |
164
|
311 xpow (const Complex& a, const Complex& b) |
1
|
312 { |
|
313 Complex result; |
|
314 result = pow (a, b); |
1567
|
315 return result; |
1
|
316 } |
|
317 |
767
|
318 // -*- 10 -*- |
2086
|
319 octave_value |
164
|
320 xpow (const Complex& a, const ComplexMatrix& b) |
1
|
321 { |
2086
|
322 octave_value retval; |
1
|
323 |
|
324 int nr = b.rows (); |
2365
|
325 int nc = b.cols (); |
1
|
326 |
|
327 if (nr == 0 || nc == 0 || nr != nc) |
2365
|
328 error ("for x^A, A must be square"); |
1
|
329 else |
|
330 { |
|
331 EIG b_eig (b); |
|
332 ComplexColumnVector lambda (b_eig.eigenvalues ()); |
|
333 ComplexMatrix Q (b_eig.eigenvectors ()); |
|
334 |
|
335 for (int i = 0; i < nr; i++) |
|
336 { |
2305
|
337 Complex elt = lambda (i); |
1
|
338 if (imag (elt) == 0.0) |
2305
|
339 lambda (i) = pow (a, real (elt)); |
1
|
340 else |
2305
|
341 lambda (i) = pow (a, elt); |
1
|
342 } |
|
343 ComplexDiagMatrix D (lambda); |
|
344 |
1567
|
345 retval = ComplexMatrix (Q * D * Q.inverse ()); |
1
|
346 } |
|
347 |
|
348 return retval; |
|
349 } |
|
350 |
767
|
351 // -*- 11 -*- |
2086
|
352 octave_value |
164
|
353 xpow (const ComplexMatrix& a, double b) |
1
|
354 { |
2086
|
355 octave_value retval; |
1
|
356 |
|
357 int nr = a.rows (); |
2365
|
358 int nc = a.cols (); |
1
|
359 |
|
360 if (nr == 0 || nc == 0 || nr != nc) |
2365
|
361 error ("for A^b, A must be square"); |
1567
|
362 else |
1
|
363 { |
2800
|
364 if (static_cast<int> (b) == b) |
1
|
365 { |
2804
|
366 int btmp = static_cast<int> (b); |
1567
|
367 if (btmp == 0) |
|
368 { |
|
369 retval = DiagMatrix (nr, nr, 1.0); |
|
370 } |
|
371 else |
|
372 { |
|
373 // Too much copying? |
|
374 // XXX FIXME XXX -- we shouldn't do this if the exponent is |
|
375 // large... |
|
376 |
|
377 ComplexMatrix atmp; |
|
378 if (btmp < 0) |
|
379 { |
|
380 btmp = -btmp; |
1655
|
381 |
|
382 int info; |
|
383 double rcond = 0.0; |
|
384 |
1656
|
385 atmp = a.inverse (info, rcond, 1); |
1655
|
386 |
|
387 if (info == -1) |
|
388 warning ("inverse: matrix singular to machine\ |
|
389 precision, rcond = %g", rcond); |
1567
|
390 } |
|
391 else |
|
392 atmp = a; |
|
393 |
|
394 ComplexMatrix result (atmp); |
3178
|
395 |
|
396 btmp--; |
|
397 |
|
398 while (btmp > 0) |
|
399 { |
|
400 if (btmp & 1) |
|
401 result = result * atmp; |
|
402 |
|
403 btmp >>= 1; |
|
404 |
|
405 if (btmp > 0) |
|
406 atmp = atmp * atmp; |
|
407 } |
1567
|
408 |
|
409 retval = result; |
|
410 } |
1
|
411 } |
|
412 else |
|
413 { |
1567
|
414 EIG a_eig (a); |
|
415 ComplexColumnVector lambda (a_eig.eigenvalues ()); |
|
416 ComplexMatrix Q (a_eig.eigenvectors ()); |
|
417 |
|
418 for (int i = 0; i < nr; i++) |
2305
|
419 lambda (i) = pow (lambda (i), b); |
1567
|
420 |
|
421 ComplexDiagMatrix D (lambda); |
|
422 |
|
423 retval = ComplexMatrix (Q * D * Q.inverse ()); |
|
424 } |
|
425 } |
1358
|
426 |
1567
|
427 return retval; |
|
428 } |
1
|
429 |
1567
|
430 // -*- 12 -*- |
2086
|
431 octave_value |
1567
|
432 xpow (const ComplexMatrix& a, const Complex& b) |
|
433 { |
2086
|
434 octave_value retval; |
1
|
435 |
1567
|
436 int nr = a.rows (); |
2365
|
437 int nc = a.cols (); |
1567
|
438 |
|
439 if (nr == 0 || nc == 0 || nr != nc) |
2365
|
440 error ("for A^b, A must be square"); |
1
|
441 else |
|
442 { |
|
443 EIG a_eig (a); |
|
444 ComplexColumnVector lambda (a_eig.eigenvalues ()); |
|
445 ComplexMatrix Q (a_eig.eigenvectors ()); |
|
446 |
|
447 for (int i = 0; i < nr; i++) |
2305
|
448 lambda (i) = pow (lambda (i), b); |
1
|
449 |
|
450 ComplexDiagMatrix D (lambda); |
|
451 |
1567
|
452 retval = ComplexMatrix (Q * D * Q.inverse ()); |
1
|
453 } |
|
454 |
|
455 return retval; |
|
456 } |
|
457 |
767
|
458 // Safer pow functions that work elementwise for matrices. |
|
459 // |
|
460 // op2 \ op1: s m cs cm |
|
461 // +-- +---+---+----+----+ |
|
462 // scalar | | * | 3 | * | 9 | |
|
463 // +---+---+----+----+ |
|
464 // matrix | 1 | 4 | 7 | 10 | |
|
465 // +---+---+----+----+ |
|
466 // complex_scalar | * | 5 | * | 11 | |
|
467 // +---+---+----+----+ |
|
468 // complex_matrix | 2 | 6 | 8 | 12 | |
|
469 // +---+---+----+----+ |
|
470 // |
|
471 // * -> not needed. |
1
|
472 |
3162
|
473 // XXX FIXME XXX -- these functions need to be fixed so that things |
|
474 // like |
|
475 // |
|
476 // a = -1; b = [ 0, 0.5, 1 ]; r = a .^ b |
|
477 // |
|
478 // and |
|
479 // |
|
480 // a = -1; b = [ 0, 0.5, 1 ]; for i = 1:3, r(i) = a .^ b(i), end |
|
481 // |
|
482 // produce identical results. Also, it would be nice if -1^0.5 |
|
483 // produced a pure imaginary result instead of a complex number with a |
|
484 // small real part. But perhaps that's really a problem with the math |
|
485 // library... |
|
486 |
767
|
487 // -*- 1 -*- |
2086
|
488 octave_value |
164
|
489 elem_xpow (double a, const Matrix& b) |
1
|
490 { |
2086
|
491 octave_value retval; |
1
|
492 |
|
493 int nr = b.rows (); |
2365
|
494 int nc = b.cols (); |
1
|
495 |
3162
|
496 double d1, d2; |
1358
|
497 |
3162
|
498 if (a < 0.0 && ! b.all_integers (d1, d2)) |
1
|
499 { |
|
500 Complex atmp (a); |
|
501 ComplexMatrix result (nr, nc); |
|
502 for (int j = 0; j < nc; j++) |
|
503 for (int i = 0; i < nr; i++) |
4153
|
504 { |
|
505 OCTAVE_QUIT; |
|
506 result (i, j) = pow (atmp, b (i, j)); |
|
507 } |
1
|
508 |
1567
|
509 retval = result; |
1
|
510 } |
|
511 else |
|
512 { |
|
513 Matrix result (nr, nc); |
|
514 for (int j = 0; j < nc; j++) |
|
515 for (int i = 0; i < nr; i++) |
4153
|
516 { |
|
517 OCTAVE_QUIT; |
|
518 result (i, j) = pow (a, b (i, j)); |
|
519 } |
1
|
520 |
1567
|
521 retval = result; |
1
|
522 } |
|
523 |
|
524 return retval; |
|
525 } |
|
526 |
767
|
527 // -*- 2 -*- |
2086
|
528 octave_value |
164
|
529 elem_xpow (double a, const ComplexMatrix& b) |
1
|
530 { |
|
531 int nr = b.rows (); |
2365
|
532 int nc = b.cols (); |
1
|
533 |
|
534 ComplexMatrix result (nr, nc); |
3125
|
535 Complex atmp (a); |
1
|
536 for (int j = 0; j < nc; j++) |
|
537 for (int i = 0; i < nr; i++) |
4153
|
538 { |
|
539 OCTAVE_QUIT; |
|
540 result (i, j) = pow (atmp, b (i, j)); |
|
541 } |
1
|
542 |
1567
|
543 return result; |
1
|
544 } |
|
545 |
767
|
546 // -*- 3 -*- |
2086
|
547 octave_value |
164
|
548 elem_xpow (const Matrix& a, double b) |
1
|
549 { |
2086
|
550 octave_value retval; |
1
|
551 |
|
552 int nr = a.rows (); |
2365
|
553 int nc = a.cols (); |
1
|
554 |
2800
|
555 if (static_cast<int> (b) != b && a.any_element_is_negative ()) |
1
|
556 { |
|
557 ComplexMatrix result (nr, nc); |
|
558 for (int j = 0; j < nc; j++) |
|
559 for (int i = 0; i < nr; i++) |
|
560 { |
4153
|
561 OCTAVE_QUIT; |
2305
|
562 Complex atmp (a (i, j)); |
|
563 result (i, j) = pow (atmp, b); |
1
|
564 } |
|
565 |
1567
|
566 retval = result; |
1
|
567 } |
|
568 else |
|
569 { |
|
570 Matrix result (nr, nc); |
|
571 for (int j = 0; j < nc; j++) |
|
572 for (int i = 0; i < nr; i++) |
4153
|
573 { |
|
574 OCTAVE_QUIT; |
|
575 result (i, j) = pow (a (i, j), b); |
|
576 } |
1
|
577 |
1567
|
578 retval = result; |
1
|
579 } |
|
580 |
|
581 return retval; |
|
582 } |
|
583 |
767
|
584 // -*- 4 -*- |
2086
|
585 octave_value |
164
|
586 elem_xpow (const Matrix& a, const Matrix& b) |
1
|
587 { |
2086
|
588 octave_value retval; |
1567
|
589 |
1
|
590 int nr = a.rows (); |
2365
|
591 int nc = a.cols (); |
|
592 |
|
593 int b_nr = b.rows (); |
|
594 int b_nc = b.cols (); |
1
|
595 |
2365
|
596 if (nr != b_nr || nc != b_nc) |
|
597 { |
|
598 gripe_nonconformant ("operator .^", nr, nc, b_nr, b_nc); |
|
599 return octave_value (); |
|
600 } |
1
|
601 |
|
602 int convert_to_complex = 0; |
|
603 for (int j = 0; j < nc; j++) |
1321
|
604 for (int i = 0; i < nr; i++) |
1
|
605 { |
4153
|
606 OCTAVE_QUIT; |
2305
|
607 double atmp = a (i, j); |
|
608 double btmp = b (i, j); |
2800
|
609 if (atmp < 0.0 && static_cast<int> (btmp) != btmp) |
1
|
610 { |
|
611 convert_to_complex = 1; |
|
612 goto done; |
|
613 } |
|
614 } |
|
615 |
2365
|
616 done: |
1
|
617 |
|
618 if (convert_to_complex) |
|
619 { |
|
620 ComplexMatrix complex_result (nr, nc); |
|
621 |
1321
|
622 for (int j = 0; j < nc; j++) |
|
623 for (int i = 0; i < nr; i++) |
1
|
624 { |
4153
|
625 OCTAVE_QUIT; |
2305
|
626 Complex atmp (a (i, j)); |
|
627 Complex btmp (b (i, j)); |
|
628 complex_result (i, j) = pow (atmp, btmp); |
1
|
629 } |
1567
|
630 |
|
631 retval = complex_result; |
1
|
632 } |
|
633 else |
|
634 { |
|
635 Matrix result (nr, nc); |
|
636 |
1321
|
637 for (int j = 0; j < nc; j++) |
|
638 for (int i = 0; i < nr; i++) |
4153
|
639 { |
|
640 OCTAVE_QUIT; |
|
641 result (i, j) = pow (a (i, j), b (i, j)); |
|
642 } |
1
|
643 |
1567
|
644 retval = result; |
1
|
645 } |
1567
|
646 |
|
647 return retval; |
1
|
648 } |
|
649 |
767
|
650 // -*- 5 -*- |
2086
|
651 octave_value |
164
|
652 elem_xpow (const Matrix& a, const Complex& b) |
1
|
653 { |
|
654 int nr = a.rows (); |
2365
|
655 int nc = a.cols (); |
1
|
656 |
|
657 ComplexMatrix result (nr, nc); |
|
658 for (int j = 0; j < nc; j++) |
|
659 for (int i = 0; i < nr; i++) |
4153
|
660 { |
|
661 OCTAVE_QUIT; |
|
662 result (i, j) = pow (Complex (a (i, j)), b); |
|
663 } |
1
|
664 |
1567
|
665 return result; |
1
|
666 } |
|
667 |
767
|
668 // -*- 6 -*- |
2086
|
669 octave_value |
164
|
670 elem_xpow (const Matrix& a, const ComplexMatrix& b) |
1
|
671 { |
|
672 int nr = a.rows (); |
2365
|
673 int nc = a.cols (); |
|
674 |
|
675 int b_nr = b.rows (); |
|
676 int b_nc = b.cols (); |
1
|
677 |
2365
|
678 if (nr != b_nr || nc != b_nc) |
|
679 { |
|
680 gripe_nonconformant ("operator .^", nr, nc, b_nr, b_nc); |
|
681 return octave_value (); |
|
682 } |
1
|
683 |
|
684 ComplexMatrix result (nr, nc); |
|
685 for (int j = 0; j < nc; j++) |
|
686 for (int i = 0; i < nr; i++) |
4153
|
687 { |
|
688 OCTAVE_QUIT; |
|
689 result (i, j) = pow (Complex (a (i, j)), b (i, j)); |
|
690 } |
1
|
691 |
1567
|
692 return result; |
1
|
693 } |
|
694 |
767
|
695 // -*- 7 -*- |
2086
|
696 octave_value |
164
|
697 elem_xpow (const Complex& a, const Matrix& b) |
1
|
698 { |
|
699 int nr = b.rows (); |
2365
|
700 int nc = b.cols (); |
1
|
701 |
|
702 ComplexMatrix result (nr, nc); |
|
703 for (int j = 0; j < nc; j++) |
|
704 for (int i = 0; i < nr; i++) |
1567
|
705 { |
4153
|
706 OCTAVE_QUIT; |
2305
|
707 double btmp = b (i, j); |
1567
|
708 if (xisint (btmp)) |
2800
|
709 result (i, j) = pow (a, static_cast<int> (btmp)); |
1567
|
710 else |
2305
|
711 result (i, j) = pow (a, btmp); |
1567
|
712 } |
1
|
713 |
1567
|
714 return result; |
1
|
715 } |
|
716 |
767
|
717 // -*- 8 -*- |
2086
|
718 octave_value |
164
|
719 elem_xpow (const Complex& a, const ComplexMatrix& b) |
1
|
720 { |
|
721 int nr = b.rows (); |
2365
|
722 int nc = b.cols (); |
1
|
723 |
|
724 ComplexMatrix result (nr, nc); |
|
725 for (int j = 0; j < nc; j++) |
|
726 for (int i = 0; i < nr; i++) |
4153
|
727 { |
|
728 OCTAVE_QUIT; |
|
729 result (i, j) = pow (a, b (i, j)); |
|
730 } |
1
|
731 |
1567
|
732 return result; |
1
|
733 } |
|
734 |
767
|
735 // -*- 9 -*- |
2086
|
736 octave_value |
164
|
737 elem_xpow (const ComplexMatrix& a, double b) |
1
|
738 { |
|
739 int nr = a.rows (); |
2365
|
740 int nc = a.cols (); |
1
|
741 |
|
742 ComplexMatrix result (nr, nc); |
|
743 |
1567
|
744 if (xisint (b)) |
|
745 { |
|
746 for (int j = 0; j < nc; j++) |
|
747 for (int i = 0; i < nr; i++) |
4153
|
748 { |
|
749 OCTAVE_QUIT; |
|
750 result (i, j) = pow (a (i, j), static_cast<int> (b)); |
|
751 } |
1567
|
752 } |
|
753 else |
|
754 { |
|
755 for (int j = 0; j < nc; j++) |
|
756 for (int i = 0; i < nr; i++) |
4153
|
757 { |
|
758 OCTAVE_QUIT; |
|
759 result (i, j) = pow (a (i, j), b); |
|
760 } |
1567
|
761 } |
|
762 |
|
763 return result; |
1
|
764 } |
|
765 |
767
|
766 // -*- 10 -*- |
2086
|
767 octave_value |
164
|
768 elem_xpow (const ComplexMatrix& a, const Matrix& b) |
1
|
769 { |
|
770 int nr = a.rows (); |
2365
|
771 int nc = a.cols (); |
|
772 |
|
773 int b_nr = b.rows (); |
|
774 int b_nc = b.cols (); |
1
|
775 |
2365
|
776 if (nr != b_nr || nc != b_nc) |
|
777 { |
|
778 gripe_nonconformant ("operator .^", nr, nc, b_nr, b_nc); |
|
779 return octave_value (); |
|
780 } |
1
|
781 |
|
782 ComplexMatrix result (nr, nc); |
|
783 for (int j = 0; j < nc; j++) |
|
784 for (int i = 0; i < nr; i++) |
1567
|
785 { |
4153
|
786 OCTAVE_QUIT; |
2305
|
787 double btmp = b (i, j); |
1567
|
788 if (xisint (btmp)) |
2800
|
789 result (i, j) = pow (a (i, j), static_cast<int> (btmp)); |
1567
|
790 else |
2305
|
791 result (i, j) = pow (a (i, j), btmp); |
1567
|
792 } |
1
|
793 |
1567
|
794 return result; |
1
|
795 } |
|
796 |
767
|
797 // -*- 11 -*- |
2086
|
798 octave_value |
164
|
799 elem_xpow (const ComplexMatrix& a, const Complex& b) |
1
|
800 { |
|
801 int nr = a.rows (); |
2365
|
802 int nc = a.cols (); |
1
|
803 |
|
804 ComplexMatrix result (nr, nc); |
|
805 for (int j = 0; j < nc; j++) |
|
806 for (int i = 0; i < nr; i++) |
4153
|
807 { |
|
808 OCTAVE_QUIT; |
|
809 result (i, j) = pow (a (i, j), b); |
|
810 } |
1
|
811 |
1567
|
812 return result; |
1
|
813 } |
|
814 |
767
|
815 // -*- 12 -*- |
2086
|
816 octave_value |
164
|
817 elem_xpow (const ComplexMatrix& a, const ComplexMatrix& b) |
1
|
818 { |
|
819 int nr = a.rows (); |
2365
|
820 int nc = a.cols (); |
|
821 |
|
822 int b_nr = b.rows (); |
|
823 int b_nc = b.cols (); |
|
824 |
|
825 if (nr != b_nr || nc != b_nc) |
|
826 { |
|
827 gripe_nonconformant ("operator .^", nr, nc, b_nr, b_nc); |
|
828 return octave_value (); |
|
829 } |
1
|
830 |
|
831 ComplexMatrix result (nr, nc); |
|
832 for (int j = 0; j < nc; j++) |
|
833 for (int i = 0; i < nr; i++) |
4153
|
834 { |
|
835 OCTAVE_QUIT; |
|
836 result (i, j) = pow (a (i, j), b (i, j)); |
|
837 } |
1
|
838 |
1567
|
839 return result; |
1
|
840 } |
|
841 |
4543
|
842 // Safer pow functions that work elementwise for N-d arrays. |
|
843 // |
|
844 // op2 \ op1: s nd cs cnd |
|
845 // +-- +---+---+----+----+ |
|
846 // scalar | | * | 3 | * | 9 | |
|
847 // +---+---+----+----+ |
|
848 // N_d | 1 | 4 | 7 | 10 | |
|
849 // +---+---+----+----+ |
|
850 // complex_scalar | * | 5 | * | 11 | |
|
851 // +---+---+----+----+ |
|
852 // complex_N_d | 2 | 6 | 8 | 12 | |
|
853 // +---+---+----+----+ |
|
854 // |
|
855 // * -> not needed. |
|
856 |
|
857 // XXX FIXME XXX -- these functions need to be fixed so that things |
|
858 // like |
|
859 // |
|
860 // a = -1; b = [ 0, 0.5, 1 ]; r = a .^ b |
|
861 // |
|
862 // and |
|
863 // |
|
864 // a = -1; b = [ 0, 0.5, 1 ]; for i = 1:3, r(i) = a .^ b(i), end |
|
865 // |
|
866 // produce identical results. Also, it would be nice if -1^0.5 |
|
867 // produced a pure imaginary result instead of a complex number with a |
|
868 // small real part. But perhaps that's really a problem with the math |
|
869 // library... |
|
870 |
|
871 // -*- 1 -*- |
|
872 octave_value |
|
873 elem_xpow (double a, const NDArray& b) |
|
874 { |
|
875 octave_value retval; |
|
876 |
|
877 double d1, d2; |
|
878 |
|
879 if (a < 0.0 && ! b.all_integers (d1, d2)) |
|
880 { |
|
881 Complex atmp (a); |
|
882 ComplexNDArray result (b.dims ()); |
|
883 for (int i = 0; i < b.length (); i++) |
|
884 { |
|
885 OCTAVE_QUIT; |
|
886 result(i) = pow (atmp, b(i)); |
|
887 } |
|
888 |
|
889 retval = result; |
|
890 } |
|
891 else |
|
892 { |
|
893 NDArray result (b.dims ()); |
|
894 for (int i = 0; i < b.length (); i++) |
|
895 { |
|
896 OCTAVE_QUIT; |
|
897 result (i) = pow (a, b(i)); |
|
898 } |
|
899 |
|
900 retval = result; |
|
901 } |
|
902 |
|
903 return retval; |
|
904 } |
|
905 |
|
906 // -*- 2 -*- |
|
907 octave_value |
|
908 elem_xpow (double a, const ComplexNDArray& b) |
|
909 { |
|
910 ComplexNDArray result (b.dims ()); |
|
911 Complex atmp (a); |
|
912 for (int i = 0; i < b.length (); i++) |
|
913 { |
|
914 OCTAVE_QUIT; |
|
915 result(i) = pow (atmp, b(i)); |
|
916 } |
|
917 |
|
918 return result; |
|
919 } |
|
920 |
|
921 // -*- 3 -*- |
|
922 octave_value |
|
923 elem_xpow (const NDArray& a, double b) |
|
924 { |
|
925 octave_value retval; |
|
926 |
|
927 if (static_cast<int> (b) != b && a.any_element_is_negative ()) |
|
928 { |
|
929 ComplexNDArray result (a.dims ()); |
|
930 |
|
931 for (int i = 0; i < a.length (); i++) |
|
932 { |
|
933 OCTAVE_QUIT; |
|
934 Complex atmp (a(i)); |
|
935 result(i) = pow (atmp, b); |
|
936 } |
|
937 |
|
938 retval = result; |
|
939 } |
|
940 else |
|
941 { |
|
942 NDArray result (a.dims ()); |
|
943 |
|
944 for (int i = 0; i < a.length (); i++) |
|
945 { |
|
946 OCTAVE_QUIT; |
|
947 result(i) = pow (a(i), b); |
|
948 } |
|
949 |
|
950 retval = result; |
|
951 } |
|
952 |
|
953 return retval; |
|
954 } |
|
955 |
|
956 // -*- 4 -*- |
|
957 octave_value |
|
958 elem_xpow (const NDArray& a, const NDArray& b) |
|
959 { |
|
960 octave_value retval; |
|
961 |
|
962 dim_vector a_dims = a.dims (); |
|
963 dim_vector b_dims = b.dims (); |
|
964 |
|
965 if (a_dims != b_dims) |
|
966 { |
|
967 gripe_nonconformant ("operator .^", a_dims, b_dims); |
|
968 return octave_value (); |
|
969 } |
|
970 |
|
971 int len = a.length (); |
|
972 |
|
973 bool convert_to_complex = false; |
|
974 |
|
975 for (int i = 0; i < len; i++) |
|
976 { |
|
977 OCTAVE_QUIT; |
|
978 double atmp = a(i); |
|
979 double btmp = b(i); |
|
980 if (atmp < 0.0 && static_cast<int> (btmp) != btmp) |
|
981 { |
|
982 convert_to_complex = true; |
|
983 goto done; |
|
984 } |
|
985 } |
|
986 |
|
987 done: |
|
988 |
|
989 if (convert_to_complex) |
|
990 { |
|
991 ComplexNDArray complex_result (a_dims); |
|
992 |
|
993 for (int i = 0; i < len; i++) |
|
994 { |
|
995 OCTAVE_QUIT; |
|
996 Complex atmp (a(i)); |
|
997 Complex btmp (b(i)); |
|
998 complex_result(i) = pow (atmp, btmp); |
|
999 } |
|
1000 |
|
1001 retval = complex_result; |
|
1002 } |
|
1003 else |
|
1004 { |
|
1005 NDArray result (a_dims); |
|
1006 |
|
1007 for (int i = 0; i < len; i++) |
|
1008 { |
|
1009 OCTAVE_QUIT; |
|
1010 result(i) = pow (a(i), b(i)); |
|
1011 } |
|
1012 |
|
1013 retval = result; |
|
1014 } |
|
1015 |
|
1016 return retval; |
|
1017 } |
|
1018 |
|
1019 // -*- 5 -*- |
|
1020 octave_value |
|
1021 elem_xpow (const NDArray& a, const Complex& b) |
|
1022 { |
|
1023 ComplexNDArray result (a.dims ()); |
|
1024 |
|
1025 for (int i = 0; i < a.length (); i++) |
|
1026 { |
|
1027 OCTAVE_QUIT; |
|
1028 result(i) = pow (Complex (a(i)), b); |
|
1029 } |
|
1030 |
|
1031 return result; |
|
1032 } |
|
1033 |
|
1034 // -*- 6 -*- |
|
1035 octave_value |
|
1036 elem_xpow (const NDArray& a, const ComplexNDArray& b) |
|
1037 { |
|
1038 dim_vector a_dims = a.dims (); |
|
1039 dim_vector b_dims = b.dims (); |
|
1040 |
|
1041 if (a_dims != b_dims) |
|
1042 { |
|
1043 gripe_nonconformant ("operator .^", a_dims, b_dims); |
|
1044 return octave_value (); |
|
1045 } |
|
1046 |
|
1047 ComplexNDArray result (a_dims); |
|
1048 for (int i = 0; i < a.length (); i++) |
|
1049 { |
|
1050 OCTAVE_QUIT; |
|
1051 result(i) = pow (Complex (a(i)), b(i)); |
|
1052 } |
|
1053 |
|
1054 return result; |
|
1055 } |
|
1056 |
|
1057 // -*- 7 -*- |
|
1058 octave_value |
|
1059 elem_xpow (const Complex& a, const NDArray& b) |
|
1060 { |
|
1061 ComplexNDArray result (b.dims ()); |
|
1062 for (int i = 0; i < b.length (); i++) |
|
1063 { |
|
1064 OCTAVE_QUIT; |
|
1065 double btmp = b(i); |
|
1066 if (xisint (btmp)) |
|
1067 result(i) = pow (a, static_cast<int> (btmp)); |
|
1068 else |
|
1069 result(i) = pow (a, btmp); |
|
1070 } |
|
1071 |
|
1072 return result; |
|
1073 } |
|
1074 |
|
1075 // -*- 8 -*- |
|
1076 octave_value |
|
1077 elem_xpow (const Complex& a, const ComplexNDArray& b) |
|
1078 { |
|
1079 ComplexNDArray result (b.dims ()); |
|
1080 for (int i = 0; i < b.length (); i++) |
|
1081 { |
|
1082 OCTAVE_QUIT; |
|
1083 result(i) = pow (a, b(i)); |
|
1084 } |
|
1085 |
|
1086 return result; |
|
1087 } |
|
1088 |
|
1089 // -*- 9 -*- |
|
1090 octave_value |
|
1091 elem_xpow (const ComplexNDArray& a, double b) |
|
1092 { |
|
1093 ComplexNDArray result (a.dims ()); |
|
1094 |
|
1095 if (xisint (b)) |
|
1096 { |
|
1097 for (int i = 0; i < a.length (); i++) |
|
1098 { |
|
1099 OCTAVE_QUIT; |
|
1100 result(i) = pow (a(i), static_cast<int> (b)); |
|
1101 } |
|
1102 } |
|
1103 else |
|
1104 { |
|
1105 for (int i = 0; i < a.length (); i++) |
|
1106 { |
|
1107 OCTAVE_QUIT; |
|
1108 result(i) = pow (a(i), b); |
|
1109 } |
|
1110 } |
|
1111 |
|
1112 return result; |
|
1113 } |
|
1114 |
|
1115 // -*- 10 -*- |
|
1116 octave_value |
|
1117 elem_xpow (const ComplexNDArray& a, const NDArray& b) |
|
1118 { |
|
1119 dim_vector a_dims = a.dims (); |
|
1120 dim_vector b_dims = b.dims (); |
|
1121 |
|
1122 if (a_dims != b_dims) |
|
1123 { |
|
1124 gripe_nonconformant ("operator .^", a_dims, b_dims); |
|
1125 return octave_value (); |
|
1126 } |
|
1127 |
|
1128 ComplexNDArray result (a_dims); |
|
1129 for (int i = 0; i < a.length (); i++) |
|
1130 { |
|
1131 OCTAVE_QUIT; |
|
1132 double btmp = b(i); |
|
1133 if (xisint (btmp)) |
|
1134 result(i) = pow (a(i), static_cast<int> (btmp)); |
|
1135 else |
|
1136 result(i) = pow (a(i), btmp); |
|
1137 } |
|
1138 |
|
1139 return result; |
|
1140 } |
|
1141 |
|
1142 // -*- 11 -*- |
|
1143 octave_value |
|
1144 elem_xpow (const ComplexNDArray& a, const Complex& b) |
|
1145 { |
|
1146 ComplexNDArray result (a.dims ()); |
|
1147 for (int i = 0; i < a.length (); i++) |
|
1148 { |
|
1149 OCTAVE_QUIT; |
|
1150 result(i) = pow (a(i), b); |
|
1151 } |
|
1152 |
|
1153 return result; |
|
1154 } |
|
1155 |
|
1156 // -*- 12 -*- |
|
1157 octave_value |
|
1158 elem_xpow (const ComplexNDArray& a, const ComplexNDArray& b) |
|
1159 { |
|
1160 dim_vector a_dims = a.dims (); |
|
1161 dim_vector b_dims = b.dims (); |
|
1162 |
|
1163 if (a_dims != b_dims) |
|
1164 { |
|
1165 gripe_nonconformant ("operator .^", a_dims, b_dims); |
|
1166 return octave_value (); |
|
1167 } |
|
1168 |
|
1169 ComplexNDArray result (a_dims); |
|
1170 for (int i = 0; i < a.length (); i++) |
|
1171 { |
|
1172 OCTAVE_QUIT; |
|
1173 result(i) = pow (a(i), b(i)); |
|
1174 } |
|
1175 |
|
1176 return result; |
|
1177 } |
|
1178 |
1
|
1179 /* |
|
1180 ;;; Local Variables: *** |
|
1181 ;;; mode: C++ *** |
|
1182 ;;; End: *** |
|
1183 */ |