1 9 10 11 package org.nfunk.jep.type; 12 13 35 36 public class Complex 37 { 38 39 private double re; 40 41 42 private double im; 43 44 45 48 51 public Complex() { 52 re = 0; 53 im = 0; 54 } 55 56 61 public Complex(double re_in) { 62 re = re_in; 63 im = 0; 64 } 65 66 71 public Complex(Number re_in) { 72 re = re_in.doubleValue(); 73 im = 0; 74 } 75 76 79 public Complex(Complex z) { 80 re = z.re; 81 im = z.im; 82 } 83 84 88 public Complex(double re_in, double im_in) { 89 re = re_in; 90 im = im_in; 91 } 92 93 96 public double re() { 97 return re; 98 } 99 100 103 public double im() { 104 return im; 105 } 106 107 110 public void set(Complex z) { 111 re = z.re; 112 im = z.im; 113 } 114 115 118 public void set(double re_in, double im_in) { 119 re = re_in; 120 im = im_in; 121 } 122 123 126 public void setRe(double re_in) { 127 re = re_in; 128 } 129 130 133 public void setIm(double im_in) { 134 im = im_in; 135 } 136 137 140 149 public boolean equals(Complex b, double tolerance) { 150 double temp1 = (re - b.re); 151 double temp2 = (im - b.im); 152 153 return (temp1*temp1 + temp2*temp2) <= tolerance*tolerance; 154 } 155 168 public boolean equals(Object o) { 169 if(!(o instanceof Complex)) return false; 170 Complex c = (Complex) o; 171 return(Double.doubleToLongBits(this.re) == Double.doubleToLongBits(c.re) 172 && Double.doubleToLongBits(this.im) == Double.doubleToLongBits(c.im)); 173 } 174 178 public int hashCode() { 179 int result = 17; 180 long xl = Double.doubleToLongBits(this.re); 181 long yl = Double.doubleToLongBits(this.im); 182 int xi = (int)(xl^(xl>>32)); 183 int yi = (int)(yl^(yl>>32)); 184 result = 37*result+xi; 185 result = 37*result+yi; 186 return result; 187 } 188 192 public String toString() { 193 return "(" + re + ", " + im + ")"; 194 } 195 196 204 public boolean isInfinite() { 205 return (Double.isInfinite(re) || Double.isInfinite(im)); 206 } 207 208 216 public boolean isNaN() { 217 return (Double.isNaN(re) || Double.isNaN(im)); 218 } 219 220 227 public double abs() { 228 double absRe = Math.abs(re); 229 double absIm = Math.abs(im); 230 231 if (absRe == 0 && absIm == 0) { 232 return 0; 233 } else if (absRe>absIm) { 234 double temp = absIm/absRe; 235 return absRe*Math.sqrt(1 + temp*temp); 236 } else { 237 double temp = absRe/absIm; 238 return absIm*Math.sqrt(1 + temp*temp); 239 } 240 } 241 242 245 public double abs2() { 246 return re*re+im*im; 247 } 248 249 252 public double arg() { 253 return Math.atan2(im,re); 254 } 255 256 259 public Complex neg() { 260 return new Complex(-re,-im); 261 } 262 263 267 public Complex mul(double b) { 268 return new Complex(re*b, im*b); 269 } 270 271 276 public Complex add(Complex b) { 277 return new Complex(re+b.re,im+b.im); 278 } 279 280 285 public Complex sub(Complex b) { 286 return new Complex(re-b.re,im-b.im); 287 } 288 292 public Complex mul(Complex b) { 293 return new Complex(re*b.re - im*b.im, 294 im*b.re + re*b.im); 295 } 296 297 300 public Complex div(Complex b) { 301 double resRe, resIm; 304 double r, den; 305 306 if (Math.abs(b.re) >= Math.abs(b.im)) { 307 r = b.im/b.re; 308 den = b.re + r*b.im; 309 resRe = (re+r*im)/den; 310 resIm = (im-r*re)/den; 311 } else { 312 r = b.re/b.im; 313 den = b.im + r*b.re; 314 resRe = (re*r+im)/den; 315 resIm = (im*r-re)/den; 316 } 317 318 return new Complex(resRe, resIm); 319 } 320 321 338 public Complex power(double exponent) { 339 double scalar = Math.pow(abs(),exponent); 341 boolean specialCase = false; 342 int factor = 0; 343 344 if (im==0 && re<0) {specialCase = true; factor = 2;} 347 if (re==0 && im>0) {specialCase = true; factor = 1;} 348 if (re==0 && im<0) {specialCase = true; factor = -1;} 349 350 if (specialCase && factor*exponent == (int)(factor*exponent)) { 351 short[] cSin = {0,1,0,-1}; short[] cCos = {1,0,-1,0}; 354 int x = ((int)(factor*exponent))%4; 355 if (x<0) x = 4+x; 356 357 return new Complex(scalar*cCos[x], scalar*cSin[x]); 358 } 359 360 double temp = exponent * arg(); 361 362 return new Complex(scalar*Math.cos(temp), scalar*Math.sin(temp)); 363 } 364 365 369 public Complex power(Complex exponent) { 370 if (exponent.im == 0) return power(exponent.re); 371 372 double temp1Re = Math.log(abs()); 373 double temp1Im = arg(); 374 375 double temp2Re = (temp1Re*exponent.re) - (temp1Im*exponent.im); 376 double temp2Im = (temp1Re*exponent.im) + (temp1Im*exponent.re); 377 378 double scalar = Math.exp(temp2Re); 379 380 return new Complex(scalar*Math.cos(temp2Im), scalar*Math.sin(temp2Im)); 381 } 382 383 386 public Complex log() { 387 return new Complex(Math.log(abs()), arg()); 388 } 389 390 395 public Complex sqrt() { 396 Complex c; 397 double absRe,absIm,w,r; 398 399 if (re == 0 && im == 0) { 400 c = new Complex(0,0); 401 } else { 402 absRe = Math.abs(re); 403 absIm = Math.abs(im); 404 405 if (absRe>=absIm) { 406 r = absIm/absRe; 407 w = Math.sqrt(absRe)*Math.sqrt(0.5*(1.0+Math.sqrt(1.0+r*r))); 408 } else { 409 r = absRe/absIm; 410 w = Math.sqrt(absIm)*Math.sqrt(0.5*(r +Math.sqrt(1.0+r*r))); 411 } 412 413 if (re>=0) { 414 c = new Complex(w, im/(2.0*w)); 415 } else { 416 if (im<0) w = -w; 417 c = new Complex(im/(2.0*w), w); 418 } 419 } 420 421 return c; 422 } 423 424 427 430 public Complex sin() { 431 double izRe, izIm; 432 double temp1Re, temp1Im; 433 double temp2Re, temp2Im; 434 double scalar; 435 436 izRe = -im; 438 izIm = re; 439 440 scalar = Math.exp(izRe); 442 temp1Re = scalar * Math.cos(izIm); 443 temp1Im = scalar * Math.sin(izIm); 444 445 scalar = Math.exp(-izRe); 447 temp2Re = scalar * Math.cos(-izIm); 448 temp2Im = scalar * Math.sin(-izIm); 449 450 temp1Re -= temp2Re; 451 temp1Im -= temp2Im; 452 453 return new Complex(0.5*temp1Im, -0.5*temp1Re); 454 } 455 456 459 public Complex cos() { 460 double izRe, izIm; 461 double temp1Re, temp1Im; 462 double temp2Re, temp2Im; 463 double scalar; 464 465 izRe = -im; 467 izIm = re; 468 469 scalar = Math.exp(izRe); 471 temp1Re = scalar * Math.cos(izIm); 472 temp1Im = scalar * Math.sin(izIm); 473 474 scalar = Math.exp(-izRe); 476 temp2Re = scalar * Math.cos(-izIm); 477 temp2Im = scalar * Math.sin(-izIm); 478 479 temp1Re += temp2Re; 480 temp1Im += temp2Im; 481 482 return new Complex(0.5*temp1Re, 0.5*temp1Im); 483 } 484 485 486 489 public Complex tan() { 490 double izRe, izIm; 492 double temp1Re, temp1Im; 493 double temp2Re, temp2Im; 494 double scalar; 495 Complex sinResult, cosResult; 496 497 izRe = -im; 499 izIm = re; 500 501 scalar = Math.exp(izRe); 503 temp1Re = scalar * Math.cos(izIm); 504 temp1Im = scalar * Math.sin(izIm); 505 506 scalar = Math.exp(-izRe); 508 temp2Re = scalar * Math.cos(-izIm); 509 temp2Im = scalar * Math.sin(-izIm); 510 511 temp1Re -= temp2Re; 512 temp1Im -= temp2Im; 513 514 sinResult = new Complex(0.5*temp1Re, 0.5*temp1Im); 515 516 izRe = -im; 518 izIm = re; 519 520 scalar = Math.exp(izRe); 522 temp1Re = scalar * Math.cos(izIm); 523 temp1Im = scalar * Math.sin(izIm); 524 525 scalar = Math.exp(-izRe); 527 temp2Re = scalar * Math.cos(-izIm); 528 temp2Im = scalar * Math.sin(-izIm); 529 530 temp1Re += temp2Re; 531 temp1Im += temp2Im; 532 533 cosResult = new Complex(0.5*temp1Re, 0.5*temp1Im); 534 535 return sinResult.div(cosResult); 536 } 537 538 539 542 public Complex asin() { 543 Complex result; 544 double tempRe, tempIm; 545 546 548 tempRe = 1.0 - ( (re*re) - (im*im) ); 549 tempIm = 0.0 - ( (re*im) + (im*re) ); 550 551 result = new Complex(tempRe, tempIm); 552 result = result.sqrt(); 553 554 result.re += -im; 555 result.im += re; 556 557 tempRe = Math.log(result.abs()); 558 tempIm = result.arg(); 559 560 result.re = tempIm; 561 result.im = - tempRe; 562 563 return result; 564 } 565 566 public Complex acos() { 567 Complex result; 568 double tempRe, tempIm; 569 570 572 tempRe = 1.0 - ( (re*re) - (im*im) ); 573 tempIm = 0.0 - ( (re*im) + (im*re) ); 574 575 result = new Complex(tempRe, tempIm); 576 result = result.sqrt(); 577 578 tempRe = -result.im; 579 tempIm = result.re; 580 581 result.re = re + tempRe; 582 result.im = im + tempIm; 583 584 tempRe = Math.log(result.abs()); 585 tempIm = result.arg(); 586 587 result.re = tempIm; 588 result.im = - tempRe; 589 590 return result; 591 } 592 593 public Complex atan() { 594 596 double tempRe, tempIm; 597 Complex result = new Complex(-re, 1.0 - im); 598 599 tempRe = re; 600 tempIm = 1.0 + im; 601 602 result = result.div(new Complex(tempRe, tempIm)); 603 604 tempRe = Math.log(result.abs()); 605 tempIm = result.arg(); 606 607 result.re = 0.5*tempIm; 608 result.im = -0.5*tempRe; 609 610 return result; 611 } 612 613 614 617 public Complex sinh() { 618 double scalar; 619 double temp1Re, temp1Im; 620 double temp2Re, temp2Im; 621 623 scalar = Math.exp(re); 625 temp1Re = scalar * Math.cos(im); 626 temp1Im = scalar * Math.sin(im); 627 628 scalar = Math.exp(-re); 630 temp2Re = scalar * Math.cos(-im); 631 temp2Im = scalar * Math.sin(-im); 632 633 temp1Re -= temp2Re; 634 temp1Im -= temp2Im; 635 636 return new Complex(0.5*temp1Re, 0.5*temp1Im); 637 } 638 639 640 public Complex cosh() { 641 double scalar; 642 double temp1Re, temp1Im; 643 double temp2Re, temp2Im; 644 646 scalar = Math.exp(re); 648 temp1Re = scalar * Math.cos(im); 649 temp1Im = scalar * Math.sin(im); 650 651 scalar = Math.exp(-re); 653 temp2Re = scalar * Math.cos(-im); 654 temp2Im = scalar * Math.sin(-im); 655 656 temp1Re += temp2Re; 657 temp1Im += temp2Im; 658 659 return new Complex(0.5*temp1Re, 0.5*temp1Im); 660 } 661 662 public Complex tanh() { 663 double scalar; 664 double temp1Re, temp1Im; 665 double temp2Re, temp2Im; 666 Complex sinRes, cosRes; 667 669 scalar = Math.exp(re); 670 temp1Re = scalar * Math.cos(im); 671 temp1Im = scalar * Math.sin(im); 672 673 scalar = Math.exp(-re); 674 temp2Re = scalar * Math.cos(-im); 675 temp2Im = scalar * Math.sin(-im); 676 677 temp1Re -= temp2Re; 678 temp1Im -= temp2Im; 679 680 sinRes = new Complex(0.5*temp1Re, 0.5*temp1Im); 681 682 scalar = Math.exp(re); 683 temp1Re = scalar * Math.cos(im); 684 temp1Im = scalar * Math.sin(im); 685 686 scalar = Math.exp(-re); 687 temp2Re = scalar * Math.cos(-im); 688 temp2Im = scalar * Math.sin(-im); 689 690 temp1Re += temp2Re; 691 temp1Im += temp2Im; 692 693 cosRes = new Complex(0.5*temp1Re, 0.5*temp1Im); 694 695 return sinRes.div(cosRes); 696 } 697 698 699 702 public Complex asinh() { 703 Complex result; 704 706 result = new Complex( 707 ((re*re) - (im*im)) + 1, 708 (re*im) + (im*re)); 709 710 result = result.sqrt(); 711 712 result.re += re; 713 result.im += im; 714 715 double temp = result.arg(); 716 result.re = Math.log(result.abs()); 717 result.im = temp; 718 719 return result; 720 } 721 722 public Complex acosh() { 723 Complex result; 724 725 727 result = new Complex( 728 ((re*re) - (im*im)) - 1, 729 (re*im) + (im*re)); 730 731 result = result.sqrt(); 732 733 result.re += re; 734 result.im += im; 735 736 double temp = result.arg(); 737 result.re = Math.log(result.abs()); 738 result.im = temp; 739 740 return result; 741 } 742 743 public Complex atanh() { 744 746 double tempRe, tempIm; 747 Complex result = new Complex(1.0 + re, im); 748 749 tempRe = 1.0 - re; 750 tempIm = - im; 751 752 result = result.div(new Complex(tempRe, tempIm)); 753 754 tempRe = Math.log(result.abs()); 755 tempIm = result.arg(); 756 757 result.re = 0.5*tempRe; 758 result.im = 0.5*tempIm; 759 760 return result; 761 } 762 763 770 public static Complex polarValueOf(Number r,Number theta) 771 { 772 double rad = r.doubleValue(); 773 double ang = theta.doubleValue(); 774 return new Complex(rad*Math.cos(ang), rad*Math.sin(ang)); 775 776 } 777 780 public double doubleValue() { 781 return re; 782 } 783 784 787 public float floatValue() { 788 return (float) re; 789 } 790 791 794 public int intValue() { 795 return (int) re; 796 } 797 798 801 public long longValue() { 802 return (long) re; 803 } 804 805 } 806 | Popular Tags |