Module ojalgo

Class ComplexNumber

java.lang.Object
org.ojalgo.scalar.ComplexNumber
All Implemented Interfaces:
Comparable<ComplexNumber>, Field<Scalar<ComplexNumber>>, Group, Group.Additive<Scalar<ComplexNumber>>, Group.Multiplicative<Scalar<ComplexNumber>>, NormedVectorSpace<Scalar<ComplexNumber>,ComplexNumber>, Operation, Operation.Addition<Scalar<ComplexNumber>>, Operation.Division<Scalar<ComplexNumber>>, Operation.Multiplication<Scalar<ComplexNumber>>, Operation.Subtraction<Scalar<ComplexNumber>>, Ring<Scalar<ComplexNumber>>, ScalarOperation, ScalarOperation.Addition<Scalar<ComplexNumber>,ComplexNumber>, ScalarOperation.Division<Scalar<ComplexNumber>,ComplexNumber>, ScalarOperation.Multiplication<Scalar<ComplexNumber>,ComplexNumber>, ScalarOperation.Subtraction<Scalar<ComplexNumber>,ComplexNumber>, VectorSpace<Scalar<ComplexNumber>,ComplexNumber>, Scalar<ComplexNumber>, Access1D<Double>, Access2D<Double>, Access2D.Collectable<Double,Mutate2D>, AccessScalar<ComplexNumber>, Structure1D, Structure2D, Transformation2D<Double>, Tensor<ComplexNumber,Scalar<ComplexNumber>>, NumberContext.Enforceable<ComplexNumber>, NumberDefinition

public final class ComplexNumber extends Object implements Access2D<Double>, Transformation2D<Double>, Access2D.Collectable<Double,Mutate2D>
ComplexNumber is an immutable complex number class. It only implements the most basic complex number operations. ComplexFunction implements some of the more complicated ones.
Author:
apete
See Also:
  • Field Details

    • FACTORY

      public static final Scalar.Factory<ComplexNumber> FACTORY
    • I

      public static final ComplexNumber I
      Complex number i, Z = (0.0 + 1.0i), satisfies i2 = -1;
    • INFINITY

      public static final ComplexNumber INFINITY
      Complex number Z = (+∞ + 0.0i)
    • N

      public static final ComplexNumber N
      Complex number -i, Z = (0.0 - 1.0i)
    • NaN

      public static final ComplexNumber NaN
      Complex number Z = (NaN + NaNi)
    • NEG

      public static final ComplexNumber NEG
      Complex number Z = (-1.0 + 0.0i)
    • ONE

      public static final ComplexNumber ONE
      Complex number Z = (1.0 + 0.0i)
    • TWO

      public static final ComplexNumber TWO
      Complex number Z = (2.0 + 0.0i)
    • ZERO

      public static final ComplexNumber ZERO
      Complex number Z = (0.0 + 0.0i)
    • i

      public final double i
  • Constructor Details

    • ComplexNumber

      public ComplexNumber()
      Complex number constructor, returns ZERO
  • Method Details

    • isAbsolute

      public static boolean isAbsolute(ComplexNumber value)
    • isInfinite

      public static boolean isInfinite(ComplexNumber value)
      Test if value is infinite. A complex number is infinite if its real part and/or its imaginary part is infinite.
      Parameters:
      value - the complex number to test
      Returns:
      true if the specified value is infinite (real and/or imaginary part) otherwise false
    • isNaN

      public static boolean isNaN(ComplexNumber value)
      Test if value is NaN. A complex number is NaN if its real and/or its imaginary part is NaN.
      Parameters:
      value - the complex number to test
      Returns:
      true if the specified value is NaN (real and/or imaginary part) otherwise false
    • isReal

      public static boolean isReal(ComplexNumber value)
      Test if value is real. A complex number Z is real if and only if Im(Z) = 0.0.
      Parameters:
      value - the complex number to test
      Returns:
      true if the imaginary part of the specified value is null otherwise false
    • isSmall

      public static boolean isSmall(double comparedTo, ComplexNumber value)
    • makePolar

      public static ComplexNumber makePolar(double norm, double phase)
      Static factory method returning a complex number from polar coordinates
      Parameters:
      norm - the complex number's norm
      phase - the complex number's phase
      Returns:
      a complex number
    • makeRotation

      public static ComplexNumber makeRotation(double angle)
    • newUnitRoot

      public static ComplexNumber newUnitRoot(int nbRoots)
    • newUnitRoots

      public static ComplexNumber[] newUnitRoots(int nbRoots)
    • of

      public static ComplexNumber of(double real, double imaginary)
      Static factory method returning a complex number from cartesian coordinates.
      Parameters:
      real - the complex number's real part
      imaginary - the complex number's imaginary part
      Returns:
      a complex number
    • valueOf

      public static ComplexNumber valueOf(Comparable<?> number)
      Static factory method returning a complex number from arbitrary number
      Parameters:
      number - a numeric value
      Returns:
      ZERO if number is null otherwise the double value of number
    • valueOf

      public static ComplexNumber valueOf(double value)
      Static factory method returning a complex number from a real value
      Parameters:
      value - the complex number's real part
      Returns:
      a complex number Z = (value + 0.0i)
    • add

      public ComplexNumber add(ComplexNumber arg)
      Performs the binary operation '+' with a complex number.
      Specified by:
      add in interface ScalarOperation.Addition<Scalar<ComplexNumber>,ComplexNumber>
      Parameters:
      arg - the complex number to add
      Returns:
      a complex number Z = ((Re(this) + Re(arg)) + (Im(this) + Im(arg))i)
    • add

      public ComplexNumber add(double arg)
      Performs the binary operation '+' with a real number
      Specified by:
      add in interface ScalarOperation.Addition<Scalar<ComplexNumber>,ComplexNumber>
      Parameters:
      arg - the real number to add
      Returns:
      a complex number Z = ((Re(this) + arg) + Im(this)i)
    • compareTo

      public int compareTo(ComplexNumber other)
      First compares the real values. Only if they are equal will compare the imaginary part.
      Specified by:
      compareTo in interface Comparable<ComplexNumber>
    • conjugate

      public ComplexNumber conjugate()
      Returns the conjugate of this complex number. A complex number conjugate is its reflexion about the real axis.
      Specified by:
      conjugate in interface VectorSpace<Scalar<ComplexNumber>,ComplexNumber>
      Returns:
      a complex number Z = (Re(this) - Im(this)i)
    • count

      public long count()
      Description copied from interface: Structure2D
      count() == countRows() * countColumns()
      Specified by:
      count in interface Structure1D
      Specified by:
      count in interface Structure2D
    • countColumns

      public long countColumns()
      Description copied from interface: Structure2D
      Only need to implement if the structure may contain more than Integer.MAX_VALUE elements.
      Specified by:
      countColumns in interface Structure2D
      Returns:
      The number of columns
    • countRows

      public long countRows()
      Description copied from interface: Structure2D
      Only need to implement if the structure may contain more than Integer.MAX_VALUE elements.
      Specified by:
      countRows in interface Structure2D
      Returns:
      The number of rows
    • divide

      public ComplexNumber divide(ComplexNumber arg)
      Performs the binary operation '/' with a complex number.
      Specified by:
      divide in interface ScalarOperation.Division<Scalar<ComplexNumber>,ComplexNumber>
      Parameters:
      arg - the complex number to divide by
      Returns:
      a complex number Z = this / arg
    • divide

      public ComplexNumber divide(double arg)
      Performs the binary operation '/' with a real number.
      Specified by:
      divide in interface ScalarOperation.Division<Scalar<ComplexNumber>,ComplexNumber>
      Parameters:
      arg - the real number to divide by
      Returns:
      a complex number Z = ((Re(this) / arg) + (Im(this) / arg)i)
    • doubleValue

      public double doubleValue()
      Specified by:
      doubleValue in interface NumberDefinition
    • doubleValue

      public double doubleValue(int index)
      Specified by:
      doubleValue in interface Access1D<Double>
      Specified by:
      doubleValue in interface Access2D<Double>
    • doubleValue

      public double doubleValue(int row, int col)
      Description copied from interface: Access2D
      Extracts one element of this matrix as a double.
      Specified by:
      doubleValue in interface Access2D<Double>
      Parameters:
      row - A row index.
      col - A column index.
      Returns:
      One matrix element
    • enforce

      public ComplexNumber enforce(NumberContext context)
      Will call NumberContext.enforce(double) on the real and imaginary parts separately.
      Specified by:
      enforce in interface NumberContext.Enforceable<ComplexNumber>
    • equals

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

      public float floatValue()
      Specified by:
      floatValue in interface NumberDefinition
    • get

      public ComplexNumber get()
      Specified by:
      get in interface AccessScalar<ComplexNumber>
    • get

      public Double get(long index)
      Specified by:
      get in interface Access1D<Double>
      Specified by:
      get in interface Access2D<Double>
    • get

      public Double get(long row, long col)
      Specified by:
      get in interface Access2D<Double>
    • getArgument

      public double getArgument()
    • getColDim

      public int getColDim()
      Specified by:
      getColDim in interface Structure2D
      Returns:
      The number of columns
    • getImaginary

      public double getImaginary()
    • getModulus

      public double getModulus()
    • getReal

      public double getReal()
    • getRowDim

      public int getRowDim()
      Specified by:
      getRowDim in interface Structure2D
      Returns:
      The number of rows
    • hashCode

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

      public int intValue()
      Specified by:
      intValue in interface NumberDefinition
    • invert

      public ComplexNumber invert()
      Performs the unary operation '1/x'
      Specified by:
      invert in interface Group.Multiplicative<Scalar<ComplexNumber>>
      Returns:
      the complex number Z inverse of this, satisfies Z * this = 1
    • isAbsolute

      public boolean isAbsolute()
      Specified by:
      isAbsolute in interface Scalar<ComplexNumber>
      Returns:
      true if this is equal to its own norm, modulus or absolute value (non-negative real part and no imaginary part); otherwise false.
      See Also:
    • isReal

      public boolean isReal()
    • isSmall

      public boolean isSmall(double comparedTo)
      Specified by:
      isSmall in interface NormedVectorSpace<Scalar<ComplexNumber>,ComplexNumber>
      Parameters:
      comparedTo - What to compare with
      Returns:
      true if this is small compared to the magnitude of the input reference value.
    • isZero

      public boolean isZero()
      Description copied from interface: Scalar
      Tests if this scalar value is exactly zero.

      Each implementation should test for zero as exactly as possible based on what's achievable with that specific scalar type. The purpose is NOT to have similar behavior between different implementations, but rather to leverage the full precision and capabilities of each type.

      For example:

      • Primitive types (like double) should use exact equality comparison
      • High-precision types (like Quadruple) should check both base and remainder components
      • Arbitrary-precision types (like BigDecimal) should use their built-in zero detection
      • Complex types should check both real and imaginary parts

      This method should NOT use tolerance-based comparisons or approximate zero detection, as those are better handled by context-aware methods like NormedVectorSpace.isSmall(double).

      Specified by:
      isZero in interface Scalar<ComplexNumber>
      Returns:
      true if this scalar represents exactly zero, false otherwise
      See Also:
    • longValue

      public long longValue()
      Specified by:
      longValue in interface NumberDefinition
    • multiply

      public ComplexNumber multiply(ComplexNumber arg)
      Performs the binary operation '*' with a complex number.
      Specified by:
      multiply in interface ScalarOperation.Multiplication<Scalar<ComplexNumber>,ComplexNumber>
      Parameters:
      arg - the complex number to multiply by
      Returns:
      a complex number Z = this * arg
    • multiply

      public ComplexNumber multiply(double arg)
      Performs the binary operation '*' with a real number.
      Specified by:
      multiply in interface ScalarOperation.Multiplication<Scalar<ComplexNumber>,ComplexNumber>
      Parameters:
      arg - the real number to multiply by
      Returns:
      a complex number Z = ((Re(this) * arg) + Im(this) * arg))
    • multiplyIm

      public double multiplyIm(double argRe, double argIm)
      The imaginary part of the complex number resulting when multiplying this with a complex number whose real and imaginary parts are argRe and argIm.
    • multiplyRe

      public double multiplyRe(double argRe, double argIm)
      The real part of the complex number resulting when multiplying this with a complex number whose real and imaginary parts are argRe and argIm.
    • negate

      public ComplexNumber negate()
      Performs the unary operation '-'.
      Specified by:
      negate in interface Group.Additive<Scalar<ComplexNumber>>
      Returns:
      a complex number Z = -this
    • norm

      public double norm()
      Returns the norm of this complex number. The norm of a complex number is defined by |Z| = (ZZ*)1/2.
      Specified by:
      norm in interface NormedVectorSpace<Scalar<ComplexNumber>,ComplexNumber>
      Returns:
      the norm of this complex number.
    • phase

      public double phase()
      Returns the phase of this complex number. The phase of a complex number Z is the angle between the positive real axis and the straight line defined by origin and Z in complex plane.
      Returns:
      the phase of this complex number
    • power

      public ComplexNumber power(int power)
      Description copied from interface: Operation.Multiplication
      Multiply by itself power times.
      Specified by:
      power in interface Operation.Multiplication<Scalar<ComplexNumber>>
    • signum

      public ComplexNumber signum()
      Description copied from interface: NormedVectorSpace
      this == this.signum().multiply(this.norm())
      Specified by:
      signum in interface NormedVectorSpace<Scalar<ComplexNumber>,ComplexNumber>
      Returns:
      A unit "vector"
    • subtract

      public ComplexNumber subtract(ComplexNumber arg)
      Performs the binary operation '-' with a complex number.
      Specified by:
      subtract in interface ScalarOperation.Subtraction<Scalar<ComplexNumber>,ComplexNumber>
      Parameters:
      arg - the complex number to subtract
      Returns:
      a complex number Z = this - arg
    • subtract

      public ComplexNumber subtract(double arg)
      Performs the binary operation '-' with a real number.
      Specified by:
      subtract in interface ScalarOperation.Subtraction<Scalar<ComplexNumber>,ComplexNumber>
      Parameters:
      arg - the real number to subtract
      Returns:
      a complex number Z = ((Re(this) - arg) + Im(this)i)
    • supplyTo

      public void supplyTo(Mutate2D receiver)
      Specified by:
      supplyTo in interface Access2D.Collectable<Double,Mutate2D>
    • toBigDecimal

      public BigDecimal toBigDecimal()
      Specified by:
      toBigDecimal in interface Scalar<ComplexNumber>
    • toMultiplicationMatrix

      public MatrixStore<Double> toMultiplicationMatrix()
    • toMultiplicationVector

      public MatrixStore<Double> toMultiplicationVector()
    • toRotationMatrix

      public MatrixStore<Double> toRotationMatrix()
    • toString

      public String toString()
      Overrides:
      toString in class Object
      See Also:
    • toString

      public String toString(NumberContext context)
      Specified by:
      toString in interface Scalar<ComplexNumber>
    • transform

      public <T extends Mutate2D.ModifiableReceiver<Double>> void transform(T transformable)
      Specified by:
      transform in interface Transformation2D<Double>
    • add

      default ComplexNumber add(float scalarAddend)
      Specified by:
      add in interface ScalarOperation.Addition<Scalar<S extends org.ojalgo.scalar.SelfDeclaringScalar<S>>,S extends org.ojalgo.scalar.SelfDeclaringScalar<S>>
      Returns:
      this + scalarAddend.
    • divide

      default ComplexNumber divide(float scalarDivisor)
      Specified by:
      divide in interface ScalarOperation.Division<Scalar<S extends org.ojalgo.scalar.SelfDeclaringScalar<S>>,S extends org.ojalgo.scalar.SelfDeclaringScalar<S>>
      Returns:
      this / scalarDivisor.
    • multiply

      default ComplexNumber multiply(float scalarMultiplicand)
      Specified by:
      multiply in interface ScalarOperation.Multiplication<Scalar<S extends org.ojalgo.scalar.SelfDeclaringScalar<S>>,S extends org.ojalgo.scalar.SelfDeclaringScalar<S>>
      Returns:
      this * scalarMultiplicand.
    • subtract

      default ComplexNumber subtract(float scalarSubtrahend)
      Specified by:
      subtract in interface ScalarOperation.Subtraction<Scalar<S extends org.ojalgo.scalar.SelfDeclaringScalar<S>>,S extends org.ojalgo.scalar.SelfDeclaringScalar<S>>
      Returns:
      this - scalarSubtrahend.