Class BigComplex


  • public final class BigComplex
    extends java.lang.Object
    Represents a complex number consisting of a real and an imaginary BigDecimal part in the form a + bi.

    It generally follows the design of BigDecimal with some convenience improvements like overloaded operator methods.

    The biggest difference to BigDecimal is that BigComplex.equals(Object) implements the mathematical equality and not the strict technical equality. This was a difficult decision because it means that BigComplex behaves slightly different than BigDecimal but considering that the strange equality of BigDecimal is a major source of bugs we decided it was worth the slight inconsistency. If you need the strict equality use strictEquals(Object)`.

    This class is immutable and therefore inherently thread safe.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static BigComplex I
      Imaginary 1 represented as complex number.
      java.math.BigDecimal im
      The imaginary BigDecimal part of this complex number.
      static BigComplex ONE
      Real 1 represented as complex number.
      java.math.BigDecimal re
      The real BigDecimal part of this complex number.
      static BigComplex ZERO
      Zero represented as complex number.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private BigComplex​(java.math.BigDecimal re, java.math.BigDecimal im)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.math.BigDecimal abs​(java.math.MathContext mathContext)
      Calculates the absolute value (also known as magnitude, length or radius) of this complex number.
      java.math.BigDecimal absSquare​(java.math.MathContext mathContext)
      Calculates the square of the absolute value of this complex number.
      BigComplex add​(double value)
      Calculates the addition of the given real double value to this complex number.
      BigComplex add​(BigComplex value)
      Calculates the addition of the given complex value to this complex number.
      BigComplex add​(BigComplex value, java.math.MathContext mathContext)
      Calculates the addition of the given complex value to this complex number using the specified MathContext.
      BigComplex add​(java.math.BigDecimal value)
      Calculates the addition of the given real BigDecimal value to this complex number.
      BigComplex add​(java.math.BigDecimal value, java.math.MathContext mathContext)
      Calculates the addition of the given real BigDecimal value to this complex number using the specified MathContext.
      java.math.BigDecimal angle​(java.math.MathContext mathContext)
      Calculates the angle in radians (also known as argument) of this complex number.
      BigComplex conjugate()
      Calculates the conjugate a - bi of this complex number.
      BigComplex divide​(double value, java.math.MathContext mathContext)
      Calculates this complex number divided by the given real double value using the specified MathContext.
      BigComplex divide​(BigComplex value, java.math.MathContext mathContext)
      Calculates this complex number divided by the given complex value using the specified MathContext.
      BigComplex divide​(java.math.BigDecimal value, java.math.MathContext mathContext)
      Calculates this complex number divided by the given real BigDecimal value using the specified MathContext.
      boolean equals​(java.lang.Object obj)
      int hashCode()  
      BigComplex im()
      Returns the imaginary part of this complex number as BigComplex number.
      boolean isReal()
      Returns whether this complex number only has a real part (the imaginary part is 0).
      BigComplex multiply​(double value)
      Calculates the multiplication of the given real double value with this complex number.
      BigComplex multiply​(BigComplex value)
      Calculates the multiplication of the given complex value to this complex number.
      BigComplex multiply​(BigComplex value, java.math.MathContext mathContext)
      Calculates the multiplication of the given complex value with this complex number using the specified MathContext.
      BigComplex multiply​(java.math.BigDecimal value)
      Calculates the multiplication of the given real BigDecimal value with this complex number.
      BigComplex multiply​(java.math.BigDecimal value, java.math.MathContext mathContext)
      Calculates the multiplication of the given real BigDecimal value with this complex number using the specified MathContext.
      BigComplex negate()
      Calculates the negation -a - bi of this complex number.
      BigComplex re()
      Returns the real part of this complex number as BigComplex number.
      BigComplex reciprocal​(java.math.MathContext mathContext)
      Calculates the reciprocal of this complex number using the specified MathContext.
      BigComplex round​(java.math.MathContext mathContext)
      Returns this complex nuber rounded to the specified precision.
      boolean strictEquals​(java.lang.Object obj)
      Returns whether the real and imaginary parts of this complex number are strictly equal.
      BigComplex subtract​(double value)
      Calculates the subtraction of the given real double value from this complex number.
      BigComplex subtract​(BigComplex value)
      Calculates the subtraction of the given complex value from this complex number.
      BigComplex subtract​(BigComplex value, java.math.MathContext mathContext)
      Calculates the subtraction of the given complex value from this complex number using the specified MathContext.
      BigComplex subtract​(java.math.BigDecimal value)
      Calculates the subtraction of the given real BigDecimal value from this complex number.
      BigComplex subtract​(java.math.BigDecimal value, java.math.MathContext mathContext)
      Calculates the subtraction of the given real BigDecimal value from this complex number using the specified MathContext.
      java.lang.String toString()  
      static BigComplex valueOf​(double real)
      Returns a complex number with the specified real double part.
      static BigComplex valueOf​(double real, double imaginary)
      Returns a complex number with the specified real and imaginary double parts.
      static BigComplex valueOf​(java.math.BigDecimal real)
      Returns a complex number with the specified real BigDecimal part.
      static BigComplex valueOf​(java.math.BigDecimal real, java.math.BigDecimal imaginary)
      Returns a complex number with the specified real and imaginary BigDecimal parts.
      static BigComplex valueOfPolar​(double radius, double angle, java.math.MathContext mathContext)  
      static BigComplex valueOfPolar​(java.math.BigDecimal radius, java.math.BigDecimal angle, java.math.MathContext mathContext)
      Returns a complex number with the specified polar BigDecimal radius and angle using the specified MathContext.
      • Methods inherited from class java.lang.Object

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

      • ZERO

        public static final BigComplex ZERO
        Zero represented as complex number.
      • ONE

        public static final BigComplex ONE
        Real 1 represented as complex number.
      • I

        public static final BigComplex I
        Imaginary 1 represented as complex number.
      • re

        public final java.math.BigDecimal re
        The real BigDecimal part of this complex number.
      • im

        public final java.math.BigDecimal im
        The imaginary BigDecimal part of this complex number.
    • Constructor Detail

      • BigComplex

        private BigComplex​(java.math.BigDecimal re,
                           java.math.BigDecimal im)
    • Method Detail

      • add

        public BigComplex add​(BigComplex value)
        Calculates the addition of the given complex value to this complex number.

        This methods does not modify this instance.

        Parameters:
        value - the BigComplex value to add
        Returns:
        the calculated BigComplex result
      • add

        public BigComplex add​(BigComplex value,
                              java.math.MathContext mathContext)
        Calculates the addition of the given complex value to this complex number using the specified MathContext.

        This methods does not modify this instance.

        Parameters:
        value - the BigComplex value to add
        mathContext - the MathContext used to calculate the result
        Returns:
        the calculated BigComplex result
      • add

        public BigComplex add​(java.math.BigDecimal value,
                              java.math.MathContext mathContext)
        Calculates the addition of the given real BigDecimal value to this complex number using the specified MathContext.

        This methods does not modify this instance.

        Parameters:
        value - the real BigDecimal value to add
        mathContext - the MathContext used to calculate the result
        Returns:
        the calculated BigComplex result
      • add

        public BigComplex add​(java.math.BigDecimal value)
        Calculates the addition of the given real BigDecimal value to this complex number.

        This methods does not modify this instance.

        Parameters:
        value - the real BigDecimal value to add
        Returns:
        the calculated BigComplex result
      • add

        public BigComplex add​(double value)
        Calculates the addition of the given real double value to this complex number.

        This methods does not modify this instance.

        Parameters:
        value - the real double value to add
        Returns:
        the calculated BigComplex result
      • subtract

        public BigComplex subtract​(BigComplex value)
        Calculates the subtraction of the given complex value from this complex number.

        This methods does not modify this instance.

        Parameters:
        value - the BigComplex value to subtract
        Returns:
        the calculated BigComplex result
      • subtract

        public BigComplex subtract​(BigComplex value,
                                   java.math.MathContext mathContext)
        Calculates the subtraction of the given complex value from this complex number using the specified MathContext.

        This methods does not modify this instance.

        Parameters:
        value - the BigComplex value to subtract
        mathContext - the MathContext used to calculate the result
        Returns:
        the calculated BigComplex result
      • subtract

        public BigComplex subtract​(java.math.BigDecimal value,
                                   java.math.MathContext mathContext)
        Calculates the subtraction of the given real BigDecimal value from this complex number using the specified MathContext.

        This methods does not modify this instance.

        Parameters:
        value - the real BigDecimal value to add
        mathContext - the MathContext used to calculate the result
        Returns:
        the calculated BigComplex result
      • subtract

        public BigComplex subtract​(java.math.BigDecimal value)
        Calculates the subtraction of the given real BigDecimal value from this complex number.

        This methods does not modify this instance.

        Parameters:
        value - the real BigDecimal value to subtract
        Returns:
        the calculated BigComplex result
      • subtract

        public BigComplex subtract​(double value)
        Calculates the subtraction of the given real double value from this complex number.

        This methods does not modify this instance.

        Parameters:
        value - the real double value to subtract
        Returns:
        the calculated BigComplex result
      • multiply

        public BigComplex multiply​(BigComplex value)
        Calculates the multiplication of the given complex value to this complex number.

        This methods does not modify this instance.

        Parameters:
        value - the BigComplex value to multiply
        Returns:
        the calculated BigComplex result
      • multiply

        public BigComplex multiply​(BigComplex value,
                                   java.math.MathContext mathContext)
        Calculates the multiplication of the given complex value with this complex number using the specified MathContext.

        This methods does not modify this instance.

        Parameters:
        value - the BigComplex value to multiply
        mathContext - the MathContext used to calculate the result
        Returns:
        the calculated BigComplex result
      • multiply

        public BigComplex multiply​(java.math.BigDecimal value,
                                   java.math.MathContext mathContext)
        Calculates the multiplication of the given real BigDecimal value with this complex number using the specified MathContext.

        This methods does not modify this instance.

        Parameters:
        value - the real BigDecimal value to multiply
        mathContext - the MathContext used to calculate the result
        Returns:
        the calculated BigComplex result
      • multiply

        public BigComplex multiply​(java.math.BigDecimal value)
        Calculates the multiplication of the given real BigDecimal value with this complex number.

        This methods does not modify this instance.

        Parameters:
        value - the real BigDecimal value to multiply
        Returns:
        the calculated BigComplex result
      • multiply

        public BigComplex multiply​(double value)
        Calculates the multiplication of the given real double value with this complex number.

        This methods does not modify this instance.

        Parameters:
        value - the real double value to multiply
        Returns:
        the calculated BigComplex result
      • divide

        public BigComplex divide​(BigComplex value,
                                 java.math.MathContext mathContext)
        Calculates this complex number divided by the given complex value using the specified MathContext.

        This methods does not modify this instance.

        Parameters:
        value - the BigComplex value to divide by
        mathContext - the MathContext used to calculate the result
        Returns:
        the calculated BigComplex result
      • divide

        public BigComplex divide​(java.math.BigDecimal value,
                                 java.math.MathContext mathContext)
        Calculates this complex number divided by the given real BigDecimal value using the specified MathContext.

        This methods does not modify this instance.

        Parameters:
        value - the BigDecimal value to divide by
        mathContext - the MathContext used to calculate the result
        Returns:
        the calculated BigComplex result
      • divide

        public BigComplex divide​(double value,
                                 java.math.MathContext mathContext)
        Calculates this complex number divided by the given real double value using the specified MathContext.

        This methods does not modify this instance.

        Parameters:
        value - the double value to divide by
        mathContext - the MathContext used to calculate the result
        Returns:
        the calculated BigComplex result
      • reciprocal

        public BigComplex reciprocal​(java.math.MathContext mathContext)
        Calculates the reciprocal of this complex number using the specified MathContext.

        This methods does not modify this instance.

        Parameters:
        mathContext - the MathContext used to calculate the result
        Returns:
        the calculated BigComplex result
      • conjugate

        public BigComplex conjugate()
        Calculates the conjugate a - bi of this complex number.

        This methods does not modify this instance.

        Returns:
        the calculated BigComplex result
      • negate

        public BigComplex negate()
        Calculates the negation -a - bi of this complex number.

        This methods does not modify this instance.

        Returns:
        the calculated BigComplex result
      • abs

        public java.math.BigDecimal abs​(java.math.MathContext mathContext)
        Calculates the absolute value (also known as magnitude, length or radius) of this complex number.

        This method is slower than absSquare(MathContext) since it needs to calculate the BigDecimalMath.sqrt(BigDecimal, MathContext).

        This methods does not modify this instance.

        Parameters:
        mathContext - the MathContext used to calculate the result
        Returns:
        the calculated BigComplex result
        See Also:
        absSquare(MathContext)
      • angle

        public java.math.BigDecimal angle​(java.math.MathContext mathContext)
        Calculates the angle in radians (also known as argument) of this complex number.

        This methods does not modify this instance.

        Parameters:
        mathContext - the MathContext used to calculate the result
        Returns:
        the calculated BigComplex result
      • absSquare

        public java.math.BigDecimal absSquare​(java.math.MathContext mathContext)
        Calculates the square of the absolute value of this complex number.

        This method is faster than abs(MathContext) since it does not need to calculate the BigDecimalMath.sqrt(BigDecimal, MathContext).

        This methods does not modify this instance.

        Parameters:
        mathContext - the MathContext used to calculate the result
        Returns:
        the calculated BigComplex result
        See Also:
        abs(MathContext)
      • isReal

        public boolean isReal()
        Returns whether this complex number only has a real part (the imaginary part is 0).
        Returns:
        true if this complex number only has a real part, false if the imaginary part is not 0
      • im

        public BigComplex im()
        Returns the imaginary part of this complex number as BigComplex number.
        Returns:
        the imaginary part as as BigComplex number
      • round

        public BigComplex round​(java.math.MathContext mathContext)
        Returns this complex nuber rounded to the specified precision.

        This methods does not modify this instance.

        Parameters:
        mathContext - the MathContext used to calculate the result
        Returns:
        the rounded BigComplex result
      • hashCode

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

        public boolean equals​(java.lang.Object obj)

        Contrary to BigDecimal.equals(Object) this method implements mathematical equality (by calling BigDecimal.compareTo(BigDecimal) on the real and imaginary parts) instead of strict equality.

        Overrides:
        equals in class java.lang.Object
        See Also:
        strictEquals(Object)
      • strictEquals

        public boolean strictEquals​(java.lang.Object obj)
        Returns whether the real and imaginary parts of this complex number are strictly equal.

        This method uses the strict equality as defined by BigDecimal.equals(Object) on the real and imaginary parts.

        Please note that BigComplex.equals(Object) implements mathematical equality instead (by calling on the real and imaginary parts).

        Parameters:
        obj - the object to compare for strict equality
        Returns:
        true if the specified object is strictly equal to this complex number
        See Also:
        equals(Object)
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • valueOf

        public static BigComplex valueOf​(java.math.BigDecimal real)
        Returns a complex number with the specified real BigDecimal part.
        Parameters:
        real - the real BigDecimal part
        Returns:
        the complex number
      • valueOf

        public static BigComplex valueOf​(double real)
        Returns a complex number with the specified real double part.
        Parameters:
        real - the real double part
        Returns:
        the complex number
      • valueOf

        public static BigComplex valueOf​(double real,
                                         double imaginary)
        Returns a complex number with the specified real and imaginary double parts.
        Parameters:
        real - the real double part
        imaginary - the imaginary double part
        Returns:
        the complex number
      • valueOf

        public static BigComplex valueOf​(java.math.BigDecimal real,
                                         java.math.BigDecimal imaginary)
        Returns a complex number with the specified real and imaginary BigDecimal parts.
        Parameters:
        real - the real BigDecimal part
        imaginary - the imaginary BigDecimal part
        Returns:
        the complex number
      • valueOfPolar

        public static BigComplex valueOfPolar​(java.math.BigDecimal radius,
                                              java.math.BigDecimal angle,
                                              java.math.MathContext mathContext)
        Returns a complex number with the specified polar BigDecimal radius and angle using the specified MathContext.
        Parameters:
        radius - the BigDecimal radius of the polar representation
        angle - the BigDecimal angle in radians of the polar representation
        mathContext - the MathContext used to calculate the result
        Returns:
        the complex number
      • valueOfPolar

        public static BigComplex valueOfPolar​(double radius,
                                              double angle,
                                              java.math.MathContext mathContext)