Class Complex
- All Implemented Interfaces:
Serializable
This class is immutable. All arithmetic will create a new instance for the result.
Arithmetic in this class conforms to the C99 standard for complex numbers defined in ISO/IEC 9899, Annex G. Methods have been named using the equivalent method in ISO C99. The behavior for special cases is listed as defined in C99.
For functions \( f \) which obey the conjugate equality \( conj(f(z)) = f(conj(z)) \), the specifications for the upper half-plane imply the specifications for the lower half-plane.
For functions that are either odd, \( f(z) = -f(-z) \), or even, \( f(z) = f(-z) \), the specifications for the first quadrant imply the specifications for the other three quadrants.
Special cases of branch cuts
for multivalued functions adopt the principle value convention from C99. Specials cases
from C99 that raise the "invalid" or "divide-by-zero"
floating-point
exceptions return the documented value without an explicit mechanism to notify
of the exception case, that is no exceptions are thrown during computations in-line with
the convention of the corresponding single-valued functions in
Math.
These cases are documented in the method special cases as "invalid" or "divide-by-zero"
floating-point operation.
Note: Invalid floating-point exception cases will result in a complex number where the
cardinality of NaN component parts has increased as a real or imaginary part could
not be computed and is set to NaN.
- See Also:
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptiondoubleabs()Returns the absolute value of this complex number.acos()Returns the inverse cosine of this complex number.acosh()Returns the inverse hyperbolic 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).addImaginary(double addend) Returns aComplexwhose value is(this + addend), withaddendinterpreted as an imaginary number.doublearg()Returns the argument of this complex number.asin()Returns the inverse sine of this complex number.asinh()Returns the inverse hyperbolic sine of this complex number.atan()Returns the inverse tangent of this complex number.atanh()Returns the inverse hyperbolic tangent of this complex number.conj()Returns the conjugate \( \overline{z} \) of this complex number \( z \).cos()Returns the cosine of this complex number.cosh()Returns the hyperbolic cosine of this complex number.divide(double divisor) Returns aComplexwhose value is(this / divisor), withdivisorinterpreted as a real number.Returns aComplexwhose value is(this / divisor).divideImaginary(double divisor) Returns aComplexwhose value is(this / divisor), withdivisorinterpreted as an imaginary number.booleanTest for equality with another object.exp()Returns the exponential function of this complex number.doubleGets the imaginary part \( b \) of this complex number \( (a + i b) \).doublegetReal()Gets the real part \( a \) of this complex number \( (a + i b) \).inthashCode()Gets a hash code for the complex number.doubleimag()Gets the imaginary part \( b \) of this complex number \( (a + i b) \).booleanisFinite()Returnstrueif both real and imaginary component of the complex number are finite.booleanReturnstrueif either real or imaginary component of the complex number is infinite.booleanisNaN()Returnstrueif either the real or imaginary component of the complex number is NaN and the complex number is not infinite.log()Returns the natural logarithm of this complex number.log10()Returns the base 10 common logarithm of this complex number.multiply(double factor) Returns aComplexwhose value isthis * factor, withfactorinterpreted as a real number.Returns aComplexwhose value isthis * factor.multiplyImaginary(double factor) Returns aComplexwhose value isthis * factor, withfactorinterpreted as an imaginary number.negate()Returns aComplexwhose value is the negation of both the real and imaginary parts of complex number \( z \).doublenorm()Returns the squared norm value of this complex number.nthRoot(int n) Returns 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) Create a complex cis number.static ComplexofPolar(double rho, double theta) Creates a complex number from its polar representation using modulusrho(\( \rho \)) and phase angletheta(\( \theta \)). \[ \begin{aligned} x &= \rho \cos(\theta) \\ y &= \rho \sin(\theta) \end{aligned} \]static ComplexReturns aComplexinstance representing the specified strings.pow(double x) Returns the complex power of this complex number raised to the power ofx, withxinterpreted as a real number.Returns the complex power of this complex number raised to the power ofx.proj()Returns the projection of this complex number onto the Riemann sphere.doublereal()Gets the real part \( a \) of this complex number \( (a + i b) \).sin()Returns the sine of this complex number.sinh()Returns the hyperbolic sine of this complex number.sqrt()Returns the square root of this complex number.subtract(double subtrahend) Returns aComplexwhose value is(this - subtrahend), withsubtrahendinterpreted as a real number.Returns aComplexwhose value is(this - subtrahend).subtractFrom(double minuend) Returns aComplexwhose value is(minuend - this), withminuendinterpreted as a real number.subtractFromImaginary(double minuend) Returns aComplexwhose value is(this - subtrahend), withminuendinterpreted as an imaginary number.subtractImaginary(double subtrahend) Returns aComplexwhose value is(this - subtrahend), withsubtrahendinterpreted as an imaginary number.tan()Returns the tangent of this complex number.tanh()Returns the hyperbolic tangent of this complex number.toString()Returns a string representation of the complex number.
-
Field Details
-
I
A complex number representing \( i \), the square root of \( -1 \).\( (0 + i 1) \).
-
ONE
A complex number representing one.\( (1 + i 0) \).
-
ZERO
A complex number representing zero.\( (0 + i 0) \).
-
-
Method Details
-
ofCartesian
Create a complex number given the real and imaginary parts.- Parameters:
real- Real part.imaginary- Imaginary part.- Returns:
Complexnumber.
-
ofPolar
Creates a complex number from its polar representation using modulusrho(\( \rho \)) and phase angletheta(\( \theta \)). \[ \begin{aligned} x &= \rho \cos(\theta) \\ y &= \rho \sin(\theta) \end{aligned} \]Requires that
rhois non-negative and non-NaN andthetais finite; otherwise returns a complex with NaN real and imaginary parts. Arhovalue of-0.0is considered negative and an invalid modulus.A non-NaN complex number constructed using this method will satisfy the following to within floating-point error when
thetais in the range \( -\pi\ \lt \theta \leq \pi \):Complex.ofPolar(rho, theta).abs() == rho Complex.ofPolar(rho, theta).arg() == theta
If
rhois infinite then the resulting parts may be infinite or NaN following the rules for double arithmetic, for example:ofPolar(\( -0.0 \),\( 0 \)) =\( \text{NaN} + i \text{NaN} \)ofPolar(\( 0.0 \),\( 0 \)) =\( 0 + i 0 \)ofPolar(\( 1 \),\( 0 \)) =\( 1 + i 0 \)ofPolar(\( 1 \),\( \pi \)) =\( -1 + i \sin(\pi) \)ofPolar(\( \infty \),\( \pi \)) =\( -\infty + i \infty \)ofPolar(\( \infty \),\( 0 \)) =\( -\infty + i \text{NaN} \)ofPolar(\( \infty \),\( -\frac{\pi}{4} \)) =\( \infty - i \infty \)ofPolar(\( \infty \),\( 5\frac{\pi}{4} \)) =\( -\infty - i \infty \)
This method is the functional equivalent of the C++ method
std::polar.- Parameters:
rho- The modulus of the complex number.theta- The argument of the complex number.- Returns:
Complexnumber.- See Also:
-
ofCis
Create a complex cis number. This is also known as the complex exponential: \[ \text{cis}(x) = e^{ix} = \cos(x) + i \sin(x) \]- Parameters:
x-doubleto build the cis number.- Returns:
Complexcis number.- See Also:
-
parse
Returns aComplexinstance representing the specified strings.If
sisnull, then aNullPointerExceptionis thrown.The string must be in a format compatible with that produced by
Complex.toString(). The format expects a start and end parentheses surrounding two numeric parts split by a separator. Leading and trailing spaces are allowed around each numeric part. Each numeric part is parsed usingDouble.parseDouble(String). The parts are interpreted as the real and imaginary parts of the complex number.Examples of valid strings and the equivalent
Complexare shown below:"(0,0)" = Complex.ofCartesian(0, 0) "(0.0,0.0)" = Complex.ofCartesian(0, 0) "(-0.0, 0.0)" = Complex.ofCartesian(-0.0, 0) "(-1.23, 4.56)" = Complex.ofCartesian(-1.23, 4.56) "(1e300,-1.1e-2)" = Complex.ofCartesian(1e300, -1.1e-2)
- Parameters:
s- String representation.- Returns:
Complexnumber.- Throws:
NullPointerException- if the string is null.NumberFormatException- if the string does not contain a parsable complex number.- See Also:
-
getReal
Gets the real part \( a \) of this complex number \( (a + i b) \).- Returns:
- The real part.
-
real
Gets the real part \( a \) of this complex number \( (a + i b) \).This method is the equivalent of the C++ method
std::complex::real.- Returns:
- The real part.
- See Also:
-
getImaginary
Gets the imaginary part \( b \) of this complex number \( (a + i b) \).- Returns:
- The imaginary part.
-
imag
Gets the imaginary part \( b \) of this complex number \( (a + i b) \).This method is the equivalent of the C++ method
std::complex::imag.- Returns:
- The imaginary part.
- See Also:
-
abs
Returns the absolute value of this complex number. This is also called complex norm, modulus, or magnitude.\[ \text{abs}(x + i y) = \sqrt{(x^2 + y^2)} \]
Special cases:
abs(x + iy) == abs(y + ix) == abs(x - iy).- If
zis ±∞ + iy for any y, returns +∞. - If
zis x + iNaN for non-infinite x, returns NaN. - If
zis x + i0, returns |x|.
The cases ensure that if either component is infinite then the result is positive infinity. If either component is NaN and this is not
infinitethen the result is NaN.This method follows the ISO C Standard, Annex G, in calculating the returned value without intermediate overflow or underflow.
The computed result will be within 1 ulp of the exact result.
- Returns:
- The absolute value.
- See Also:
-
arg
Returns 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, then the result is NaN. Infinite parts are handled as Math.atan2(double, double) handles them, essentially treating finite parts as zero in the presence of an infinite coordinate and returning a multiple of \( \frac{\pi}{4} \) depending on the signs of the infinite parts.
This code follows the ISO C Standard, Annex G, in calculating the returned value using the
atan2(y, x)method for complex \( x + iy \).- Returns:
- The argument of this complex number.
- See Also:
-
norm
Returns the squared norm value of this complex number. This is also called the absolute square.\[ \text{norm}(x + i y) = x^2 + y^2 \]
If either component is infinite then the result is positive infinity. If either component is NaN and this is not
infinitethen the result is NaN.Note: This method may not return the same value as the square of
abs()as that method uses an extended precision computation.norm()can be used as a faster alternative thanabs()for ranking by magnitude. If used for ranking any overflow to infinity will create an equal ranking for values that may be still distinguished byabs().- Returns:
- The square norm value.
- See Also:
-
isNaN
Returnstrueif either the real or imaginary component of the complex number is NaN and the complex number is not infinite.Note that:
- There is more than one complex number that can return
true. - Different representations of NaN can be distinguished by the
Complex.equals(Object)method.
- Returns:
trueif this instance contains NaN and no infinite parts.- See Also:
- There is more than one complex number that can return
-
isInfinite
Returnstrueif either real or imaginary component of the complex number is infinite.Note: A complex number with at least one infinite part is regarded as an infinity (even if its other part is a NaN).
- Returns:
trueif this instance contains an infinite value.- See Also:
-
isFinite
Returnstrueif both real and imaginary component of the complex number are finite.- Returns:
trueif this instance contains finite values.- See Also:
-
conj
Returns the conjugate \( \overline{z} \) of this complex number \( z \).\[ \begin{aligned} z &= a + i b \\ \overline{z} &= a - i b \end{aligned}\]
- Returns:
- The conjugate (\( \overline{z} \)) of this complex number.
-
negate
Returns aComplexwhose value is the negation of both the real and imaginary parts of complex number \( z \).\[ \begin{aligned} z &= a + i b \\ -z &= -a - i b \end{aligned} \]
- Returns:
- \( -z \).
-
proj
Returns the projection of this complex number onto the Riemann sphere.\( z \) projects to \( z \), except that all complex infinities (even those with one infinite part and one NaN part) project to positive infinity on the real axis. If \( z \) has an infinite part, then
z.proj()shall be equivalent to:return Complex.ofCartesian(Double.POSITIVE_INFINITY, Math.copySign(0.0, z.imag());
- Returns:
- \( z \) projected onto the Riemann sphere.
- See Also:
-
add
Returns aComplexwhose value is(this + addend). Implements the formula:\[ (a + i b) + (c + i d) = (a + c) + i (b + d) \]
- Parameters:
addend- Value to be added to this complex number.- Returns:
this + addend.- See Also:
-
add
Returns aComplexwhose value is(this + addend), withaddendinterpreted as a real number. Implements the formula:\[ (a + i b) + c = (a + c) + i b \]
This method is included for compatibility with ISO C99 which defines arithmetic between real-only and complex numbers.
Note: This method preserves the sign of the imaginary component \( b \) if it is
-0.0. The sign would be lost if adding \( (c + i 0) \) usingadd(Complex.ofCartesian(addend, 0))since-0.0 + 0.0 = 0.0.- Parameters:
addend- Value to be added to this complex number.- Returns:
this + addend.- See Also:
-
addImaginary
Returns aComplexwhose value is(this + addend), withaddendinterpreted as an imaginary number. Implements the formula:\[ (a + i b) + i d = a + i (b + d) \]
This method is included for compatibility with ISO C99 which defines arithmetic between imaginary-only and complex numbers.
Note: This method preserves the sign of the real component \( a \) if it is
-0.0. The sign would be lost if adding \( (0 + i d) \) usingadd(Complex.ofCartesian(0, addend))since-0.0 + 0.0 = 0.0.- Parameters:
addend- Value to be added to this complex number.- Returns:
this + addend.- See Also:
-
subtract
Returns aComplexwhose value is(this - subtrahend). Implements the formula:\[ (a + i b) - (c + i d) = (a - c) + i (b - d) \]
- Parameters:
subtrahend- Value to be subtracted from this complex number.- Returns:
this - subtrahend.- See Also:
-
subtract
Returns aComplexwhose value is(this - subtrahend), withsubtrahendinterpreted as a real number. Implements the formula:\[ (a + i b) - c = (a - c) + i b \]
This method is included for compatibility with ISO C99 which defines arithmetic between real-only and complex numbers.
- Parameters:
subtrahend- Value to be subtracted from this complex number.- Returns:
this - subtrahend.- See Also:
-
subtractImaginary
Returns aComplexwhose value is(this - subtrahend), withsubtrahendinterpreted as an imaginary number. Implements the formula:\[ (a + i b) - i d = a + i (b - d) \]
This method is included for compatibility with ISO C99 which defines arithmetic between imaginary-only and complex numbers.
- Parameters:
subtrahend- Value to be subtracted from this complex number.- Returns:
this - subtrahend.- See Also:
-
subtractFrom
Returns aComplexwhose value is(minuend - this), withminuendinterpreted as a real number. Implements the formula: \[ c - (a + i b) = (c - a) - i b \]This method is included for compatibility with ISO C99 which defines arithmetic between real-only and complex numbers.
Note: This method inverts the sign of the imaginary component \( b \) if it is
0.0. The sign would not be inverted if subtracting from \( c + i 0 \) usingComplex.ofCartesian(minuend, 0).subtract(this)since0.0 - 0.0 = 0.0.- Parameters:
minuend- Value this complex number is to be subtracted from.- Returns:
minuend - this.- See Also:
-
subtractFromImaginary
Returns aComplexwhose value is(this - subtrahend), withminuendinterpreted as an imaginary number. Implements the formula: \[ i d - (a + i b) = -a + i (d - b) \]This method is included for compatibility with ISO C99 which defines arithmetic between imaginary-only and complex numbers.
Note: This method inverts the sign of the real component \( a \) if it is
0.0. The sign would not be inverted if subtracting from \( 0 + i d \) usingComplex.ofCartesian(0, minuend).subtract(this)since0.0 - 0.0 = 0.0.- Parameters:
minuend- Value this complex number is to be subtracted from.- Returns:
this - subtrahend.- See Also:
-
multiply
Returns aComplexwhose value isthis * factor. Implements the formula:\[ (a + i b)(c + i d) = (ac - bd) + i (ad + bc) \]
Recalculates to recover infinities as specified in C99 standard G.5.1.
- Parameters:
factor- Value to be multiplied by this complex number.- Returns:
this * factor.- See Also:
-
multiply
Returns aComplexwhose value isthis * factor, withfactorinterpreted as a real number. Implements the formula:\[ (a + i b) c = (ac) + i (bc) \]
This method is included for compatibility with ISO C99 which defines arithmetic between real-only and complex numbers.
Note: This method should be preferred over using
multiply(Complex.ofCartesian(factor, 0)). Multiplication can generate signed zeros if eitherthiscomplex has zeros for the real and/or imaginary component, or if the factor is zero. The summation of signed zeros inmultiply(Complex)may create zeros in the result that differ in sign from the equivalent call to multiply by a real-only number.- Parameters:
factor- Value to be multiplied by this complex number.- Returns:
this * factor.- See Also:
-
multiplyImaginary
Returns aComplexwhose value isthis * factor, withfactorinterpreted as an imaginary number. Implements the formula:\[ (a + i b) id = (-bd) + i (ad) \]
This method can be used to compute the multiplication of this complex number \( z \) by \( i \) using a factor with magnitude 1.0. This should be used in preference to
\[ \begin{aligned} iz &= (-b + i a) \\ -iz &= (b - i a) \end{aligned} \]multiply(Complex.I)with or withoutnegation:This method is included for compatibility with ISO C99 which defines arithmetic between imaginary-only and complex numbers.
Note: This method should be preferred over using
multiply(Complex.ofCartesian(0, factor)). Multiplication can generate signed zeros if eitherthiscomplex has zeros for the real and/or imaginary component, or if the factor is zero. The summation of signed zeros inmultiply(Complex)may create zeros in the result that differ in sign from the equivalent call to multiply by an imaginary-only number.- Parameters:
factor- Value to be multiplied by this complex number.- Returns:
this * factor.- See Also:
-
divide
Returns aComplexwhose value is(this / divisor). Implements the formula:\[ \frac{a + i b}{c + i d} = \frac{(ac + bd) + i (bc - ad)}{c^2+d^2} \]
Re-calculates NaN result values to recover infinities as specified in C99 standard G.5.1.
- Parameters:
divisor- Value by which this complex number is to be divided.- Returns:
this / divisor.- See Also:
-
divide
Returns aComplexwhose value is(this / divisor), withdivisorinterpreted as a real number. Implements the formula:\[ \frac{a + i b}{c} = \frac{a}{c} + i \frac{b}{c} \]
This method is included for compatibility with ISO C99 which defines arithmetic between real-only and complex numbers.
Note: This method should be preferred over using
divide(Complex.ofCartesian(divisor, 0)). Division can generate signed zeros ifthiscomplex has zeros for the real and/or imaginary component, or the divisor is infinite. The summation of signed zeros individe(Complex)may create zeros in the result that differ in sign from the equivalent call to divide by a real-only number.- Parameters:
divisor- Value by which this complex number is to be divided.- Returns:
this / divisor.- See Also:
-
divideImaginary
Returns aComplexwhose value is(this / divisor), withdivisorinterpreted as an imaginary number. Implements the formula:\[ \frac{a + i b}{id} = \frac{b}{d} - i \frac{a}{d} \]
This method is included for compatibility with ISO C99 which defines arithmetic between imaginary-only and complex numbers.
Note: This method should be preferred over using
divide(Complex.ofCartesian(0, divisor)). Division can generate signed zeros ifthiscomplex has zeros for the real and/or imaginary component, or the divisor is infinite. The summation of signed zeros individe(Complex)may create zeros in the result that differ in sign from the equivalent call to divide by an imaginary-only number.Warning: This method will generate a different result from
divide(Complex.ofCartesian(0, divisor))if the divisor is zero. In this case the divide method using a zero-valued Complex will produce the same result as dividing by a real-only zero. The output from dividing by imaginary zero will create infinite and NaN values in the same component parts as the output fromthis.divide(Complex.ZERO).multiplyImaginary(1), however the sign of some infinite values may be negated.- Parameters:
divisor- Value by which this complex number is to be divided.- Returns:
this / divisor.- See Also:
-
exp
Returns the exponential function of this complex number.\[ \exp(z) = e^z \]
The exponential function of \( z \) is an entire function in the complex plane. Special cases:
z.conj().exp() == z.exp().conj().- If
zis ±0 + i0, returns 1 + i0. - If
zis x + i∞ for finite x, returns NaN + iNaN ("invalid" floating-point operation). - If
zis x + iNaN for finite x, returns NaN + iNaN ("invalid" floating-point operation). - If
zis +∞ + i0, returns +∞ + i0. - If
zis −∞ + iy for finite y, returns +0 cis(y) (seeofCis(double)). - If
zis +∞ + iy for finite nonzero y, returns +∞ cis(y). - If
zis −∞ + i∞, returns ±0 ± i0 (where the signs of the real and imaginary parts of the result are unspecified). - If
zis +∞ + i∞, returns ±∞ + iNaN (where the sign of the real part of the result is unspecified; "invalid" floating-point operation). - If
zis −∞ + iNaN, returns ±0 ± i0 (where the signs of the real and imaginary parts of the result are unspecified). - If
zis +∞ + iNaN, returns ±∞ + iNaN (where the sign of the real part of the result is unspecified). - If
zis NaN + i0, returns NaN + i0. - If
zis NaN + iy for all nonzero numbers y, returns NaN + iNaN ("invalid" floating-point operation). - If
zis NaN + iNaN, returns NaN + iNaN.
Implements the formula:
\[ \exp(x + iy) = e^x (\cos(y) + i \sin(y)) \]
- Returns:
- The exponential of this complex number.
- See Also:
-
log
Returns the natural logarithm of this complex number.The natural logarithm of \( z \) is unbounded along the real axis and in the range \( [-\pi, \pi] \) along the imaginary axis. The imaginary part of the natural logarithm has a branch cut along the negative real axis \( (-infty,0] \). Special cases:
z.conj().log() == z.log().conj().- If
zis −0 + i0, returns −∞ + iπ ("divide-by-zero" floating-point operation). - If
zis +0 + i0, returns −∞ + i0 ("divide-by-zero" floating-point operation). - If
zis x + i∞ for finite x, returns +∞ + iπ/2. - If
zis x + iNaN for finite x, returns NaN + iNaN ("invalid" floating-point operation). - If
zis −∞ + iy for finite positive-signed y, returns +∞ + iπ. - If
zis +∞ + iy for finite positive-signed y, returns +∞ + i0. - If
zis −∞ + i∞, returns +∞ + i3π/4. - If
zis +∞ + i∞, returns +∞ + iπ/4. - If
zis ±∞ + iNaN, returns +∞ + iNaN. - If
zis NaN + iy for finite y, returns NaN + iNaN ("invalid" floating-point operation). - If
zis NaN + i∞, returns +∞ + iNaN. - If
zis NaN + iNaN, returns NaN + iNaN.
Implements the formula:
\[ \ln(z) = \ln |z| + i \arg(z) \]
where \( |z| \) is the absolute and \( \arg(z) \) is the argument.
The implementation is based on the method described in:
T E Hull, Thomas F Fairgrieve and Ping Tak Peter Tang (1994) Implementing complex elementary functions using exception handling. ACM Transactions on Mathematical Software, Vol 20, No 2, pp 215-244.
- Returns:
- The natural logarithm of this complex number.
- See Also:
-
log10
Returns the base 10 common logarithm of this complex number.The common logarithm of \( z \) is unbounded along the real axis and in the range \( [-\pi, \pi] \) along the imaginary axis. The imaginary part of the common logarithm has a branch cut along the negative real axis \( (-infty,0] \). Special cases are as defined in the
natural logarithm:Implements the formula:
\[ \log_{10}(z) = \log_{10} |z| + i \arg(z) \]
where \( |z| \) is the absolute and \( \arg(z) \) is the argument.
- Returns:
- The base 10 logarithm of this complex number.
- See Also:
-
pow
Returns the complex power of this complex number raised to the power ofx. Implements the formula:\[ z^x = e^{x \ln(z)} \]
If this complex number is zero then this method returns zero if
xis positive in the real component and zero in the imaginary component; otherwise it returns NaN + iNaN.- Parameters:
x- The exponent to which this complex number is to be raised.- Returns:
- This complex number raised to the power of
x. - See Also:
-
pow
Returns the complex power of this complex number raised to the power ofx, withxinterpreted as a real number. Implements the formula:\[ z^x = e^{x \ln(z)} \]
If this complex number is zero then this method returns zero if
xis positive; otherwise it returns NaN + iNaN.- Parameters:
x- The exponent to which this complex number is to be raised.- Returns:
- This complex number raised to the power of
x. - See Also:
-
sqrt
Returns the square root of this complex number.\[ \sqrt{x + iy} = \frac{1}{2} \sqrt{2} \left( \sqrt{ \sqrt{x^2 + y^2} + x } + i\ \text{sgn}(y) \sqrt{ \sqrt{x^2 + y^2} - x } \right) \]
The square root of \( z \) is in the range \( [0, +\infty) \) along the real axis and is unbounded along the imaginary axis. The imaginary part of the square root has a branch cut along the negative real axis \( (-infty,0) \). Special cases:
z.conj().sqrt() == z.sqrt().conj().- If
zis ±0 + i0, returns +0 + i0. - If
zis x + i∞ for all x (including NaN), returns +∞ + i∞. - If
zis x + iNaN for finite x, returns NaN + iNaN ("invalid" floating-point operation). - If
zis −∞ + iy for finite positive-signed y, returns +0 + i∞. - If
zis +∞ + iy for finite positive-signed y, returns +∞ + i0. - If
zis −∞ + iNaN, returns NaN ± i∞ (where the sign of the imaginary part of the result is unspecified). - If
zis +∞ + iNaN, returns +∞ + iNaN. - If
zis NaN + iy for finite y, returns NaN + iNaN ("invalid" floating-point operation). - If
zis NaN + iNaN, returns NaN + iNaN.
Implements the following algorithm to compute \( \sqrt{x + iy} \):
- Let \( t = \sqrt{2 (|x| + |x + iy|)} \)
- if \( x \geq 0 \) return \( \frac{t}{2} + i \frac{y}{t} \)
- else return \( \frac{|y|}{t} + i\ \text{sgn}(y) \frac{t}{2} \)
The implementation is overflow and underflow safe based on the method described in:
T E Hull, Thomas F Fairgrieve and Ping Tak Peter Tang (1994) Implementing complex elementary functions using exception handling. ACM Transactions on Mathematical Software, Vol 20, No 2, pp 215-244.
- Returns:
- The square root of this complex number.
- See Also:
-
sin
Returns the sine of this complex number.\[ \sin(z) = \frac{1}{2} i \left( e^{-iz} - e^{iz} \right) \]
This is an odd function: \( \sin(z) = -\sin(-z) \). The sine is an entire function and requires no branch cuts.
This is implemented using real \( x \) and imaginary \( y \) parts:
\[ \sin(x + iy) = \sin(x)\cosh(y) + i \cos(x)\sinh(y) \]
As per the C99 standard this function is computed using the trigonomic identity:
\[ \sin(z) = -i \sinh(iz) \]
- Returns:
- The sine of this complex number.
- See Also:
-
cos
Returns the cosine of this complex number.\[ \cos(z) = \frac{1}{2} \left( e^{iz} + e^{-iz} \right) \]
This is an even function: \( \cos(z) = \cos(-z) \). The cosine is an entire function and requires no branch cuts.
This is implemented using real \( x \) and imaginary \( y \) parts:
\[ \cos(x + iy) = \cos(x)\cosh(y) - i \sin(x)\sinh(y) \]
As per the C99 standard this function is computed using the trigonomic identity:
\[ cos(z) = cosh(iz) \]
- Returns:
- The cosine of this complex number.
- See Also:
-
tan
Returns the tangent of this complex number.\[ \tan(z) = \frac{i(e^{-iz} - e^{iz})}{e^{-iz} + e^{iz}} \]
This is an odd function: \( \tan(z) = -\tan(-z) \). The tangent is an entire function and requires no branch cuts.
This is implemented using real \( x \) and imaginary \( y \) parts:
\[ \tan(x + iy) = \frac{\sin(2x)}{\cos(2x)+\cosh(2y)} + i \frac{\sinh(2y)}{\cos(2x)+\cosh(2y)} \]As per the C99 standard this function is computed using the trigonomic identity:
\[ \tan(z) = -i \tanh(iz) \]- Returns:
- The tangent of this complex number.
- See Also:
-
asin
Returns the inverse sine of this complex number.\[ \sin^{-1}(z) = - i \left(\ln{iz + \sqrt{1 - z^2}}\right) \]
The inverse sine of \( z \) is unbounded along the imaginary axis and in the range \( [-\pi, \pi] \) along the real axis. Special cases are handled as if the operation is implemented using \( \sin^{-1}(z) = -i \sinh^{-1}(iz) \).
The inverse sine is a multivalued function and requires a branch cut in the complex plane; the cut is conventionally placed at the line segments \( (\infty,-1) \) and \( (1,\infty) \) of the real axis.
This is implemented using real \( x \) and imaginary \( y \) parts:
\[ \begin{aligned} \sin^{-1}(z) &= \sin^{-1}(B) + i\ \text{sgn}(y)\ln \left(A + \sqrt{A^2-1} \right) \\ A &= \frac{1}{2} \left[ \sqrt{(x+1)^2+y^2} + \sqrt{(x-1)^2+y^2} \right] \\ B &= \frac{1}{2} \left[ \sqrt{(x+1)^2+y^2} - \sqrt{(x-1)^2+y^2} \right] \end{aligned} \]
where \( \text{sgn}(y) \) is the sign function implemented using
copySign(1.0, y).The implementation is based on the method described in:
T E Hull, Thomas F Fairgrieve and Ping Tak Peter Tang (1997) Implementing the complex Arcsine and Arccosine Functions using Exception Handling. ACM Transactions on Mathematical Software, Vol 23, No 3, pp 299-335.
The code has been adapted from the Boost
c++implementation<boost/math/complex/asin.hpp>.- Returns:
- The inverse sine of this complex number.
- See Also:
-
acos
Returns the inverse cosine of this complex number.\[ \cos^{-1}(z) = \frac{\pi}{2} + i \left(\ln{iz + \sqrt{1 - z^2}}\right) \]
The inverse cosine of \( z \) is in the range \( [0, \pi) \) along the real axis and unbounded along the imaginary axis. Special cases:
z.conj().acos() == z.acos().conj().- If
zis ±0 + i0, returns π/2 − i0. - If
zis ±0 + iNaN, returns π/2 + iNaN. - If
zis x + i∞ for finite x, returns π/2 − i∞. - If
zis x + iNaN, returns NaN + iNaN ("invalid" floating-point operation). - If
zis −∞ + iy for positive-signed finite y, returns π − i∞. - If
zis +∞ + iy for positive-signed finite y, returns +0 − i∞. - If
zis −∞ + i∞, returns 3π/4 − i∞. - If
zis +∞ + i∞, returns π/4 − i∞. - If
zis ±∞ + iNaN, returns NaN ± i∞ where the sign of the imaginary part of the result is unspecified. - If
zis NaN + iy for finite y, returns NaN + iNaN ("invalid" floating-point operation). - If
zis NaN + i∞, returns NaN − i∞. - If
zis NaN + iNaN, returns NaN + iNaN.
The inverse cosine is a multivalued function and requires a branch cut in the complex plane; the cut is conventionally placed at the line segments \( (-\infty,-1) \) and \( (1,\infty) \) of the real axis.
This function is implemented using real \( x \) and imaginary \( y \) parts:
\[ \begin{aligned} \cos^{-1}(z) &= \cos^{-1}(B) - i\ \text{sgn}(y) \ln\left(A + \sqrt{A^2-1}\right) \\ A &= \frac{1}{2} \left[ \sqrt{(x+1)^2+y^2} + \sqrt{(x-1)^2+y^2} \right] \\ B &= \frac{1}{2} \left[ \sqrt{(x+1)^2+y^2} - \sqrt{(x-1)^2+y^2} \right] \end{aligned} \]
where \( \text{sgn}(y) \) is the sign function implemented using
copySign(1.0, y).The implementation is based on the method described in:
T E Hull, Thomas F Fairgrieve and Ping Tak Peter Tang (1997) Implementing the complex Arcsine and Arccosine Functions using Exception Handling. ACM Transactions on Mathematical Software, Vol 23, No 3, pp 299-335.
The code has been adapted from the Boost
c++implementation<boost/math/complex/acos.hpp>.- Returns:
- The inverse cosine of this complex number.
- See Also:
-
atan
Returns the inverse tangent of this complex number.\[ \tan^{-1}(z) = \frac{i}{2} \ln \left( \frac{i + z}{i - z} \right) \]
The inverse hyperbolic tangent of \( z \) is unbounded along the imaginary axis and in the range \( [-\pi/2, \pi/2] \) along the real axis.
The inverse tangent is a multivalued function and requires a branch cut in the complex plane; the cut is conventionally placed at the line segments \( (i \infty,-i] \) and \( [i,i \infty) \) of the imaginary axis.
As per the C99 standard this function is computed using the trigonomic identity: \[ \tan^{-1}(z) = -i \tanh^{-1}(iz) \]
- Returns:
- The inverse tangent of this complex number.
- See Also:
-
sinh
Returns the hyperbolic sine of this complex number.\[ \sinh(z) = \frac{1}{2} \left( e^{z} - e^{-z} \right) \]
The hyperbolic sine of \( z \) is an entire function in the complex plane and is periodic with respect to the imaginary component with period \( 2\pi i \). Special cases:
z.conj().sinh() == z.sinh().conj().- This is an odd function: \( \sinh(z) = -\sinh(-z) \).
- If
zis +0 + i0, returns +0 + i0. - If
zis +0 + i∞, returns ±0 + iNaN (where the sign of the real part of the result is unspecified; "invalid" floating-point operation). - If
zis +0 + iNaN, returns ±0 + iNaN (where the sign of the real part of the result is unspecified). - If
zis x + i∞ for positive finite x, returns NaN + iNaN ("invalid" floating-point operation). - If
zis x + iNaN for finite nonzero x, returns NaN + iNaN ("invalid" floating-point operation). - If
zis +∞ + i0, returns +∞ + i0. - If
zis +∞ + iy for positive finite y, returns +∞ cis(y) (seeofCis(double). - If
zis +∞ + i∞, returns ±∞ + iNaN (where the sign of the real part of the result is unspecified; "invalid" floating-point operation). - If
zis +∞ + iNaN, returns ±∞ + iNaN (where the sign of the real part of the result is unspecified). - If
zis NaN + i0, returns NaN + i0. - If
zis NaN + iy for all nonzero numbers y, returns NaN + iNaN ("invalid" floating-point operation). - If
zis NaN + iNaN, returns NaN + iNaN.
This is implemented using real \( x \) and imaginary \( y \) parts:
\[ \sinh(x + iy) = \sinh(x)\cos(y) + i \cosh(x)\sin(y) \]
- Returns:
- The hyperbolic sine of this complex number.
- See Also:
-
cosh
Returns the hyperbolic cosine of this complex number.\[ \cosh(z) = \frac{1}{2} \left( e^{z} + e^{-z} \right) \]
The hyperbolic cosine of \( z \) is an entire function in the complex plane and is periodic with respect to the imaginary component with period \( 2\pi i \). Special cases:
z.conj().cosh() == z.cosh().conj().- This is an even function: \( \cosh(z) = \cosh(-z) \).
- If
zis +0 + i0, returns 1 + i0. - If
zis +0 + i∞, returns NaN ± i0 (where the sign of the imaginary part of the result is unspecified; "invalid" floating-point operation). - If
zis +0 + iNaN, returns NaN ± i0 (where the sign of the imaginary part of the result is unspecified). - If
zis x + i∞ for finite nonzero x, returns NaN + iNaN ("invalid" floating-point operation). - If
zis x + iNaN for finite nonzero x, returns NaN + iNaN ("invalid" floating-point operation). - If
zis +∞ + i0, returns +∞ + i0. - If
zis +∞ + iy for finite nonzero y, returns +∞ cis(y) (seeofCis(double)). - If
zis +∞ + i∞, returns ±∞ + iNaN (where the sign of the real part of the result is unspecified). - If
zis +∞ + iNaN, returns +∞ + iNaN. - If
zis NaN + i0, returns NaN ± i0 (where the sign of the imaginary part of the result is unspecified). - If
zis NaN + iy for all nonzero numbers y, returns NaN + iNaN ("invalid" floating-point operation). - If
zis NaN + iNaN, returns NaN + iNaN.
This is implemented using real \( x \) and imaginary \( y \) parts:
\[ \cosh(x + iy) = \cosh(x)\cos(y) + i \sinh(x)\sin(y) \]
- Returns:
- The hyperbolic cosine of this complex number.
- See Also:
-
tanh
Returns the hyperbolic tangent of this complex number.\[ \tanh(z) = \frac{e^z - e^{-z}}{e^z + e^{-z}} \]
The hyperbolic tangent of \( z \) is an entire function in the complex plane and is periodic with respect to the imaginary component with period \( \pi i \) and has poles of the first order along the imaginary line, at coordinates \( (0, \pi(\frac{1}{2} + n)) \). Note that the
doublefloating-point representation is unable to exactly represent \( \pi/2 \) and there is no value for which a pole error occurs. Special cases:z.conj().tanh() == z.tanh().conj().- This is an odd function: \( \tanh(z) = -\tanh(-z) \).
- If
zis +0 + i0, returns +0 + i0. - If
zis 0 + i∞, returns 0 + iNaN. - If
zis x + i∞ for finite non-zero x, returns NaN + iNaN ("invalid" floating-point operation). - If
zis 0 + iNaN, returns 0 + iNAN. - If
zis x + iNaN for finite non-zero x, returns NaN + iNaN ("invalid" floating-point operation). - If
zis +∞ + iy for positive-signed finite y, returns 1 + i0 sin(2y). - If
zis +∞ + i∞, returns 1 ± i0 (where the sign of the imaginary part of the result is unspecified). - If
zis +∞ + iNaN, returns 1 ± i0 (where the sign of the imaginary part of the result is unspecified). - If
zis NaN + i0, returns NaN + i0. - If
zis NaN + iy for all nonzero numbers y, returns NaN + iNaN ("invalid" floating-point operation). - If
zis NaN + iNaN, returns NaN + iNaN.
Special cases include the technical corrigendum DR 471: Complex math functions cacosh and ctanh.
This is defined using real \( x \) and imaginary \( y \) parts:
\[ \tan(x + iy) = \frac{\sinh(2x)}{\cosh(2x)+\cos(2y)} + i \frac{\sin(2y)}{\cosh(2x)+\cos(2y)} \]
The implementation uses double-angle identities to avoid overflow of
2xand2y.- Returns:
- The hyperbolic tangent of this complex number.
- See Also:
-
asinh
Returns the inverse hyperbolic sine of this complex number.\[ \sinh^{-1}(z) = \ln \left(z + \sqrt{1 + z^2} \right) \]
The inverse hyperbolic sine of \( z \) is unbounded along the real axis and in the range \( [-\pi, \pi] \) along the imaginary axis. Special cases:
z.conj().asinh() == z.asinh().conj().- This is an odd function: \( \sinh^{-1}(z) = -\sinh^{-1}(-z) \).
- If
zis +0 + i0, returns 0 + i0. - If
zis x + i∞ for positive-signed finite x, returns +∞ + iπ/2. - If
zis x + iNaN for finite x, returns NaN + iNaN ("invalid" floating-point operation). - If
zis +∞ + iy for positive-signed finite y, returns +∞ + i0. - If
zis +∞ + i∞, returns +∞ + iπ/4. - If
zis +∞ + iNaN, returns +∞ + iNaN. - If
zis NaN + i0, returns NaN + i0. - If
zis NaN + iy for finite nonzero y, returns NaN + iNaN ("invalid" floating-point operation). - If
zis NaN + i∞, returns ±∞ + iNaN (where the sign of the real part of the result is unspecified). - If
zis NaN + iNaN, returns NaN + iNaN.
The inverse hyperbolic sine is a multivalued function and requires a branch cut in the complex plane; the cut is conventionally placed at the line segments \( (-i \infty,-i) \) and \( (i,i \infty) \) of the imaginary axis.
This function is computed using the trigonomic identity:
\[ \sinh^{-1}(z) = -i \sin^{-1}(iz) \]
- Returns:
- The inverse hyperbolic sine of this complex number.
- See Also:
-
acosh
Returns the inverse hyperbolic cosine of this complex number.\[ \cosh^{-1}(z) = \ln \left(z + \sqrt{z + 1} \sqrt{z - 1} \right) \]
The inverse hyperbolic cosine of \( z \) is in the range \( [0, \infty) \) along the real axis and in the range \( [-\pi, \pi] \) along the imaginary axis. Special cases:
z.conj().acosh() == z.acosh().conj().- If
zis ±0 + i0, returns +0 + iπ/2. - If
zis x + i∞ for finite x, returns +∞ + iπ/2. - If
zis 0 + iNaN, returns NaN + iπ/2 [1]. - If
zis x + iNaN for finite non-zero x, returns NaN + iNaN ("invalid" floating-point operation). - If
zis −∞ + iy for positive-signed finite y, returns +∞ + iπ. - If
zis +∞ + iy for positive-signed finite y, returns +∞ + i0. - If
zis −∞ + i∞, returns +∞ + i3π/4. - If
zis +∞ + i∞, returns +∞ + iπ/4. - If
zis ±∞ + iNaN, returns +∞ + iNaN. - If
zis NaN + iy for finite y, returns NaN + iNaN ("invalid" floating-point operation). - If
zis NaN + i∞, returns +∞ + iNaN. - If
zis NaN + iNaN, returns NaN + iNaN.
Special cases include the technical corrigendum DR 471: Complex math functions cacosh and ctanh.
The inverse hyperbolic cosine is a multivalued function and requires a branch cut in the complex plane; the cut is conventionally placed at the line segment \( (-\infty,-1) \) of the real axis.
This function is computed using the trigonomic identity:
\[ \cosh^{-1}(z) = \pm i \cos^{-1}(z) \]
The sign of the multiplier is chosen to give
z.acosh().real() >= 0and compatibility with the C99 standard.- Returns:
- The inverse hyperbolic cosine of this complex number.
- See Also:
-
atanh
Returns the inverse hyperbolic tangent of this complex number.\[ \tanh^{-1}(z) = \frac{1}{2} \ln \left( \frac{1 + z}{1 - z} \right) \]
The inverse hyperbolic tangent of \( z \) is unbounded along the real axis and in the range \( [-\pi/2, \pi/2] \) along the imaginary axis. Special cases:
z.conj().atanh() == z.atanh().conj().- This is an odd function: \( \tanh^{-1}(z) = -\tanh^{-1}(-z) \).
- If
zis +0 + i0, returns +0 + i0. - If
zis +0 + iNaN, returns +0 + iNaN. - If
zis +1 + i0, returns +∞ + i0 ("divide-by-zero" floating-point operation). - If
zis x + i∞ for finite positive-signed x, returns +0 + iπ/2. - If
zis x+iNaN for nonzero finite x, returns NaN+iNaN ("invalid" floating-point operation). - If
zis +∞ + iy for finite positive-signed y, returns +0 + iπ/2. - If
zis +∞ + i∞, returns +0 + iπ/2. - If
zis +∞ + iNaN, returns +0 + iNaN. - If
zis NaN+iy for finite y, returns NaN+iNaN ("invalid" floating-point operation). - If
zis NaN + i∞, returns ±0 + iπ/2 (where the sign of the real part of the result is unspecified). - If
zis NaN + iNaN, returns NaN + iNaN.
The inverse hyperbolic tangent is a multivalued function and requires a branch cut in the complex plane; the cut is conventionally placed at the line segments \( (\infty,-1] \) and \( [1,\infty) \) of the real axis.
This is implemented using real \( x \) and imaginary \( y \) parts:
\[ \tanh^{-1}(z) = \frac{1}{4} \ln \left(1 + \frac{4x}{(1-x)^2+y^2} \right) + \\ i \frac{1}{2} \left( \tan^{-1} \left(\frac{2y}{1-x^2-y^2} \right) + \frac{\pi}{2} \left(\text{sgn}(x^2+y^2-1)+1 \right) \text{sgn}(y) \right) \]
The imaginary part is computed using
Math.atan2(double, double)to ensure the correct quadrant is returned from \( \tan^{-1} \left(\frac{2y}{1-x^2-y^2} \right) \).The code has been adapted from the Boost
c++implementation<boost/math/complex/atanh.hpp>.- Returns:
- The inverse hyperbolic tangent of this complex number.
- See Also:
-
nthRoot
Returns the n-th roots of this complex number. The nth roots are defined by the formula:\[ z_k = |z|^{\frac{1}{n}} \left( \cos \left(\phi + \frac{2\pi k}{n} \right) + i \sin \left(\phi + \frac{2\pi k}{n} \right) \right) \]
for \( k=0, 1, \ldots, n-1 \), where \( |z| \) and \( \phi \) are respectively the
modulusandargumentof this complex number.If one or both parts of this complex number is NaN, a list with all all elements set to
NaN + i NaNis returned.- Parameters:
n- Degree of root.- Returns:
- A list of all
n-th roots of this complex number. - Throws:
IllegalArgumentException- ifnis zero.- See Also:
-
equals
Test for equality with another object. If the other object is aComplexthen a comparison is made of the real and imaginary parts; otherwisefalseis returned.If both the real and imaginary parts of two complex numbers are exactly the same the two
Complexobjects are considered to be equal. For this purpose, twodoublevalues are considered to be the same if and only if the method#doubleToLongBits(double)returns the identicallongvalue when applied to each.Note that in most cases, for two instances of class
Complex,c1andc2, the value ofc1.equals(c2)istrueif and only ifc1.getReal() == c2.getReal() && c1.getImaginary() == c2.getImaginary()also has the value
true. However, there are exceptions:-
Instances that contain
NaNvalues in the same part are considered to be equal for that part, even thoughDouble.NaN == Double.NaNhas the valuefalse. -
Instances that share a
NaNvalue in one part but have different values in the other part are not considered equal. -
Instances that contain different representations of zero in the same part
are not considered to be equal for that part, even though
-0.0 == 0.0has the valuetrue.
The behavior is the same as if the components of the two complex numbers were passed to
Arrays.equals(double[], double[]):Arrays.equals(new double[]{c1.getReal(), c1.getImaginary()}, new double[]{c2.getReal(), c2.getImaginary()}); -
Instances that contain
-
hashCode
Gets a hash code for the complex number.The behavior is the same as if the components of the complex number were passed to
Arrays.hashCode(double[]):Arrays.hashCode(new double[] {getReal(), getImaginary()}) -
toString
Returns a string representation of the complex number.The string will represent the numeric values of the real and imaginary parts. The values are split by a separator and surrounded by parentheses. The string can be
parsedto obtain an instance with the same value.The format for complex number \( x + i y \) is
"(x,y)", with \( x \) and \( y \) converted as if usingDouble.toString(double).
-