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