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