Class Complex

java.lang.Object
edu.sc.seis.TauP.Complex
All Implemented Interfaces:
Serializable, Cloneable

public class Complex extends Object implements Serializable, Cloneable
This class implements complex numbers. It provides the basic operations (addition, subtraction, multiplication, division) as well as a set of complex functions. The binary operations have the form, where op is plus, minus, times or over.
 public static Complex op(Complex x, Complex y)   // x op y
 public static Complex op(Complex x, double y)    // x op y
 public static Complex op(double x, Complex y)    // x op y
 public Complex op(Complex y)                     // this op y
 public Complex op(double y)                      // this op y
 public Complex opReverse(double x)               // x op this
 public Complex opEquals(Complex y)               // this op= y
 public Complex opEquals(double y)                // this op= y
 
The functions in this class follow the rules for complex arithmetic as defined C9x Annex G:"IEC 559-compatible complex arithmetic." The API is not the same, but handling of infinities, NaNs, and positive and negative zeros is intended to follow the same rules. This class depends on the standard java.lang.Math class following certain rules, as defined in the C9x Annex F, for the handling of infinities, NaNs, and positive and negative zeros. Sun's specification is that java.lang.Math should reproduce the results in the Sun's fdlibm C library. This library appears to follow the Annex F specification. At least on Windows, Sun's JDK 1.0 and 1.1 do NOT follow this specification. Sun's JDK 1.2(RC2) does follow the Annex F specification. Thesefore, this class will not give the expected results for edge cases with JDK 1.0 and 1.1.
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    double
     
    static final Complex
     
    double
     
    static String
    String used in converting Complex to String.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a Complex equal to zero.
    Complex(double re)
    Constructs a Complex with a zero imaginary part.
    Complex(double re, double im)
    Constructs a Complex with real and imaginary parts given by the input arguments.
    Constructs a Complex equal to the argument.
  • Method Summary

    Modifier and Type
    Method
    Description
    double
    abs()
    Returns the absolute value (modulus) of this Complex, |z|.
    static double
    Returns the absolute value (modulus) of a Complex, |z|.
    static Complex
    Returns the inverse cosine (arc cosine) of a Complex, with branch cuts outside the interval [-1,1] along the real axis.
    static Complex
    Returns the inverse hyperbolic cosine (arc cosh) of a Complex, with a branch cut at values less than one along the real axis.
    static double
    Returns the argument (phase) of a Complex, in radians, with a branch cut along the negative real axis.
    static Complex
    Returns the inverse sine (arc sine) of a Complex, with branch cuts outside the interval [-1,1] along the real axis.
    static Complex
    Returns the inverse hyperbolic sine (arc sinh) of a Complex, with a branch cuts outside the interval [-i,i].
    assign(double x)
    Copies the contents of a double into this Complex.
    Copies the contents of a Complex into this Complex.
    static Complex
    Returns the inverse tangent (arc tangent) of a Complex, with branch cuts outside the interval [-i,i] along the imaginary axis.
    static Complex
    Returns the inverse hyperbolic tangent (arc tanh) of a Complex, with a branch cuts outside the interval [-1,1] on the real axis.
    static Complex
    Returns the complex conjugate of a Complex object.
    static Complex
    Returns the cosine of a Complex.
    static Complex
    Returns the hyperbolic cosh of a Complex.
    boolean
    Compares with another Complex.
    boolean
    Compares this object against the specified object.
    static Complex
    Returns the exponential of a Complex z, exp(z).
    int
    Returns a hashcode for this Complex.
    static double
    Returns the imaginary part of a Complex object.
    boolean
    Tests if this is an infinite complex number.
    boolean
    Tests if this is a complex Not-a-Number (NaN) value.
    static Complex
    Returns the logarithm of a Complex z, with a branch cut along the negative real axis.
    minus(double y)
    Subtracts a double from this Complex and returns the difference, this-y.
    static Complex
    minus(double x, Complex y)
    Returns the difference of a double and a Complex object, x-y.
    Returns the difference of this Complex object and another Complex object, this-y.
    static Complex
    minus(Complex x, double y)
    Returns the difference of a Complex object and a double, x-y.
    static Complex
    Returns the difference of two Complex objects, x-y.
    minusEquals(double y)
    Subtracts a double from this Complex and returns the difference, this -= y.
    Subtracts a Complex from this Complex and returns the difference, this -= y.
    minusReverse(double x)
    Returns the difference of this Complex object and a double, this-y.
    static Complex
    Returns the negative of a Complex object, -z.
    over(double y)
    Returns this Complex object divided by double, this/y.
    static Complex
    over(double x, Complex y)
    Returns a double divided by a Complex object, x/y.
    Returns this Complex object divided by another Complex object, this/y.
    static Complex
    over(Complex x, double y)
    Returns Complex object divided by a double, x/y.
    static Complex
    Returns Complex object divided by a Complex object, x/y.
    overEquals(double y)
    Divides this Complex by a double and returns the result, this /= y.
    Divides this Complex by a Complex and returns the result, this /= y.
    overReverse(double x)
    Returns a double dividied by this Complex object, x/this.
    plus(double y)
    Returns the sum of this Complex a double, this+y.
    static Complex
    plus(double x, Complex y)
    Returns the sum of a double and a Complex, x+y.
    Returns the sum of this Complex and another Complex, this+y.
    static Complex
    plus(Complex x, double y)
    Returns the sum of a Complex and a double, x+y.
    static Complex
    Returns the sum of two Complex objects, x+y.
    plusEquals(double y)
    Adds a double into this Complex and returns the sum, this += y.
    Adds a Complex to this Complex and returns the sum, this += y.
    plusReverse(double x)
    Returns the sum of this Complex and a double, x+this.
    static Complex
    pow(Complex z, double x)
    Returns the Complex z raised to the x power, with a branch cut for the first parameter (z) along the negative real axis.
    static Complex
    Returns the Complex x raised to the Complex y power.
    static double
    Returns the real part of a Complex object.
    static Complex
    Returns the sine of a Complex.
    static Complex
    Returns the hyperbolic sine of a Complex.
    static Complex
    Returns the square root of a Complex, with a branch cut along the negative real axis.
    static Complex
    Returns the tangent of a Complex.
    static Complex
    Returns the hyperbolic tanh of a Complex.
    times(double y)
    Returns the product of this Complex object and a double, this*y.
    static Complex
    times(double x, Complex y)
    Returns the product of a double and a Complex object, x*y.
    Returns the product of this Complex object and another Complex object, this*y.
    static Complex
    times(Complex x, double y)
    Returns the product of a Complex object and a double, x*y.
    static Complex
    Returns the product of two Complex objects, x*y.
    timesEquals(double y)
    Multiplies this Complex by a double and returns the product, this *= y.
    Multiplies this Complex object by another Complex and returns the product, this *= y.
    timesReverse(double x)
    Returns the product of a double and this Complex, x*this.
    Returns a String representation for the specified Complex.
    static Complex
    Parses a string into a Complex.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • re

      public double re
    • im

      public double im
    • suffix

      public static String suffix
      String used in converting Complex to String. Default is "i", but sometimes "j" is desired. Note that this is set for the class, not for a particular instance of a Complex.
    • NaN

      public static final Complex NaN
  • Constructor Details

    • Complex

      public Complex(Complex z)
      Constructs a Complex equal to the argument.
      Parameters:
      z - A Complex object If z is null then a NullPointerException is thrown.
    • Complex

      public Complex(double re, double im)
      Constructs a Complex with real and imaginary parts given by the input arguments.
      Parameters:
      re - A double value equal to the real part of the Complex object.
      im - A double value equal to the imaginary part of the Complex object.
    • Complex

      public Complex(double re)
      Constructs a Complex with a zero imaginary part.
      Parameters:
      re - A double value equal to the real part of the Complex object.
    • Complex

      public Complex()
      Constructs a Complex equal to zero.
  • Method Details

    • isNaN

      public boolean isNaN()
      Tests if this is a complex Not-a-Number (NaN) value.
      Returns:
      True if either component of the Complex object is NaN; false, otherwise.
    • isInfinite

      public boolean isInfinite()
      Tests if this is an infinite complex number.
      Returns:
      True if either component of the Complex object is NaN; false, otherwise.
    • equals

      public boolean equals(Complex z)
      Compares with another Complex.

      Note: To be useful in hashtables this method considers two NaN double values to be equal. This is not according to IEEE specification.

      Parameters:
      z - A Complex object.
      Returns:
      True if the real and imaginary parts of this object are equal to their counterparts in the argument; false, otherwise.
    • equals

      public boolean equals(Object obj)
      Compares this object against the specified object.

      Note: To be useful in hashtables this method considers two NaN double values to be equal. This is not according to IEEE specification

      Overrides:
      equals in class Object
      Parameters:
      obj - The object to compare with.
      Returns:
      True if the objects are the same; false otherwise.
    • hashCode

      public int hashCode()
      Returns a hashcode for this Complex.
      Overrides:
      hashCode in class Object
      Returns:
      A hash code value for this object.
    • real

      public static double real(Complex z)
      Returns the real part of a Complex object.
      Parameters:
      z - A Complex object.
      Returns:
      The real part of z.
    • imag

      public static double imag(Complex z)
      Returns the imaginary part of a Complex object.
      Parameters:
      z - A Complex object.
      Returns:
      The imaginary part of z.
    • assign

      public Complex assign(double x)
      Copies the contents of a double into this Complex.
      Parameters:
      x - The real part.
      Returns:
      The modified Complex number. The real part is set to x and the imaginary part is set to zero.
    • assign

      public Complex assign(Complex z)
      Copies the contents of a Complex into this Complex.
      Parameters:
      z - A Complex object.
      Returns:
      The modified Complex number. If the argument is null then a NullPointerException is thrown.
    • negative

      public static Complex negative(Complex z)
      Returns the negative of a Complex object, -z.
      Parameters:
      z - A Complex object.
      Returns:
      A newly constructed Complex initialized to the negative of the argument.
    • conjugate

      public static Complex conjugate(Complex z)
      Returns the complex conjugate of a Complex object.
      Parameters:
      z - A Complex object.
      Returns:
      A newly constructed Complex initialized to complex conjugate of z.
    • plus

      public static Complex plus(Complex x, Complex y)
      Returns the sum of two Complex objects, x+y.
      Parameters:
      x - A Complex object.
      y - A Complex object.
      Returns:
      A newly constructed Complex initialized to x+y.
    • plus

      public static Complex plus(Complex x, double y)
      Returns the sum of a Complex and a double, x+y.
      Parameters:
      x - A Complex object.
      y - A double value.
      Returns:
      A newly constructed Complex initialized to x+y.
    • plus

      public static Complex plus(double x, Complex y)
      Returns the sum of a double and a Complex, x+y.
      Parameters:
      x - A double value.
      y - A Complex object.
      Returns:
      A newly constructed Complex initialized to x+y.
    • plus

      public Complex plus(Complex y)
      Returns the sum of this Complex and another Complex, this+y.
      Parameters:
      y - A Complex object.
      Returns:
      A newly constructed Complex initialized to this+y.
    • plus

      public Complex plus(double y)
      Returns the sum of this Complex a double, this+y.
      Parameters:
      y - A double value.
      Returns:
      A newly constructed Complex initialized to this+y.
    • plusReverse

      public Complex plusReverse(double x)
      Returns the sum of this Complex and a double, x+this.
      Parameters:
      x - A double value.
      Returns:
      A newly constructed Complex initialized to x+this.
    • plusEquals

      public Complex plusEquals(Complex y)
      Adds a Complex to this Complex and returns the sum, this += y.
      Parameters:
      y - A Complex object.
      Returns:
      This object augmented by the argument.
    • plusEquals

      public Complex plusEquals(double y)
      Adds a double into this Complex and returns the sum, this += y.
      Parameters:
      y - A double value.
      Returns:
      This object augmented by the argument.
    • minus

      public static Complex minus(Complex x, Complex y)
      Returns the difference of two Complex objects, x-y.
      Parameters:
      x - A Complex object.
      y - A Complex object.
      Returns:
      A newly constructed Complex initialized to x-y.
    • minus

      public static Complex minus(Complex x, double y)
      Returns the difference of a Complex object and a double, x-y.
      Parameters:
      x - A Complex object.
      y - A double value.
      Returns:
      A newly constructed Complex initialized to x-y.
    • minus

      public static Complex minus(double x, Complex y)
      Returns the difference of a double and a Complex object, x-y.
      Parameters:
      x - A double value.
      y - A Complex object.
      Returns:
      A newly constructed Complex initialized to x-y..
    • minus

      public Complex minus(Complex y)
      Returns the difference of this Complex object and another Complex object, this-y.
      Parameters:
      y - A Complex object.
      Returns:
      A newly constructed Complex initialized to this-y.
    • minus

      public Complex minus(double y)
      Subtracts a double from this Complex and returns the difference, this-y.
      Parameters:
      y - A double value.
      Returns:
      A newly constructed Complex initialized to this-y.
    • minusReverse

      public Complex minusReverse(double x)
      Returns the difference of this Complex object and a double, this-y.
      Parameters:
      x - A double value.
      Returns:
      A newly constructed Complex initialized to x-this.
    • minusEquals

      public Complex minusEquals(Complex y)
      Subtracts a Complex from this Complex and returns the difference, this -= y.
      Parameters:
      y - A Complex object.
      Returns:
      This object less the input argument.
    • minusEquals

      public Complex minusEquals(double y)
      Subtracts a double from this Complex and returns the difference, this -= y.
      Parameters:
      y - A double value.
      Returns:
      This object less the input argument.
    • times

      public static Complex times(Complex x, Complex y)
      Returns the product of two Complex objects, x*y.
      Parameters:
      x - A Complex object.
      y - A Complex object.
      Returns:
      A newly constructed Complex initialized to x*y.
    • times

      public static Complex times(Complex x, double y)
      Returns the product of a Complex object and a double, x*y.
      Parameters:
      x - A Complex object.
      y - A double value.
      Returns:
      A newly constructed Complex initialized to x*y.
    • times

      public static Complex times(double x, Complex y)
      Returns the product of a double and a Complex object, x*y.
      Parameters:
      x - A double value.
      y - A Complex object.
      Returns:
      A newly constructed Complex initialized to x*y.
    • times

      public Complex times(Complex y)
      Returns the product of this Complex object and another Complex object, this*y.
      Parameters:
      y - A Complex object.
      Returns:
      A newly constructed Complex initialized to this*y.
    • times

      public Complex times(double y)
      Returns the product of this Complex object and a double, this*y.
      Parameters:
      y - A double value.
      Returns:
      A newly constructed Complex initialized to this*y.
    • timesReverse

      public Complex timesReverse(double x)
      Returns the product of a double and this Complex, x*this.
      Parameters:
      x - A double value.
      Returns:
      A newly constructed Complex initialized to x*this.
    • timesEquals

      public Complex timesEquals(Complex y)
      Multiplies this Complex object by another Complex and returns the product, this *= y.
      Parameters:
      y - A Complex object.
      Returns:
      This object multiplied by the input argument.
    • timesEquals

      public Complex timesEquals(double y)
      Multiplies this Complex by a double and returns the product, this *= y.
      Parameters:
      y - A double value.
      Returns:
      This object multiplied by the input argument.
    • over

      public static Complex over(Complex x, Complex y)
      Returns Complex object divided by a Complex object, x/y.
      Parameters:
      x - The numerator, a Complex object.
      y - The denominator, a Complex object.
      Returns:
      A newly constructed Complex initialized to x/y.
    • over

      public static Complex over(Complex x, double y)
      Returns Complex object divided by a double, x/y.
      Parameters:
      x - The numerator, a Complex object.
      y - The denominator, a double.
      Returns:
      A newly constructed Complex initialized to x/y.
    • over

      public static Complex over(double x, Complex y)
      Returns a double divided by a Complex object, x/y.
      Parameters:
      x - A double value.
      y - The denominator, a Complex object.
      Returns:
      A newly constructed Complex initialized to x/y.
    • over

      public Complex over(Complex y)
      Returns this Complex object divided by another Complex object, this/y.
      Parameters:
      y - The denominator, a Complex object.
      Returns:
      A newly constructed Complex initialized to x/y.
    • over

      public Complex over(double y)
      Returns this Complex object divided by double, this/y.
      Parameters:
      y - The denominator, a double.
      Returns:
      A newly constructed Complex initialized to x/y.
    • overReverse

      public Complex overReverse(double x)
      Returns a double dividied by this Complex object, x/this.
      Parameters:
      x - The numerator, a double.
      Returns:
      A newly constructed Complex initialized to x/this.
    • overEquals

      public Complex overEquals(Complex y)
      Divides this Complex by a Complex and returns the result, this /= y.
      Parameters:
      y - The denominator, a Complex object.
      Returns:
      This object divided by the input argument.
    • overEquals

      public Complex overEquals(double y)
      Divides this Complex by a double and returns the result, this /= y.
      Parameters:
      y - The denominator, a double.
      Returns:
      This object divided by the input argument.
    • abs

      public double abs()
      Returns the absolute value (modulus) of this Complex, |z|.
    • abs

      public static double abs(Complex z)
      Returns the absolute value (modulus) of a Complex, |z|.
      Parameters:
      z - A Complex object.
      Returns:
      A double value equal to the absolute value of the argument.
    • argument

      public static double argument(Complex z)
      Returns the argument (phase) of a Complex, in radians, with a branch cut along the negative real axis.
      Parameters:
      z - A Complex object.
      Returns:
      A double value equal to the argument (or phase) of a Complex. It is in the interval [-pi,pi].
    • sqrt

      public static Complex sqrt(Complex z)
      Returns the square root of a Complex, with a branch cut along the negative real axis.
      Parameters:
      z - A Complex object.
      Returns:
      A newly constructed Complex initialized to square root of z. Its real part is non-negative.
    • exp

      public static Complex exp(Complex z)
      Returns the exponential of a Complex z, exp(z).
      Parameters:
      z - A Complex object.
      Returns:
      A newly constructed Complex initialized to exponential of the argument.
    • log

      public static Complex log(Complex z)
      Returns the logarithm of a Complex z, with a branch cut along the negative real axis.
      Parameters:
      z - A Complex object.
      Returns:
      A newly constructed Complex initialized to logarithm of the argument. Its imaginary part is in the interval [-i*pi,i*pi].
    • sin

      public static Complex sin(Complex z)
      Returns the sine of a Complex.
      Parameters:
      z - A Complex object.
      Returns:
      A newly constructed Complex initialized to sine of the argument.
    • cos

      public static Complex cos(Complex z)
      Returns the cosine of a Complex.
      Parameters:
      z - A Complex object.
      Returns:
      A newly constructed Complex initialized to cosine of the argument.
    • tan

      public static Complex tan(Complex z)
      Returns the tangent of a Complex.
      Parameters:
      z - A Complex object.
      Returns:
      A newly constructed Complex initialized to tangent of the argument.
    • asin

      public static Complex asin(Complex z)
      Returns the inverse sine (arc sine) of a Complex, with branch cuts outside the interval [-1,1] along the real axis.
      Parameters:
      z - A Complex object.
      Returns:
      A newly constructed Complex initialized to inverse (arc) sine of the argument. The real part of the result is in the interval [-pi/2,+pi/2].
    • acos

      public static Complex acos(Complex z)
      Returns the inverse cosine (arc cosine) of a Complex, with branch cuts outside the interval [-1,1] along the real axis.
      Parameters:
      z - A Complex object.
      Returns:
      A newly constructed Complex initialized to inverse (arc) cosine of the argument. The real part of the result is in the interval [0,pi].
    • atan

      public static Complex atan(Complex z)
      Returns the inverse tangent (arc tangent) of a Complex, with branch cuts outside the interval [-i,i] along the imaginary axis.
      Parameters:
      z - A Complex object.
      Returns:
      A newly constructed Complex initialized to inverse (arc) tangent of the argument. Its real part is in the interval [-pi/2,pi/2].
    • sinh

      public static Complex sinh(Complex z)
      Returns the hyperbolic sine of a Complex.
      Parameters:
      z - A Complex object.
      Returns:
      A newly constructed Complex initialized to hyperbolic sine of the argument.
    • cosh

      public static Complex cosh(Complex z)
      Returns the hyperbolic cosh of a Complex.
      Parameters:
      z - A Complex object.
      Returns:
      A newly constructed Complex initialized to the hyperbolic cosine of the argument.
    • tanh

      public static Complex tanh(Complex z)
      Returns the hyperbolic tanh of a Complex.
      Parameters:
      z - A Complex object.
      Returns:
      A newly constructed Complex initialized to the hyperbolic tangent of the argument.
    • pow

      public static Complex pow(Complex z, double x)
      Returns the Complex z raised to the x power, with a branch cut for the first parameter (z) along the negative real axis.
      Parameters:
      z - A Complex object.
      x - A double value.
      Returns:
      A newly constructed Complex initialized to z to the power x.
    • asinh

      public static Complex asinh(Complex z)
      Returns the inverse hyperbolic sine (arc sinh) of a Complex, with a branch cuts outside the interval [-i,i].
      Parameters:
      z - A Complex object.
      Returns:
      A newly constructed Complex initialized to inverse (arc) hyperbolic sine of the argument. Its imaginary part is in the interval [-i*pi/2,i*pi/2].
    • acosh

      public static Complex acosh(Complex z)
      Returns the inverse hyperbolic cosine (arc cosh) of a Complex, with a branch cut at values less than one along the real axis.
      Parameters:
      z - A Complex object.
      Returns:
      A newly constructed Complex initialized to inverse (arc) hyperbolic cosine of the argument. The real part of the result is non-negative and its imaginary part is in the interval [-i*pi,i*pi].
    • atanh

      public static Complex atanh(Complex z)
      Returns the inverse hyperbolic tangent (arc tanh) of a Complex, with a branch cuts outside the interval [-1,1] on the real axis.
      Parameters:
      z - A Complex object.
      Returns:
      A newly constructed Complex initialized to inverse (arc) hyperbolic tangent of the argument. The imaginary part of the result is in the interval [-i*pi/2,i*pi/2].
    • pow

      public static Complex pow(Complex x, Complex y)
      Returns the Complex x raised to the Complex y power.
      Parameters:
      x - A Complex object.
      y - A Complex object.
      Returns:
      A newly constructed Complex initialized to xy.
    • toString

      public String toString()
      Returns a String representation for the specified Complex.
      Overrides:
      toString in class Object
      Returns:
      A String representation for this object.
    • valueOf

      public static Complex valueOf(String s) throws NumberFormatException
      Parses a string into a Complex.
      Parameters:
      s - The string to be parsed.
      Returns:
      A newly constructed Complex initialized to the value represented by the string argument.
      Throws:
      NumberFormatException - If the string does not contain a parsable Complex number.
      NullPointerException - If the input argument is null.