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; |
5785
|
93 MatrixType 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; |
5785
|
166 MatrixType 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 |
5775
|
217 // FIXME -- these functions need to be fixed so that things |
5164
|
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; |
5953
|
252 result (i, j) = std::pow (atmp, b(i,j)); |
5164
|
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; |
5953
|
267 result (i, j) = std::pow (a, b(i,j)); |
5164
|
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; |
5953
|
292 result (i, j) = std::pow (atmp, b(i,j)); |
5164
|
293 } |
|
294 } |
|
295 |
|
296 return result; |
|
297 } |
|
298 |
|
299 // -*- 3 -*- |
|
300 octave_value |
|
301 elem_xpow (const SparseMatrix& a, double b) |
|
302 { |
5775
|
303 // FIXME What should a .^ 0 give?? Matlab gives a |
5164
|
304 // sparse matrix with same structure as a, which is strictly |
|
305 // incorrect. Keep compatiability. |
|
306 |
|
307 octave_value retval; |
|
308 |
5604
|
309 octave_idx_type nz = a.nzmax (); |
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 { |
5953
|
318 ComplexMatrix result (nr, nc, Complex (std::pow (0.0, b))); |
5164
|
319 |
5775
|
320 // FIXME -- avoid apparent GNU libm bug by |
5164
|
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 |
5953
|
331 result (a.ridx(i), j) = std::pow (atmp, btmp); |
5164
|
332 } |
|
333 |
|
334 retval = octave_value (result); |
|
335 } |
|
336 else |
|
337 { |
5953
|
338 Matrix result (nr, nc, (std::pow (0.0, b))); |
5164
|
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; |
5953
|
344 result (a.ridx(i), j) = std::pow (a.data (i), b); |
5164
|
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 |
5775
|
358 // FIXME -- avoid apparent GNU libm bug by |
5164
|
359 // converting A and B to complex instead of just A. |
|
360 |
|
361 Complex atmp (a.data (i)); |
|
362 Complex btmp (b); |
|
363 |
5953
|
364 result.data (i) = std::pow (atmp, btmp); |
5164
|
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; |
5953
|
378 result.data (i) = std::pow (a.data (i), b); |
5164
|
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++) |
5953
|
409 for (octave_idx_type i = a.cidx(j); i < a.cidx(j+1); i++) |
5164
|
410 { |
5953
|
411 if (a.data(i) < 0.0) |
5164
|
412 { |
5953
|
413 double btmp = b (a.ridx(i), j); |
|
414 if (static_cast<int> (btmp) != btmp) |
|
415 { |
|
416 convert_to_complex = 1; |
|
417 goto done; |
|
418 } |
5164
|
419 } |
|
420 } |
|
421 |
|
422 done: |
|
423 |
5953
|
424 // This is a dumb operator for sparse matrices anyway, and there is |
|
425 // no sensible way to handle the 0.^0 versus the 0.^x cases. Therefore |
|
426 // allocate a full matrix filled for the 0.^0 case and shrink it later |
|
427 // as needed |
5164
|
428 |
|
429 if (convert_to_complex) |
|
430 { |
5953
|
431 SparseComplexMatrix complex_result (nr, nc, Complex(1.0, 0.0)); |
5164
|
432 |
5275
|
433 for (octave_idx_type j = 0; j < nc; j++) |
5164
|
434 { |
5953
|
435 for (octave_idx_type i = a.cidx(j); i < a.cidx(j+1); i++) |
5164
|
436 { |
|
437 OCTAVE_QUIT; |
5953
|
438 complex_result.xelem(a.ridx(i), j) = |
|
439 std::pow (Complex(a.data(i)), Complex(b(a.ridx(i), j))); |
5164
|
440 } |
|
441 } |
5953
|
442 complex_result.maybe_compress (true); |
5164
|
443 retval = complex_result; |
|
444 } |
|
445 else |
|
446 { |
5953
|
447 SparseMatrix result (nr, nc, 1.0); |
5164
|
448 |
5275
|
449 for (octave_idx_type j = 0; j < nc; j++) |
5164
|
450 { |
5953
|
451 for (octave_idx_type i = a.cidx(j); i < a.cidx(j+1); i++) |
5164
|
452 { |
|
453 OCTAVE_QUIT; |
5953
|
454 result.xelem(a.ridx(i), j) = std::pow (a.data(i), |
|
455 b (a.ridx(i), j)); |
5164
|
456 } |
|
457 } |
5953
|
458 result.maybe_compress (true); |
5164
|
459 retval = result; |
|
460 } |
|
461 |
|
462 return retval; |
|
463 } |
|
464 |
|
465 // -*- 5 -*- |
|
466 octave_value |
|
467 elem_xpow (const SparseMatrix& a, const Complex& b) |
|
468 { |
|
469 octave_value retval; |
|
470 |
|
471 if (b == 0.0) |
|
472 // Can this case ever happen, due to automatic retyping with maybe_mutate? |
|
473 retval = octave_value (NDArray (a.dims (), 1)); |
|
474 else |
|
475 { |
5604
|
476 octave_idx_type nz = a.nzmax (); |
5164
|
477 SparseComplexMatrix result (a); |
|
478 |
5275
|
479 for (octave_idx_type i = 0; i < nz; i++) |
5164
|
480 { |
|
481 OCTAVE_QUIT; |
5953
|
482 result.data (i) = std::pow (Complex (a.data (i)), b); |
5164
|
483 } |
|
484 |
|
485 result.maybe_compress (true); |
|
486 |
|
487 retval = result; |
|
488 } |
|
489 |
|
490 return retval; |
|
491 } |
|
492 |
|
493 // -*- 6 -*- |
|
494 octave_value |
|
495 elem_xpow (const SparseMatrix& a, const SparseComplexMatrix& b) |
|
496 { |
5275
|
497 octave_idx_type nr = a.rows (); |
|
498 octave_idx_type nc = a.cols (); |
5164
|
499 |
5275
|
500 octave_idx_type b_nr = b.rows (); |
|
501 octave_idx_type b_nc = b.cols (); |
5164
|
502 |
|
503 if (nr != b_nr || nc != b_nc) |
|
504 { |
|
505 gripe_nonconformant ("operator .^", nr, nc, b_nr, b_nc); |
|
506 return octave_value (); |
|
507 } |
|
508 |
5953
|
509 SparseComplexMatrix result (nr, nc, Complex(1.0, 0.0)); |
5275
|
510 for (octave_idx_type j = 0; j < nc; j++) |
5164
|
511 { |
5953
|
512 for (octave_idx_type i = a.cidx(j); i < a.cidx(j+1); i++) |
5164
|
513 { |
|
514 OCTAVE_QUIT; |
5953
|
515 result.xelem(a.ridx(i), j) = std::pow (a.data(i), b (a.ridx(i), j)); |
5164
|
516 } |
|
517 } |
|
518 |
5953
|
519 result.maybe_compress (true); |
5164
|
520 |
|
521 return result; |
|
522 } |
|
523 |
|
524 // -*- 7 -*- |
|
525 octave_value |
|
526 elem_xpow (const Complex& a, const SparseMatrix& b) |
|
527 { |
5275
|
528 octave_idx_type nr = b.rows (); |
|
529 octave_idx_type nc = b.cols (); |
5164
|
530 |
|
531 ComplexMatrix result (nr, nc); |
|
532 |
5275
|
533 for (octave_idx_type j = 0; j < nc; j++) |
5164
|
534 { |
5275
|
535 for (octave_idx_type i = 0; i < nr; i++) |
5164
|
536 { |
|
537 OCTAVE_QUIT; |
|
538 double btmp = b (i, j); |
|
539 if (xisint (btmp)) |
5953
|
540 result (i, j) = std::pow (a, static_cast<int> (btmp)); |
5164
|
541 else |
5953
|
542 result (i, j) = std::pow (a, btmp); |
5164
|
543 } |
|
544 } |
|
545 |
|
546 return result; |
|
547 } |
|
548 |
|
549 // -*- 8 -*- |
|
550 octave_value |
|
551 elem_xpow (const Complex& a, const SparseComplexMatrix& b) |
|
552 { |
5275
|
553 octave_idx_type nr = b.rows (); |
|
554 octave_idx_type nc = b.cols (); |
5164
|
555 |
|
556 ComplexMatrix result (nr, nc); |
5275
|
557 for (octave_idx_type j = 0; j < nc; j++) |
|
558 for (octave_idx_type i = 0; i < nr; i++) |
5164
|
559 { |
|
560 OCTAVE_QUIT; |
5953
|
561 result (i, j) = std::pow (a, b (i, j)); |
5164
|
562 } |
|
563 |
|
564 return result; |
|
565 } |
|
566 |
|
567 // -*- 9 -*- |
|
568 octave_value |
|
569 elem_xpow (const SparseComplexMatrix& a, double b) |
|
570 { |
|
571 octave_value retval; |
|
572 |
|
573 if (b <= 0) |
|
574 { |
5275
|
575 octave_idx_type nr = a.rows (); |
|
576 octave_idx_type nc = a.cols (); |
5164
|
577 |
5953
|
578 ComplexMatrix result (nr, nc, Complex (std::pow (0.0, b))); |
5164
|
579 |
|
580 if (xisint (b)) |
|
581 { |
5275
|
582 for (octave_idx_type j = 0; j < nc; j++) |
|
583 for (octave_idx_type i = a.cidx(j); i < a.cidx(j+1); i++) |
5164
|
584 { |
|
585 OCTAVE_QUIT; |
|
586 result (a.ridx(i), j) = |
5953
|
587 std::pow (a.data (i), static_cast<int> (b)); |
5164
|
588 } |
|
589 } |
|
590 else |
|
591 { |
5275
|
592 for (octave_idx_type j = 0; j < nc; j++) |
|
593 for (octave_idx_type i = a.cidx(j); i < a.cidx(j+1); i++) |
5164
|
594 { |
|
595 OCTAVE_QUIT; |
5953
|
596 result (a.ridx(i), j) = std::pow (a.data (i), b); |
5164
|
597 } |
|
598 } |
|
599 |
|
600 retval = result; |
|
601 } |
|
602 else |
|
603 { |
5604
|
604 octave_idx_type nz = a.nzmax (); |
5164
|
605 |
|
606 SparseComplexMatrix result (a); |
|
607 |
|
608 if (xisint (b)) |
|
609 { |
5275
|
610 for (octave_idx_type i = 0; i < nz; i++) |
5164
|
611 { |
|
612 OCTAVE_QUIT; |
5953
|
613 result.data (i) = std::pow (a.data (i), static_cast<int> (b)); |
5164
|
614 } |
|
615 } |
|
616 else |
|
617 { |
5275
|
618 for (octave_idx_type i = 0; i < nz; i++) |
5164
|
619 { |
|
620 OCTAVE_QUIT; |
5953
|
621 result.data (i) = std::pow (a.data (i), b); |
5164
|
622 } |
|
623 } |
|
624 |
|
625 result.maybe_compress (true); |
|
626 |
|
627 retval = result; |
|
628 } |
|
629 |
|
630 return retval; |
|
631 } |
|
632 |
|
633 // -*- 10 -*- |
|
634 octave_value |
|
635 elem_xpow (const SparseComplexMatrix& a, const SparseMatrix& b) |
|
636 { |
5275
|
637 octave_idx_type nr = a.rows (); |
|
638 octave_idx_type nc = a.cols (); |
5164
|
639 |
5275
|
640 octave_idx_type b_nr = b.rows (); |
|
641 octave_idx_type b_nc = b.cols (); |
5164
|
642 |
|
643 if (nr != b_nr || nc != b_nc) |
|
644 { |
|
645 gripe_nonconformant ("operator .^", nr, nc, b_nr, b_nc); |
|
646 return octave_value (); |
|
647 } |
|
648 |
5953
|
649 SparseComplexMatrix result (nr, nc, Complex(1.0, 0.0)); |
5275
|
650 for (octave_idx_type j = 0; j < nc; j++) |
5164
|
651 { |
5953
|
652 for (octave_idx_type i = a.cidx(j); i < a.cidx(j+1); i++) |
5164
|
653 { |
|
654 OCTAVE_QUIT; |
5953
|
655 double btmp = b (a.ridx(i), j); |
5164
|
656 Complex tmp; |
|
657 |
|
658 if (xisint (btmp)) |
5953
|
659 result.xelem(a.ridx(i), j) = std::pow (a.data (i), |
|
660 static_cast<int> (btmp)); |
5164
|
661 else |
5953
|
662 result.xelem(a.ridx(i), j) = std::pow (a.data (i), btmp); |
5164
|
663 } |
|
664 } |
|
665 |
5953
|
666 result.maybe_compress (true); |
5164
|
667 |
|
668 return result; |
|
669 } |
|
670 |
|
671 // -*- 11 -*- |
|
672 octave_value |
|
673 elem_xpow (const SparseComplexMatrix& a, const Complex& b) |
|
674 { |
|
675 octave_value retval; |
|
676 |
|
677 if (b == 0.0) |
|
678 // Can this case ever happen, due to automatic retyping with maybe_mutate? |
|
679 retval = octave_value (NDArray (a.dims (), 1)); |
|
680 else |
|
681 { |
|
682 |
5604
|
683 octave_idx_type nz = a.nzmax (); |
5164
|
684 |
|
685 SparseComplexMatrix result (a); |
|
686 |
5275
|
687 for (octave_idx_type i = 0; i < nz; i++) |
5164
|
688 { |
|
689 OCTAVE_QUIT; |
5953
|
690 result.data (i) = std::pow (a.data (i), b); |
5164
|
691 } |
|
692 |
|
693 result.maybe_compress (true); |
|
694 |
|
695 retval = result; |
|
696 } |
|
697 |
|
698 return retval; |
|
699 } |
|
700 |
|
701 // -*- 12 -*- |
|
702 octave_value |
|
703 elem_xpow (const SparseComplexMatrix& a, const SparseComplexMatrix& b) |
|
704 { |
5275
|
705 octave_idx_type nr = a.rows (); |
|
706 octave_idx_type nc = a.cols (); |
5164
|
707 |
5275
|
708 octave_idx_type b_nr = b.rows (); |
|
709 octave_idx_type b_nc = b.cols (); |
5164
|
710 |
|
711 if (nr != b_nr || nc != b_nc) |
|
712 { |
|
713 gripe_nonconformant ("operator .^", nr, nc, b_nr, b_nc); |
|
714 return octave_value (); |
|
715 } |
|
716 |
5953
|
717 SparseComplexMatrix result (nr, nc, Complex(1.0, 0.0)); |
5275
|
718 for (octave_idx_type j = 0; j < nc; j++) |
5164
|
719 { |
5953
|
720 for (octave_idx_type i = a.cidx(j); i < a.cidx(j+1); i++) |
5164
|
721 { |
|
722 OCTAVE_QUIT; |
5953
|
723 result.xelem(a.ridx(i), j) = std::pow (a.data (i), b (a.ridx(i), j)); |
5164
|
724 } |
|
725 } |
|
726 result.maybe_compress (true); |
|
727 |
|
728 return result; |
|
729 } |
|
730 |
|
731 /* |
|
732 ;;; Local Variables: *** |
|
733 ;;; mode: C++ *** |
|
734 ;;; End: *** |
|
735 */ |