Changeset 2021 for trunk/psLib/src/dataManip/psMatrixVectorArithmetic.c
- Timestamp:
- Oct 7, 2004, 4:48:01 PM (22 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/dataManip/psMatrixVectorArithmetic.c
r1648 r2021 1 2 1 /** @file psMatrixVectorArithmetic.c 3 2 * … … 30 29 * @author Ross Harman, MHPCC 31 30 * 32 * @version $Revision: 1.2 7$ $Name: not supported by cvs2svn $33 * @date $Date: 2004- 08-27 23:33:31 $31 * @version $Revision: 1.28 $ $Name: not supported by cvs2svn $ 32 * @date $Date: 2004-10-08 02:48:01 $ 34 33 * 35 34 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii 36 35 */ 37 36 38 /******************************************************************************/ 39 40 /* INCLUDE FILES */ 41 42 /******************************************************************************/ 37 /****************************************************************************** 38 * INCLUDE FILES * 39 ******************************************************************************/ 43 40 #include <string.h> 44 41 #include <math.h> … … 50 47 #include "psVector.h" 51 48 #include "psScalar.h" 52 53 /******************************************************************************/ 54 55 /* DEFINE STATEMENTS */ 56 57 /******************************************************************************/ 58 59 // None 60 61 /******************************************************************************/ 62 63 /* TYPE DEFINITIONS */ 64 65 /******************************************************************************/ 66 67 // None 68 69 /*****************************************************************************/ 70 71 /* GLOBAL VARIABLES */ 72 73 /*****************************************************************************/ 74 75 // None 76 77 /*****************************************************************************/ 78 79 /* FILE STATIC VARIABLES */ 80 81 /*****************************************************************************/ 82 83 // None 84 85 /*****************************************************************************/ 86 87 /* FUNCTION IMPLEMENTATION - LOCAL */ 88 89 /*****************************************************************************/ 49 #include "psLogMsg.h" 50 51 /***************************************************************************** 52 * FUNCTION IMPLEMENTATION - LOCAL * 53 *****************************************************************************/ 90 54 91 55 // Conversion for degrees to radians … … 95 59 #define R2D 57.29577950924861 /* 180.0/PI */ 96 60 61 /* IEEE Standards on floating-point makes this function pointless, really. 97 62 // Division with NAN checking 98 63 static complex double psNanDiv(complex double a, complex double b) 99 64 { 100 65 complex double out = 0 + 0i; 101 66 102 67 out = a / b; 103 68 if (isnan(creal(out)) || isnan(cimag(out))) { 104 69 psError(__func__, ": Divide by zero"); 105 70 } 106 71 107 72 return out; 108 73 } 74 */ 109 75 110 76 // Binary SCALAR_XXXX operations … … 186 152 if(n1 != n2) { \ 187 153 psError(__func__, ": Inconsistent element count: %d vs %d", n1, n2); \ 188 return OUT; \ 154 if (OUT != IN1 && OUT != IN2) { \ 155 psFree(OUT); \ 156 } \ 157 return NULL; \ 189 158 } \ 190 159 o = ((psVector* )OUT)->data.TYPE; \ … … 215 184 if(n1!=numRows2) { \ 216 185 psError(__func__, ": Inconsistent element count: %d vs %d", n1, numRows2); \ 217 return OUT; \ 186 if (OUT != IN1 && OUT != IN2) { \ 187 psFree(OUT); \ 188 } \ 189 return NULL; \ 218 190 } \ 219 191 \ … … 229 201 if(n1!=numCols2) { \ 230 202 psError(__func__, ": Inconsistent element count: %d vs %d", n1, numCols2); \ 231 return OUT; \ 203 if (OUT != IN1 && OUT != IN2) { \ 204 psFree(OUT); \ 205 } \ 206 return NULL; \ 232 207 } \ 233 208 \ … … 284 259 if(n2!=numRows1) { \ 285 260 psError(__func__, ": Inconsistent element count: %d vs %d", n2, numRows1); \ 286 return OUT; \ 261 if (OUT != IN1 && OUT != IN2) { \ 262 psFree(OUT); \ 263 } \ 264 return NULL; \ 287 265 } \ 288 266 \ … … 298 276 if(n2!=numCols1) { \ 299 277 psError(__func__, ": Inconsistent element count: %d vs %d", n2, numCols1); \ 300 return OUT; \ 278 if (OUT != IN1) { \ 279 psFree(OUT); \ 280 } \ 281 return NULL; \ 301 282 } \ 302 283 \ … … 330 311 psError(__func__, ": Inconsistent element count: numRows: %d vs %d numCols: %d vs %d", numRows1, \ 331 312 numRows2, numCols1, numCols2); \ 332 return OUT; \ 313 if (OUT != IN1 && OUT != IN2) { \ 314 psFree(OUT); \ 315 } \ 316 return NULL; \ 333 317 } \ 334 318 for(j = 0; j < numRows1; j++) { \ … … 345 329 #define BINARY_TYPE(DIM1,DIM2,OUT,IN1,OP,IN2) \ 346 330 switch (IN1->type) { \ 331 case PS_TYPE_U8: \ 332 DIM1##_##DIM2(OUT,IN1,OP,IN2,U8); \ 333 break; \ 334 case PS_TYPE_U16: \ 335 DIM1##_##DIM2(OUT,IN1,OP,IN2,U16); \ 336 break; \ 337 case PS_TYPE_U32: \ 338 DIM1##_##DIM2(OUT,IN1,OP,IN2,U32); \ 339 break; \ 340 case PS_TYPE_U64: \ 341 DIM1##_##DIM2(OUT,IN1,OP,IN2,U64); \ 342 break; \ 343 case PS_TYPE_S8: \ 344 DIM1##_##DIM2(OUT,IN1,OP,IN2,S8); \ 345 break; \ 346 case PS_TYPE_S16: \ 347 DIM1##_##DIM2(OUT,IN1,OP,IN2,S16); \ 348 break; \ 347 349 case PS_TYPE_S32: \ 348 350 DIM1##_##DIM2(OUT,IN1,OP,IN2,S32); \ 349 351 break; \ 352 case PS_TYPE_S64: \ 353 DIM1##_##DIM2(OUT,IN1,OP,IN2,S64); \ 354 break; \ 350 355 case PS_TYPE_F32: \ 351 356 DIM1##_##DIM2(OUT,IN1,OP,IN2,F32); \ … … 357 362 DIM1##_##DIM2(OUT,IN1,OP,IN2,C32); \ 358 363 break; \ 364 case PS_TYPE_C64: \ 365 DIM1##_##DIM2(OUT,IN1,OP,IN2,C64); \ 366 break; \ 359 367 default: \ 360 case PS_TYPE_S8: \361 case PS_TYPE_U8: \362 case PS_TYPE_S16: \363 case PS_TYPE_U16: \364 case PS_TYPE_U32: \365 case PS_TYPE_S64: \366 case PS_TYPE_U64: \367 case PS_TYPE_C64: \368 368 psError(__func__, ": Invalid PS_TYPE: %d", IN1->type); \ 369 if (OUT != IN1 && OUT != IN2) { \ 370 psFree(OUT); \ 371 } \ 372 return NULL; \ 369 373 } 370 374 … … 378 382 BINARY_TYPE(DIM1,DIM2,OUT,IN1,*i1 * *i2,IN2); \ 379 383 } else if(!strncmp(OP, "/", 1)) { \ 380 BINARY_TYPE(DIM1,DIM2,OUT,IN1, psNanDiv(*i1,*i2),IN2); \384 BINARY_TYPE(DIM1,DIM2,OUT,IN1,*i1 / *i2,IN2); \ 381 385 } else if(!strncmp(OP, "^", 1)) { \ 382 386 if(PS_IS_PSELEMTYPE_COMPLEX(IN1->type)) { \ … … 388 392 if(PS_IS_PSELEMTYPE_COMPLEX(IN1->type)) { \ 389 393 psError(__func__, ": Minimum operation not supported for complex numbers"); \ 394 if (OUT != IN1 && OUT != IN2) { \ 395 psFree(OUT); \ 396 } \ 397 return NULL; \ 390 398 } else { \ 391 399 BINARY_TYPE(DIM1,DIM2,OUT,IN1,fmin(*i1,*i2),IN2); \ … … 394 402 if(PS_IS_PSELEMTYPE_COMPLEX(IN1->type)) { \ 395 403 psError(__func__, ": Maximum operation not supported for complex numbers"); \ 404 if (OUT != IN1 && OUT != IN2) { \ 405 psFree(OUT); \ 406 } \ 407 return NULL; \ 396 408 } else { \ 397 409 BINARY_TYPE(DIM1,DIM2,OUT,IN1,fmax(*i1,*i2),IN2); \ … … 399 411 } else { \ 400 412 psError(__func__, ": Invalid operation: %s", OP); \ 413 if (OUT != IN1 && OUT != IN2) { \ 414 psFree(OUT); \ 415 } \ 416 return NULL; \ 401 417 } 402 418 … … 405 421 psDimen dim1 = 0; 406 422 psDimen dim2 = 0; 407 psDimen dimOut = 0;408 423 psElemType elType1 = 0; 409 424 psElemType elType2 = 0; 410 psElemType elTypeOut = 0;411 425 psType* psType1 = NULL; 412 426 psType* psType2 = NULL; 413 psType* psTypeOut = NULL;414 415 psTypeOut = (psType* ) out;416 if (psTypeOut == NULL) {417 psError(__func__, ": Line %d - Null out argument", __LINE__);418 return out;419 }420 427 421 428 psType1 = (psType* ) in1; 422 429 if (psType1 == NULL) { 423 430 psError(__func__, ": Line %d - Null in1 argument", __LINE__); 424 return out; 431 if (out != in1 && out != in2) { 432 psFree(out); 433 } 434 return NULL; 425 435 } 426 436 … … 428 438 if (psType2 == NULL) { 429 439 psError(__func__, ": Line %d - Null in2 argument", __LINE__); 430 return out; 440 if (out != in1 && out != in2) { 441 psFree(out); 442 } 443 return NULL; 431 444 } 432 445 433 446 if (op == NULL) { 434 447 psError(__func__, ": Line %d - Null op argument", __LINE__); 435 return out; 448 if (out != in1 && out != in2) { 449 psFree(out); 450 } 451 return NULL; 436 452 } 437 453 438 454 dim1 = psType1->dimen; 439 455 dim2 = psType2->dimen; 440 dimOut = psTypeOut->dimen;441 456 elType1 = psType1->type; 442 457 elType2 = psType2->type; 443 elTypeOut = psTypeOut->type; 444 445 if (elType1 != elType2 && elType1 != elTypeOut) { 446 psError(__func__, ": Line %d - Element types for arguments inconsistent: (%d, %d, %d)", __LINE__, 447 elType1, elType2, elTypeOut); 448 return out; 449 } 450 451 if (dim1 == PS_DIMEN_OTHER || dim2 == PS_DIMEN_OTHER || dimOut == PS_DIMEN_OTHER) { 452 psError(__func__, ": Line %d - PS_DIMEN_OTHER not allowed for arguments: (%d, %d, %d)", __LINE__, 453 dim1, dim2, dimOut); 454 return out; 455 } 456 457 if (dim1 == PS_DIMEN_VECTOR) { 458 459 if (elType1 != elType2) { 460 psError(__func__, ": Line %d - Element types for arguments inconsistent: (%d, %d)", __LINE__, 461 elType1, elType2); 462 if (out != in1 && out != in2) { 463 psFree(out); 464 } 465 return NULL; 466 } 467 468 if (dim1 == PS_DIMEN_OTHER || dim2 == PS_DIMEN_OTHER) { 469 psError(__func__, ": Line %d - PS_DIMEN_OTHER not allowed for arguments: (%d, %d)", __LINE__, 470 dim1, dim2); 471 if (out != in1 && out != in2) { 472 psFree(out); 473 } 474 return NULL; 475 } 476 477 if (dim1 == PS_DIMEN_VECTOR || dim1 == PS_DIMEN_TRANSV) { 458 478 if (((psVector* ) in1)->n == 0) { 459 ps Error(__func__, ": Line %d - Vector contains zero elements");479 psLogMsg(__func__, PS_LOG_WARN, ": Line %d - Vector contains zero elements"); 460 480 } 461 481 } else if (dim1 == PS_DIMEN_IMAGE) { 462 482 if (((psImage* ) in1)->numCols == 0 || ((psImage* ) in1)->numRows == 0) { 463 ps Error(__func__, ": Line %d - Image contains zero length row or cols");464 } 465 } 466 467 if (dim2 == PS_DIMEN_VECTOR ) {483 psLogMsg(__func__, PS_LOG_WARN, ": Line %d - Image contains zero length row or cols"); 484 } 485 } 486 487 if (dim2 == PS_DIMEN_VECTOR || dim2 == PS_DIMEN_TRANSV) { 468 488 if (((psVector* ) in2)->n == 0) { 469 ps Error(__func__, ": Line %d - Vector contains zero elements");489 psLogMsg(__func__, PS_LOG_WARN, ": Line %d - Vector contains zero elements"); 470 490 } 471 491 } else if (dim2 == PS_DIMEN_IMAGE) { 472 492 if (((psImage* ) in2)->numCols == 0 || ((psImage* ) in2)->numRows == 0) { 473 ps Error(__func__, ": Line %d - Image contains zero length row or cols");493 psLogMsg(__func__, PS_LOG_WARN, ": Line %d - Image contains zero length row or cols"); 474 494 } 475 495 } 476 496 477 497 if (dim1 == PS_DIMEN_SCALAR) { 498 if ( out != NULL && ((psType*)out)->dimen != dim2) { 499 if (out != in1 && out != in2) { 500 psFree(out); 501 } 502 out = NULL; 503 } 478 504 if (dim2 == PS_DIMEN_SCALAR) { 505 if (out == NULL || ((psScalar*)out)->type.type != elType1) { 506 if (out != in1 && out != in2) { 507 psFree(out); 508 } 509 out = psScalarAlloc(0.0,elType1); 510 } 479 511 BINARY_OP(SCALAR, SCALAR, out, psType1, op, psType2); // scalar op scalar 480 512 } else if (dim2 == PS_DIMEN_VECTOR || dim2 == PS_DIMEN_TRANSV) { 513 out = psVectorRecycle(out,((psVector*)in2)->n,elType1); 514 if (out == NULL) { 515 psError(__func__, "Couldn't create a proper output psVector."); 516 return NULL; 517 } 481 518 BINARY_OP(SCALAR, VECTOR, out, psType1, op, psType2); // scalar op vector 482 519 } else if (dim2 == PS_DIMEN_IMAGE) { 520 out = psImageRecycle(out, ((psImage* ) in2)->numCols, ((psImage* ) in2)->numRows,elType1); 521 if (out == NULL) { 522 psError(__func__, "Couldn't create a proper output psImage."); 523 return NULL; 524 } 483 525 BINARY_OP(SCALAR, IMAGE, out, psType1, op, psType2); // scalar op image 484 526 } else { … … 487 529 } else if (dim1 == PS_DIMEN_VECTOR || dim1 == PS_DIMEN_TRANSV) { 488 530 if (dim2 == PS_DIMEN_SCALAR) { 531 out = psVectorRecycle(out,((psVector*)in1)->n,elType1); 532 if (out == NULL) { 533 psError(__func__, "Couldn't create a proper output psVector."); 534 return NULL; 535 } 489 536 BINARY_OP(VECTOR, SCALAR, out, psType1, op, psType2); // vector op scalar 490 537 } else if (dim2 == PS_DIMEN_VECTOR || dim2 == PS_DIMEN_TRANSV) { 538 out = psVectorRecycle(out,((psVector*)in2)->n,elType2); 539 if (out == NULL) { 540 psError(__func__, "Couldn't create a proper output psVector."); 541 return NULL; 542 } 491 543 BINARY_OP(VECTOR, VECTOR, out, psType1, op, psType2); // vector op vector 492 544 } else if (dim2 == PS_DIMEN_IMAGE) { 545 out = psImageRecycle(out, ((psImage* ) in2)->numCols, ((psImage* ) in2)->numRows, elType2); 546 if (out == NULL) { 547 psError(__func__, "Couldn't create a proper output psImage."); 548 return NULL; 549 } 493 550 BINARY_OP(VECTOR, IMAGE, out, psType1, op, psType2); // vector op image 494 551 } else { … … 496 553 } 497 554 } else if (dim1 == PS_DIMEN_IMAGE) { 555 out = psImageRecycle(out, ((psImage*)in1)->numCols, ((psImage*)in1)->numRows, elType1); 556 if (out == NULL) { 557 psError(__func__, "Couldn't create a proper output psImage."); 558 return NULL; 559 } 498 560 if (dim2 == PS_DIMEN_SCALAR) { 499 561 BINARY_OP(IMAGE, SCALAR, out, psType1, op, psType2); // image op scalar … … 503 565 BINARY_OP(IMAGE, IMAGE, out, psType1, op, psType2); // image op image 504 566 } else { 567 if (out != in1 && out != in2) { 568 psFree(out); 569 } 505 570 psError(__func__, ": Line %d - Invalid dimensionality for in2 arg: %d", __LINE__, dim2); 571 return NULL; 506 572 } 507 573 } else { 574 if (out != in1 && out != in2) { 575 psFree(out); 576 } 508 577 psError(__func__, ": Line %d - Invalid dimensionality for in1 arg: %d", __LINE__, dim1); 578 return NULL; 509 579 } 510 580 … … 544 614 if(nIn != nOut) { \ 545 615 psError(__func__, ": Inconsistent element count: %d vs %d", nIn, nOut); \ 546 return OUT; \ 616 if (OUT != IN) { \ 617 psFree(OUT); \ 618 } \ 619 return NULL; \ 547 620 } \ 548 621 o = ((psVector* )OUT)->data.TYPE; \ … … 571 644 psError(__func__, ": Inconsistent element count: numRows: %d vs %d numCols: %d vs %d", numRowsIn, \ 572 645 numRowsOut, numColsIn, numColsOut); \ 573 return OUT; \ 646 if (OUT != IN) { \ 647 psFree(OUT); \ 648 } \ 649 return NULL; \ 574 650 } \ 575 651 for(j = 0; j < numRowsIn; j++) { \ … … 597 673 DIM(OUT,IN,OP,C32); \ 598 674 break; \ 675 case PS_TYPE_S8: \ 676 DIM(OUT,IN,OP,C32); \ 677 break; \ 678 case PS_TYPE_U8: \ 679 DIM(OUT,IN,OP,C32); \ 680 break; \ 681 case PS_TYPE_S16: \ 682 DIM(OUT,IN,OP,C32); \ 683 break; \ 684 case PS_TYPE_U16: \ 685 DIM(OUT,IN,OP,C32); \ 686 break; \ 687 case PS_TYPE_U32: \ 688 DIM(OUT,IN,OP,C32); \ 689 break; \ 690 case PS_TYPE_S64: \ 691 DIM(OUT,IN,OP,C32); \ 692 break; \ 693 case PS_TYPE_U64: \ 694 DIM(OUT,IN,OP,C32); \ 695 break; \ 696 case PS_TYPE_C64: \ 697 DIM(OUT,IN,OP,C32); \ 698 break; \ 599 699 default: \ 600 case PS_TYPE_S8: \601 case PS_TYPE_U8: \602 case PS_TYPE_S16: \603 case PS_TYPE_U16: \604 case PS_TYPE_U32: \605 case PS_TYPE_S64: \606 case PS_TYPE_U64: \607 case PS_TYPE_C64: \608 700 psError(__func__, ": Invalid PS_TYPE: %d", IN->type); \ 701 if (OUT != IN) { \ 702 psFree(OUT); \ 703 } \ 704 return NULL; \ 609 705 } 610 706 … … 725 821 { 726 822 psDimen dimIn = 0; 727 psDimen dimOut = 0;728 823 psElemType elTypeIn = 0; 729 psElemType elTypeOut = 0;730 824 psType* psTypeIn = NULL; 731 psType* psTypeOut = NULL;732 733 psTypeOut = (psType* ) out;734 if (psTypeOut == NULL) {735 psError(__func__, ": Line %d - Null out argument", __LINE__);736 return out;737 }738 825 739 826 psTypeIn = (psType* ) in; 740 827 if (psTypeIn == NULL) { 741 828 psError(__func__, ": Line %d - Null in argument", __LINE__); 742 return out; 829 if (out != in) { 830 psFree(out); 831 } 832 return NULL; 743 833 } 744 834 745 835 if (op == NULL) { 746 836 psError(__func__, ": Line %d - Null op argument", __LINE__); 747 return out; 837 if (out != in) { 838 psFree(out); 839 } 840 return NULL; 748 841 } 749 842 750 843 dimIn = psTypeIn->dimen; 751 dimOut = psTypeOut->dimen;752 844 elTypeIn = psTypeIn->type; 753 elTypeOut = psTypeOut->type; 754 755 if (elTypeIn != elTypeOut) { 756 psError(__func__, ": Line %d - Element types for arguments inconsistent: (%d, %d)", __LINE__, 757 elTypeIn, elTypeOut); 758 return out; 759 } 760 761 if (dimIn != dimOut) { 762 psError(__func__, ": Line %d - Dimensionality for arguments inconsistent: (%d, %d)", __LINE__, 763 dimIn, dimOut); 764 return out; 765 } 766 767 if (dimIn == PS_DIMEN_OTHER || dimOut == PS_DIMEN_OTHER) { 768 psError(__func__, ": Line %d - PS_DIMEN_OTHER not allowed for arguments: (%d, %d)", __LINE__, 769 dimIn, dimOut); 770 return out; 771 } 772 773 if (dimIn == PS_DIMEN_VECTOR) { 774 if (((psVector* ) in)->n == 0) { 845 846 switch (dimIn) { 847 case PS_DIMEN_SCALAR: 848 if (out == NULL || 849 ((psType*)out)->dimen != PS_DIMEN_SCALAR || 850 ((psScalar*)out)->type.type != elTypeIn) { 851 psFree(out); 852 out = psScalarAlloc(0.0,elTypeIn); 853 } 854 UNARY_OP(SCALAR, out, psTypeIn, op); // scalar 855 break; 856 case PS_DIMEN_VECTOR: 857 case PS_DIMEN_TRANSV: 858 if (((psVector*)in)->n == 0) { 859 if (out != in) { 860 psFree(out); 861 } 775 862 psError(__func__, ": Line %d - Vector contains zero elements"); 776 } 777 } else if (dimIn == PS_DIMEN_IMAGE) { 863 return NULL; 864 } 865 866 out = psVectorRecycle(out, 867 ((psVector*)in)->n, 868 elTypeIn); 869 if (out == NULL) { 870 psError(__func__, "Couldn't create a proper output psVector."); 871 return NULL; 872 } 873 874 UNARY_OP(VECTOR, out, psTypeIn, op); // vector 875 break; 876 case PS_DIMEN_IMAGE: 778 877 if (((psImage* ) in)->numCols == 0 || ((psImage* ) in)->numRows == 0) { 878 if (out != in) { 879 psFree(out); 880 } 779 881 psError(__func__, ": Line %d - Image contains zero length row or cols"); 780 } 781 } 782 783 if (dimOut == PS_DIMEN_VECTOR) { 784 if (((psVector* ) out)->n == 0) { 785 psError(__func__, ": Line %d - Vector contains zero elements"); 786 } 787 } else if (dimOut == PS_DIMEN_IMAGE) { 788 if (((psImage* ) out)->numCols == 0 || ((psImage* ) out)->numRows == 0) { 789 psError(__func__, ": Line %d - Image contains zero length row or cols"); 790 } 791 } 792 793 if (dimIn == PS_DIMEN_SCALAR) { 794 UNARY_OP(SCALAR, out, psTypeIn, op); // scalar 795 } else if (dimIn == PS_DIMEN_VECTOR || dimIn == PS_DIMEN_TRANSV) { 796 UNARY_OP(VECTOR, out, psTypeIn, op); // vector 797 } else if (dimIn == PS_DIMEN_IMAGE) { 882 return NULL; 883 } 884 885 out = psImageRecycle(out, 886 ((psImage*)in)->numCols, 887 ((psImage*)in)->numRows, 888 elTypeIn); 889 if (out == NULL) { 890 psError(__func__, "Couldn't create a proper output psImage."); 891 return NULL; 892 } 893 798 894 UNARY_OP(IMAGE, out, psTypeIn, op); // image 799 } else { 895 break; 896 default: 897 if (out != in) { 898 psFree(out); 899 } 800 900 psError(__func__, ": Line %d - Invalid dimensionality for in arg: %d", __LINE__, dimIn); 901 return NULL; 801 902 } 802 903
Note:
See TracChangeset
for help on using the changeset viewer.
