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