Class BigComplex
- java.lang.Object
-
- ch.obermuhlner.math.big.BigComplex
-
public final class BigComplex extends java.lang.ObjectRepresents a complex number consisting of a real and an imaginaryBigDecimalpart in the forma + bi.It generally follows the design of
BigDecimalwith some convenience improvements like overloaded operator methods.The biggest difference to
BigDecimalis thatBigComplex.equals(Object)implements the mathematical equality and not the strict technical equality. This was a difficult decision because it means thatBigComplexbehaves slightly different thanBigDecimalbut considering that the strange equality ofBigDecimalis a major source of bugs we decided it was worth the slight inconsistency. If you need the strict equality usestrictEquals(Object)`.This class is immutable and therefore inherently thread safe.
-
-
Field Summary
Fields Modifier and Type Field Description static BigComplexIImaginary 1 represented as complex number.java.math.BigDecimalimThe imaginaryBigDecimalpart of this complex number.static BigComplexONEReal 1 represented as complex number.java.math.BigDecimalreThe realBigDecimalpart of this complex number.static BigComplexZEROZero represented as complex number.
-
Constructor Summary
Constructors Modifier Constructor Description privateBigComplex(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.BigDecimalabs(java.math.MathContext mathContext)Calculates the absolute value (also known as magnitude, length or radius) of this complex number.java.math.BigDecimalabsSquare(java.math.MathContext mathContext)Calculates the square of the absolute value of this complex number.BigComplexadd(double value)Calculates the addition of the given realdoublevalue to this complex number.BigComplexadd(BigComplex value)Calculates the addition of the given complex value to this complex number.BigComplexadd(BigComplex value, java.math.MathContext mathContext)Calculates the addition of the given complex value to this complex number using the specifiedMathContext.BigComplexadd(java.math.BigDecimal value)Calculates the addition of the given realBigDecimalvalue to this complex number.BigComplexadd(java.math.BigDecimal value, java.math.MathContext mathContext)Calculates the addition of the given realBigDecimalvalue to this complex number using the specifiedMathContext.java.math.BigDecimalangle(java.math.MathContext mathContext)Calculates the angle in radians (also known as argument) of this complex number.BigComplexconjugate()Calculates the conjugatea - biof this complex number.BigComplexdivide(double value, java.math.MathContext mathContext)Calculates this complex number divided by the given realdoublevalue using the specifiedMathContext.BigComplexdivide(BigComplex value, java.math.MathContext mathContext)Calculates this complex number divided by the given complex value using the specifiedMathContext.BigComplexdivide(java.math.BigDecimal value, java.math.MathContext mathContext)Calculates this complex number divided by the given realBigDecimalvalue using the specifiedMathContext.booleanequals(java.lang.Object obj)inthashCode()BigComplexim()Returns the imaginary part of this complex number asBigComplexnumber.booleanisReal()Returns whether this complex number only has a real part (the imaginary part is 0).BigComplexmultiply(double value)Calculates the multiplication of the given realdoublevalue with this complex number.BigComplexmultiply(BigComplex value)Calculates the multiplication of the given complex value to this complex number.BigComplexmultiply(BigComplex value, java.math.MathContext mathContext)Calculates the multiplication of the given complex value with this complex number using the specifiedMathContext.BigComplexmultiply(java.math.BigDecimal value)Calculates the multiplication of the given realBigDecimalvalue with this complex number.BigComplexmultiply(java.math.BigDecimal value, java.math.MathContext mathContext)Calculates the multiplication of the given realBigDecimalvalue with this complex number using the specifiedMathContext.BigComplexnegate()Calculates the negation-a - biof this complex number.BigComplexre()Returns the real part of this complex number asBigComplexnumber.BigComplexreciprocal(java.math.MathContext mathContext)Calculates the reciprocal of this complex number using the specifiedMathContext.BigComplexround(java.math.MathContext mathContext)Returns this complex nuber rounded to the specified precision.booleanstrictEquals(java.lang.Object obj)Returns whether the real and imaginary parts of this complex number are strictly equal.BigComplexsubtract(double value)Calculates the subtraction of the given realdoublevalue from this complex number.BigComplexsubtract(BigComplex value)Calculates the subtraction of the given complex value from this complex number.BigComplexsubtract(BigComplex value, java.math.MathContext mathContext)Calculates the subtraction of the given complex value from this complex number using the specifiedMathContext.BigComplexsubtract(java.math.BigDecimal value)Calculates the subtraction of the given realBigDecimalvalue from this complex number.BigComplexsubtract(java.math.BigDecimal value, java.math.MathContext mathContext)Calculates the subtraction of the given realBigDecimalvalue from this complex number using the specifiedMathContext.java.lang.StringtoString()static BigComplexvalueOf(double real)Returns a complex number with the specified realdoublepart.static BigComplexvalueOf(double real, double imaginary)Returns a complex number with the specified real and imaginarydoubleparts.static BigComplexvalueOf(java.math.BigDecimal real)Returns a complex number with the specified realBigDecimalpart.static BigComplexvalueOf(java.math.BigDecimal real, java.math.BigDecimal imaginary)Returns a complex number with the specified real and imaginaryBigDecimalparts.static BigComplexvalueOfPolar(double radius, double angle, java.math.MathContext mathContext)static BigComplexvalueOfPolar(java.math.BigDecimal radius, java.math.BigDecimal angle, java.math.MathContext mathContext)Returns a complex number with the specified polarBigDecimalradius and angle using the specifiedMathContext.
-
-
-
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 realBigDecimalpart of this complex number.
-
im
public final java.math.BigDecimal im
The imaginaryBigDecimalpart of this complex number.
-
-
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- theBigComplexvalue to add- Returns:
- the calculated
BigComplexresult
-
add
public BigComplex add(BigComplex value, java.math.MathContext mathContext)
Calculates the addition of the given complex value to this complex number using the specifiedMathContext.This methods does not modify this instance.
- Parameters:
value- theBigComplexvalue to addmathContext- theMathContextused to calculate the result- Returns:
- the calculated
BigComplexresult
-
add
public BigComplex add(java.math.BigDecimal value, java.math.MathContext mathContext)
Calculates the addition of the given realBigDecimalvalue to this complex number using the specifiedMathContext.This methods does not modify this instance.
- Parameters:
value- the realBigDecimalvalue to addmathContext- theMathContextused to calculate the result- Returns:
- the calculated
BigComplexresult
-
add
public BigComplex add(java.math.BigDecimal value)
Calculates the addition of the given realBigDecimalvalue to this complex number.This methods does not modify this instance.
- Parameters:
value- the realBigDecimalvalue to add- Returns:
- the calculated
BigComplexresult
-
add
public BigComplex add(double value)
Calculates the addition of the given realdoublevalue to this complex number.This methods does not modify this instance.
- Parameters:
value- the realdoublevalue to add- Returns:
- the calculated
BigComplexresult
-
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- theBigComplexvalue to subtract- Returns:
- the calculated
BigComplexresult
-
subtract
public BigComplex subtract(BigComplex value, java.math.MathContext mathContext)
Calculates the subtraction of the given complex value from this complex number using the specifiedMathContext.This methods does not modify this instance.
- Parameters:
value- theBigComplexvalue to subtractmathContext- theMathContextused to calculate the result- Returns:
- the calculated
BigComplexresult
-
subtract
public BigComplex subtract(java.math.BigDecimal value, java.math.MathContext mathContext)
Calculates the subtraction of the given realBigDecimalvalue from this complex number using the specifiedMathContext.This methods does not modify this instance.
- Parameters:
value- the realBigDecimalvalue to addmathContext- theMathContextused to calculate the result- Returns:
- the calculated
BigComplexresult
-
subtract
public BigComplex subtract(java.math.BigDecimal value)
Calculates the subtraction of the given realBigDecimalvalue from this complex number.This methods does not modify this instance.
- Parameters:
value- the realBigDecimalvalue to subtract- Returns:
- the calculated
BigComplexresult
-
subtract
public BigComplex subtract(double value)
Calculates the subtraction of the given realdoublevalue from this complex number.This methods does not modify this instance.
- Parameters:
value- the realdoublevalue to subtract- Returns:
- the calculated
BigComplexresult
-
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- theBigComplexvalue to multiply- Returns:
- the calculated
BigComplexresult
-
multiply
public BigComplex multiply(BigComplex value, java.math.MathContext mathContext)
Calculates the multiplication of the given complex value with this complex number using the specifiedMathContext.This methods does not modify this instance.
- Parameters:
value- theBigComplexvalue to multiplymathContext- theMathContextused to calculate the result- Returns:
- the calculated
BigComplexresult
-
multiply
public BigComplex multiply(java.math.BigDecimal value, java.math.MathContext mathContext)
Calculates the multiplication of the given realBigDecimalvalue with this complex number using the specifiedMathContext.This methods does not modify this instance.
- Parameters:
value- the realBigDecimalvalue to multiplymathContext- theMathContextused to calculate the result- Returns:
- the calculated
BigComplexresult
-
multiply
public BigComplex multiply(java.math.BigDecimal value)
Calculates the multiplication of the given realBigDecimalvalue with this complex number.This methods does not modify this instance.
- Parameters:
value- the realBigDecimalvalue to multiply- Returns:
- the calculated
BigComplexresult
-
multiply
public BigComplex multiply(double value)
Calculates the multiplication of the given realdoublevalue with this complex number.This methods does not modify this instance.
- Parameters:
value- the realdoublevalue to multiply- Returns:
- the calculated
BigComplexresult
-
divide
public BigComplex divide(BigComplex value, java.math.MathContext mathContext)
Calculates this complex number divided by the given complex value using the specifiedMathContext.This methods does not modify this instance.
- Parameters:
value- theBigComplexvalue to divide bymathContext- theMathContextused to calculate the result- Returns:
- the calculated
BigComplexresult
-
divide
public BigComplex divide(java.math.BigDecimal value, java.math.MathContext mathContext)
Calculates this complex number divided by the given realBigDecimalvalue using the specifiedMathContext.This methods does not modify this instance.
- Parameters:
value- theBigDecimalvalue to divide bymathContext- theMathContextused to calculate the result- Returns:
- the calculated
BigComplexresult
-
divide
public BigComplex divide(double value, java.math.MathContext mathContext)
Calculates this complex number divided by the given realdoublevalue using the specifiedMathContext.This methods does not modify this instance.
- Parameters:
value- thedoublevalue to divide bymathContext- theMathContextused to calculate the result- Returns:
- the calculated
BigComplexresult
-
reciprocal
public BigComplex reciprocal(java.math.MathContext mathContext)
Calculates the reciprocal of this complex number using the specifiedMathContext.This methods does not modify this instance.
- Parameters:
mathContext- theMathContextused to calculate the result- Returns:
- the calculated
BigComplexresult
-
conjugate
public BigComplex conjugate()
Calculates the conjugatea - biof this complex number.This methods does not modify this instance.
- Returns:
- the calculated
BigComplexresult
-
negate
public BigComplex negate()
Calculates the negation-a - biof this complex number.This methods does not modify this instance.
- Returns:
- the calculated
BigComplexresult
-
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 theBigDecimalMath.sqrt(BigDecimal, MathContext).This methods does not modify this instance.
- Parameters:
mathContext- theMathContextused to calculate the result- Returns:
- the calculated
BigComplexresult - 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- theMathContextused to calculate the result- Returns:
- the calculated
BigComplexresult
-
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 theBigDecimalMath.sqrt(BigDecimal, MathContext).This methods does not modify this instance.
- Parameters:
mathContext- theMathContextused to calculate the result- Returns:
- the calculated
BigComplexresult - See Also:
abs(MathContext)
-
isReal
public boolean isReal()
Returns whether this complex number only has a real part (the imaginary part is 0).- Returns:
trueif this complex number only has a real part,falseif the imaginary part is not 0
-
re
public BigComplex re()
Returns the real part of this complex number asBigComplexnumber.- Returns:
- the real part as as
BigComplexnumber
-
im
public BigComplex im()
Returns the imaginary part of this complex number asBigComplexnumber.- Returns:
- the imaginary part as as
BigComplexnumber
-
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- theMathContextused to calculate the result- Returns:
- the rounded
BigComplexresult
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)
Contrary to
BigDecimal.equals(Object)this method implements mathematical equality (by callingBigDecimal.compareTo(BigDecimal)on the real and imaginary parts) instead of strict equality.- Overrides:
equalsin classjava.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 callingon the real and imaginary parts).- Parameters:
obj- the object to compare for strict equality- Returns:
trueif the specified object is strictly equal to this complex number- See Also:
equals(Object)
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
valueOf
public static BigComplex valueOf(java.math.BigDecimal real)
Returns a complex number with the specified realBigDecimalpart.- Parameters:
real- the realBigDecimalpart- Returns:
- the complex number
-
valueOf
public static BigComplex valueOf(double real)
Returns a complex number with the specified realdoublepart.- Parameters:
real- the realdoublepart- Returns:
- the complex number
-
valueOf
public static BigComplex valueOf(double real, double imaginary)
Returns a complex number with the specified real and imaginarydoubleparts.- Parameters:
real- the realdoublepartimaginary- the imaginarydoublepart- 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 imaginaryBigDecimalparts.- Parameters:
real- the realBigDecimalpartimaginary- the imaginaryBigDecimalpart- 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 polarBigDecimalradius and angle using the specifiedMathContext.- Parameters:
radius- theBigDecimalradius of the polar representationangle- theBigDecimalangle in radians of the polar representationmathContext- theMathContextused to calculate the result- Returns:
- the complex number
-
valueOfPolar
public static BigComplex valueOfPolar(double radius, double angle, java.math.MathContext mathContext)
-
-