Class Complex

java.lang.Object
imagingbook.common.math.Complex

public class Complex extends Object

This class represents complex numbers. All instances are immutable. Methods are mostly defined to be compatible with org.apache.commons.math3.complex.Complex and (newer) org.apache.commons.numbers.complex.Complex. Arithmetic operations are generally more precise than with the Apache implementation. See also Appendix Sec. A.5 of [1].

[1] W. Burger, M.J. Burge, Digital Image Processing – An Algorithmic Introduction, 3rd ed, Springer (2022).

Version:
2022/07/05
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Complex
    Constant - imaginary unit value (z = 0 + i 1).
    final double
    The imaginary part of this complex number (publicly accessible but read-only).
    static final Complex
    Constant - real unit value (z = 1 + i 0).
    final double
    The real part of this complex number (publicly accessible but read-only).
    static final Complex
    Constant - complex zero value (z = 0 + i 0).
  • Constructor Summary

    Constructors
    Constructor
    Description
    Complex(double phi)
    Constructor, creates a complex quantity on the unit circle with angle phi: e^(i * phi) = cos(phi) + i * sin(phi).
    Complex(double[] z)
    Constructor.
    Complex(double re, double im)
    Constructor.
    Constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    double
    abs()
    Returns the absolute value of this complex number, i.e., its radius or distance from the origin.
    double
    Returns the squared absolute value of this complex number, i.e., its squared radius or distance from the origin.
    Adds a complex quantity to this complex number and returns a new complex number.
    double
    arg()
    Returns the 'argument' of this complex number, i.e., its angle w.r.t.
    Returns the conjugate z* of this complex number, i.e, if z = a + i b then z* = a - i b.
    boolean
    equals(double re, double im)
    Checks if the given complex quantity is equal to this Complex quantity, using the default tolerance (Arithmetic.EPSILON_DOUBLE).
    boolean
    equals(double re, double im, double tolerance)
    Checks if the given complex quantity is equal to this Complex quantity, using the specified tolerance.
    boolean
    equals(Complex z, double tolerance)
    Checks if the given Complex quantity is equal to this Complex quantity.
    boolean
    equals(Object other)
    Checks if the given Object is equal to this Complex quantity.
    double
    Returns the imaginary part of this complex number.
    double
    Returns the real part of this complex number.
    boolean
    Returns true if the real or imaginary component of this complex number is NaN.
    multiply(double s)
    Multiplies this complex number with the scalar factor s and returns a new complex number.
    Multiplies this complex number with another complex quantity and returns a new complex number.
    pow(int k)
    Returns of value of this complex number (z) raised to the power k (integer).
    rotate(double phi)
    Rotates this complex number by angle phi and returns the resulting complex number.
    double[]
    Returns a 2-element array with the real and imaginary part of this complex number.
     

    Methods inherited from class java.lang.Object

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

    • ONE

      public static final Complex ONE
      Constant - real unit value (z = 1 + i 0).
    • ZERO

      public static final Complex ZERO
      Constant - complex zero value (z = 0 + i 0).
    • I

      public static final Complex I
      Constant - imaginary unit value (z = 0 + i 1).
    • re

      public final double re
      The real part of this complex number (publicly accessible but read-only).
    • im

      public final double im
      The imaginary part of this complex number (publicly accessible but read-only).
  • Constructor Details

    • Complex

      public Complex(double re, double im)
      Constructor.
      Parameters:
      re - real part
      im - imaginary part
    • Complex

      public Complex(double[] z)
      Constructor.
      Parameters:
      z - a two-element double array with real and imaginary part
    • Complex

      public Complex(Complex z)
      Constructor.
      Parameters:
      z - complex quantity, which is duplicated
    • Complex

      public Complex(double phi)
      Constructor, creates a complex quantity on the unit circle with angle phi: e^(i * phi) = cos(phi) + i * sin(phi).
      Parameters:
      phi - the angle
      See Also:
  • Method Details

    • abs

      public double abs()
      Returns the absolute value of this complex number, i.e., its radius or distance from the origin.
      Returns:
      the absolute value
    • abs2

      public double abs2()
      Returns the squared absolute value of this complex number, i.e., its squared radius or distance from the origin.
      Returns:
      the squared absolute value
    • arg

      public double arg()
      Returns the 'argument' of this complex number, i.e., its angle w.r.t. to the real axis.
      Returns:
      the argument (in radians)
    • conjugate

      public Complex conjugate()
      Returns the conjugate z* of this complex number, i.e, if z = a + i b then z* = a - i b.
      Returns:
      the complex conjugate
    • add

      public Complex add(Complex z)
      Adds a complex quantity to this complex number and returns a new complex number.
      Parameters:
      z - complex value
      Returns:
      the sum of this complex number and z
    • rotate

      public Complex rotate(double phi)
      Rotates this complex number by angle phi and returns the resulting complex number.
      Parameters:
      phi - the angle (in radians)
      Returns:
      the rotated complex value
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • isNaN

      public boolean isNaN()
      Returns true if the real or imaginary component of this complex number is NaN.
      Returns:
      true if NaN, otherwise false
    • getRe

      public double getRe()
      Returns the real part of this complex number.
      Returns:
      the real part
    • getIm

      public double getIm()
      Returns the imaginary part of this complex number.
      Returns:
      the imaginary part
    • multiply

      public Complex multiply(Complex z)
      Multiplies this complex number with another complex quantity and returns a new complex number.
      Parameters:
      z - a complex quantity
      Returns:
      this complex number multiplied by z
    • multiply

      public Complex multiply(double s)
      Multiplies this complex number with the scalar factor s and returns a new complex number.
      Parameters:
      s - a scalar factor
      Returns:
      this complex number multiplied by s
    • pow

      public Complex pow(int k)
      Returns of value of this complex number (z) raised to the power k (integer).
      Parameters:
      k - the integer exponent (≥ 0)
      Returns:
      z^k
    • toArray

      public double[] toArray()
      Returns a 2-element array with the real and imaginary part of this complex number.
      Returns:
      (re, im)
    • equals

      public boolean equals(Object other)
      Checks if the given Object is equal to this Complex quantity. Calls equals(Complex, double) if the argument is of type Complex, otherwise null is returned.
      Overrides:
      equals in class Object
    • equals

      public boolean equals(Complex z, double tolerance)
      Checks if the given Complex quantity is equal to this Complex quantity.
      Parameters:
      z - another Complex quantity
      tolerance - the maximum difference of real and imaginary parts
      Returns:
      true if the two complex quantities are sufficiently close, false otherwise
    • equals

      public boolean equals(double re, double im)
      Checks if the given complex quantity is equal to this Complex quantity, using the default tolerance (Arithmetic.EPSILON_DOUBLE).
      Parameters:
      re - real part of other complex quantity
      im - imaginary part of other complex quantity
      Returns:
      true if the two complex quantities are sufficiently close, false otherwise
    • equals

      public boolean equals(double re, double im, double tolerance)
      Checks if the given complex quantity is equal to this Complex quantity, using the specified tolerance.
      Parameters:
      re - real part of other complex quantity
      im - imaginary part of other complex quantity
      tolerance - the maximum difference of real and imaginary parts
      Returns:
      true if the two complex quantities are sufficiently close, false otherwise