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