Class Complex

java.lang.Object
com.irurueta.algebra.Complex
All Implemented Interfaces:
Serializable, Cloneable

public class Complex extends Object implements Serializable, Cloneable
Class defining a Complex number having real and imaginary parts.
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private double
    Imaginary part of the complex number.
    private double
    Real part of the complex number.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructor.
    Complex(double real)
    Creates new Complex number having provided real value and an imaginary part equal to zero.
    Complex(double real, double imaginary)
    Creates new Complex number having provided real and imaginary parts.
    Complex(Complex initValue)
    Creates new Complex number by copying provided value.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(Complex other)
    Adds provided complex into this instance
    void
    add(Complex other, Complex result)
    Adds this instance with provided complex value and stores the result in provided instance
    Adds this instance to provided complex and returns the result as a new instance.
    Makes a copy of this instance having the same real and imaginary parts.
    void
    Changes this instance into its complex conjugate.
    void
    Computes the complex conjugate of this instance and stores the result into provided complex instance.
    Computes the complex conjugate of this instance and returns the result as a new instance.
    void
    Copies provided value into current instance.
    void
    divide(Complex other)
    Divides this instance by provided complex.
    void
    divide(Complex other, Complex result)
    Divides this instance by provided instance and stores the result in provided instance.
    Divides this instance by provided instance and returns the result as a new instance.
    boolean
    equals(Complex other, double tolerance)
    Determines whether two Complex objects are equal or not up to a certain level of tolerance in both their real and imaginary parts.
    boolean
    Determines whether two Complex objects are equal or not.
    double
    Returns imaginary part of complex number.
    double
    Returns modulus of current complex number, which is equal to the length of the vector formed by the real and imaginary parts.
    double
    Returns phase of current complex number, in radians.
    double
    Returns real part of complex number.
    int
    Computes the hashcode of this instance.
    void
    Multiplies provided complex with this instance.
    void
    multiply(Complex other, Complex result)
    Multiplies this instance with provided instance and stores the result in provided instance.
    Multiplies provided instance with this instance and returns the result as a new instance.
    void
    multiplyByScalar(double scalar)
    Multiplies this instance by provided scalar
    void
    multiplyByScalar(double scalar, Complex result)
    Multiplies this instance by provided scalar value (multiplying both real and imaginary parts by provided value) and stores the result in provided complex instance.
    Multiplies this instance by provided scalar value (multiplying both real and imaginary parts by provided value) and returns the result as a new instance.
    void
    pow(double exponent)
    Computes the power of this instance by provided exponent
    void
    pow(double exponent, Complex result)
    Computes the power of this instance by provided exponent and stores the result in provided instance.
    powAndReturnNew(double exponent)
    Computes the power of this instance by provided exponent and returns the result as a new instance.
    void
    setImaginary(double imaginary)
    Sets imaginary part of complex number.
    void
    setModulusAndPhase(double modulus, double phase)
    Sets both modulus and phase of this complex number.
    void
    setReal(double real)
    Sets real part of complex number.
    void
    setRealAndImaginary(double real, double imaginary)
    Sets both real and imaginary parts of this complex number.
    void
    Computes the squared root of this instance
    void
    sqrt(Complex result)
    Computes the squared root of this instance and returns the result as a new instance.
    Computes the squared root of this instance and returns the result as a new instance.
    void
    Subtracts provided complex from this instance.
    void
    subtract(Complex other, Complex result)
    Subtracts provided instance from this instance and stores the result in provided instance.
    Subtracts provided instance from this instance and returns the result as a new instance

    Methods inherited from class java.lang.Object

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

    • real

      private double real
      Real part of the complex number.
    • imaginary

      private double imaginary
      Imaginary part of the complex number.
  • Constructor Details

    • Complex

      public Complex()
      Constructor. Sets both real and imaginary parts to zero.
    • Complex

      public Complex(double real)
      Creates new Complex number having provided real value and an imaginary part equal to zero.
      Parameters:
      real - Real part.
    • Complex

      public Complex(double real, double imaginary)
      Creates new Complex number having provided real and imaginary parts.
      Parameters:
      real - Real part.
      imaginary - Imaginary part.
    • Complex

      public Complex(Complex initValue)
      Creates new Complex number by copying provided value.
      Parameters:
      initValue - Initial value to be copied.
  • Method Details

    • getReal

      public double getReal()
      Returns real part of complex number.
      Returns:
      Real part.
    • setReal

      public void setReal(double real)
      Sets real part of complex number.
      Parameters:
      real - Real part.
    • getImaginary

      public double getImaginary()
      Returns imaginary part of complex number.
      Returns:
      Imaginary part.
    • setImaginary

      public void setImaginary(double imaginary)
      Sets imaginary part of complex number.
      Parameters:
      imaginary - Imaginary part.
    • setRealAndImaginary

      public void setRealAndImaginary(double real, double imaginary)
      Sets both real and imaginary parts of this complex number.
      Parameters:
      real - Real part.
      imaginary - Imaginary part.
    • getModulus

      public double getModulus()
      Returns modulus of current complex number, which is equal to the length of the vector formed by the real and imaginary parts.
      Returns:
      Modulus.
    • getPhase

      public double getPhase()
      Returns phase of current complex number, in radians. Phase is equal to the angle of the vector formed by the real and imaginary parts.
      Returns:
      Phase in radians.
    • setModulusAndPhase

      public void setModulusAndPhase(double modulus, double phase)
      Sets both modulus and phase of this complex number.
      Parameters:
      modulus - Modulus to be set.
      phase - Phase to be set in radians.
    • conjugate

      public void conjugate(Complex result)
      Computes the complex conjugate of this instance and stores the result into provided complex instance. The complex conjugate is obtained by negating the sign of the imaginary part.
      Parameters:
      result - Complex instance where conjugate is stored.
    • conjugateAndReturnNew

      public Complex conjugateAndReturnNew()
      Computes the complex conjugate of this instance and returns the result as a new instance.
      Returns:
      Complex conjugate.
    • conjugate

      public void conjugate()
      Changes this instance into its complex conjugate.
    • add

      public void add(Complex other, Complex result)
      Adds this instance with provided complex value and stores the result in provided instance
      Parameters:
      other - Complex to be added to current instance.
      result - Complex instance where result is stored.
    • addAndReturnNew

      public Complex addAndReturnNew(Complex other)
      Adds this instance to provided complex and returns the result as a new instance.
      Parameters:
      other - Complex to be added to current instance.
      Returns:
      Result of summation.
    • add

      public void add(Complex other)
      Adds provided complex into this instance
      Parameters:
      other - Complex to be added to current instance.
    • subtract

      public void subtract(Complex other, Complex result)
      Subtracts provided instance from this instance and stores the result in provided instance.
      Parameters:
      other - Complex to be subtracted from this instance.
      result - Complex instance where result is stored.
    • subtractAndReturnNew

      public Complex subtractAndReturnNew(Complex other)
      Subtracts provided instance from this instance and returns the result as a new instance
      Parameters:
      other - Complex to be subtracted from current instance.
      Returns:
      Result of subtraction.
    • subtract

      public void subtract(Complex other)
      Subtracts provided complex from this instance.
      Parameters:
      other - Complex to be subtracted from current instance.
    • multiply

      public void multiply(Complex other, Complex result)
      Multiplies this instance with provided instance and stores the result in provided instance.
      Parameters:
      other - Complex to be multiplied to this instance
      result - Complex instance where result is stored.
    • multiplyAndReturnNew

      public Complex multiplyAndReturnNew(Complex other)
      Multiplies provided instance with this instance and returns the result as a new instance.
      Parameters:
      other - Complex to be multiplied to current instance.
      Returns:
      Result of multiplication.
    • multiply

      public void multiply(Complex other)
      Multiplies provided complex with this instance.
      Parameters:
      other - Complex to be multiplied to this instance.
    • divide

      public void divide(Complex other, Complex result)
      Divides this instance by provided instance and stores the result in provided instance.
      Parameters:
      other - Complex to divide this instance by.
      result - Complex instance where result is stored.
    • divideAndReturnNew

      public Complex divideAndReturnNew(Complex other)
      Divides this instance by provided instance and returns the result as a new instance.
      Parameters:
      other - Complex to divide this instance by.
      Returns:
      Result of division.
    • divide

      public void divide(Complex other)
      Divides this instance by provided complex.
      Parameters:
      other - Complex to divide this instance by.
    • multiplyByScalar

      public void multiplyByScalar(double scalar, Complex result)
      Multiplies this instance by provided scalar value (multiplying both real and imaginary parts by provided value) and stores the result in provided complex instance.
      Parameters:
      scalar - Value to multiply this instance by.
      result - Complex where result of multiplication is stored.
    • multiplyByScalarAndReturnNew

      public Complex multiplyByScalarAndReturnNew(double scalar)
      Multiplies this instance by provided scalar value (multiplying both real and imaginary parts by provided value) and returns the result as a new instance.
      Parameters:
      scalar - Value to multiply this instance by.
      Returns:
      Result of multiplication by scalar.
    • multiplyByScalar

      public void multiplyByScalar(double scalar)
      Multiplies this instance by provided scalar
      Parameters:
      scalar - Value to multiply this instance by.
    • pow

      public void pow(double exponent, Complex result)
      Computes the power of this instance by provided exponent and stores the result in provided instance.
      Parameters:
      exponent - Exponent to power this instance by.
      result - Complex where the power is stored.
    • powAndReturnNew

      public Complex powAndReturnNew(double exponent)
      Computes the power of this instance by provided exponent and returns the result as a new instance.
      Parameters:
      exponent - Exponent to power this instance by.
      Returns:
      Complex where the power is stored.
    • pow

      public void pow(double exponent)
      Computes the power of this instance by provided exponent
      Parameters:
      exponent - Exponent to power this instance by.
    • sqrt

      public void sqrt(Complex result)
      Computes the squared root of this instance and returns the result as a new instance.
      Parameters:
      result - Complex where squared root is stored.
    • sqrtAndReturnNew

      public Complex sqrtAndReturnNew()
      Computes the squared root of this instance and returns the result as a new instance.
      Returns:
      Squared root of this instance.
    • sqrt

      public void sqrt()
      Computes the squared root of this instance
    • equals

      public boolean equals(Object obj)
      Determines whether two Complex objects are equal or not. Two Complex are considered to be equal if both their real and imaginary parts are equal.
      Overrides:
      equals in class Object
      Parameters:
      obj - Object to compare.
      Returns:
      True if both Complex objects are equal.
    • hashCode

      public int hashCode()
      Computes the hashcode of this instance.
      Overrides:
      hashCode in class Object
      Returns:
      Hashcode
    • equals

      public boolean equals(Complex other, double tolerance)
      Determines whether two Complex objects are equal or not up to a certain level of tolerance in both their real and imaginary parts. When the difference in both their real and imaginary parts is below the tolerance level, then both instances are considered to be equal.
      Parameters:
      other - Other Complex to compare.
      tolerance - Margin of tolerance.
      Returns:
      Returns true if both Complex instances are considered to be equal.
    • clone

      public Complex clone() throws CloneNotSupportedException
      Makes a copy of this instance having the same real and imaginary parts.
      Overrides:
      clone in class Object
      Returns:
      a copy of this instance.
      Throws:
      CloneNotSupportedException - if clone fails.
    • copyFrom

      public void copyFrom(Complex value)
      Copies provided value into current instance.
      Parameters:
      value - Value to copy from.