001/* A data type representing a complex number. 002 003 Copyright (c) 1998-2014 The Regents of the University of California. 004 All rights reserved. 005 006 Permission is hereby granted, without written agreement and without 007 license or royalty fees, to use, copy, modify, and distribute this 008 software and its documentation for any purpose, provided that the above 009 copyright notice and the following two paragraphs appear in all copies 010 of this software. 011 012 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 013 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 014 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 015 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 016 SUCH DAMAGE. 017 018 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 019 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 020 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 021 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 022 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 023 ENHANCEMENTS, OR MODIFICATIONS. 024 025 PT_COPYRIGHT_VERSION_2 026 COPYRIGHTENDKEY 027 028 isCloseTo(t), isCloseTo(t, e), EPSILON 029 030 */ 031package ptolemy.math; 032 033/** This class provides a complex data type and a library of functions that 034 operate on and return complex numbers. An instance of the class is 035 immutable, meaning that its value is set in the constructor and 036 cannot then be modified. This is similar to the Java built-in classes 037 like Double, Integer, etc. 038 <p> 039 Although this code is written from scratch, I looked at several designs 040 and borrowed elements from each of them: 041 <ul> 042 <li> The ComplexSubset class in Ptolemy 0.x, written by Joe Buck, 043 which borrowed design elements from the cfront and libg++ 044 Complex classes. 045 <li> Version 1.0 of the Complex class by Alma services, dated Fri 046 29-Aug-97, written by Sandy Anderson and Priyantha Jayanetti, 047 and obtained from the <a href="http://www.netlib.org/"> 048 Netlib Repository</a>. 049 <li> The Complex class in JNL, a Java Numerical Library, dated 1997, 050 by Visual Numerics, Inc. This was used for interface design only, 051 to compare the choice of method names and method templates. 052 <li> Matlab, which was used to validate the results in the test 053 suite, and influenced some of the method name choices. 054 </ul> 055 @author Edward A. Lee, Jeff Tsay, Steve Neuendorffer, Adam Cataldo 056 @version $Id$ 057 @since Ptolemy II 0.2 058 @Pt.ProposedRating Yellow (eal) 059 @Pt.AcceptedRating Red (cxh) 060 */ 061public class Complex { 062 /** Construct a Complex equal to zero. 063 * @deprecated Use Complex.ZERO instead. 064 */ 065 @Deprecated 066 public Complex() { 067 this.real = 0.0; 068 this.imag = 0.0; 069 } 070 071 /** Construct a Complex with a zero imaginary part. 072 * @param real The real part. 073 */ 074 public Complex(double real) { 075 this.real = real; 076 this.imag = 0.0; 077 } 078 079 /** Construct a Complex with the specified real and imaginary parts. 080 * @param real The real part. 081 * @param imag The imaginary part. 082 */ 083 public Complex(double real, double imag) { 084 this.real = real; 085 this.imag = imag; 086 } 087 088 // NOTE: There is no need for a constructor that takes a Complex 089 // argument because instances of this class are immutable. There 090 // is never a need to make another instance with the same value. 091 /////////////////////////////////////////////////////////////////// 092 //// public methods //// 093 094 /** Return the magnitude or absolute value of the specified complex number. 095 * @param x The specified number. 096 * @return A non-negative number that is the absolute value of 097 * this complex number. 098 */ 099 public static double abs(Complex x) { 100 return x.magnitude(); 101 } 102 103 /** Return the principal arc cosine of this complex number. This 104 * is defined by: 105 * <pre> 106 * acos(z) = -i * log(z + i*sqrt(1 - z*z)) 107 * </pre> 108 * where <code>z</code> is this complex number. 109 * 110 * @return A new complex number with value equal to the 111 * arc cosine of the given complex number. 112 */ 113 public final Complex acos() { 114 Complex c1 = new Complex(1.0 - real * real + imag * imag, 115 -2.0 * real * imag); 116 Complex c2 = c1.sqrt(); 117 Complex c3 = new Complex(real - c2.imag, imag + c2.real); 118 Complex c4 = c3.log(); 119 return new Complex(c4.imag, -c4.real); 120 } 121 122 /** Return the principal arc cosine of the specified complex number. This 123 * is defined by: 124 * <pre> 125 * acos(z) = -i * log(z + i*sqrt(1 - z*z)) 126 * </pre> 127 * where <code>z</code> is this complex number. 128 * 129 * @param z A complex number. 130 * @return A new complex number with value equal to the 131 * arc cosine of the given complex number. 132 */ 133 public static Complex acos(Complex z) { 134 return z.acos(); 135 } 136 137 /** Return the principal hyperbolic arc cosine of this 138 * complex number. This is defined by: 139 * <pre> 140 * acosh(z) = log(z + sqrt(z*z - 1)) 141 * </pre> 142 * where <code>z</code> is this complex number. 143 * 144 * @return A new complex number with value equal to the 145 * principal hyperbolic arc cosine of this complex number. 146 */ 147 public final Complex acosh() { 148 Complex c1 = new Complex(real * real - imag * imag - 1.0, 149 2.0 * real * imag); 150 Complex c2 = c1.sqrt(); 151 Complex c3 = add(c2); 152 return c3.log(); 153 } 154 155 /** Return the principal hyperbolic arc cosine of the given 156 * complex number. This is defined by: 157 * <pre> 158 * acosh(z) = log(z + sqrt(z*z - 1)) 159 * </pre> 160 * where <code>z</code> is this complex number. 161 * 162 * @param z A complex number. 163 * @return A new complex number with value equal to the 164 * principal hyperbolic arc cosine of this complex number. 165 */ 166 public static Complex acosh(Complex z) { 167 return z.acosh(); 168 } 169 170 /** Return the sum of this complex number and the argument <i>z</i>. 171 * @param z A complex number. 172 * @return A new complex number equal to the sume of the given complex 173 * number and the argument. 174 */ 175 public final Complex add(Complex z) { 176 return new Complex(real + z.real, imag + z.imag); 177 } 178 179 /** Return the angle or argument of this complex number. 180 * @return A double in the range -<em>pi </em> to <em>pi</em>. 181 */ 182 public final double angle() { 183 return Math.atan2(imag, real); 184 } 185 186 /** Return the angle or argument of this complex number. 187 * @param z A complex number. 188 * @return A double in the range -<em>pi </em> to <em>pi</em>. 189 */ 190 public static double angle(Complex z) { 191 return z.angle(); 192 } 193 194 /** Return the principal arc sine of this complex number. 195 * This is defined by: 196 * <pre> 197 * asin(z) = -i * log(i*z + sqrt(1 - z*z)) 198 * </pre> 199 * where <code>z</code> is this complex number. 200 * 201 * @return A new complex number equal to the principal arc sine 202 * of this complex number. 203 */ 204 public final Complex asin() { 205 Complex c1 = new Complex(1.0 - real * real + imag * imag, 206 -2.0 * real * imag); 207 Complex c2 = c1.sqrt(); 208 Complex c3 = new Complex(c2.real - imag, c2.imag + real); 209 Complex c4 = c3.log(); 210 return new Complex(c4.imag, -c4.real); 211 } 212 213 /** Return the principal arc sine of the given complex number. 214 * This is defined by: 215 * <pre> 216 * asin(z) = -i * log(i*z + sqrt(1 - z*z)) 217 * </pre> 218 * where <code>z</code> is this complex number. 219 * 220 * @param z A complex number. 221 * @return A new complex number equal to the principal arc sine 222 * of this complex number. 223 */ 224 public static Complex asin(Complex z) { 225 return z.asin(); 226 } 227 228 /** Return the principal hyperbolic arc sine of this 229 * complex number. This is defined by: 230 * <pre> 231 * asinh(z) = log(z + sqrt(z*z + 1)) 232 * </pre> 233 * where <code>z</code> is this complex number. 234 * 235 * @return A new complex number with value equal to the principal 236 * hyperbolic arc sine of this complex number. 237 */ 238 public final Complex asinh() { 239 Complex c1 = new Complex(1.0 + real * real - imag * imag, 240 2.0 * real * imag); 241 Complex c2 = c1.sqrt(); 242 Complex c3 = add(c2); 243 return c3.log(); 244 } 245 246 /** Return the principal hyperbolic arc sine of the given 247 * complex number. This is defined by: 248 * <pre> 249 * asinh(z) = log(z + sqrt(z*z + 1)) 250 * </pre> 251 * where <code>z</code> is this complex number. 252 * 253 * @param z A complex number. 254 * @return A new complex number with value equal to the principal 255 * hyperbolic arc sine of this complex number. 256 */ 257 public static Complex asinh(Complex z) { 258 return z.asinh(); 259 } 260 261 /** Return the principal arc tangent of this complex 262 * number. This is defined by: 263 * <pre> 264 * atan(z) = -i/2 * log((i-z)/(i+z)) 265 * </pre> 266 * where <code>z</code> is this complex number. 267 * 268 * @return a new complex number with value equal to the principal arc 269 * tangent of this complex number. 270 */ 271 public final Complex atan() { 272 double denominator = real * real + (imag + 1.0) * (imag + 1.0); 273 Complex c1 = new Complex( 274 (-real * real - imag * imag + 1.0) / denominator, 275 2.0 * real / denominator); 276 Complex c2 = c1.log(); 277 return new Complex(c2.imag * 0.5, -c2.real * 0.5); 278 } 279 280 /** Return the principal arc tangent of the given complex 281 * number. This is defined by: 282 * <pre> 283 * atan(z) = -i/2 * log((i-z)/(i+z)) 284 * </pre> 285 * where <code>z</code> is this complex number. 286 * 287 * @param z A complex number. 288 * @return a new complex number with value equal to the principal arc 289 * tangent of this complex number. 290 */ 291 public static Complex atan(Complex z) { 292 return z.atan(); 293 } 294 295 /** Return the principal hyperbolic arc tangent of 296 * this complex number. This is defined by: 297 * <pre> 298 * atanh(z) = 1/2 * log((1+z)/(1-z)) 299 * </pre> 300 * where <code>z</code> is this complex number. 301 * 302 * @return a new complex number with value equal to the principal 303 * hyperbolic arc tangent of this complex number. 304 */ 305 public final Complex atanh() { 306 double denominator = (1.0 - real) * (1.0 - real) + imag * imag; 307 Complex c1 = new Complex( 308 (-real * real - imag * imag + 1.0) / denominator, 309 2.0 * imag / denominator); 310 Complex c2 = c1.log(); 311 return new Complex(c2.real * 0.5, c2.imag * 0.5); 312 } 313 314 /** Return the principal hyperbolic arc tangent of 315 * the given complex number. This is defined by: 316 * <pre> 317 * atanh(z) = 1/2 * log((1+z)/(1-z)) 318 * </pre> 319 * where <code>z</code> is this complex number. 320 * 321 * @param z A complex number. 322 * @return a new complex number with value equal to the principal 323 * hyperbolic arc tangent of this complex number. 324 */ 325 public static Complex atanh(Complex z) { 326 return z.atanh(); 327 } 328 329 /** Return the complex conjugate of this complex number. 330 * @return A new Complex with value equal to the complex conjugate of 331 * of this complex number. 332 */ 333 public final Complex conjugate() { 334 // Avoid negative zero. 335 if (imag != 0.0) { 336 return new Complex(real, -imag); 337 } 338 339 return new Complex(real, imag); 340 } 341 342 /** Return the complex conjugate of the specified complex number. 343 * @param z The specified complex number. 344 * @return A new Complex with value equal to the complex conjugate of 345 * of the specified complex number. 346 */ 347 public static final Complex conjugate(Complex z) { 348 return z.conjugate(); 349 } 350 351 /** Return the complex conjugate of the specified real number, which is 352 * just the real number itself. This method is provided for completeness 353 * in the expression language. 354 * @param z The specified real number. 355 * @return The number provided as an argument, but converted to complex. 356 */ 357 public static final Complex conjugate(double z) { 358 return new Complex(z, 0.0); 359 } 360 361 /** Return the cosine of this complex number. This is defined by: 362 * <pre> 363 * cos(z) = (exp(i*z) + exp(-i*z))/2 364 * </pre> 365 * where <code>z</code> is this complex number. 366 * 367 * @return a new complex number with value equal to the cosine 368 * of this complex number. 369 */ 370 public final Complex cos() { 371 Complex c1 = new Complex(-imag, real); 372 Complex c2 = c1.exp(); 373 Complex c3 = new Complex(imag, -real); 374 Complex c4 = c2.add(c3.exp()); 375 return new Complex(c4.real * 0.5, c4.imag * 0.5); 376 } 377 378 /** Return the cosine of the given complex number. This is defined by: 379 * <pre> 380 * cos(z) = (exp(i*z) + exp(-i*z))/2 381 * </pre> 382 * where <code>z</code> is this complex number. 383 * 384 * @param z A complex number. 385 * @return a new complex number with value equal to the cosine 386 * of this complex number. 387 */ 388 public static Complex cos(Complex z) { 389 return z.cos(); 390 } 391 392 /** Return the hyperbolic cosine of this complex 393 * number. This is defined by: 394 * <pre> 395 * cosh(z) = (exp(z) + exp(-z))/2 396 * </pre> 397 * where <code>z</code> is this complex number. 398 * 399 * @return A new complex number with value equal to the hyperbolic cosine 400 * of this complex number. 401 */ 402 public final Complex cosh() { 403 Complex c1 = exp(); 404 Complex c2 = new Complex(-real, -imag); 405 Complex c3 = c1.add(c2.exp()); 406 return new Complex(c3.real * 0.5, c3.imag * 0.5); 407 } 408 409 /** Return the hyperbolic cosine of the given complex 410 * number. This is defined by: 411 * <pre> 412 * cosh(z) = (exp(z) + exp(-z))/2 413 * </pre> 414 * where <code>z</code> is this complex number. 415 * 416 * @param z A complex number. 417 * @return A new complex number with value equal to the hyperbolic cosine 418 * of this complex number. 419 */ 420 public static Complex cosh(Complex z) { 421 return z.cosh(); 422 } 423 424 /** Return the cotangent of this complex number. This is simply: 425 * <pre> 426 * cot(z) = 1/tan(z) 427 * </pre> 428 * where <code>z</code> is this complex number. 429 * 430 * @return A new complex number with value equal to the cotangent 431 * of this complex number. 432 */ 433 public final Complex cot() { 434 Complex c1 = tan(); 435 return c1.reciprocal(); 436 } 437 438 /** Return the cotangent of the given complex number. This is simply: 439 * <pre> 440 * cot(z) = 1/tan(z) 441 * </pre> 442 * where <code>z</code> is this complex number. 443 * 444 * @param z A complex number. 445 * @return A new complex number with value equal to the cotangent 446 * of this complex number. 447 */ 448 public static Complex cot(Complex z) { 449 return z.cot(); 450 } 451 452 /** Return the cosecant of this complex number. This is simply: 453 * <pre> 454 * csc(z) = 1/sin(z) 455 * </pre> 456 * where <code>z</code> is this complex number. 457 * 458 * @return A new complex number with value equal to the cosecant 459 * of this complex number. 460 */ 461 public Complex csc() { 462 Complex c1 = sin(); 463 return c1.reciprocal(); 464 } 465 466 /** Return the cosecant of the given complex number. This is simply: 467 * <pre> 468 * csc(z) = 1/sin(z) 469 * </pre> 470 * where <code>z</code> is this complex number. 471 * 472 * @param z A complex number. 473 * @return A new complex number with value equal to the cosecant 474 * of this complex number. 475 */ 476 public static Complex csc(Complex z) { 477 return z.csc(); 478 } 479 480 /** Divide this complex number by the argument, and return the result 481 * in a new Complex object. 482 * 483 * @param divisor The denominator in the division. 484 * @return A new complex number equal to this complex number divided 485 * by the argument. 486 */ 487 public final Complex divide(Complex divisor) { 488 // This algorithm results from writing a/b as (ab*)/magSquared(b). 489 double denominator = divisor.magnitudeSquared(); 490 return new Complex( 491 (real * divisor.real + imag * divisor.imag) / denominator, 492 (imag * divisor.real - real * divisor.imag) / denominator); 493 } 494 495 /** Return true if the real and imaginary parts of this complex number 496 * are equal to those of the argument. 497 * @param z The argument to which this number is being compared. 498 * @return True if the real and imaginary parts are equal. 499 */ 500 @Override 501 public final boolean equals(Object z) { 502 if (z instanceof Complex) { 503 return ((Complex) z).real == real && ((Complex) z).imag == imag; 504 } 505 return false; 506 } 507 508 /** Return the exponential of this complex number, 509 * or <em>e<sup>z</sup></em>, 510 * where <code>z</code> is this complex number. 511 * 512 * @return A new complex number with value equal to the exponential 513 * of this complex number. 514 */ 515 public final Complex exp() { 516 double magnitude = Math.exp(real); 517 return polarToComplex(magnitude, imag); 518 } 519 520 /** Return the exponential of the specified complex number, 521 * or <em>e<sup>z</sup></em>, 522 * where <code>z</code> is the argument. 523 * @param z A complex exponent. 524 * @return A new complex number with value equal to the exponential 525 * of this complex number. 526 */ 527 public static Complex exp(Complex z) { 528 return z.exp(); 529 } 530 531 /** Return a hash code value for this Complex. This method returns the 532 * bitwise xor of the hashcode of the real and imaginary parts. 533 * @return A hash code value for this Complex. 534 */ 535 @Override 536 public int hashCode() { 537 // Use bitwise xor here so that if either real or imag is 0 538 // we get better values. 539 return Double.valueOf(real).hashCode() >>> Double.valueOf(imag) 540 .hashCode(); 541 } 542 543 /** Return the imaginary part of the specified complex number. 544 * @param z The complex number. 545 * @return The imaginary part of the argument. 546 */ 547 public static double imag(Complex z) { 548 return z.imag; 549 } 550 551 /** Return the imaginary part of the specified real number, which is 0.0. 552 * @param z The complex number. 553 * @return 0.0. 554 */ 555 public static double imag(double z) { 556 return 0.0; 557 } 558 559 /** Return true if the distance between this complex number and 560 * the argument is less than or equal to EPSILON. 561 * @param z The number to compare against. 562 * @return True if the distance to the argument is less 563 * than or equal to EPSILON. 564 * @see #EPSILON 565 */ 566 public final boolean isCloseTo(Complex z) { 567 return isCloseTo(z, EPSILON); 568 } 569 570 /** Return true if the distance between this complex number and 571 * the first argument is less than or equal to the second argument. If 572 * the distance argument is negative, return false. 573 * @param z The number to compare against. 574 * @param distance The distance criterion. 575 * @return True if the distance to the first argument is less 576 * than or equal to the second argument. 577 */ 578 public final boolean isCloseTo(Complex z, double distance) { 579 // NOTE: I couldn't find a way to make this as precise as double. 580 // With this implementation, the following example yields the 581 // wrong answer due to rounding errors: 582 // close (1.0i, 1.1i, 0.1) 583 // (This is how to invoke this in the expression language.) 584 if (distance < 0.0) { 585 return false; 586 } 587 588 double differenceSquared = subtract(z).magnitudeSquared(); 589 double distanceSquared = distance * distance; 590 591 if (differenceSquared > distanceSquared) { 592 return false; 593 } else { 594 return true; 595 } 596 } 597 598 /** Return true if either the real or imaginary part is infinite. 599 * This is determined by the isInfinite() method of the java.lang.Double 600 * class. 601 * 602 * @return True if this is infinite. 603 */ 604 public final boolean isInfinite() { 605 return Double.isInfinite(real) || Double.isInfinite(imag); 606 } 607 608 /** Return true if either the real or imaginary part of the given 609 * complex number is infinite. This is determined by the 610 * isInfinite() method of the java.lang.Double class. 611 * 612 * @param z A complex number. 613 * @return True if this is infinite. 614 */ 615 public static boolean isInfinite(Complex z) { 616 return z.isInfinite(); 617 } 618 619 /** Return true if either the real or imaginary part is NaN. NaN means 620 * "not a number," per the IEEE floating point standard. 621 * This is determined by the isNaN() method of the java.lang.Double 622 * class. 623 * 624 * @return True if this is NaN. 625 */ 626 public final boolean isNaN() { 627 return Double.isNaN(real) || Double.isNaN(imag); 628 } 629 630 /** Return true if either the real or imaginary part of the given 631 * Complex number is NaN. NaN means "not a number," per the IEEE 632 * floating point standard. This is determined by the isNaN() 633 * method of the java.lang.Double class. 634 * 635 * @param z A complex number. 636 * @return True if this is NaN. 637 */ 638 public static boolean isNaN(Complex z) { 639 return z.isNaN(); 640 } 641 642 /** Return the natural logarithm of this complex 643 * number. The principal value is returned, which 644 * is 645 * <pre> 646 * log(z) = log(abs(z)) + i * angle(z) 647 * </pre> 648 * where <code>z</code> is this complex number, <code>abs(z)</code> 649 * is its magnitude, and <code>angle(z)</code> is its angle. 650 * 651 * @return A new complex number with value equal to the natural logarithm 652 * of this complex number. 653 */ 654 public final Complex log() { 655 return new Complex(Math.log(magnitude()), angle()); 656 } 657 658 /** Return the natural logarithm of the specified complex 659 * number. The principal value is returned, which 660 * is 661 * <pre> 662 * log(z) = log(abs(z)) + i * angle(z) 663 * </pre> 664 * where <code>z</code> is this complex number, <code>abs(z)</code> 665 * is its magnitude, and <code>angle(z)</code> is its angle. 666 * 667 * @param z A complex number. 668 * @return A new complex number with value equal to the natural logarithm 669 * of this complex number. 670 */ 671 public static Complex log(Complex z) { 672 return z.log(); 673 } 674 675 /** Return the magnitude or absolute value of this complex number. 676 * 677 * @return A non-negative number that is the absolute value of 678 * this complex number. 679 */ 680 public final double magnitude() { 681 return Math.sqrt(magnitudeSquared()); 682 } 683 684 /** Return the magnitude or absolute value of the given complex number. 685 * 686 * @param z A complex number. 687 * @return A non-negative number that is the absolute value of 688 * this complex number. 689 */ 690 public static double magnitude(Complex z) { 691 return z.magnitude(); 692 } 693 694 /** Return the square of the magnitude of this complex number. 695 * This is provided for efficiency, since it is considerably easier 696 * to compute than the magnitude (which is the square root of this 697 * result). 698 * 699 * @return A non-negative number which is the magnitude of this 700 * complex number. 701 */ 702 public double magnitudeSquared() { 703 return real * real + imag * imag; 704 } 705 706 /** Return the square of the magnitude of this complex number. 707 * This is provided for efficiency, since it is considerably easier 708 * to compute than the magnitude (which is the square root of this 709 * result). 710 * 711 * @param z A complex number. 712 * @return A non-negative number which is the magnitude of this 713 * complex number. 714 */ 715 public static double magnitudeSquared(Complex z) { 716 return z.magnitudeSquared(); 717 } 718 719 /** Return a new complex number that is formed by multiplying this 720 * complex number by the specified complex number. 721 * 722 * @param w The specified complex number. 723 * @return A new complex number which is the product of this complex 724 * number and the specified complex number. 725 * @see Complex#scale 726 */ 727 public Complex multiply(Complex w) { 728 return new Complex(w.real * real - w.imag * imag, 729 w.real * imag + w.imag * real); 730 } 731 732 /** Negate this complex number. 733 * 734 * @return A new complex number that is formed by taking the 735 * negatives of both the real and imaginary parts of this complex 736 * number. 737 */ 738 public final Complex negate() { 739 // Avoid negative zero. 740 double r = 0.0; 741 double i = 0.0; 742 743 if (real != 0.0) { 744 r = -real; 745 } 746 747 if (imag != 0.0) { 748 i = -imag; 749 } 750 751 return new Complex(r, i); 752 } 753 754 /** Return a new complex number with the specified magnitude and angle. 755 * 756 * @param magnitude The magnitude. 757 * @param angle The angle. 758 * @return A new complex number with the specified magnitude and angle. 759 */ 760 public static Complex polarToComplex(double magnitude, double angle) { 761 if (magnitude < 0.0) { 762 angle += Math.PI; 763 magnitude = -magnitude; 764 } 765 766 if (magnitude == 0.0) { 767 return Complex.ZERO; 768 } 769 770 return new Complex(magnitude * Math.cos(angle), 771 magnitude * Math.sin(angle)); 772 } 773 774 /** Return a new complex number with value <em>z <sup>y</sup></em> 775 * where <em>z</em> is this complex number and <em>y</em> is the 776 * argument, a double. 777 * 778 * @param y The exponent, which is a double. 779 * @return A new complex number that with value <em>z <sup>y</sup></em>. 780 */ 781 public Complex pow(double y) { 782 // This formula follows from expanding the input form 783 // (rho e^(j theta))^(c + dj) 784 // to something of the form ae^jb. 785 double lnrho = Math.log(magnitude()); 786 double magnitude = Math.exp(lnrho * y); 787 double angle = angle() * y; 788 return polarToComplex(magnitude, angle); 789 } 790 791 /** Return a new complex number with value <em>z <sup>y</sup></em> 792 * where <em>z</em> is the first argument and <em>y</em> is the second 793 * argument. 794 * @param z The number to be raised to a power. 795 * @param y The exponent. 796 * @return A new complex number that with value <em>z <sup>y</sup></em>. 797 */ 798 public static Complex pow(Complex z, double y) { 799 return z.pow(y); 800 } 801 802 /** Return <em>z<sup>y</sup></em> 803 * where <em>z</em> is this complex number and <em>y</em> is the 804 * argument, a Complex. 805 * 806 * @param y The exponent, which is a complex number. 807 * @return A new complex number equal to <em>z<sup>y</sup></em>. 808 */ 809 public final Complex pow(Complex y) { 810 // This formula follows from expanding the input form 811 // (rho e^(j theta))^(c + dj) 812 // to something of the form ae^jb. 813 double lnrho = Math.log(magnitude()); 814 double theta = angle(); 815 double magnitude = Math.exp(lnrho * y.real - theta * y.imag); 816 double angle = lnrho * y.imag + theta * y.real; 817 return polarToComplex(magnitude, angle); 818 } 819 820 /** Return a new complex number with value <em>z <sup>y</sup></em> 821 * where <em>z</em> is the first argument and <em>y</em> is the second 822 * argument. 823 * @param z The number to be raised to a power. 824 * @param y The exponent. 825 * @return A new complex number that with value <em>z <sup>y</sup></em>. 826 */ 827 public static Complex pow(Complex z, Complex y) { 828 return z.pow(y); 829 } 830 831 /** Return a new complex number with value <em>z <sup>y</sup></em> 832 * where <em>z</em> is the first argument and <em>y</em> is the second 833 * argument. 834 * @param z The number to be raised to a power. 835 * @param y The exponent. 836 * @return A new complex number that with value <em>z <sup>y</sup></em>. 837 */ 838 public static Complex pow(double z, Complex y) { 839 return new Complex(z, 0.0).pow(y); 840 } 841 842 /** Return the real part of the specified complex number. 843 * @param z The complex number. 844 * @return The real part of the argument. 845 */ 846 public static double real(Complex z) { 847 return z.real; 848 } 849 850 /** Return the real part of the specified real number, which is the 851 * real number itself. 852 * @param z The complex number. 853 * @return The argument. 854 */ 855 public static double real(double z) { 856 return z; 857 } 858 859 /** Return the reciprocal of this complex number. 860 * The result 1/a is given by (a*)/|a|^2. 861 * 862 * @return A new complex number that is the reciprocal of this one. 863 */ 864 public final Complex reciprocal() { 865 double magSquared = magnitudeSquared(); 866 return new Complex(real / magSquared, -imag / magSquared); 867 } 868 869 /** Return the reciprocal of this complex number. 870 * The result 1/a is given by (a*)/|a|^2. 871 * 872 * @param z A complex number. 873 * @return A new complex number that is the reciprocal of this one. 874 */ 875 public static Complex reciprocal(Complex z) { 876 return z.reciprocal(); 877 } 878 879 /** Return the nth roots of this complex number in an array. There are 880 * n of them, computed by : 881 * 882 * r<sup>1/n</sup>(cos((theta + 2kPI) / n + i sin((theta + 2kPI)/n) 883 * where k is the index of the returned array. If n is not greater than or 884 * equal to one, throw a IllegalArgumentException. 885 * 886 * @param n An integer that must be greater than or equal to one. 887 * @return An array of Complex numbers, containing the n roots. 888 */ 889 public final Complex[] roots(int n) { 890 if (n < 1) { 891 throw new IllegalArgumentException("Complex.roots(): " 892 + "n must be greater than or equal to one."); 893 } 894 895 Complex[] returnValue = new Complex[n]; 896 897 double oneOverN = 1.0 / n; 898 double twoPIOverN = 2.0 * Math.PI * oneOverN; 899 double thetaOverN = angle() * oneOverN; 900 double twoPIkOverN = 0.0; 901 902 // r^(1/n) = (r^2)^(0.5 / n) 903 double retMag = Math.pow(magnitudeSquared(), 0.5 * oneOverN); 904 905 for (int k = 0; k < n; k++) { 906 returnValue[k] = polarToComplex(retMag, thetaOverN + twoPIkOverN); 907 twoPIkOverN += twoPIOverN; 908 } 909 910 return returnValue; 911 } 912 913 /** Return the nth roots of the given complex number in an 914 * array. There are n of them, computed by : 915 * 916 * r<sup>1/n</sup>(cos((theta + 2kPI) / n + i sin((theta + 2kPI)/n) 917 * where k is the index of the returned array. If n is not greater than or 918 * equal to one, throw a IllegalArgumentException. 919 * 920 * @param z A complex number. 921 * @param n An integer that must be greater than or equal to one. 922 * @return An array of Complex numbers, containing the n roots. 923 */ 924 public static Complex[] roots(Complex z, int n) { 925 return z.roots(n); 926 } 927 928 /** Return a new complex number with value equal to the product 929 * of this complex number and the real argument. 930 * 931 * @param scalar A real number. 932 * @return A new complex number with value equal to the product 933 * of this complex number and the real argument. 934 * @see Complex#multiply 935 */ 936 public final Complex scale(double scalar) { 937 return new Complex(real * scalar, imag * scalar); 938 } 939 940 /** Return a new complex number with value equal to the secant 941 * of this complex number. This is simply: 942 * <pre> 943 * sec(z) = 1/cos(z) 944 * </pre> 945 * where <code>z</code> is this complex number. 946 * 947 * @return A new complex number equal to the secant of this 948 * complex number. 949 */ 950 public Complex sec() { 951 Complex c1 = cos(); 952 return c1.reciprocal(); 953 } 954 955 /** Return a new complex number with value equal to the secant 956 * of the given complex number. This is simply: 957 * <pre> 958 * sec(z) = 1/cos(z) 959 * </pre> 960 * where <code>z</code> is this complex number. 961 * 962 * @param z A complex number. 963 * @return A new complex number equal to the secant of this 964 * complex number. 965 */ 966 public static Complex sec(Complex z) { 967 return z.sec(); 968 } 969 970 /** Return a new complex number with value equal to the sine 971 * of this complex number. This is defined by: 972 * <pre> 973 * sin(z) = (exp(i*z) - exp(-i*z))/(2*i) 974 * </pre> 975 * where <code>z</code> is this complex number. 976 * 977 * @return A new complex number equal to the sine of this complex number. 978 */ 979 public final Complex sin() { 980 Complex c1 = new Complex(-imag, real); 981 Complex c2 = c1.exp(); 982 Complex c3 = new Complex(imag, -real); 983 Complex c4 = c2.subtract(c3.exp()); 984 return new Complex(c4.imag * 0.5, -c4.real * 0.5); 985 } 986 987 /** Return a new complex number with value equal to the sine 988 * of the given complex number. This is defined by: 989 * <pre> 990 * sin(z) = (exp(i*z) - exp(-i*z))/(2*i) 991 * </pre> 992 * where <code>z</code> is this complex number. 993 * 994 * @param z A complex number. 995 * @return A new complex number equal to the sine of this complex number. 996 */ 997 public static Complex sin(Complex z) { 998 return z.sin(); 999 } 1000 1001 /** Return a new complex number with value equal to the hyperbolic sine 1002 * of this complex number. This is defined by: 1003 * <pre> 1004 * sinh(z) = (exp(z) - exp(-z))/2 1005 * </pre> 1006 * where <code>z</code> is this complex number. 1007 * 1008 * @return A new complex number equal to the hyperbolic sine 1009 * of this complex number. 1010 */ 1011 public final Complex sinh() { 1012 Complex c1 = exp(); 1013 Complex c2 = new Complex(-real, -imag); 1014 Complex c3 = c1.subtract(c2.exp()); 1015 return new Complex(c3.real * 0.5, c3.imag * 0.5); 1016 } 1017 1018 /** Return a new complex number with value equal to the hyperbolic sine 1019 * of this complex number. This is defined by: 1020 * <pre> 1021 * sinh(z) = (exp(z) - exp(-z))/2 1022 * </pre> 1023 * where <code>z</code> is this complex number. 1024 * 1025 * @param z A complex number. 1026 * @return A new complex number equal to the hyperbolic sine 1027 * of this complex number. 1028 */ 1029 public static Complex sinh(Complex z) { 1030 return z.sinh(); 1031 } 1032 1033 /** Return a new complex number with its value equal to the 1034 * the square root of this complex number. 1035 * The square root is defined to be: 1036 * <pre> 1037 * sqrt(z) = sqrt(mag(z))*(cos(angle(z)/2) + i * sin(angle(z)/2) ) 1038 * </pre> 1039 * where <code>z</code> is this complex number. 1040 * 1041 * @return A new complex number equal to the square root of 1042 * this complex number. 1043 */ 1044 public final Complex sqrt() { 1045 double magnitude = Math.sqrt(magnitude()); 1046 double angle = angle() * 0.5; 1047 return polarToComplex(magnitude, angle); 1048 } 1049 1050 /** Return a new complex number with its value equal to the 1051 * the square root of the specified complex number. 1052 * The square root is defined to be: 1053 * <pre> 1054 * sqrt(z) = sqrt(mag(z))*(cos(angle(z)/2) + i * sin(angle(z)/2) ) 1055 * </pre> 1056 * where <code>z</code> is this complex number. 1057 * 1058 * @param z A complex number. 1059 * @return A new complex number equal to the square root of 1060 * this complex number. 1061 */ 1062 public static Complex sqrt(Complex z) { 1063 return z.sqrt(); 1064 } 1065 1066 /** Return a new complex number formed by subtracting the specified 1067 * complex number from this complex number. 1068 * 1069 * @param w The number that is being subtracted. 1070 * @return A new complex number formed by subtracting the specified 1071 * complex number from this complex number. 1072 */ 1073 public final Complex subtract(Complex w) { 1074 return new Complex(real - w.real, imag - w.imag); 1075 } 1076 1077 /** Return a new complex number with value equal to the tangent 1078 * of this complex number. This is defined by: 1079 * <pre> 1080 * tan(z) = sin(z)/cos(z) 1081 * </pre> 1082 * where <code>z</code> is this complex number. 1083 * 1084 * @return A new complex number equal to sin(z)/cos(z). 1085 */ 1086 public final Complex tan() { 1087 Complex c1 = sin(); 1088 return c1.divide(cos()); 1089 } 1090 1091 /** Return a new complex number with value equal to the tangent 1092 * of the given complex number. This is defined by: 1093 * <pre> 1094 * tan(z) = sin(z)/cos(z) 1095 * </pre> 1096 * where <code>z</code> is this complex number. 1097 * 1098 * @param z A complex number. 1099 * @return A new complex number equal to sin(z)/cos(z). 1100 */ 1101 public static Complex tan(Complex z) { 1102 return z.tan(); 1103 } 1104 1105 /** Return a new complex number with value equal to the hyperbolic tangent 1106 * of this complex number. This is defined by: 1107 * <pre> 1108 * tanh(z) = sinh(z)/cosh(z) 1109 * </pre> 1110 * where <code>z</code> is this complex number. 1111 * 1112 * @return A new complex number equal to sinh(z)/cosh(z). 1113 */ 1114 public final Complex tanh() { 1115 Complex c1 = sinh(); 1116 return c1.divide(cosh()); 1117 } 1118 1119 /** Return a new complex number with value equal to the hyperbolic tangent 1120 * of the given complex number. This is defined by: 1121 * <pre> 1122 * tanh(z) = sinh(z)/cosh(z) 1123 * </pre> 1124 * where <code>z</code> is this complex number. 1125 * 1126 * @param z A complex number. 1127 * @return A new complex number equal to sinh(z)/cosh(z). 1128 */ 1129 public static Complex tanh(Complex z) { 1130 return z.tanh(); 1131 } 1132 1133 /** Return a string representation of this Complex. 1134 * 1135 * @return A string of the form "<em>x</em> + <em>y</em>i". 1136 */ 1137 @Override 1138 public final String toString() { 1139 if (imag >= 0) { 1140 return Double.toString(real) + " + " + Double.toString(imag) + "i"; 1141 } else { 1142 return Double.toString(real) + " - " + Double.toString(-imag) + "i"; 1143 } 1144 } 1145 1146 /** Return a string representation of the given Complex. 1147 * 1148 * @param value The given value. 1149 * @return A string of the form "<em>x</em> + <em>y</em>i". 1150 */ 1151 public static String toString(Complex value) { 1152 if (value.imag >= 0) { 1153 return Double.toString(value.real) + " + " 1154 + Double.toString(value.imag) + "i"; 1155 } else { 1156 return Double.toString(value.real) + " - " 1157 + Double.toString(-value.imag) + "i"; 1158 } 1159 } 1160 1161 /////////////////////////////////////////////////////////////////// 1162 //// public variables //// 1163 1164 /** The real part. This is a "blank final," which means that it 1165 * can only be set in the constructor. 1166 */ 1167 public final double real; 1168 1169 /** The imaginary part. This is a "blank final," which means that it 1170 * can only be set in the constructor. 1171 */ 1172 public final double imag; 1173 1174 /** A small number ( = 1.0e-9). This number is used by algorithms to 1175 * detect whether a double is close to zero. This value is 1176 * public so that it can be changed on platforms with different 1177 * precisions. 1178 * This variable is not final so that users may set it as necessary. 1179 */ 1180 public static/*final*/double EPSILON = 1.0e-9; 1181 1182 /** A Complex number representing negative infinity, by which we mean 1183 * that both the real and imaginary parts are equal to 1184 * Double.NEGATIVE_INFINITY. 1185 */ 1186 public static final Complex NEGATIVE_INFINITY = new Complex( 1187 Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY); 1188 1189 /** A Complex number representing positive infinity, by which we mean 1190 * that both the real and imaginary parts are equal to 1191 * Double.POSITIVE_INFINITY. 1192 */ 1193 public static final Complex POSITIVE_INFINITY = new Complex( 1194 Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY); 1195 1196 /** A Complex number representing zero. Reference this to save 1197 * memory usage and construction overhead. 1198 */ 1199 public static final Complex ZERO = new Complex(0.0, 0.0); 1200 1201 /** A Complex number representing one. Reference this to save 1202 * memory usage and construction overhead. 1203 */ 1204 public static final Complex ONE = new Complex(1.0, 0.0); 1205 1206 /** A Complex number representing <i>i</i>. Reference this to save 1207 * memory usage and construction overhead. 1208 */ 1209 public static final Complex I = new Complex(0.0, 1.0); 1210}