Class Complex

java.lang.Object
org.djutils.complex.Complex

public class Complex extends Object
Complex.java. Immutable complex numbers. This implementation stores the real and imaginary component as a double value. These fields are directly accessible, or through a getter. There are also getters for the norm and phi, but this are a CPU intensive.

Copyright (c) 2021-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
BSD-style license. See DJUTILS License.

Author:
Alexander Verbraeck, Peter Knoppers
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Complex
    The imaginary unit value (i).
    final double
    Imaginary component.
    static final Complex
    The negative imaginary unit value (i).
    static final Complex
    The (real) minus one value in the complex number space.
    static final Complex
    The (real) one value in the complex number space.
    final double
    Real component.
    static final Complex
    The zero value in the complex number space.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Complex(double re)
    Construct a new complex number with specified real component and zero imaginary component.
    Complex(double re, double im)
    Construct a new complex number.
  • Method Summary

    Modifier and Type
    Method
    Description
    Construct the complex conjugate of this Complex.
    divideBy(double rightOperand)
    Divide this Complex by a scalar.
    divideBy(Complex rightOperand)
    Divide this Complex by another Complex.
    boolean
     
    double
    Retrieve the imaginary component of this complex number.
    double
    Retrieve the real component of this complex number.
    int
     
    static double
    hypot(double x, double y)
    Better implementation of the hypotenuse function (faster and more accurate than the one in the java Math library).
    boolean
    Determine if this Complex has a real component of zero.
    boolean
    Determine if this Complex has an imaginary component of zero.
    minus(double rightOperand)
    Subtract a scalar from this Complex.
    minus(Complex rightOperand)
    Subtract another Complex from this Complex.
    double
    Compute and return the norm, or radius, or absolute value of this complex number.
    double
    phi()
    Compute and return the phase or phi of this complex number.
    plus(double rightOperand)
    Add a scalar to this Complex.
    plus(Complex rightOperand)
    Add this Complex and another Complex.
    Compute the reciprocal of this Complex.
    rotate(double angle)
    Rotate this Complex by an angle.
    times(double rightOperand)
    Multiply this Complex with a scalar.
    times(Complex rightOperand)
    Multiply this Complex with another Complex.
     

    Methods inherited from class java.lang.Object

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

    • re

      public final double re
      Real component.
    • im

      public final double im
      Imaginary component.
    • ZERO

      public static final Complex ZERO
      The zero value in the complex number space.
    • ONE

      public static final Complex ONE
      The (real) one value in the complex number space.
    • MINUS_ONE

      public static final Complex MINUS_ONE
      The (real) minus one value in the complex number space.
    • I

      public static final Complex I
      The imaginary unit value (i).
    • MINUS_I

      public static final Complex MINUS_I
      The negative imaginary unit value (i).
  • Constructor Details

    • Complex

      public Complex(double re, double im)
      Construct a new complex number.
      Parameters:
      re - double; real component of the new complex number
      im - double; imaginary component of the new complex number
    • Complex

      public Complex(double re)
      Construct a new complex number with specified real component and zero imaginary component.
      Parameters:
      re - double; the real component of the new complex number
  • Method Details

    • getRe

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

      public double getIm()
      Retrieve the imaginary component of this complex number.
      Returns:
      double; the imaginary component of this complex number
    • norm

      public double norm()
      Compute and return the norm, or radius, or absolute value of this complex number.
      Returns:
      double; the norm, or radius, or absolute value of this complex number. Due to the fact that in this implementation of complex numbers, all values are stored as a real an imaginary double value; the result may overflow to Double.POSITIVE_INFINITY, even though both the real and imaginary part of the complex number can be represented.
    • hypot

      public static double hypot(double x, double y)
      Better implementation of the hypotenuse function (faster and more accurate than the one in the java Math library).
      Derived from An improved algorithm for hypot(a, b) by Carlos F. Borges.
      Parameters:
      x - double; the x value
      y - double; the y value
      Returns:
      double; hypot(x, y)
    • phi

      public double phi()
      Compute and return the phase or phi of this complex number.
      Returns:
      double; the phase or phi of this complex number in Radians. Due to the fact that in this implementation of complex numbers, all values are stored as a real and imaginary value; the result of this method is always normalized to the interval (-π,π].
    • isReal

      public boolean isReal()
      Determine if this Complex has an imaginary component of zero.
      Returns:
      boolean; true if the imaginary component of this Complex number is 0.0; false if the imaginary component of this Complex number is not 0.0.
    • isImaginary

      public boolean isImaginary()
      Determine if this Complex has a real component of zero.
      Returns:
      boolean; true if the real component of this Complex number is 0.0; false if the real component of this Complex number is not 0.0
    • conjugate

      public Complex conjugate()
      Construct the complex conjugate of this Complex.
      Returns:
      Complex; the complex conjugate of this
    • rotate

      public Complex rotate(double angle)
      Rotate this Complex by an angle.
      Parameters:
      angle - double; the angle (in Radians)
      Returns:
      complex; the result of the rotation
    • plus

      public Complex plus(Complex rightOperand)
      Add this Complex and another Complex.
      Parameters:
      rightOperand - Complex; the other Complex
      Returns:
      Complex; the sum of this Complex and the other Complex
    • plus

      public Complex plus(double rightOperand)
      Add a scalar to this Complex.
      Parameters:
      rightOperand - double; the scalar
      Returns:
      Complex; the sum of this Complex and the scalar
    • minus

      public Complex minus(Complex rightOperand)
      Subtract another Complex from this Complex.
      Parameters:
      rightOperand - Complex; the other Complex
      Returns:
      Complex; the difference of this Complex and the other Complex
    • minus

      public Complex minus(double rightOperand)
      Subtract a scalar from this Complex.
      Parameters:
      rightOperand - double; the scalar
      Returns:
      Complex; the difference of this Complex and the scalar
    • times

      public Complex times(Complex rightOperand)
      Multiply this Complex with another Complex.
      Parameters:
      rightOperand - Complex; the right hand side operand
      Returns:
      Complex; the product of this Complex and the other Complex
    • times

      public Complex times(double rightOperand)
      Multiply this Complex with a scalar.
      Parameters:
      rightOperand - double; the right hand side operand
      Returns:
      Complex; the product of this Complex and the scalar
    • reciprocal

      public Complex reciprocal()
      Compute the reciprocal of this Complex. If this is zero, the result will have the re field set to Double.POSITIVE_INFINITY and the imaginary part set to NEGATIVE_INFINITY.
      Returns:
      Complex; the reciprocal of this Complex (1 / this)
    • divideBy

      public Complex divideBy(Complex rightOperand)
      Divide this Complex by another Complex. Division by ZERO yields a Complex with re set to Infinity if this.re != 0 and NaN if this.re == 0 and im set to Infinity if this.im != 0 and NaN if this.im == 0.
      Parameters:
      rightOperand - Complex; the right hand side operand
      Returns:
      Complex; the ratio of this Complex and the right hand side operand
    • divideBy

      public Complex divideBy(double rightOperand)
      Divide this Complex by a scalar. Division by 0.0 yields a Complex with re set to Infinity if this.re != 0 and NaN if this.re == 0 and im set to Infinity if this.im != 0 and NaN if this.im == 0.
      Parameters:
      rightOperand - double; the scalar right hand side operand
      Returns:
      Complex; the ratio of this Complex and the right hand side operand
    • toString

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

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object