Class ComplexNumber


  • public class ComplexNumber
    extends Object
    ComplexNumber is a class which implements complex numbers in Java. It includes basic operations that can be performed on complex numbers such as, addition, subtraction, multiplication, conjugate, modulus and squaring. The data type for Complex Numbers. The features of this library include: Arithmetic Operations (addition, subtraction, multiplication, division) Complex Specific Operations - Conjugate, Inverse, Absolute/Magnitude, Argument/Phase Trigonometric Operations - sin, cos, tan, cot, sec, cosec Mathematical Functions - exp Complex Parsing of type x+yi
    Version:
    1.2
    Author:
    Abdul Fatir
    • Field Detail

      • XY

        public static final int XY
        Used in format(int) to format the complex number as x+yi
        See Also:
        Constant Field Values
      • RCIS

        public static final int RCIS
        Used in format(int) to format the complex number as R.cis(theta), where theta is arg(z)
        See Also:
        Constant Field Values
    • Constructor Detail

      • ComplexNumber

        public ComplexNumber()
        Constructs a new ComplexNumber object with both real and imaginary parts 0 (z = 0 + 0i).
      • ComplexNumber

        public ComplexNumber​(double real,
                             double imaginary)
        Constructs a new ComplexNumber object.
        Parameters:
        real - the real part, Re(z), of the complex number
        imaginary - the imaginary part, Im(z), of the complex number
    • Method Detail

      • add

        public void add​(ComplexNumber z)
        Adds another ComplexNumber to the current complex number.
        Parameters:
        z - the complex number to be added to the current complex number
      • subtract

        public void subtract​(ComplexNumber z)
        Subtracts another ComplexNumber from the current complex number.
        Parameters:
        z - the complex number to be subtracted from the current complex number
      • multiply

        public void multiply​(ComplexNumber z)
        Multiplies another ComplexNumber to the current complex number.
        Parameters:
        z - the complex number to be multiplied to the current complex number
      • divide

        public void divide​(ComplexNumber z)
        Divides the current ComplexNumber by another ComplexNumber.
        Parameters:
        z - the divisor
      • set

        public void set​(ComplexNumber z)
        Sets the value of current complex number to the passed complex number.
        Parameters:
        z - the complex number
      • add

        public static ComplexNumber add​(ComplexNumber z1,
                                        ComplexNumber z2)
        Adds two ComplexNumber.
        Parameters:
        z1 - the first ComplexNumber.
        z2 - the second ComplexNumber.
        Returns:
        the resultant ComplexNumber (z1 + z2).
      • subtract

        public static ComplexNumber subtract​(ComplexNumber z1,
                                             ComplexNumber z2)
        Subtracts one ComplexNumber from another.
        Parameters:
        z1 - the first ComplexNumber.
        z2 - the second ComplexNumber.
        Returns:
        the resultant ComplexNumber (z1 - z2).
      • multiply

        public static ComplexNumber multiply​(ComplexNumber z1,
                                             ComplexNumber z2)
        Multiplies one ComplexNumber to another.
        Parameters:
        z1 - the first ComplexNumber.
        z2 - the second ComplexNumber.
        Returns:
        the resultant ComplexNumber (z1 * z2).
      • divide

        public static ComplexNumber divide​(ComplexNumber z1,
                                           ComplexNumber z2)
        Divides one ComplexNumber by another.
        Parameters:
        z1 - the first ComplexNumber.
        z2 - the second ComplexNumber.
        Returns:
        the resultant ComplexNumber (z1 / z2).
      • conjugate

        public ComplexNumber conjugate()
        The complex conjugate of the current complex number.
        Returns:
        a ComplexNumber object which is the conjugate of the current complex number
      • mod

        public double mod()
        The modulus, magnitude or the absolute value of current complex number.
        Returns:
        the magnitude or modulus of current complex number
      • square

        public ComplexNumber square()
        The square of the current complex number.
        Returns:
        a ComplexNumber which is the square of the current complex number.
      • toString

        public String toString()
        Overrides:
        toString in class Object
        Returns:
        the complex number in x + yi format
      • exp

        public static ComplexNumber exp​(ComplexNumber z)
        Calculates the exponential of the ComplexNumber
        Parameters:
        z - The input complex number
        Returns:
        a ComplexNumber which is e^(input z)
      • pow

        public static ComplexNumber pow​(ComplexNumber z,
                                        int power)
        Calculates the ComplexNumber to the passed integer power.
        Parameters:
        z - The input complex number
        power - The power.
        Returns:
        a ComplexNumber which is (z)^power
      • sin

        public static ComplexNumber sin​(ComplexNumber z)
        Calculates the sine of the ComplexNumber
        Parameters:
        z - the input complex number
        Returns:
        a ComplexNumber which is the sine of z.
      • cos

        public static ComplexNumber cos​(ComplexNumber z)
        Calculates the cosine of the ComplexNumber
        Parameters:
        z - the input complex number
        Returns:
        a ComplexNumber which is the cosine of z.
      • tan

        public static ComplexNumber tan​(ComplexNumber z)
        Calculates the tangent of the ComplexNumber
        Parameters:
        z - the input complex number
        Returns:
        a ComplexNumber which is the tangent of z.
      • cot

        public static ComplexNumber cot​(ComplexNumber z)
        Calculates the co-tangent of the ComplexNumber
        Parameters:
        z - the input complex number
        Returns:
        a ComplexNumber which is the co-tangent of z.
      • sec

        public static ComplexNumber sec​(ComplexNumber z)
        Calculates the secant of the ComplexNumber
        Parameters:
        z - the input complex number
        Returns:
        a ComplexNumber which is the secant of z.
      • cosec

        public static ComplexNumber cosec​(ComplexNumber z)
        Calculates the co-secant of the ComplexNumber
        Parameters:
        z - the input complex number
        Returns:
        a ComplexNumber which is the co-secant of z.
      • getRe

        public double getRe()
        The real part of ComplexNumber
        Returns:
        the real part of the complex number
      • getIm

        public double getIm()
        The imaginary part of ComplexNumber
        Returns:
        the imaginary part of the complex number
      • getArg

        public double getArg()
        The argument/phase of the current complex number.
        Returns:
        arg(z) - the argument of current complex number
      • parseComplex

        public static ComplexNumber parseComplex​(String s)
        Parses the String as a ComplexNumber of type x+yi.
        Parameters:
        s - the input complex number as string
        Returns:
        a ComplexNumber which is represented by the string.
      • equals

        public final boolean equals​(Object z)
        Checks if the passed ComplexNumber is equal to the current.
        Overrides:
        equals in class Object
        Parameters:
        z - the complex number to be checked
        Returns:
        true if they are equal, false otherwise
      • inverse

        public ComplexNumber inverse()
        The inverse/reciprocal of the complex number.
        Returns:
        the reciprocal of current complex number.
      • format

        public String format​(int format_id)
                      throws IllegalArgumentException
        Formats the Complex number as x+yi or r.cis(theta)
        Parameters:
        format_id - the format ID ComplexNumber.XY or ComplexNumber.RCIS.
        Returns:
        a string representation of the complex number
        Throws:
        IllegalArgumentException - if the format_id does not match.