Class Complex
- All Implemented Interfaces:
Serializable,FieldElement<Complex>
Implementations of arithmetic operations handle NaN and
infinite values according to the rules for Double, i.e.
equals(java.lang.Object) is an equivalence relation for all instances that have
a NaN in either real or imaginary part, e.g. the following are
considered equal:
1 + NaNiNaN + iNaN + NaNi
Note that this contradicts the IEEE-754 standard for floating
point numbers (according to which the test x == x must fail if
x is NaN). The method
equals for primitive double in Precision
conforms with IEEE-754 while this class conforms with the standard behavior
for Java object types.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final ComplexThe square root of -1.static final ComplexA complex number representing "+INF + INFi"static final ComplexA complex number representing "NaN + NaNi"static final ComplexA complex number representing "1.0 + 0.0i"static final ComplexA complex number representing "0.0 + 0.0i" -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiondoubleabs()Return the absolute value of this complex number.acos()Compute the inverse cosine of this complex number.add(double addend) Returns aComplexwhose value is(this + addend), withaddendinterpreted as a real number.Returns aComplexwhose value is(this + addend).asin()Compute the inverse sine of this complex number.atan()Compute the inverse tangent of this complex number.Returns the conjugate of this complex number.cos()Compute the cosine of this complex number.cosh()Compute the hyperbolic cosine of this complex number.protected ComplexcreateComplex(double realPart, double imaginaryPart) Create a complex number given the real and imaginary parts.divide(double divisor) Returns aComplexwhose value is(this / divisor), withdivisorinterpreted as a real number.Returns aComplexwhose value is(this / divisor).booleanTest for equality with another object.static booleanReturnstrueiff the values are equal as defined byequals(x, y, 1).static booleanReturnstrueif, both for the real part and for the imaginary part, there is no double value strictly between the arguments or the difference between them is within the range of allowed error (inclusive).static booleanTest for the floating-point equality between Complex objects.static booleanequalsWithRelativeTolerance(Complex x, Complex y, double eps) Returnstrueif, both for the real part and for the imaginary part, there is no double value strictly between the arguments or the relative difference between them is smaller or equal to the given tolerance.exp()Compute the exponential function of this complex number.doubleCompute the argument of this complex number.getField()Get theFieldto which the instance belongs.doubleAccess the imaginary part.doublegetReal()Access the real part.inthashCode()Get a hashCode for the complex number.booleanChecks whether either the real or imaginary part of this complex number takes an infinite value (eitherDouble.POSITIVE_INFINITYorDouble.NEGATIVE_INFINITY) and neither part isNaN.booleanisNaN()Checks whether either or both parts of this complex number isNaN.log()Compute the natural logarithm of this complex number.multiply(double factor) Returns aComplexwhose value isthis * factor, withfactorinterpreted as a real number.multiply(int factor) Returns aComplexwhose value isthis * factor, withfactorinterpreted as a integer number.Returns aComplexwhose value isthis * factor.negate()Returns aComplexwhose value is(-this).nthRoot(int n) Computes the n-th roots of this complex number.pow(double x) Returns of value of this complex number raised to the power ofx.Returns of value of this complex number raised to the power ofx.protected final ObjectResolve the transient fields in a deserialized Complex Object.Returns the multiplicative inverse ofthiselement.sin()Compute the sine of this complex number.sinh()Compute the hyperbolic sine of this complex number.sqrt()Compute the square root of this complex number.sqrt1z()Compute the square root of1 - this2for this complex number.subtract(double subtrahend) Returns aComplexwhose value is(this - subtrahend).Returns aComplexwhose value is(this - subtrahend).tan()Compute the tangent of this complex number.tanh()Compute the hyperbolic tangent of this complex number.toString()static ComplexvalueOf(double realPart) Create a complex number given only the real part.static ComplexvalueOf(double realPart, double imaginaryPart) Create a complex number given the real and imaginary parts.
-
Field Details
-
I
The square root of -1. A number representing "0.0 + 1.0i" -
NaN
A complex number representing "NaN + NaNi" -
INF
A complex number representing "+INF + INFi" -
ONE
A complex number representing "1.0 + 0.0i" -
ZERO
A complex number representing "0.0 + 0.0i"
-
-
Constructor Details
-
Complex
public Complex(double real) Create a complex number given only the real part.- Parameters:
real- Real part.
-
Complex
public Complex(double real, double imaginary) Create a complex number given the real and imaginary parts.- Parameters:
real- Real part.imaginary- Imaginary part.
-
-
Method Details
-
abs
public double abs()Return the absolute value of this complex number. ReturnsNaNif either real or imaginary part isNaNandDouble.POSITIVE_INFINITYif neither part isNaN, but at least one part is infinite.- Returns:
- the absolute value.
-
add
Returns aComplexwhose value is(this + addend). Uses the definitional formula
If either(a + bi) + (c + di) = (a+c) + (b+d)ithisoraddendhas aNaNvalue in either part,NaNis returned; otherwiseInfiniteandNaNvalues are returned in the parts of the result according to the rules forDoublearithmetic.- Specified by:
addin interfaceFieldElement<Complex>- Parameters:
addend- Value to be added to thisComplex.- Returns:
this + addend.- Throws:
NullArgumentException- ifaddendisnull.
-
add
Returns aComplexwhose value is(this + addend), withaddendinterpreted as a real number.- Parameters:
addend- Value to be added to thisComplex.- Returns:
this + addend.- See Also:
-
conjugate
Returns the conjugate of this complex number. The conjugate ofa + biisa - bi.NaNis returned if either the real or imaginary part of this Complex number equalsDouble.NaN.If the imaginary part is infinite, and the real part is not
NaN, the returned value has infinite imaginary part of the opposite sign, e.g. the conjugate of1 + POSITIVE_INFINITY iis1 - NEGATIVE_INFINITY i.- Returns:
- the conjugate of this Complex object.
-
divide
Returns aComplexwhose value is(this / divisor). Implements the definitional formula
but uses prescaling of operands to limit the effects of overflows and underflows in the computation.a + bi ac + bd + (bc - ad)i ----------- = ------------------------- c + di c2 + d2InfiniteandNaNvalues are handled according to the following rules, applied in the order presented:- If either
thisordivisorhas aNaNvalue in either part,NaNis returned. - If
divisorequalsZERO,NaNis returned. - If
thisanddivisorare both infinite,NaNis returned. - If
thisis finite (i.e., has noInfiniteorNaNparts) anddivisoris infinite (one or both parts infinite),ZEROis returned. - If
thisis infinite anddivisoris finite,NaNvalues are returned in the parts of the result if theDoublerules applied to the definitional formula forceNaNresults.
- Specified by:
dividein interfaceFieldElement<Complex>- Parameters:
divisor- Value by which thisComplexis to be divided.- Returns:
this / divisor.- Throws:
NullArgumentException- ifdivisorisnull.
- If either
-
divide
Returns aComplexwhose value is(this / divisor), withdivisorinterpreted as a real number.- Parameters:
divisor- Value by which thisComplexis to be divided.- Returns:
this / divisor.- See Also:
-
reciprocal
Returns the multiplicative inverse ofthiselement.- Specified by:
reciprocalin interfaceFieldElement<Complex>- Returns:
- the inverse of
this.
-
equals
Test for equality with another object. If both the real and imaginary parts of two complex numbers are exactly the same, and neither isDouble.NaN, the two Complex objects are considered to be equal. The behavior is the same as for JDK'sDouble:- All
NaNvalues are considered to be equal, i.e, if either (or both) real and imaginary parts of the complex number are equal toDouble.NaN, the complex number is equal toNaN. - Instances constructed with different representations of zero (i.e. either "0" or "-0") are not considered to be equal.
- All
-
equals
Test for the floating-point equality between Complex objects. It returnstrueif both arguments are equal or within the range of allowed error (inclusive).- Parameters:
x- First value (cannot benull).y- Second value (cannot benull).maxUlps-(maxUlps - 1)is the number of floating point values between the real (resp. imaginary) parts ofxandy.- Returns:
trueif there are fewer thanmaxUlpsfloating point values between the real (resp. imaginary) parts ofxandy.- Since:
- 3.3
- See Also:
-
equals
Returnstrueiff the values are equal as defined byequals(x, y, 1).- Parameters:
x- First value (cannot benull).y- Second value (cannot benull).- Returns:
trueif the values are equal.- Since:
- 3.3
-
equals
Returnstrueif, both for the real part and for the imaginary part, there is no double value strictly between the arguments or the difference between them is within the range of allowed error (inclusive). Returnsfalseif either of the arguments is NaN.- Parameters:
x- First value (cannot benull).y- Second value (cannot benull).eps- Amount of allowed absolute error.- Returns:
trueif the values are two adjacent floating point numbers or they are within range of each other.- Since:
- 3.3
- See Also:
-
equalsWithRelativeTolerance
Returnstrueif, both for the real part and for the imaginary part, there is no double value strictly between the arguments or the relative difference between them is smaller or equal to the given tolerance. Returnsfalseif either of the arguments is NaN.- Parameters:
x- First value (cannot benull).y- Second value (cannot benull).eps- Amount of allowed relative error.- Returns:
trueif the values are two adjacent floating point numbers or they are within range of each other.- Since:
- 3.3
- See Also:
-
hashCode
public int hashCode()Get a hashCode for the complex number. AnyDouble.NaNvalue in real or imaginary part produces the same hash code7. -
getImaginary
public double getImaginary()Access the imaginary part.- Returns:
- the imaginary part.
-
getReal
public double getReal()Access the real part.- Returns:
- the real part.
-
isNaN
public boolean isNaN()Checks whether either or both parts of this complex number isNaN.- Returns:
- true if either or both parts of this complex number is
NaN; false otherwise.
-
isInfinite
public boolean isInfinite()Checks whether either the real or imaginary part of this complex number takes an infinite value (eitherDouble.POSITIVE_INFINITYorDouble.NEGATIVE_INFINITY) and neither part isNaN.- Returns:
- true if one or both parts of this complex number are infinite
and neither part is
NaN.
-
multiply
Returns aComplexwhose value isthis * factor. Implements preliminary checks forNaNand infinity followed by the definitional formula:
Returns(a + bi)(c + di) = (ac - bd) + (ad + bc)iNaNif eitherthisorfactorhas one or moreNaNparts.Returns
INFif neitherthisnorfactorhas one or moreNaNparts and if eitherthisorfactorhas one or more infinite parts (same result is returned regardless of the sign of the components).Returns finite values in components of the result per the definitional formula in all remaining cases.
- Specified by:
multiplyin interfaceFieldElement<Complex>- Parameters:
factor- value to be multiplied by thisComplex.- Returns:
this * factor.- Throws:
NullArgumentException- iffactorisnull.
-
multiply
Returns aComplexwhose value isthis * factor, withfactorinterpreted as a integer number.- Specified by:
multiplyin interfaceFieldElement<Complex>- Parameters:
factor- value to be multiplied by thisComplex.- Returns:
this * factor.- See Also:
-
multiply
Returns aComplexwhose value isthis * factor, withfactorinterpreted as a real number.- Parameters:
factor- value to be multiplied by thisComplex.- Returns:
this * factor.- See Also:
-
negate
Returns aComplexwhose value is(-this). ReturnsNaNif either real or imaginary part of this Complex number isDouble.NaN.- Specified by:
negatein interfaceFieldElement<Complex>- Returns:
-this.
-
subtract
Returns aComplexwhose value is(this - subtrahend). Uses the definitional formula
If either(a + bi) - (c + di) = (a-c) + (b-d)ithisorsubtrahendhas aNaN]value in either part,NaNis returned; otherwise infinite andNaNvalues are returned in the parts of the result according to the rules forDoublearithmetic.- Specified by:
subtractin interfaceFieldElement<Complex>- Parameters:
subtrahend- value to be subtracted from thisComplex.- Returns:
this - subtrahend.- Throws:
NullArgumentException- ifsubtrahendisnull.
-
subtract
Returns aComplexwhose value is(this - subtrahend).- Parameters:
subtrahend- value to be subtracted from thisComplex.- Returns:
this - subtrahend.- See Also:
-
acos
Compute the inverse cosine of this complex number. Implements the formula:
Returnsacos(z) = -i (log(z + i (sqrt(1 - z<sup>2</sup>))))NaNif either real or imaginary part of the input argument isNaNor infinite.- Returns:
- the inverse cosine of this complex number.
- Since:
- 1.2
-
asin
Compute the inverse sine of this complex number. Implements the formula:asin(z) = -i (log(sqrt(1 - z<sup>2</sup>) + iz))Returns
NaNif either real or imaginary part of the input argument isNaNor infinite.- Returns:
- the inverse sine of this complex number.
- Since:
- 1.2
-
atan
Compute the inverse tangent of this complex number. Implements the formula:atan(z) = (i/2) log((i + z)/(i - z))Returns
NaNif either real or imaginary part of the input argument isNaNor infinite.- Returns:
- the inverse tangent of this complex number
- Since:
- 1.2
-
cos
Compute the cosine of this complex number. Implements the formula:cos(a + bi) = cos(a)cosh(b) - sin(a)sinh(b)iwhere the (real) functions on the right-hand side are
FastMath.sin(double),FastMath.cos(double),FastMath.cosh(double)andFastMath.sinh(double).Returns
NaNif either real or imaginary part of the input argument isNaN.Infinite values in real or imaginary parts of the input may result in infinite or NaN values returned in parts of the result.
Examples:
cos(1 ± INFINITY i) = 1 ∓ INFINITY i cos(±INFINITY + i) = NaN + NaN i cos(±INFINITY ± INFINITY i) = NaN + NaN i- Returns:
- the cosine of this complex number.
- Since:
- 1.2
-
cosh
Compute the hyperbolic cosine of this complex number. Implements the formula:
where the (real) functions on the right-hand side arecosh(a + bi) = cosh(a)cos(b) + sinh(a)sin(b)iFastMath.sin(double),FastMath.cos(double),FastMath.cosh(double)andFastMath.sinh(double).Returns
Infinite values in real or imaginary parts of the input may result in infinite or NaN values returned in parts of the result.NaNif either real or imaginary part of the input argument isNaN.Examples:
cosh(1 ± INFINITY i) = NaN + NaN i cosh(±INFINITY + i) = INFINITY ± INFINITY i cosh(±INFINITY ± INFINITY i) = NaN + NaN i- Returns:
- the hyperbolic cosine of this complex number.
- Since:
- 1.2
-
exp
Compute the exponential function of this complex number. Implements the formula:
where the (real) functions on the right-hand side areexp(a + bi) = exp(a)cos(b) + exp(a)sin(b)iFastMath.exp(double),FastMath.cos(double), andFastMath.sin(double).Returns
Infinite values in real or imaginary parts of the input may result in infinite or NaN values returned in parts of the result.NaNif either real or imaginary part of the input argument isNaN.Examples:
exp(1 ± INFINITY i) = NaN + NaN i exp(INFINITY + i) = INFINITY + INFINITY i exp(-INFINITY + i) = 0 + 0i exp(±INFINITY ± INFINITY i) = NaN + NaN i- Returns:
ethis.- Since:
- 1.2
-
log
Compute the natural logarithm of this complex number. Implements the formula:
where ln on the right hand side islog(a + bi) = ln(|a + bi|) + arg(a + bi)iFastMath.log(double),|a + bi|is the modulus,abs(), andarg(a + bi) =FastMath.atan2(double, double)(b, a).Returns
Infinite (or critical) values in real or imaginary parts of the input may result in infinite or NaN values returned in parts of the result.NaNif either real or imaginary part of the input argument isNaN.Examples:
log(1 ± INFINITY i) = INFINITY ± (π/2)i log(INFINITY + i) = INFINITY + 0i log(-INFINITY + i) = INFINITY + πi log(INFINITY ± INFINITY i) = INFINITY ± (π/4)i log(-INFINITY ± INFINITY i) = INFINITY ± (3π/4)i log(0 + 0i) = -INFINITY + 0i- Returns:
- the value
ln this, the natural logarithm ofthis. - Since:
- 1.2
-
pow
Returns of value of this complex number raised to the power ofx. Implements the formula:
whereyx = exp(x·log(y))expandlogareexp()andlog(), respectively.Returns
NaNif either real or imaginary part of the input argument isNaNor infinite, or ifyequalsZERO.- Parameters:
x- exponent to which thisComplexis to be raised.- Returns:
thisx.- Throws:
NullArgumentException- if x isnull.- Since:
- 1.2
-
pow
Returns of value of this complex number raised to the power ofx.- Parameters:
x- exponent to which thisComplexis to be raised.- Returns:
thisx.- See Also:
-
sin
Compute the sine of this complex number. Implements the formula:
where the (real) functions on the right-hand side aresin(a + bi) = sin(a)cosh(b) - cos(a)sinh(b)iFastMath.sin(double),FastMath.cos(double),FastMath.cosh(double)andFastMath.sinh(double).Returns
NaNif either real or imaginary part of the input argument isNaN.Infinite values in real or imaginary parts of the input may result in infinite or
NaNvalues returned in parts of the result.Examples:
sin(1 ± INFINITY i) = 1 ± INFINITY i sin(±INFINITY + i) = NaN + NaN i sin(±INFINITY ± INFINITY i) = NaN + NaN i- Returns:
- the sine of this complex number.
- Since:
- 1.2
-
sinh
Compute the hyperbolic sine of this complex number. Implements the formula:
where the (real) functions on the right-hand side aresinh(a + bi) = sinh(a)cos(b)) + cosh(a)sin(b)iFastMath.sin(double),FastMath.cos(double),FastMath.cosh(double)andFastMath.sinh(double).Returns
NaNif either real or imaginary part of the input argument isNaN.Infinite values in real or imaginary parts of the input may result in infinite or NaN values returned in parts of the result.
Examples:
sinh(1 ± INFINITY i) = NaN + NaN i sinh(±INFINITY + i) = ± INFINITY + INFINITY i sinh(±INFINITY ± INFINITY i) = NaN + NaN i- Returns:
- the hyperbolic sine of
this. - Since:
- 1.2
-
sqrt
Compute the square root of this complex number. Implements the following algorithm to computesqrt(a + bi):- Let
t = sqrt((|a| + |a + bi|) / 2) if
a ≥ 0returnt + (b/2t)ielse return|b|/2t + sign(b)t i
|a| =FastMath.abs(int)(a)|a + bi| =abs()(a + bi)sign(b) =copySign(1d, b)
Returns
Infinite values in real or imaginary parts of the input may result in infinite or NaN values returned in parts of the result.NaNif either real or imaginary part of the input argument isNaN.Examples:
sqrt(1 ± INFINITY i) = INFINITY + NaN i sqrt(INFINITY + i) = INFINITY + 0i sqrt(-INFINITY + i) = 0 + INFINITY i sqrt(INFINITY ± INFINITY i) = INFINITY + NaN i sqrt(-INFINITY ± INFINITY i) = NaN ± INFINITY i- Returns:
- the square root of
this. - Since:
- 1.2
- Let
-
sqrt1z
Compute the square root of1 - this2for this complex number. Computes the result directly assqrt(ONE.subtract(z.multiply(z))).Returns
Infinite values in real or imaginary parts of the input may result in infinite or NaN values returned in parts of the result.NaNif either real or imaginary part of the input argument isNaN.- Returns:
- the square root of
1 - this2. - Since:
- 1.2
-
tan
Compute the tangent of this complex number. Implements the formula:
where the (real) functions on the right-hand side aretan(a + bi) = sin(2a)/(cos(2a)+cosh(2b)) + [sinh(2b)/(cos(2a)+cosh(2b))]iFastMath.sin(double),FastMath.cos(double),FastMath.cosh(double)andFastMath.sinh(double).Returns
Infinite (or critical) values in real or imaginary parts of the input may result in infinite or NaN values returned in parts of the result.NaNif either real or imaginary part of the input argument isNaN.Examples:
tan(a ± INFINITY i) = 0 ± i tan(±INFINITY + bi) = NaN + NaN i tan(±INFINITY ± INFINITY i) = NaN + NaN i tan(±π/2 + 0 i) = ±INFINITY + NaN i- Returns:
- the tangent of
this. - Since:
- 1.2
-
tanh
Compute the hyperbolic tangent of this complex number. Implements the formula:
where the (real) functions on the right-hand side aretan(a + bi) = sinh(2a)/(cosh(2a)+cos(2b)) + [sin(2b)/(cosh(2a)+cos(2b))]iFastMath.sin(double),FastMath.cos(double),FastMath.cosh(double)andFastMath.sinh(double).Returns
Infinite values in real or imaginary parts of the input may result in infinite or NaN values returned in parts of the result.NaNif either real or imaginary part of the input argument isNaN.Examples:
tanh(a ± INFINITY i) = NaN + NaN i tanh(±INFINITY + bi) = ±1 + 0 i tanh(±INFINITY ± INFINITY i) = NaN + NaN i tanh(0 + (π/2)i) = NaN + INFINITY i- Returns:
- the hyperbolic tangent of
this. - Since:
- 1.2
-
getArgument
public double getArgument()Compute the argument of this complex number. The argument is the angle phi between the positive real axis and the point representing this number in the complex plane. The value returned is between -PI (not inclusive) and PI (inclusive), with negative values returned for numbers with negative imaginary parts.If either real or imaginary part (or both) is NaN, NaN is returned. Infinite parts are handled as
Math.atan2handles them, essentially treating finite parts as zero in the presence of an infinite coordinate and returning a multiple of pi/4 depending on the signs of the infinite parts. See the javadoc forMath.atan2for full details.- Returns:
- the argument of
this.
-
nthRoot
Computes the n-th roots of this complex number. The nth roots are defined by the formula:
forzk = abs1/n (cos(phi + 2πk/n) + i (sin(phi + 2πk/n))k=0, 1, ..., n-1, whereabsandphiare respectively themodulusandargumentof this complex number.If one or both parts of this complex number is NaN, a list with just one element,
NaNis returned. if neither part is NaN, but at least one part is infinite, the result is a one-element list containingINF.- Parameters:
n- Degree of root.- Returns:
- a List of all
n-th roots ofthis. - Throws:
NotPositiveException- ifn <= 0.- Since:
- 2.0
-
createComplex
Create a complex number given the real and imaginary parts.- Parameters:
realPart- Real part.imaginaryPart- Imaginary part.- Returns:
- a new complex number instance.
- Since:
- 1.2
- See Also:
-
valueOf
Create a complex number given the real and imaginary parts.- Parameters:
realPart- Real part.imaginaryPart- Imaginary part.- Returns:
- a Complex instance.
-
valueOf
Create a complex number given only the real part.- Parameters:
realPart- Real part.- Returns:
- a Complex instance.
-
readResolve
Resolve the transient fields in a deserialized Complex Object. Subclasses will need to overridecreateComplex(double, double)to deserialize properly.- Returns:
- A Complex instance with all fields resolved.
- Since:
- 2.0
-
getField
Get theFieldto which the instance belongs.- Specified by:
getFieldin interfaceFieldElement<Complex>- Returns:
Fieldto which the instance belongs
-
toString
-