Class Complex

  • All Implemented Interfaces:
    Serializable

    public final class Complex
    extends Object
    implements Serializable
    Cartesian representation of a complex number. The complex number is expressed in the form \( a + ib \) where \( a \) and \( b \) are real numbers and \( i \) is the imaginary unit which satisfies the equation \( i^2 = -1 \). For the complex number \( a + ib \), \( a \) is called the real part and \( b \) is called the imaginary part.

    This class is immutable. All arithmetic will create a new instance for the result.

    Arithmetic in this class conforms to the C99 standard for complex numbers defined in ISO/IEC 9899, Annex G. Methods have been named using the equivalent method in ISO C99. The behavior for special cases is listed as defined in C99.

    For functions \( f \) which obey the conjugate equality \( conj(f(z)) = f(conj(z)) \), the specifications for the upper half-plane imply the specifications for the lower half-plane.

    For functions that are either odd, \( f(z) = -f(-z) \), or even, \( f(z) = f(-z) \), the specifications for the first quadrant imply the specifications for the other three quadrants.

    Special cases of branch cuts for multivalued functions adopt the principle value convention from C99. Specials cases from C99 that raise the "invalid" or "divide-by-zero" floating-point exceptions return the documented value without an explicit mechanism to notify of the exception case, that is no exceptions are thrown during computations in-line with the convention of the corresponding single-valued functions in Math. These cases are documented in the method special cases as "invalid" or "divide-by-zero" floating-point operation. Note: Invalid floating-point exception cases will result in a complex number where the cardinality of NaN component parts has increased as a real or imaginary part could not be computed and is set to NaN.

    See Also:
    ISO/IEC 9899 - Programming languages - C, Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static Complex I
      A complex number representing \( i \), the square root of \( -1 \).
      static Complex ONE
      A complex number representing one.
      static Complex ZERO
      A complex number representing zero.
    • Field Detail

      • I

        public static final Complex I
        A complex number representing \( i \), the square root of \( -1 \).

        \( (0 + i 1) \).

      • ONE

        public static final Complex ONE
        A complex number representing one.

        \( (1 + i 0) \).

      • ZERO

        public static final Complex ZERO
        A complex number representing zero.

        \( (0 + i 0) \).

    • Method Detail

      • ofCartesian

        public static Complex ofCartesian​(double real,
                                          double imaginary)
        Create a complex number given the real and imaginary parts.
        Parameters:
        real - Real part.
        imaginary - Imaginary part.
        Returns:
        Complex number.
      • ofPolar

        public static Complex ofPolar​(double rho,
                                      double theta)
        Creates a complex number from its polar representation using modulus rho (\( \rho \)) and phase angle theta (\( \theta \)). \[ \begin{aligned} x &= \rho \cos(\theta) \\ y &= \rho \sin(\theta) \end{aligned} \]

        Requires that rho is non-negative and non-NaN and theta is finite; otherwise returns a complex with NaN real and imaginary parts. A rho value of -0.0 is considered negative and an invalid modulus.

        A non-NaN complex number constructed using this method will satisfy the following to within floating-point error when theta is in the range \( -\pi\ \lt \theta \leq \pi \):

          Complex.ofPolar(rho, theta).abs() == rho
          Complex.ofPolar(rho, theta).arg() == theta

        If rho is infinite then the resulting parts may be infinite or NaN following the rules for double arithmetic, for example:

        • ofPolar(\( -0.0 \), \( 0 \)) = \( \text{NaN} + i \text{NaN} \)
        • ofPolar(\( 0.0 \), \( 0 \)) = \( 0 + i 0 \)
        • ofPolar(\( 1 \), \( 0 \)) = \( 1 + i 0 \)
        • ofPolar(\( 1 \), \( \pi \)) = \( -1 + i \sin(\pi) \)
        • ofPolar(\( \infty \), \( \pi \)) = \( -\infty + i \infty \)
        • ofPolar(\( \infty \), \( 0 \)) = \( -\infty + i \text{NaN} \)
        • ofPolar(\( \infty \), \( -\frac{\pi}{4} \)) = \( \infty - i \infty \)
        • ofPolar(\( \infty \), \( 5\frac{\pi}{4} \)) = \( -\infty - i \infty \)

        This method is the functional equivalent of the C++ method std::polar.

        Parameters:
        rho - The modulus of the complex number.
        theta - The argument of the complex number.
        Returns:
        Complex number.
        See Also:
        Polar Coordinates
      • ofCis

        public static Complex ofCis​(double x)
        Create a complex cis number. This is also known as the complex exponential: \[ \text{cis}(x) = e^{ix} = \cos(x) + i \sin(x) \]
        Parameters:
        x - double to build the cis number.
        Returns:
        Complex cis number.
        See Also:
        Cis
      • parse

        public static Complex parse​(String s)
        Returns a Complex instance representing the specified string s.

        If s is null, then a NullPointerException is thrown.

        The string must be in a format compatible with that produced by Complex.toString(). The format expects a start and end parentheses surrounding two numeric parts split by a separator. Leading and trailing spaces are allowed around each numeric part. Each numeric part is parsed using Double.parseDouble(String). The parts are interpreted as the real and imaginary parts of the complex number.

        Examples of valid strings and the equivalent Complex are shown below:

         "(0,0)"             = Complex.ofCartesian(0, 0)
         "(0.0,0.0)"         = Complex.ofCartesian(0, 0)
         "(-0.0, 0.0)"       = Complex.ofCartesian(-0.0, 0)
         "(-1.23, 4.56)"     = Complex.ofCartesian(-1.23, 4.56)
         "(1e300,-1.1e-2)"   = Complex.ofCartesian(1e300, -1.1e-2)
        Parameters:
        s - String representation.
        Returns:
        Complex number.
        Throws:
        NullPointerException - if the string is null.
        NumberFormatException - if the string does not contain a parsable complex number.
        See Also:
        Double.parseDouble(String), toString()
      • getReal

        public double getReal()
        Gets the real part \( a \) of this complex number \( (a + i b) \).
        Returns:
        The real part.
      • real

        public double real()
        Gets the real part \( a \) of this complex number \( (a + i b) \).

        This method is the equivalent of the C++ method std::complex::real.

        Returns:
        The real part.
        See Also:
        getReal()
      • getImaginary

        public double getImaginary()
        Gets the imaginary part \( b \) of this complex number \( (a + i b) \).
        Returns:
        The imaginary part.
      • imag

        public double imag()
        Gets the imaginary part \( b \) of this complex number \( (a + i b) \).

        This method is the equivalent of the C++ method std::complex::imag.

        Returns:
        The imaginary part.
        See Also:
        getImaginary()
      • abs

        public double abs()
        Returns the absolute value of this complex number. This is also called complex norm, modulus, or magnitude.

        \[ \text{abs}(x + i y) = \sqrt{(x^2 + y^2)} \]

        Special cases:

        • abs(x + iy) == abs(y + ix) == abs(x - iy).
        • If z is ±∞ + iy for any y, returns +∞.
        • If z is x + iNaN for non-infinite x, returns NaN.
        • If z is x + i0, returns |x|.

        The cases ensure that if either component is infinite then the result is positive infinity. If either component is NaN and this is not infinite then the result is NaN.

        This method follows the ISO C Standard, Annex G, in calculating the returned value without intermediate overflow or underflow.

        The computed result will be within 1 ulp of the exact result.

        Returns:
        The absolute value.
        See Also:
        isInfinite(), isNaN(), Complex modulus
      • arg

        public double arg()
        Returns the argument of this complex number.

        The argument is the angle phi between the positive real axis and the point representing this number in the complex plane. The value returned is between \( -\pi \) (not inclusive) and \( \pi \) (inclusive), with negative values returned for numbers with negative imaginary parts.

        If either real or imaginary part (or both) is NaN, then the result is NaN. Infinite parts are handled as Math.atan2(double, double) handles them, essentially treating finite parts as zero in the presence of an infinite coordinate and returning a multiple of \( \frac{\pi}{4} \) depending on the signs of the infinite parts.

        This code follows the ISO C Standard, Annex G, in calculating the returned value using the atan2(y, x) method for complex \( x + iy \).

        Returns:
        The argument of this complex number.
        See Also:
        Math.atan2(double, double)
      • norm

        public double norm()
        Returns the squared norm value of this complex number. This is also called the absolute square.

        \[ \text{norm}(x + i y) = x^2 + y^2 \]

        If either component is infinite then the result is positive infinity. If either component is NaN and this is not infinite then the result is NaN.

        Note: This method may not return the same value as the square of abs() as that method uses an extended precision computation.

        norm() can be used as a faster alternative than abs() for ranking by magnitude. If used for ranking any overflow to infinity will create an equal ranking for values that may be still distinguished by abs().

        Returns:
        The square norm value.
        See Also:
        isInfinite(), isNaN(), abs(), Absolute square
      • isNaN

        public boolean isNaN()
        Returns true if either the real or imaginary component of the complex number is NaN and the complex number is not infinite.

        Note that:

        • There is more than one complex number that can return true.
        • Different representations of NaN can be distinguished by the Complex.equals(Object) method.
        Returns:
        true if this instance contains NaN and no infinite parts.
        See Also:
        Double.isNaN(double), isInfinite(), Complex.equals(Object)
      • isInfinite

        public boolean isInfinite()
        Returns true if either real or imaginary component of the complex number is infinite.

        Note: A complex number with at least one infinite part is regarded as an infinity (even if its other part is a NaN).

        Returns:
        true if this instance contains an infinite value.
        See Also:
        Double.isInfinite(double)
      • isFinite

        public boolean isFinite()
        Returns true if both real and imaginary component of the complex number are finite.
        Returns:
        true if this instance contains finite values.
        See Also:
        Double.isFinite(double)
      • conj

        public Complex conj()
        Returns the conjugate \( \overline{z} \) of this complex number \( z \).

        \[ \begin{aligned} z &= a + i b \\ \overline{z} &= a - i b \end{aligned}\]

        Returns:
        The conjugate (\( \overline{z} \)) of this complex number.
      • negate

        public Complex negate()
        Returns a Complex whose value is the negation of both the real and imaginary parts of complex number \( z \).

        \[ \begin{aligned} z &= a + i b \\ -z &= -a - i b \end{aligned} \]

        Returns:
        \( -z \).
      • proj

        public Complex proj()
        Returns the projection of this complex number onto the Riemann sphere.

        \( z \) projects to \( z \), except that all complex infinities (even those with one infinite part and one NaN part) project to positive infinity on the real axis. If \( z \) has an infinite part, then z.proj() shall be equivalent to:

        return Complex.ofCartesian(Double.POSITIVE_INFINITY, Math.copySign(0.0, z.imag());
        Returns:
        \( z \) projected onto the Riemann sphere.
        See Also:
        isInfinite(), IEEE and ISO C standards: cproj
      • add

        public Complex add​(Complex addend)
        Returns a Complex whose value is (this + addend). Implements the formula:

        \[ (a + i b) + (c + i d) = (a + c) + i (b + d) \]

        Parameters:
        addend - Value to be added to this complex number.
        Returns:
        this + addend.
        See Also:
        Complex Addition
      • add

        public Complex add​(double addend)
        Returns a Complex whose value is (this + addend), with addend interpreted as a real number. Implements the formula:

        \[ (a + i b) + c = (a + c) + i b \]

        This method is included for compatibility with ISO C99 which defines arithmetic between real-only and complex numbers.

        Note: This method preserves the sign of the imaginary component \( b \) if it is -0.0. The sign would be lost if adding \( (c + i 0) \) using add(Complex.ofCartesian(addend, 0)) since -0.0 + 0.0 = 0.0.

        Parameters:
        addend - Value to be added to this complex number.
        Returns:
        this + addend.
        See Also:
        add(Complex), ofCartesian(double, double)
      • addImaginary

        public Complex addImaginary​(double addend)
        Returns a Complex whose value is (this + addend), with addend interpreted as an imaginary number. Implements the formula:

        \[ (a + i b) + i d = a + i (b + d) \]

        This method is included for compatibility with ISO C99 which defines arithmetic between imaginary-only and complex numbers.

        Note: This method preserves the sign of the real component \( a \) if it is -0.0. The sign would be lost if adding \( (0 + i d) \) using add(Complex.ofCartesian(0, addend)) since -0.0 + 0.0 = 0.0.

        Parameters:
        addend - Value to be added to this complex number.
        Returns:
        this + addend.
        See Also:
        add(Complex), ofCartesian(double, double)
      • subtract

        public Complex subtract​(Complex subtrahend)
        Returns a Complex whose value is (this - subtrahend). Implements the formula:

        \[ (a + i b) - (c + i d) = (a - c) + i (b - d) \]

        Parameters:
        subtrahend - Value to be subtracted from this complex number.
        Returns:
        this - subtrahend.
        See Also:
        Complex Subtraction
      • subtract

        public Complex subtract​(double subtrahend)
        Returns a Complex whose value is (this - subtrahend), with subtrahend interpreted as a real number. Implements the formula:

        \[ (a + i b) - c = (a - c) + i b \]

        This method is included for compatibility with ISO C99 which defines arithmetic between real-only and complex numbers.

        Parameters:
        subtrahend - Value to be subtracted from this complex number.
        Returns:
        this - subtrahend.
        See Also:
        subtract(Complex)
      • subtractImaginary

        public Complex subtractImaginary​(double subtrahend)
        Returns a Complex whose value is (this - subtrahend), with subtrahend interpreted as an imaginary number. Implements the formula:

        \[ (a + i b) - i d = a + i (b - d) \]

        This method is included for compatibility with ISO C99 which defines arithmetic between imaginary-only and complex numbers.

        Parameters:
        subtrahend - Value to be subtracted from this complex number.
        Returns:
        this - subtrahend.
        See Also:
        subtract(Complex)
      • subtractFrom

        public Complex subtractFrom​(double minuend)
        Returns a Complex whose value is (minuend - this), with minuend interpreted as a real number. Implements the formula: \[ c - (a + i b) = (c - a) - i b \]

        This method is included for compatibility with ISO C99 which defines arithmetic between real-only and complex numbers.

        Note: This method inverts the sign of the imaginary component \( b \) if it is 0.0. The sign would not be inverted if subtracting from \( c + i 0 \) using Complex.ofCartesian(minuend, 0).subtract(this) since 0.0 - 0.0 = 0.0.

        Parameters:
        minuend - Value this complex number is to be subtracted from.
        Returns:
        minuend - this.
        See Also:
        subtract(Complex), ofCartesian(double, double)
      • subtractFromImaginary

        public Complex subtractFromImaginary​(double minuend)
        Returns a Complex whose value is (this - subtrahend), with minuend interpreted as an imaginary number. Implements the formula: \[ i d - (a + i b) = -a + i (d - b) \]

        This method is included for compatibility with ISO C99 which defines arithmetic between imaginary-only and complex numbers.

        Note: This method inverts the sign of the real component \( a \) if it is 0.0. The sign would not be inverted if subtracting from \( 0 + i d \) using Complex.ofCartesian(0, minuend).subtract(this) since 0.0 - 0.0 = 0.0.

        Parameters:
        minuend - Value this complex number is to be subtracted from.
        Returns:
        this - subtrahend.
        See Also:
        subtract(Complex), ofCartesian(double, double)
      • multiply

        public Complex multiply​(Complex factor)
        Returns a Complex whose value is this * factor. Implements the formula:

        \[ (a + i b)(c + i d) = (ac - bd) + i (ad + bc) \]

        Recalculates to recover infinities as specified in C99 standard G.5.1.

        Parameters:
        factor - Value to be multiplied by this complex number.
        Returns:
        this * factor.
        See Also:
        Complex Muliplication
      • multiply

        public Complex multiply​(double factor)
        Returns a Complex whose value is this * factor, with factor interpreted as a real number. Implements the formula:

        \[ (a + i b) c = (ac) + i (bc) \]

        This method is included for compatibility with ISO C99 which defines arithmetic between real-only and complex numbers.

        Note: This method should be preferred over using multiply(Complex.ofCartesian(factor, 0)). Multiplication can generate signed zeros if either this complex has zeros for the real and/or imaginary component, or if the factor is zero. The summation of signed zeros in multiply(Complex) may create zeros in the result that differ in sign from the equivalent call to multiply by a real-only number.

        Parameters:
        factor - Value to be multiplied by this complex number.
        Returns:
        this * factor.
        See Also:
        multiply(Complex)
      • multiplyImaginary

        public Complex multiplyImaginary​(double factor)
        Returns a Complex whose value is this * factor, with factor interpreted as an imaginary number. Implements the formula:

        \[ (a + i b) id = (-bd) + i (ad) \]

        This method can be used to compute the multiplication of this complex number \( z \) by \( i \) using a factor with magnitude 1.0. This should be used in preference to multiply(Complex.I) with or without negation:

        \[ \begin{aligned} iz &= (-b + i a) \\ -iz &= (b - i a) \end{aligned} \]

        This method is included for compatibility with ISO C99 which defines arithmetic between imaginary-only and complex numbers.

        Note: This method should be preferred over using multiply(Complex.ofCartesian(0, factor)). Multiplication can generate signed zeros if either this complex has zeros for the real and/or imaginary component, or if the factor is zero. The summation of signed zeros in multiply(Complex) may create zeros in the result that differ in sign from the equivalent call to multiply by an imaginary-only number.

        Parameters:
        factor - Value to be multiplied by this complex number.
        Returns:
        this * factor.
        See Also:
        multiply(Complex)
      • divide

        public Complex divide​(Complex divisor)
        Returns a Complex whose value is (this / divisor). Implements the formula:

        \[ \frac{a + i b}{c + i d} = \frac{(ac + bd) + i (bc - ad)}{c^2+d^2} \]

        Re-calculates NaN result values to recover infinities as specified in C99 standard G.5.1.

        Parameters:
        divisor - Value by which this complex number is to be divided.
        Returns:
        this / divisor.
        See Also:
        Complex Division
      • divide

        public Complex divide​(double divisor)
        Returns a Complex whose value is (this / divisor), with divisor interpreted as a real number. Implements the formula:

        \[ \frac{a + i b}{c} = \frac{a}{c} + i \frac{b}{c} \]

        This method is included for compatibility with ISO C99 which defines arithmetic between real-only and complex numbers.

        Note: This method should be preferred over using divide(Complex.ofCartesian(divisor, 0)). Division can generate signed zeros if this complex has zeros for the real and/or imaginary component, or the divisor is infinite. The summation of signed zeros in divide(Complex) may create zeros in the result that differ in sign from the equivalent call to divide by a real-only number.

        Parameters:
        divisor - Value by which this complex number is to be divided.
        Returns:
        this / divisor.
        See Also:
        divide(Complex)
      • divideImaginary

        public Complex divideImaginary​(double divisor)
        Returns a Complex whose value is (this / divisor), with divisor interpreted as an imaginary number. Implements the formula:

        \[ \frac{a + i b}{id} = \frac{b}{d} - i \frac{a}{d} \]

        This method is included for compatibility with ISO C99 which defines arithmetic between imaginary-only and complex numbers.

        Note: This method should be preferred over using divide(Complex.ofCartesian(0, divisor)). Division can generate signed zeros if this complex has zeros for the real and/or imaginary component, or the divisor is infinite. The summation of signed zeros in divide(Complex) may create zeros in the result that differ in sign from the equivalent call to divide by an imaginary-only number.

        Warning: This method will generate a different result from divide(Complex.ofCartesian(0, divisor)) if the divisor is zero. In this case the divide method using a zero-valued Complex will produce the same result as dividing by a real-only zero. The output from dividing by imaginary zero will create infinite and NaN values in the same component parts as the output from this.divide(Complex.ZERO).multiplyImaginary(1), however the sign of some infinite values may be negated.

        Parameters:
        divisor - Value by which this complex number is to be divided.
        Returns:
        this / divisor.
        See Also:
        divide(Complex), divide(double)
      • exp

        public Complex exp()
        Returns the exponential function of this complex number.

        \[ \exp(z) = e^z \]

        The exponential function of \( z \) is an entire function in the complex plane. Special cases:

        • z.conj().exp() == z.exp().conj().
        • If z is ±0 + i0, returns 1 + i0.
        • If z is x + i∞ for finite x, returns NaN + iNaN ("invalid" floating-point operation).
        • If z is x + iNaN for finite x, returns NaN + iNaN ("invalid" floating-point operation).
        • If z is +∞ + i0, returns +∞ + i0.
        • If z is −∞ + iy for finite y, returns +0 cis(y) (see ofCis(double)).
        • If z is +∞ + iy for finite nonzero y, returns +∞ cis(y).
        • If z is −∞ + i∞, returns ±0 ± i0 (where the signs of the real and imaginary parts of the result are unspecified).
        • If z is +∞ + i∞, returns ±∞ + iNaN (where the sign of the real part of the result is unspecified; "invalid" floating-point operation).
        • If z is −∞ + iNaN, returns ±0 ± i0 (where the signs of the real and imaginary parts of the result are unspecified).
        • If z is +∞ + iNaN, returns ±∞ + iNaN (where the sign of the real part of the result is unspecified).
        • If z is NaN + i0, returns NaN + i0.
        • If z is NaN + iy for all nonzero numbers y, returns NaN + iNaN ("invalid" floating-point operation).
        • If z is NaN + iNaN, returns NaN + iNaN.

        Implements the formula:

        \[ \exp(x + iy) = e^x (\cos(y) + i \sin(y)) \]

        Returns:
        The exponential of this complex number.
        See Also:
        Exp
      • log

        public Complex log()
        Returns the natural logarithm of this complex number.

        The natural logarithm of \( z \) is unbounded along the real axis and in the range \( [-\pi, \pi] \) along the imaginary axis. The imaginary part of the natural logarithm has a branch cut along the negative real axis \( (-infty,0] \). Special cases:

        • z.conj().log() == z.log().conj().
        • If z is −0 + i0, returns −∞ + iπ ("divide-by-zero" floating-point operation).
        • If z is +0 + i0, returns −∞ + i0 ("divide-by-zero" floating-point operation).
        • If z is x + i∞ for finite x, returns +∞ + iπ/2.
        • If z is x + iNaN for finite x, returns NaN + iNaN ("invalid" floating-point operation).
        • If z is −∞ + iy for finite positive-signed y, returns +∞ + iπ.
        • If z is +∞ + iy for finite positive-signed y, returns +∞ + i0.
        • If z is −∞ + i∞, returns +∞ + i3π/4.
        • If z is +∞ + i∞, returns +∞ + iπ/4.
        • If z is ±∞ + iNaN, returns +∞ + iNaN.
        • If z is NaN + iy for finite y, returns NaN + iNaN ("invalid" floating-point operation).
        • If z is NaN + i∞, returns +∞ + iNaN.
        • If z is NaN + iNaN, returns NaN + iNaN.

        Implements the formula:

        \[ \ln(z) = \ln |z| + i \arg(z) \]

        where \( |z| \) is the absolute and \( \arg(z) \) is the argument.

        The implementation is based on the method described in:

        T E Hull, Thomas F Fairgrieve and Ping Tak Peter Tang (1994) Implementing complex elementary functions using exception handling. ACM Transactions on Mathematical Software, Vol 20, No 2, pp 215-244.
        Returns:
        The natural logarithm of this complex number.
        See Also:
        Math.log(double), abs(), arg(), Log
      • log10

        public Complex log10()
        Returns the base 10 common logarithm of this complex number.

        The common logarithm of \( z \) is unbounded along the real axis and in the range \( [-\pi, \pi] \) along the imaginary axis. The imaginary part of the common logarithm has a branch cut along the negative real axis \( (-infty,0] \). Special cases are as defined in the natural logarithm:

        Implements the formula:

        \[ \log_{10}(z) = \log_{10} |z| + i \arg(z) \]

        where \( |z| \) is the absolute and \( \arg(z) \) is the argument.

        Returns:
        The base 10 logarithm of this complex number.
        See Also:
        Math.log10(double), abs(), arg()
      • pow

        public Complex pow​(Complex x)
        Returns the complex power of this complex number raised to the power of x. Implements the formula:

        \[ z^x = e^{x \ln(z)} \]

        If this complex number is zero then this method returns zero if x is positive in the real component and zero in the imaginary component; otherwise it returns NaN + iNaN.

        Parameters:
        x - The exponent to which this complex number is to be raised.
        Returns:
        This complex number raised to the power of x.
        See Also:
        log(), multiply(Complex), exp(), Complex exponentiation, Power
      • pow

        public Complex pow​(double x)
        Returns the complex power of this complex number raised to the power of x, with x interpreted as a real number. Implements the formula:

        \[ z^x = e^{x \ln(z)} \]

        If this complex number is zero then this method returns zero if x is positive; otherwise it returns NaN + iNaN.

        Parameters:
        x - The exponent to which this complex number is to be raised.
        Returns:
        This complex number raised to the power of x.
        See Also:
        log(), multiply(double), exp(), pow(Complex), Power
      • sqrt

        public Complex sqrt()
        Returns the square root of this complex number.

        \[ \sqrt{x + iy} = \frac{1}{2} \sqrt{2} \left( \sqrt{ \sqrt{x^2 + y^2} + x } + i\ \text{sgn}(y) \sqrt{ \sqrt{x^2 + y^2} - x } \right) \]

        The square root of \( z \) is in the range \( [0, +\infty) \) along the real axis and is unbounded along the imaginary axis. The imaginary part of the square root has a branch cut along the negative real axis \( (-infty,0) \). Special cases:

        • z.conj().sqrt() == z.sqrt().conj().
        • If z is ±0 + i0, returns +0 + i0.
        • If z is x + i∞ for all x (including NaN), returns +∞ + i∞.
        • If z is x + iNaN for finite x, returns NaN + iNaN ("invalid" floating-point operation).
        • If z is −∞ + iy for finite positive-signed y, returns +0 + i∞.
        • If z is +∞ + iy for finite positive-signed y, returns +∞ + i0.
        • If z is −∞ + iNaN, returns NaN ± i∞ (where the sign of the imaginary part of the result is unspecified).
        • If z is +∞ + iNaN, returns +∞ + iNaN.
        • If z is NaN + iy for finite y, returns NaN + iNaN ("invalid" floating-point operation).
        • If z is NaN + iNaN, returns NaN + iNaN.

        Implements the following algorithm to compute \( \sqrt{x + iy} \):

        1. Let \( t = \sqrt{2 (|x| + |x + iy|)} \)
        2. if \( x \geq 0 \) return \( \frac{t}{2} + i \frac{y}{t} \)
        3. else return \( \frac{|y|}{t} + i\ \text{sgn}(y) \frac{t}{2} \)
        where:
        • \( |x| =\ \)abs(x)
        • \( |x + y i| =\ \)abs()
        • \( \text{sgn}(y) =\ \)copySign(1.0, y)

        The implementation is overflow and underflow safe based on the method described in:

        T E Hull, Thomas F Fairgrieve and Ping Tak Peter Tang (1994) Implementing complex elementary functions using exception handling. ACM Transactions on Mathematical Software, Vol 20, No 2, pp 215-244.
        Returns:
        The square root of this complex number.
        See Also:
        Sqrt
      • sin

        public Complex sin()
        Returns the sine of this complex number.

        \[ \sin(z) = \frac{1}{2} i \left( e^{-iz} - e^{iz} \right) \]

        This is an odd function: \( \sin(z) = -\sin(-z) \). The sine is an entire function and requires no branch cuts.

        This is implemented using real \( x \) and imaginary \( y \) parts:

        \[ \sin(x + iy) = \sin(x)\cosh(y) + i \cos(x)\sinh(y) \]

        As per the C99 standard this function is computed using the trigonomic identity:

        \[ \sin(z) = -i \sinh(iz) \]

        Returns:
        The sine of this complex number.
        See Also:
        Sin
      • cos

        public Complex cos()
        Returns the cosine of this complex number.

        \[ \cos(z) = \frac{1}{2} \left( e^{iz} + e^{-iz} \right) \]

        This is an even function: \( \cos(z) = \cos(-z) \). The cosine is an entire function and requires no branch cuts.

        This is implemented using real \( x \) and imaginary \( y \) parts:

        \[ \cos(x + iy) = \cos(x)\cosh(y) - i \sin(x)\sinh(y) \]

        As per the C99 standard this function is computed using the trigonomic identity:

        \[ cos(z) = cosh(iz) \]

        Returns:
        The cosine of this complex number.
        See Also:
        Cos
      • tan

        public Complex tan()
        Returns the tangent of this complex number.

        \[ \tan(z) = \frac{i(e^{-iz} - e^{iz})}{e^{-iz} + e^{iz}} \]

        This is an odd function: \( \tan(z) = -\tan(-z) \). The tangent is an entire function and requires no branch cuts.

        This is implemented using real \( x \) and imaginary \( y \) parts:

        \[ \tan(x + iy) = \frac{\sin(2x)}{\cos(2x)+\cosh(2y)} + i \frac{\sinh(2y)}{\cos(2x)+\cosh(2y)} \]

        As per the C99 standard this function is computed using the trigonomic identity:

        \[ \tan(z) = -i \tanh(iz) \]
        Returns:
        The tangent of this complex number.
        See Also:
        Tangent
      • asin

        public Complex asin()
        Returns the inverse sine of this complex number.

        \[ \sin^{-1}(z) = - i \left(\ln{iz + \sqrt{1 - z^2}}\right) \]

        The inverse sine of \( z \) is unbounded along the imaginary axis and in the range \( [-\pi, \pi] \) along the real axis. Special cases are handled as if the operation is implemented using \( \sin^{-1}(z) = -i \sinh^{-1}(iz) \).

        The inverse sine is a multivalued function and requires a branch cut in the complex plane; the cut is conventionally placed at the line segments \( (\infty,-1) \) and \( (1,\infty) \) of the real axis.

        This is implemented using real \( x \) and imaginary \( y \) parts:

        \[ \begin{aligned} \sin^{-1}(z) &= \sin^{-1}(B) + i\ \text{sgn}(y)\ln \left(A + \sqrt{A^2-1} \right) \\ A &= \frac{1}{2} \left[ \sqrt{(x+1)^2+y^2} + \sqrt{(x-1)^2+y^2} \right] \\ B &= \frac{1}{2} \left[ \sqrt{(x+1)^2+y^2} - \sqrt{(x-1)^2+y^2} \right] \end{aligned} \]

        where \( \text{sgn}(y) \) is the sign function implemented using copySign(1.0, y).

        The implementation is based on the method described in:

        T E Hull, Thomas F Fairgrieve and Ping Tak Peter Tang (1997) Implementing the complex Arcsine and Arccosine Functions using Exception Handling. ACM Transactions on Mathematical Software, Vol 23, No 3, pp 299-335.

        The code has been adapted from the Boost c++ implementation <boost/math/complex/asin.hpp>.

        Returns:
        The inverse sine of this complex number.
        See Also:
        ArcSin
      • acos

        public Complex acos()
        Returns the inverse cosine of this complex number.

        \[ \cos^{-1}(z) = \frac{\pi}{2} + i \left(\ln{iz + \sqrt{1 - z^2}}\right) \]

        The inverse cosine of \( z \) is in the range \( [0, \pi) \) along the real axis and unbounded along the imaginary axis. Special cases:

        • z.conj().acos() == z.acos().conj().
        • If z is ±0 + i0, returns π/2 − i0.
        • If z is ±0 + iNaN, returns π/2 + iNaN.
        • If z is x + i∞ for finite x, returns π/2 − i∞.
        • If z is x + iNaN, returns NaN + iNaN ("invalid" floating-point operation).
        • If z is −∞ + iy for positive-signed finite y, returns π − i∞.
        • If z is +∞ + iy for positive-signed finite y, returns +0 − i∞.
        • If z is −∞ + i∞, returns 3π/4 − i∞.
        • If z is +∞ + i∞, returns π/4 − i∞.
        • If z is ±∞ + iNaN, returns NaN ± i∞ where the sign of the imaginary part of the result is unspecified.
        • If z is NaN + iy for finite y, returns NaN + iNaN ("invalid" floating-point operation).
        • If z is NaN + i∞, returns NaN − i∞.
        • If z is NaN + iNaN, returns NaN + iNaN.

        The inverse cosine is a multivalued function and requires a branch cut in the complex plane; the cut is conventionally placed at the line segments \( (-\infty,-1) \) and \( (1,\infty) \) of the real axis.

        This function is implemented using real \( x \) and imaginary \( y \) parts:

        \[ \begin{aligned} \cos^{-1}(z) &= \cos^{-1}(B) - i\ \text{sgn}(y) \ln\left(A + \sqrt{A^2-1}\right) \\ A &= \frac{1}{2} \left[ \sqrt{(x+1)^2+y^2} + \sqrt{(x-1)^2+y^2} \right] \\ B &= \frac{1}{2} \left[ \sqrt{(x+1)^2+y^2} - \sqrt{(x-1)^2+y^2} \right] \end{aligned} \]

        where \( \text{sgn}(y) \) is the sign function implemented using copySign(1.0, y).

        The implementation is based on the method described in:

        T E Hull, Thomas F Fairgrieve and Ping Tak Peter Tang (1997) Implementing the complex Arcsine and Arccosine Functions using Exception Handling. ACM Transactions on Mathematical Software, Vol 23, No 3, pp 299-335.

        The code has been adapted from the Boost c++ implementation <boost/math/complex/acos.hpp>.

        Returns:
        The inverse cosine of this complex number.
        See Also:
        ArcCos
      • atan

        public Complex atan()
        Returns the inverse tangent of this complex number.

        \[ \tan^{-1}(z) = \frac{i}{2} \ln \left( \frac{i + z}{i - z} \right) \]

        The inverse hyperbolic tangent of \( z \) is unbounded along the imaginary axis and in the range \( [-\pi/2, \pi/2] \) along the real axis.

        The inverse tangent is a multivalued function and requires a branch cut in the complex plane; the cut is conventionally placed at the line segments \( (i \infty,-i] \) and \( [i,i \infty) \) of the imaginary axis.

        As per the C99 standard this function is computed using the trigonomic identity: \[ \tan^{-1}(z) = -i \tanh^{-1}(iz) \]

        Returns:
        The inverse tangent of this complex number.
        See Also:
        ArcTan
      • sinh

        public Complex sinh()
        Returns the hyperbolic sine of this complex number.

        \[ \sinh(z) = \frac{1}{2} \left( e^{z} - e^{-z} \right) \]

        The hyperbolic sine of \( z \) is an entire function in the complex plane and is periodic with respect to the imaginary component with period \( 2\pi i \). Special cases:

        • z.conj().sinh() == z.sinh().conj().
        • This is an odd function: \( \sinh(z) = -\sinh(-z) \).
        • If z is +0 + i0, returns +0 + i0.
        • If z is +0 + i∞, returns ±0 + iNaN (where the sign of the real part of the result is unspecified; "invalid" floating-point operation).
        • If z is +0 + iNaN, returns ±0 + iNaN (where the sign of the real part of the result is unspecified).
        • If z is x + i∞ for positive finite x, returns NaN + iNaN ("invalid" floating-point operation).
        • If z is x + iNaN for finite nonzero x, returns NaN + iNaN ("invalid" floating-point operation).
        • If z is +∞ + i0, returns +∞ + i0.
        • If z is +∞ + iy for positive finite y, returns +∞ cis(y) (see ofCis(double).
        • If z is +∞ + i∞, returns ±∞ + iNaN (where the sign of the real part of the result is unspecified; "invalid" floating-point operation).
        • If z is +∞ + iNaN, returns ±∞ + iNaN (where the sign of the real part of the result is unspecified).
        • If z is NaN + i0, returns NaN + i0.
        • If z is NaN + iy for all nonzero numbers y, returns NaN + iNaN ("invalid" floating-point operation).
        • If z is NaN + iNaN, returns NaN + iNaN.

        This is implemented using real \( x \) and imaginary \( y \) parts:

        \[ \sinh(x + iy) = \sinh(x)\cos(y) + i \cosh(x)\sin(y) \]

        Returns:
        The hyperbolic sine of this complex number.
        See Also:
        Sinh
      • cosh

        public Complex cosh()
        Returns the hyperbolic cosine of this complex number.

        \[ \cosh(z) = \frac{1}{2} \left( e^{z} + e^{-z} \right) \]

        The hyperbolic cosine of \( z \) is an entire function in the complex plane and is periodic with respect to the imaginary component with period \( 2\pi i \). Special cases:

        • z.conj().cosh() == z.cosh().conj().
        • This is an even function: \( \cosh(z) = \cosh(-z) \).
        • If z is +0 + i0, returns 1 + i0.
        • If z is +0 + i∞, returns NaN ± i0 (where the sign of the imaginary part of the result is unspecified; "invalid" floating-point operation).
        • If z is +0 + iNaN, returns NaN ± i0 (where the sign of the imaginary part of the result is unspecified).
        • If z is x + i∞ for finite nonzero x, returns NaN + iNaN ("invalid" floating-point operation).
        • If z is x + iNaN for finite nonzero x, returns NaN + iNaN ("invalid" floating-point operation).
        • If z is +∞ + i0, returns +∞ + i0.
        • If z is +∞ + iy for finite nonzero y, returns +∞ cis(y) (see ofCis(double)).
        • If z is +∞ + i∞, returns ±∞ + iNaN (where the sign of the real part of the result is unspecified).
        • If z is +∞ + iNaN, returns +∞ + iNaN.
        • If z is NaN + i0, returns NaN ± i0 (where the sign of the imaginary part of the result is unspecified).
        • If z is NaN + iy for all nonzero numbers y, returns NaN + iNaN ("invalid" floating-point operation).
        • If z is NaN + iNaN, returns NaN + iNaN.

        This is implemented using real \( x \) and imaginary \( y \) parts:

        \[ \cosh(x + iy) = \cosh(x)\cos(y) + i \sinh(x)\sin(y) \]

        Returns:
        The hyperbolic cosine of this complex number.
        See Also:
        Cosh
      • tanh

        public Complex tanh()
        Returns the hyperbolic tangent of this complex number.

        \[ \tanh(z) = \frac{e^z - e^{-z}}{e^z + e^{-z}} \]

        The hyperbolic tangent of \( z \) is an entire function in the complex plane and is periodic with respect to the imaginary component with period \( \pi i \) and has poles of the first order along the imaginary line, at coordinates \( (0, \pi(\frac{1}{2} + n)) \). Note that the double floating-point representation is unable to exactly represent \( \pi/2 \) and there is no value for which a pole error occurs. Special cases:

        • z.conj().tanh() == z.tanh().conj().
        • This is an odd function: \( \tanh(z) = -\tanh(-z) \).
        • If z is +0 + i0, returns +0 + i0.
        • If z is 0 + i∞, returns 0 + iNaN.
        • If z is x + i∞ for finite non-zero x, returns NaN + iNaN ("invalid" floating-point operation).
        • If z is 0 + iNaN, returns 0 + iNAN.
        • If z is x + iNaN for finite non-zero x, returns NaN + iNaN ("invalid" floating-point operation).
        • If z is +∞ + iy for positive-signed finite y, returns 1 + i0 sin(2y).
        • If z is +∞ + i∞, returns 1 ± i0 (where the sign of the imaginary part of the result is unspecified).
        • If z is +∞ + iNaN, returns 1 ± i0 (where the sign of the imaginary part of the result is unspecified).
        • If z is NaN + i0, returns NaN + i0.
        • If z is NaN + iy for all nonzero numbers y, returns NaN + iNaN ("invalid" floating-point operation).
        • If z is NaN + iNaN, returns NaN + iNaN.

        Special cases include the technical corrigendum DR 471: Complex math functions cacosh and ctanh.

        This is defined using real \( x \) and imaginary \( y \) parts:

        \[ \tan(x + iy) = \frac{\sinh(2x)}{\cosh(2x)+\cos(2y)} + i \frac{\sin(2y)}{\cosh(2x)+\cos(2y)} \]

        The implementation uses double-angle identities to avoid overflow of 2x and 2y.

        Returns:
        The hyperbolic tangent of this complex number.
        See Also:
        Tanh
      • asinh

        public Complex asinh()
        Returns the inverse hyperbolic sine of this complex number.

        \[ \sinh^{-1}(z) = \ln \left(z + \sqrt{1 + z^2} \right) \]

        The inverse hyperbolic sine of \( z \) is unbounded along the real axis and in the range \( [-\pi, \pi] \) along the imaginary axis. Special cases:

        • z.conj().asinh() == z.asinh().conj().
        • This is an odd function: \( \sinh^{-1}(z) = -\sinh^{-1}(-z) \).
        • If z is +0 + i0, returns 0 + i0.
        • If z is x + i∞ for positive-signed finite x, returns +∞ + iπ/2.
        • If z is x + iNaN for finite x, returns NaN + iNaN ("invalid" floating-point operation).
        • If z is +∞ + iy for positive-signed finite y, returns +∞ + i0.
        • If z is +∞ + i∞, returns +∞ + iπ/4.
        • If z is +∞ + iNaN, returns +∞ + iNaN.
        • If z is NaN + i0, returns NaN + i0.
        • If z is NaN + iy for finite nonzero y, returns NaN + iNaN ("invalid" floating-point operation).
        • If z is NaN + i∞, returns ±∞ + iNaN (where the sign of the real part of the result is unspecified).
        • If z is NaN + iNaN, returns NaN + iNaN.

        The inverse hyperbolic sine is a multivalued function and requires a branch cut in the complex plane; the cut is conventionally placed at the line segments \( (-i \infty,-i) \) and \( (i,i \infty) \) of the imaginary axis.

        This function is computed using the trigonomic identity:

        \[ \sinh^{-1}(z) = -i \sin^{-1}(iz) \]

        Returns:
        The inverse hyperbolic sine of this complex number.
        See Also:
        ArcSinh
      • acosh

        public Complex acosh()
        Returns the inverse hyperbolic cosine of this complex number.

        \[ \cosh^{-1}(z) = \ln \left(z + \sqrt{z + 1} \sqrt{z - 1} \right) \]

        The inverse hyperbolic cosine of \( z \) is in the range \( [0, \infty) \) along the real axis and in the range \( [-\pi, \pi] \) along the imaginary axis. Special cases:

        • z.conj().acosh() == z.acosh().conj().
        • If z is ±0 + i0, returns +0 + iπ/2.
        • If z is x + i∞ for finite x, returns +∞ + iπ/2.
        • If z is 0 + iNaN, returns NaN + iπ/2 [1].
        • If z is x + iNaN for finite non-zero x, returns NaN + iNaN ("invalid" floating-point operation).
        • If z is −∞ + iy for positive-signed finite y, returns +∞ + iπ.
        • If z is +∞ + iy for positive-signed finite y, returns +∞ + i0.
        • If z is −∞ + i∞, returns +∞ + i3π/4.
        • If z is +∞ + i∞, returns +∞ + iπ/4.
        • If z is ±∞ + iNaN, returns +∞ + iNaN.
        • If z is NaN + iy for finite y, returns NaN + iNaN ("invalid" floating-point operation).
        • If z is NaN + i∞, returns +∞ + iNaN.
        • If z is NaN + iNaN, returns NaN + iNaN.

        Special cases include the technical corrigendum DR 471: Complex math functions cacosh and ctanh.

        The inverse hyperbolic cosine is a multivalued function and requires a branch cut in the complex plane; the cut is conventionally placed at the line segment \( (-\infty,-1) \) of the real axis.

        This function is computed using the trigonomic identity:

        \[ \cosh^{-1}(z) = \pm i \cos^{-1}(z) \]

        The sign of the multiplier is chosen to give z.acosh().real() >= 0 and compatibility with the C99 standard.

        Returns:
        The inverse hyperbolic cosine of this complex number.
        See Also:
        ArcCosh
      • atanh

        public Complex atanh()
        Returns the inverse hyperbolic tangent of this complex number.

        \[ \tanh^{-1}(z) = \frac{1}{2} \ln \left( \frac{1 + z}{1 - z} \right) \]

        The inverse hyperbolic tangent of \( z \) is unbounded along the real axis and in the range \( [-\pi/2, \pi/2] \) along the imaginary axis. Special cases:

        • z.conj().atanh() == z.atanh().conj().
        • This is an odd function: \( \tanh^{-1}(z) = -\tanh^{-1}(-z) \).
        • If z is +0 + i0, returns +0 + i0.
        • If z is +0 + iNaN, returns +0 + iNaN.
        • If z is +1 + i0, returns +∞ + i0 ("divide-by-zero" floating-point operation).
        • If z is x + i∞ for finite positive-signed x, returns +0 + iπ/2.
        • If z is x+iNaN for nonzero finite x, returns NaN+iNaN ("invalid" floating-point operation).
        • If z is +∞ + iy for finite positive-signed y, returns +0 + iπ/2.
        • If z is +∞ + i∞, returns +0 + iπ/2.
        • If z is +∞ + iNaN, returns +0 + iNaN.
        • If z is NaN+iy for finite y, returns NaN+iNaN ("invalid" floating-point operation).
        • If z is NaN + i∞, returns ±0 + iπ/2 (where the sign of the real part of the result is unspecified).
        • If z is NaN + iNaN, returns NaN + iNaN.

        The inverse hyperbolic tangent is a multivalued function and requires a branch cut in the complex plane; the cut is conventionally placed at the line segments \( (\infty,-1] \) and \( [1,\infty) \) of the real axis.

        This is implemented using real \( x \) and imaginary \( y \) parts:

        \[ \tanh^{-1}(z) = \frac{1}{4} \ln \left(1 + \frac{4x}{(1-x)^2+y^2} \right) + \\ i \frac{1}{2} \left( \tan^{-1} \left(\frac{2y}{1-x^2-y^2} \right) + \frac{\pi}{2} \left(\text{sgn}(x^2+y^2-1)+1 \right) \text{sgn}(y) \right) \]

        The imaginary part is computed using Math.atan2(double, double) to ensure the correct quadrant is returned from \( \tan^{-1} \left(\frac{2y}{1-x^2-y^2} \right) \).

        The code has been adapted from the Boost c++ implementation <boost/math/complex/atanh.hpp>.

        Returns:
        The inverse hyperbolic tangent of this complex number.
        See Also:
        ArcTanh
      • nthRoot

        public List<ComplexnthRoot​(int n)
        Returns the n-th roots of this complex number. The nth roots are defined by the formula:

        \[ z_k = |z|^{\frac{1}{n}} \left( \cos \left(\phi + \frac{2\pi k}{n} \right) + i \sin \left(\phi + \frac{2\pi k}{n} \right) \right) \]

        for \( k=0, 1, \ldots, n-1 \), where \( |z| \) and \( \phi \) are respectively the modulus and argument of this complex number.

        If one or both parts of this complex number is NaN, a list with all all elements set to NaN + i NaN is returned.

        Parameters:
        n - Degree of root.
        Returns:
        A list of all n-th roots of this complex number.
        Throws:
        IllegalArgumentException - if n is zero.
        See Also:
        Root
      • equals

        public boolean equals​(Object other)
        Test for equality with another object. If the other object is a Complex then a comparison is made of the real and imaginary parts; otherwise false is returned.

        If both the real and imaginary parts of two complex numbers are exactly the same the two Complex objects are considered to be equal. For this purpose, two double values are considered to be the same if and only if the method #doubleToLongBits(double) returns the identical long value when applied to each.

        Note that in most cases, for two instances of class Complex, c1 and c2, the value of c1.equals(c2) is true if and only if

          c1.getReal() == c2.getReal() && c1.getImaginary() == c2.getImaginary()

        also has the value true. However, there are exceptions:

        • Instances that contain NaN values in the same part are considered to be equal for that part, even though Double.NaN == Double.NaN has the value false.
        • Instances that share a NaN value in one part but have different values in the other part are not considered equal.
        • Instances that contain different representations of zero in the same part are not considered to be equal for that part, even though -0.0 == 0.0 has the value true.

        The behavior is the same as if the components of the two complex numbers were passed to Arrays.equals(double[], double[]):

          Arrays.equals(new double[]{c1.getReal(), c1.getImaginary()},
                        new double[]{c2.getReal(), c2.getImaginary()}); 
        Overrides:
        equals in class Object
        Parameters:
        other - Object to test for equality with this instance.
        Returns:
        true if the objects are equal, false if object is null, not an instance of Complex, or not equal to this instance.
        See Also:
        Double.doubleToLongBits(double), Arrays.equals(double[], double[])
      • hashCode

        public int hashCode()
        Gets a hash code for the complex number.

        The behavior is the same as if the components of the complex number were passed to Arrays.hashCode(double[]):

          Arrays.hashCode(new double[] {getReal(), getImaginary()})
        Overrides:
        hashCode in class Object
        Returns:
        A hash code value for this object.
        See Also:
        Arrays.hashCode(double[])
      • toString

        public String toString()
        Returns a string representation of the complex number.

        The string will represent the numeric values of the real and imaginary parts. The values are split by a separator and surrounded by parentheses. The string can be parsed to obtain an instance with the same value.

        The format for complex number \( x + i y \) is "(x,y)", with \( x \) and \( y \) converted as if using Double.toString(double).

        Overrides:
        toString in class Object
        Returns:
        A string representation of the complex number.
        See Also:
        parse(String), Double.toString(double)