Class Complex
- All Implemented Interfaces:
java.io.Serializable
public final class Complex
extends java.lang.Object
implements java.io.Serializable
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 class Precision conforms with
IEEE-754 while this class conforms with the standard behavior for Java
object types.
- See Also:
- Serialized Form
-
Field Summary
-
Method Summary
Modifier and Type Method Description doubleabs()Return the absolute value of this complex number.Complexacos()Compute the inverse cosine of this complex number.Complexacosh()Compute the inverse hyperbolic cosine of this complex number.Complexadd(double addend)Returns aComplexwhose value is(this + addend), withaddendinterpreted as a real number.Complexadd(Complex addend)Returns aComplexwhose value is(this + addend).doublearg()Compute the argument of this complex number.Complexasin()Compute the inverse sine of this complex number.Complexasinh()Compute the inverse hyperbolic sine of this complex number.Complexatan()Compute the inverse tangent of this complex number.Complexatanh()Compute the inverse hyperbolic tangent of this complex number.Complexconj()Returns the conjugate of this complex number.Complexconjugate()Returns the conjugate of this complex number.Complexcos()Compute the cosine of this complex number.Complexcosh()Compute the hyperbolic cosine of this complex number.Complexdivide(double divisor)Returns aComplexwhose value is(this / divisor), withdivisorinterpreted as a real number.Complexdivide(Complex divisor)Returns aComplexwhose value is(this / divisor).booleanequals(java.lang.Object other)Test for equality with another object.static booleanequals(Complex x, Complex y)Returnstrueiff the values are equal as defined byequals(x, y, 1).static booleanequals(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 difference between them is within the range of allowed error (inclusive).static booleanequals(Complex x, Complex y, int maxUlps)Test 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.Complexexp()Compute the exponential function of this complex number.doublegetArgument()Compute the argument of this complex number.doublegetImaginary()Access the imaginary part.doublegetReal()Access the real part.inthashCode()Get a hash code for the complex number.doubleimag()Access the imaginary part (C++ grammar)booleanisInfinite()Returns true if either real or imaginary component of the Complex is InfinitebooleanisNaN()Returns true if either real or imaginary component of the Complex is NaNComplexlog()Compute the natural logarithm of this complex number.Complexlog10()Compute the base 10 or common logarithm of this complex number.Complexmultiply(double factor)Returns aComplexwhose value isthis * factor, withfactorinterpreted as a real number.Complexmultiply(int factor)Returns aComplexwhose value isthis * factor, withfactorinterpreted as a integer number.Complexmultiply(Complex factor)Returns aComplexwhose value isthis * factor.Complexnegate()Returns aComplexwhose value is(-this).java.util.List<Complex>nthRoot(int n)Computes the n-th roots of this complex number.static ComplexofCartesian(double real, double imaginary)Create a complex number given the real and imaginary parts.static ComplexofCis(double x)For a real constructor argument x, returns a new Complex object c wherec = cos(x) + i sin (x)static ComplexofPolar(double r, double theta)Creates a Complex from its polar representation.static ComplexofReal(double real)Create a complex number given the real part.static Complexparse(java.lang.String s)Parses a string that would be produced bytoString()and instantiates the corresponding object.Complexpow(double x)Returns of value of this complex number raised to the power ofx.Complexpow(Complex x)Returns of value of this complex number raised to the power ofx.Complexproj()Returns projection of this complex number onto the Riemann sphere, i.e.doublereal()Access the real part (C++ grammar)Complexreciprocal()Returns the multiplicative inverse of this instance.Complexsin()Compute the sine of this complex number.Complexsinh()Compute the hyperbolic sine of this complex number.Complexsqrt()Compute the square root of this complex number.Complexsquare()Compute the square of this complex number.Complexsubtract(double subtrahend)Returns aComplexwhose value is(this - subtrahend).Complexsubtract(Complex subtrahend)Returns aComplexwhose value is(this - subtrahend).Complextan()Compute the tangent of this complex number.Complextanh()Compute the hyperbolic tangent of this complex number.java.lang.StringtoString()
-
Field Details
-
Method Details
-
ofCartesian
Create a complex number given the real and imaginary parts.- Parameters:
real- Real part.imaginary- Imaginary part.- Returns:
Complexobject
-
ofReal
Create a complex number given the real part.- Parameters:
real- Real part.- Returns:
Complexobject
-
ofPolar
Creates a Complex from its polar representation. Ifris infinite andthetais finite, infinite or NaN values may be returned in parts of the result, following the rules for double arithmetic.Examples:
polar2Complex(INFINITY, \(\pi\)) = INFINITY + INFINITY i polar2Complex(INFINITY, 0) = INFINITY + NaN i polar2Complex(INFINITY, \(-\frac{\pi}{4}\)) = INFINITY - INFINITY i polar2Complex(INFINITY, \(5\frac{\pi}{4}\)) = -INFINITY - INFINITY i- Parameters:
r- the modulus of the complex number to createtheta- the argument of the complex number to create- Returns:
Complex
-
ofCis
For a real constructor argument x, returns a new Complex object c wherec = cos(x) + i sin (x)- Parameters:
x-doubleto build the cis number- Returns:
Complex
-
parse
Parses a string that would be produced bytoString()and instantiates the corresponding object.- Parameters:
s- String representation.- Returns:
- an instance.
- Throws:
java.lang.IllegalArgumentException- if the string does not conform to the specification.
-
isNaN
public boolean isNaN()Returns true if either real or imaginary component of the Complex is NaN- Returns:
boolean
-
isInfinite
public boolean isInfinite()Returns true if either real or imaginary component of the Complex is Infinite- Returns:
boolean
-
proj
Returns projection of this complex number onto the Riemann sphere, i.e. all infinities (including those with an NaN component) project onto real infinity, as described in the IEEE and ISO C standards.- Returns:
Complexprojected onto the Riemann sphere.
-
abs
public double abs()Return the absolute value of this complex number. This code follows the ISO C Standard, Annex G, in calculating the returned value (i.e. the hypot(x,y) method) and in handling of NaNs.- Returns:
- the absolute value.
-
add
Returns aComplexwhose value is(this + addend). Uses the definitional formula(a + bi) + (c + di) = (a+c) + (b+d)i- Parameters:
addend- Value to be added to thisComplex.- Returns:
this + addend.
-
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:
add(Complex)
-
conjugate
Returns the conjugate of this complex number. The conjugate ofa + biisa - bi.- Returns:
- the conjugate of this complex object.
-
conj
Returns the conjugate of this complex number. C++11 grammar.- Returns:
- the conjugate of this complex object.
-
divide
Returns aComplexwhose value is(this / divisor). Implements the definitional formula
Recalculates to recover infinities as specified in C.99 standard G.5.1. Method is fully in accordance with C++11 standards for complex numbers.a + bi ac + bd + (bc - ad)i ----------- = ------------------------- c + di c2 + d2- Parameters:
divisor- Value by which thisComplexis to be divided.- Returns:
this / divisor.
-
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:
divide(Complex)
-
reciprocal
Returns the multiplicative inverse of this instance.- Returns:
1 / this.- See Also:
divide(Complex)
-
equals
public boolean equals(java.lang.Object other)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.
- Overrides:
equalsin classjava.lang.Object- Parameters:
other- Object to test for equality with this instance.- Returns:
trueif the objects are equal,falseif object isnull, not an instance ofComplex, or not equal to this instance.
- 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.- See Also:
Precision.equals(double,double,int)
-
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.
-
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.- See Also:
Precision.equals(double,double,double)
-
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.- See Also:
Precision.equalsWithRelativeTolerance(double,double,double)
-
hashCode
public int hashCode()Get a hash code for the complex number. AnyDouble.NaNvalue in real or imaginary part produces the same hash code7.- Overrides:
hashCodein classjava.lang.Object- Returns:
- a hash code value for this object.
-
getImaginary
public double getImaginary()Access the imaginary part.- Returns:
- the imaginary part.
-
imag
public double imag()Access the imaginary part (C++ grammar)- Returns:
- the imaginary part.
-
getReal
public double getReal()Access the real part.- Returns:
- the real part.
-
real
public double real()Access the real part (C++ grammar)- Returns:
- the real part.
-
multiply
Returns aComplexwhose value isthis * factor. Implements the definitional formula:(a + bi)(c + di) = (ac - bd) + (ad + bc)iRecalculates to recover infinities as specified in C.99 standard G.5.1. Method is fully in accordance with C++11 standards for complex numbers.- Parameters:
factor- value to be multiplied by thisComplex.- Returns:
this * factor.
-
multiply
Returns aComplexwhose value isthis * factor, withfactorinterpreted as a integer number.- Parameters:
factor- value to be multiplied by thisComplex.- Returns:
this * factor.- See Also:
multiply(Complex)
-
multiply
Returns aComplexwhose value isthis * factor, withfactorinterpreted as a real number.- Parameters:
factor- value to be multiplied by thisComplex.- Returns:
this * factor.- See Also:
multiply(Complex)
-
negate
Returns aComplexwhose value is(-this).- Returns:
-this.
-
subtract
Returns aComplexwhose value is(this - subtrahend). Uses the definitional formula(a + bi) - (c + di) = (a-c) + (b-d)i- Parameters:
subtrahend- value to be subtracted from thisComplex.- Returns:
this - subtrahend.
-
subtract
Returns aComplexwhose value is(this - subtrahend).- Parameters:
subtrahend- value to be subtracted from thisComplex.- Returns:
this - subtrahend.- See Also:
subtract(Complex)
-
acos
Compute the inverse cosine of this complex number. Implements the formula:acos(z) = -i (log(z + i (sqrt(1 - z<sup>2</sup>))))- Returns:
- the inverse cosine of this complex number.
-
asin
Compute the inverse sine of this complex number.asin(z) = -i (log(sqrt(1 - z<sup>2</sup>) + iz))- Returns:
- the inverse sine of this complex number
-
atan
Compute the inverse tangent of this complex number. Implements the formula:atan(z) = (i/2) log((i + z)/(i - z))- Returns:
- the inverse tangent of this complex number
-
asinh
Compute the inverse hyperbolic sine of this complex number. Implements the formula:asinh(z) = log(z+sqrt(z^2+1))- Returns:
- the inverse hyperbolic cosine of this complex number
-
atanh
Compute the inverse hyperbolic tangent of this complex number. Implements the formula:atanh(z) = log((1+z)/(1-z))/2- Returns:
- the inverse hyperbolic cosine of this complex number
-
acosh
Compute the inverse hyperbolic cosine of this complex number. Implements the formula:acosh(z) = log(z+sqrt(z^2-1))- Returns:
- the inverse hyperbolic cosine of this complex number
-
square
Compute the square of this complex number.- Returns:
- square of this complex number
-
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
Math.sin(double),Math.cos(double),Math.cosh(double)andMath.sinh(double).- Returns:
- the cosine of this complex number.
-
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)iMath.sin(double),Math.cos(double),Math.cosh(double)andMath.sinh(double).- Returns:
- the hyperbolic cosine of this complex number.
-
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)iMath.exp(double),Math.cos(double), andMath.sin(double).- Returns:
ethis.
-
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)iMath.log(double),|a + bi|is the modulus,abs(), andarg(a + bi) =Math.atan2(double, double)(b, a).- Returns:
- the value
ln this, the natural logarithm ofthis.
-
log10
Compute the base 10 or common logarithm of this complex number.- Returns:
- the base 10 logarithm of
this.
-
pow
Returns of value of this complex number raised to the power ofx. Implements the formula:
whereyx = exp(x·log(y))expandlogareexp()andlog(), respectively.- Parameters:
x- exponent to which thisComplexis to be raised.- Returns:
thisx.
-
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:
pow(Complex)
-
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)iMath.sin(double),Math.cos(double),Math.cosh(double)andMath.sinh(double).- Returns:
- the sine of this complex number.
-
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)iMath.sin(double),Math.cos(double),Math.cosh(double)andMath.sinh(double).- Returns:
- the hyperbolic sine of
this.
-
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| =Math.abs(int)(a)|a + bi| =abs()(a + bi)sign(b) =copySign(1d, b)
- Returns:
- the square root of
this.
- Let
-
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))]iMath.sin(double),Math.cos(double),Math.cosh(double)andMath.sinh(double).- Returns:
- the tangent of
this.
-
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))]iMath.sin(double),Math.cos(double),Math.cosh(double)andMath.sinh(double).- Returns:
- the hyperbolic tangent of
this.
-
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.
-
arg
public double arg()Compute the argument of this complex number. C++11 syntax- 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,
NaN + NaN iis 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.
-
toString
public java.lang.String toString()- Overrides:
toStringin classjava.lang.Object
-