comparison src/sparse-xpow.cc @ 5953:164214586706

[project @ 2006-08-22 15:31:32 by jwe]
author jwe
date Tue, 22 Aug 2006 15:31:32 +0000
parents 6b9cec830d72
children 93c65f2a5668
comparison
equal deleted inserted replaced
5952:7dc99bfdd87a 5953:164214586706
247 for (octave_idx_type j = 0; j < nc; j++) 247 for (octave_idx_type j = 0; j < nc; j++)
248 { 248 {
249 for (octave_idx_type i = 0; i < nr; i++) 249 for (octave_idx_type i = 0; i < nr; i++)
250 { 250 {
251 OCTAVE_QUIT; 251 OCTAVE_QUIT;
252 result (i, j) = pow (atmp, b(i,j)); 252 result (i, j) = std::pow (atmp, b(i,j));
253 } 253 }
254 } 254 }
255 255
256 retval = result; 256 retval = result;
257 } 257 }
262 for (octave_idx_type j = 0; j < nc; j++) 262 for (octave_idx_type j = 0; j < nc; j++)
263 { 263 {
264 for (octave_idx_type i = 0; i < nr; i++) 264 for (octave_idx_type i = 0; i < nr; i++)
265 { 265 {
266 OCTAVE_QUIT; 266 OCTAVE_QUIT;
267 result (i, j) = pow (a, b(i,j)); 267 result (i, j) = std::pow (a, b(i,j));
268 } 268 }
269 } 269 }
270 270
271 retval = result; 271 retval = result;
272 } 272 }
287 for (octave_idx_type j = 0; j < nc; j++) 287 for (octave_idx_type j = 0; j < nc; j++)
288 { 288 {
289 for (octave_idx_type i = 0; i < nr; i++) 289 for (octave_idx_type i = 0; i < nr; i++)
290 { 290 {
291 OCTAVE_QUIT; 291 OCTAVE_QUIT;
292 result (i, j) = pow (atmp, b(i,j)); 292 result (i, j) = std::pow (atmp, b(i,j));
293 } 293 }
294 } 294 }
295 295
296 return result; 296 return result;
297 } 297 }
313 octave_idx_type nr = a.rows (); 313 octave_idx_type nr = a.rows ();
314 octave_idx_type nc = a.cols (); 314 octave_idx_type nc = a.cols ();
315 315
316 if (static_cast<int> (b) != b && a.any_element_is_negative ()) 316 if (static_cast<int> (b) != b && a.any_element_is_negative ())
317 { 317 {
318 ComplexMatrix result (nr, nc, Complex (pow (0.0, b))); 318 ComplexMatrix result (nr, nc, Complex (std::pow (0.0, b)));
319 319
320 // FIXME -- avoid apparent GNU libm bug by 320 // FIXME -- avoid apparent GNU libm bug by
321 // converting A and B to complex instead of just A. 321 // converting A and B to complex instead of just A.
322 Complex btmp (b); 322 Complex btmp (b);
323 323
326 { 326 {
327 OCTAVE_QUIT; 327 OCTAVE_QUIT;
328 328
329 Complex atmp (a.data (i)); 329 Complex atmp (a.data (i));
330 330
331 result (a.ridx(i), j) = pow (atmp, btmp); 331 result (a.ridx(i), j) = std::pow (atmp, btmp);
332 } 332 }
333 333
334 retval = octave_value (result); 334 retval = octave_value (result);
335 } 335 }
336 else 336 else
337 { 337 {
338 Matrix result (nr, nc, (pow (0.0, b))); 338 Matrix result (nr, nc, (std::pow (0.0, b)));
339 339
340 for (octave_idx_type j = 0; j < nc; j++) 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++) 341 for (octave_idx_type i = a.cidx(j); i < a.cidx(j+1); i++)
342 { 342 {
343 OCTAVE_QUIT; 343 OCTAVE_QUIT;
344 result (a.ridx(i), j) = pow (a.data (i), b); 344 result (a.ridx(i), j) = std::pow (a.data (i), b);
345 } 345 }
346 346
347 retval = octave_value (result); 347 retval = octave_value (result);
348 } 348 }
349 } 349 }
359 // converting A and B to complex instead of just A. 359 // converting A and B to complex instead of just A.
360 360
361 Complex atmp (a.data (i)); 361 Complex atmp (a.data (i));
362 Complex btmp (b); 362 Complex btmp (b);
363 363
364 result.data (i) = pow (atmp, btmp); 364 result.data (i) = std::pow (atmp, btmp);
365 } 365 }
366 366
367 result.maybe_compress (true); 367 result.maybe_compress (true);
368 368
369 retval = result; 369 retval = result;
373 SparseMatrix result (a); 373 SparseMatrix result (a);
374 374
375 for (octave_idx_type i = 0; i < nz; i++) 375 for (octave_idx_type i = 0; i < nz; i++)
376 { 376 {
377 OCTAVE_QUIT; 377 OCTAVE_QUIT;
378 result.data (i) = pow (a.data (i), b); 378 result.data (i) = std::pow (a.data (i), b);
379 } 379 }
380 380
381 result.maybe_compress (true); 381 result.maybe_compress (true);
382 382
383 retval = result; 383 retval = result;
404 return octave_value (); 404 return octave_value ();
405 } 405 }
406 406
407 int convert_to_complex = 0; 407 int convert_to_complex = 0;
408 for (octave_idx_type j = 0; j < nc; j++) 408 for (octave_idx_type j = 0; j < nc; j++)
409 for (octave_idx_type i = 0; i < nr; i++) 409 for (octave_idx_type i = a.cidx(j); i < a.cidx(j+1); i++)
410 { 410 {
411 OCTAVE_QUIT; 411 if (a.data(i) < 0.0)
412 double atmp = a (i, j);
413 double btmp = b (i, j);
414 if (atmp < 0.0 && static_cast<int> (btmp) != btmp)
415 { 412 {
416 convert_to_complex = 1; 413 double btmp = b (a.ridx(i), j);
417 goto done; 414 if (static_cast<int> (btmp) != btmp)
415 {
416 convert_to_complex = 1;
417 goto done;
418 }
418 } 419 }
419 } 420 }
420 421
421 done: 422 done:
422 423
423 octave_idx_type nel = 0; 424 // This is a dumb operator for sparse matrices anyway, and there is
424 for (octave_idx_type j = 0; j < nc; j++) 425 // no sensible way to handle the 0.^0 versus the 0.^x cases. Therefore
425 for (octave_idx_type i = 0; i < nr; i++) 426 // allocate a full matrix filled for the 0.^0 case and shrink it later
426 if (!(a.elem (i, j) == 0. && b.elem (i, j) != 0.)) 427 // as needed
427 nel++;
428 428
429 if (convert_to_complex) 429 if (convert_to_complex)
430 { 430 {
431 SparseComplexMatrix complex_result (nr, nc, nel); 431 SparseComplexMatrix complex_result (nr, nc, Complex(1.0, 0.0));
432 432
433 octave_idx_type ii = 0;
434 complex_result.cidx(0) = 0;
435 for (octave_idx_type j = 0; j < nc; j++) 433 for (octave_idx_type j = 0; j < nc; j++)
436 { 434 {
437 for (octave_idx_type i = 0; i < nr; i++) 435 for (octave_idx_type i = a.cidx(j); i < a.cidx(j+1); i++)
438 { 436 {
439 OCTAVE_QUIT; 437 OCTAVE_QUIT;
440 Complex atmp (a (i, j)); 438 complex_result.xelem(a.ridx(i), j) =
441 Complex btmp (b (i, j)); 439 std::pow (Complex(a.data(i)), Complex(b(a.ridx(i), j)));
442 Complex tmp = pow (atmp, btmp); 440 }
443 if (tmp != 0.) 441 }
444 { 442 complex_result.maybe_compress (true);
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; 443 retval = complex_result;
454 } 444 }
455 else 445 else
456 { 446 {
457 SparseMatrix result (nr, nc, nel); 447 SparseMatrix result (nr, nc, 1.0);
458 octave_idx_type ii = 0; 448
459
460 result.cidx (0) = 0;
461 for (octave_idx_type j = 0; j < nc; j++) 449 for (octave_idx_type j = 0; j < nc; j++)
462 { 450 {
463 for (octave_idx_type i = 0; i < nr; i++) 451 for (octave_idx_type i = a.cidx(j); i < a.cidx(j+1); i++)
464 { 452 {
465 OCTAVE_QUIT; 453 OCTAVE_QUIT;
466 double tmp = pow (a (i, j), b (i, j)); 454 result.xelem(a.ridx(i), j) = std::pow (a.data(i),
467 if (tmp != 0.) 455 b (a.ridx(i), j));
468 { 456 }
469 result.data (ii) = tmp; 457 }
470 result.ridx (ii++) = i; 458 result.maybe_compress (true);
471 }
472 }
473 result.cidx (j+1) = ii;
474 }
475
476 result.maybe_compress ();
477
478 retval = result; 459 retval = result;
479 } 460 }
480 461
481 return retval; 462 return retval;
482 } 463 }
496 SparseComplexMatrix result (a); 477 SparseComplexMatrix result (a);
497 478
498 for (octave_idx_type i = 0; i < nz; i++) 479 for (octave_idx_type i = 0; i < nz; i++)
499 { 480 {
500 OCTAVE_QUIT; 481 OCTAVE_QUIT;
501 result.data (i) = pow (Complex (a.data (i)), b); 482 result.data (i) = std::pow (Complex (a.data (i)), b);
502 } 483 }
503 484
504 result.maybe_compress (true); 485 result.maybe_compress (true);
505 486
506 retval = result; 487 retval = result;
523 { 504 {
524 gripe_nonconformant ("operator .^", nr, nc, b_nr, b_nc); 505 gripe_nonconformant ("operator .^", nr, nc, b_nr, b_nc);
525 return octave_value (); 506 return octave_value ();
526 } 507 }
527 508
528 octave_idx_type nel = 0; 509 SparseComplexMatrix result (nr, nc, Complex(1.0, 0.0));
529 for (octave_idx_type j = 0; j < nc; j++)
530 for (octave_idx_type i = 0; i < nr; i++)
531 if (!(a.elem (i, j) == 0. && b.elem (i, j) != 0.))
532 nel++;
533
534 SparseComplexMatrix result (nr, nc, nel);
535 octave_idx_type ii = 0;
536
537 result.cidx(0) = 0;
538 for (octave_idx_type j = 0; j < nc; j++) 510 for (octave_idx_type j = 0; j < nc; j++)
539 { 511 {
540 for (octave_idx_type i = 0; i < nr; i++) 512 for (octave_idx_type i = a.cidx(j); i < a.cidx(j+1); i++)
541 { 513 {
542 OCTAVE_QUIT; 514 OCTAVE_QUIT;
543 Complex tmp = pow (Complex (a (i, j)), b (i, j)); 515 result.xelem(a.ridx(i), j) = std::pow (a.data(i), b (a.ridx(i), j));
544 if (tmp != 0.) 516 }
545 { 517 }
546 result.data (ii) = tmp; 518
547 result.ridx (ii++) = i; 519 result.maybe_compress (true);
548 }
549 }
550 result.cidx (j+1) = ii;
551 }
552
553 result.maybe_compress ();
554 520
555 return result; 521 return result;
556 } 522 }
557 523
558 // -*- 7 -*- 524 // -*- 7 -*-
569 for (octave_idx_type i = 0; i < nr; i++) 535 for (octave_idx_type i = 0; i < nr; i++)
570 { 536 {
571 OCTAVE_QUIT; 537 OCTAVE_QUIT;
572 double btmp = b (i, j); 538 double btmp = b (i, j);
573 if (xisint (btmp)) 539 if (xisint (btmp))
574 result (i, j) = pow (a, static_cast<int> (btmp)); 540 result (i, j) = std::pow (a, static_cast<int> (btmp));
575 else 541 else
576 result (i, j) = pow (a, btmp); 542 result (i, j) = std::pow (a, btmp);
577 } 543 }
578 } 544 }
579 545
580 return result; 546 return result;
581 } 547 }
590 ComplexMatrix result (nr, nc); 556 ComplexMatrix result (nr, nc);
591 for (octave_idx_type j = 0; j < nc; j++) 557 for (octave_idx_type j = 0; j < nc; j++)
592 for (octave_idx_type i = 0; i < nr; i++) 558 for (octave_idx_type i = 0; i < nr; i++)
593 { 559 {
594 OCTAVE_QUIT; 560 OCTAVE_QUIT;
595 result (i, j) = pow (a, b (i, j)); 561 result (i, j) = std::pow (a, b (i, j));
596 } 562 }
597 563
598 return result; 564 return result;
599 } 565 }
600 566
607 if (b <= 0) 573 if (b <= 0)
608 { 574 {
609 octave_idx_type nr = a.rows (); 575 octave_idx_type nr = a.rows ();
610 octave_idx_type nc = a.cols (); 576 octave_idx_type nc = a.cols ();
611 577
612 ComplexMatrix result (nr, nc, Complex (pow (0.0, b))); 578 ComplexMatrix result (nr, nc, Complex (std::pow (0.0, b)));
613 579
614 if (xisint (b)) 580 if (xisint (b))
615 { 581 {
616 for (octave_idx_type j = 0; j < nc; j++) 582 for (octave_idx_type j = 0; j < nc; j++)
617 for (octave_idx_type i = a.cidx(j); i < a.cidx(j+1); i++) 583 for (octave_idx_type i = a.cidx(j); i < a.cidx(j+1); i++)
618 { 584 {
619 OCTAVE_QUIT; 585 OCTAVE_QUIT;
620 result (a.ridx(i), j) = 586 result (a.ridx(i), j) =
621 pow (a.data (i), static_cast<int> (b)); 587 std::pow (a.data (i), static_cast<int> (b));
622 } 588 }
623 } 589 }
624 else 590 else
625 { 591 {
626 for (octave_idx_type j = 0; j < nc; j++) 592 for (octave_idx_type j = 0; j < nc; j++)
627 for (octave_idx_type i = a.cidx(j); i < a.cidx(j+1); i++) 593 for (octave_idx_type i = a.cidx(j); i < a.cidx(j+1); i++)
628 { 594 {
629 OCTAVE_QUIT; 595 OCTAVE_QUIT;
630 result (a.ridx(i), j) = pow (a.data (i), b); 596 result (a.ridx(i), j) = std::pow (a.data (i), b);
631 } 597 }
632 } 598 }
633 599
634 retval = result; 600 retval = result;
635 } 601 }
642 if (xisint (b)) 608 if (xisint (b))
643 { 609 {
644 for (octave_idx_type i = 0; i < nz; i++) 610 for (octave_idx_type i = 0; i < nz; i++)
645 { 611 {
646 OCTAVE_QUIT; 612 OCTAVE_QUIT;
647 result.data (i) = pow (a.data (i), static_cast<int> (b)); 613 result.data (i) = std::pow (a.data (i), static_cast<int> (b));
648 } 614 }
649 } 615 }
650 else 616 else
651 { 617 {
652 for (octave_idx_type i = 0; i < nz; i++) 618 for (octave_idx_type i = 0; i < nz; i++)
653 { 619 {
654 OCTAVE_QUIT; 620 OCTAVE_QUIT;
655 result.data (i) = pow (a.data (i), b); 621 result.data (i) = std::pow (a.data (i), b);
656 } 622 }
657 } 623 }
658 624
659 result.maybe_compress (true); 625 result.maybe_compress (true);
660 626
678 { 644 {
679 gripe_nonconformant ("operator .^", nr, nc, b_nr, b_nc); 645 gripe_nonconformant ("operator .^", nr, nc, b_nr, b_nc);
680 return octave_value (); 646 return octave_value ();
681 } 647 }
682 648
683 octave_idx_type nel = 0; 649 SparseComplexMatrix result (nr, nc, Complex(1.0, 0.0));
684 for (octave_idx_type j = 0; j < nc; j++)
685 for (octave_idx_type i = 0; i < nr; i++)
686 if (!(a.elem (i, j) == 0. && b.elem (i, j) != 0.))
687 nel++;
688
689 SparseComplexMatrix result (nr, nc, nel);
690 octave_idx_type ii = 0;
691
692 result.cidx (0) = 0;
693 for (octave_idx_type j = 0; j < nc; j++) 650 for (octave_idx_type j = 0; j < nc; j++)
694 { 651 {
695 for (octave_idx_type i = 0; i < nr; i++) 652 for (octave_idx_type i = a.cidx(j); i < a.cidx(j+1); i++)
696 { 653 {
697 OCTAVE_QUIT; 654 OCTAVE_QUIT;
698 double btmp = b (i, j); 655 double btmp = b (a.ridx(i), j);
699 Complex tmp; 656 Complex tmp;
700 657
701 if (xisint (btmp)) 658 if (xisint (btmp))
702 tmp = pow (a (i, j), static_cast<int> (btmp)); 659 result.xelem(a.ridx(i), j) = std::pow (a.data (i),
660 static_cast<int> (btmp));
703 else 661 else
704 tmp = pow (a (i, j), btmp); 662 result.xelem(a.ridx(i), j) = std::pow (a.data (i), btmp);
705 if (tmp != 0.) 663 }
706 { 664 }
707 result.data (ii) = tmp; 665
708 result.ridx (ii++) = i; 666 result.maybe_compress (true);
709 }
710 }
711 result.cidx (j+1) = ii;
712 }
713
714 result.maybe_compress ();
715 667
716 return result; 668 return result;
717 } 669 }
718 670
719 // -*- 11 -*- 671 // -*- 11 -*-
733 SparseComplexMatrix result (a); 685 SparseComplexMatrix result (a);
734 686
735 for (octave_idx_type i = 0; i < nz; i++) 687 for (octave_idx_type i = 0; i < nz; i++)
736 { 688 {
737 OCTAVE_QUIT; 689 OCTAVE_QUIT;
738 result.data (i) = pow (a.data (i), b); 690 result.data (i) = std::pow (a.data (i), b);
739 } 691 }
740 692
741 result.maybe_compress (true); 693 result.maybe_compress (true);
742 694
743 retval = result; 695 retval = result;
760 { 712 {
761 gripe_nonconformant ("operator .^", nr, nc, b_nr, b_nc); 713 gripe_nonconformant ("operator .^", nr, nc, b_nr, b_nc);
762 return octave_value (); 714 return octave_value ();
763 } 715 }
764 716
765 octave_idx_type nel = 0; 717 SparseComplexMatrix result (nr, nc, Complex(1.0, 0.0));
766 for (octave_idx_type j = 0; j < nc; j++) 718 for (octave_idx_type j = 0; j < nc; j++)
767 for (octave_idx_type i = 0; i < nr; i++) 719 {
768 if (!(a.elem (i, j) == 0. && b.elem (i, j) != 0.)) 720 for (octave_idx_type i = a.cidx(j); i < a.cidx(j+1); i++)
769 nel++; 721 {
770 722 OCTAVE_QUIT;
771 SparseComplexMatrix result (nr, nc, nel); 723 result.xelem(a.ridx(i), j) = std::pow (a.data (i), b (a.ridx(i), j));
772 octave_idx_type ii = 0; 724 }
773
774 result.cidx (0) = 0;
775 for (octave_idx_type j = 0; j < nc; j++)
776 {
777 for (octave_idx_type i = 0; i < nr; i++)
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 } 725 }
789 result.maybe_compress (true); 726 result.maybe_compress (true);
790 727
791 return result; 728 return result;
792 } 729 }