public final class Complex extends Object implements 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.
Modifier and Type | Field and Description |
---|---|
static Complex |
I
A complex number representing \( i \), the square root of \( -1 \).
|
static Complex |
ONE
A complex number representing one.
|
static Complex |
ZERO
A complex number representing zero.
|
Modifier and Type | Method and Description |
---|---|
double |
abs()
Returns the absolute value of this complex number.
|
Complex |
acos()
Returns the
inverse cosine of this complex number.
|
Complex |
acosh()
Returns the
inverse hyperbolic cosine of this complex number.
|
Complex |
add(Complex addend)
Returns a
Complex whose value is (this + addend) . |
Complex |
add(double addend)
Returns a
Complex whose value is (this + addend) ,
with addend interpreted as a real number. |
Complex |
addImaginary(double addend)
Returns a
Complex whose value is (this + addend) ,
with addend interpreted as an imaginary number. |
double |
arg()
Returns the argument of this complex number.
|
Complex |
asin()
Returns the
inverse sine of this complex number.
|
Complex |
asinh()
Returns the
inverse hyperbolic sine of this complex number.
|
Complex |
atan()
Returns the
inverse tangent of this complex number.
|
Complex |
atanh()
Returns the
inverse hyperbolic tangent of this complex number.
|
Complex |
conj()
Returns the
conjugate
\( \overline{z} \) of this complex number \( z \).
|
Complex |
cos()
Returns the
cosine of this complex number.
|
Complex |
cosh()
Returns the
hyperbolic cosine of this complex number.
|
Complex |
divide(Complex divisor)
Returns a
Complex whose value is (this / divisor) . |
Complex |
divide(double divisor)
Returns a
Complex whose value is (this / divisor) ,
with divisor interpreted as a real number. |
Complex |
divideImaginary(double divisor)
Returns a
Complex whose value is (this / divisor) ,
with divisor interpreted as an imaginary number. |
boolean |
equals(Object other)
Test for equality with another object.
|
Complex |
exp()
Returns the
exponential function of this complex number.
|
double |
getImaginary()
Gets the imaginary part \( b \) of this complex number \( (a + i b) \).
|
double |
getReal()
Gets the real part \( a \) of this complex number \( (a + i b) \).
|
int |
hashCode()
Gets a hash code for the complex number.
|
double |
imag()
Gets the imaginary part \( b \) of this complex number \( (a + i b) \).
|
boolean |
isFinite()
Returns
true if both real and imaginary component of the complex number are finite. |
boolean |
isInfinite()
Returns
true if either real or imaginary component of the complex number is infinite. |
boolean |
isNaN()
Returns
true if either the real or imaginary component of the complex number is NaN
and the complex number is not infinite. |
Complex |
log()
Returns the
natural logarithm of this complex number.
|
Complex |
log10()
Returns the base 10
common logarithm of this complex number.
|
Complex |
multiply(Complex factor)
Returns a
Complex whose value is this * factor . |
Complex |
multiply(double factor)
Returns a
Complex whose value is this * factor , with factor
interpreted as a real number. |
Complex |
multiplyImaginary(double factor)
Returns a
Complex whose value is this * factor , with factor
interpreted as an imaginary number. |
Complex |
negate()
Returns a
Complex whose value is the negation of both the real and imaginary parts
of complex number \( z \). |
double |
norm()
Returns the squared norm value of this complex number.
|
List<Complex> |
nthRoot(int n)
Returns the n-th roots of this complex number.
|
static Complex |
ofCartesian(double real,
double imaginary)
Create a complex number given the real and imaginary parts.
|
static Complex |
ofCis(double x)
Create a complex cis number.
|
static Complex |
ofPolar(double rho,
double theta)
Creates a complex number from its polar representation using modulus
rho (\( \rho \))
and phase angle theta (\( \theta \)). |
static Complex |
parse(String s)
Returns a
Complex instance representing the specified string s . |
Complex |
pow(Complex x)
Returns the complex power of this complex number raised to the power of
x . |
Complex |
pow(double x)
Returns the complex power of this complex number raised to the power of
x ,
with x interpreted as a real number. |
Complex |
proj()
Returns the projection of this complex number onto the Riemann sphere.
|
double |
real()
Gets the real part \( a \) of this complex number \( (a + i b) \).
|
Complex |
sin()
Returns the
sine of this complex number.
|
Complex |
sinh()
Returns the
hyperbolic sine of this complex number.
|
Complex |
sqrt()
Returns the
square root of this complex number.
|
Complex |
subtract(Complex subtrahend)
Returns a
Complex whose value is (this - subtrahend) . |
Complex |
subtract(double subtrahend)
Returns a
Complex whose value is (this - subtrahend) ,
with subtrahend interpreted as a real number. |
Complex |
subtractFrom(double minuend)
Returns a
Complex whose value is (minuend - this) ,
with minuend interpreted as a real number. |
Complex |
subtractFromImaginary(double minuend)
Returns a
Complex whose value is (this - subtrahend) ,
with minuend interpreted as an imaginary number. |
Complex |
subtractImaginary(double subtrahend)
Returns a
Complex whose value is (this - subtrahend) ,
with subtrahend interpreted as an imaginary number. |
Complex |
tan()
Returns the
tangent of this complex number.
|
Complex |
tanh()
Returns the
hyperbolic tangent of this complex number.
|
String |
toString()
Returns a string representation of the complex number.
|
public static final Complex I
\( (0 + i 1) \).
public static Complex ofCartesian(double real, double imaginary)
real
- Real part.imaginary
- Imaginary part.Complex
number.public static Complex ofPolar(double rho, double theta)
rho
(\( \rho \))
and phase angle theta
(\( \theta \)).
\[ \begin{aligned}
x &= \rho \cos(\theta) \\
y &= \rho \sin(\theta) \end{aligned} \]
Requires that rho
is non-negative and non-NaN and theta
is finite;
otherwise returns a complex with NaN real and imaginary parts. A rho
value of
-0.0
is 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 theta
is in the range
\( -\pi\ \lt \theta \leq \pi \):
Complex.ofPolar(rho, theta).abs() == rho Complex.ofPolar(rho, theta).arg() == theta
If rho
is 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
.
rho
- The modulus of the complex number.theta
- The argument of the complex number.Complex
number.public static Complex ofCis(double x)
x
- double
to build the cis number.Complex
cis number.public static Complex parse(String s)
Complex
instance representing the specified string s
.
If s
is null
, then a NullPointerException
is 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 using Double.parseDouble(String)
. The parts
are interpreted as the real and imaginary parts of the complex number.
Examples of valid strings and the equivalent Complex
are 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)
s
- String representation.Complex
number.NullPointerException
- if the string is null.NumberFormatException
- if the string does not contain a parsable complex number.Double.parseDouble(String)
,
toString()
public double getReal()
public double real()
This method is the equivalent of the C++ method std::complex::real
.
getReal()
public double getImaginary()
public double imag()
This method is the equivalent of the C++ method std::complex::imag
.
getImaginary()
public double abs()
\[ \text{abs}(x + i y) = \sqrt{(x^2 + y^2)} \]
Special cases:
abs(x + iy) == abs(y + ix) == abs(x - iy)
.
z
is ±∞ + iy for any y, returns +∞.
z
is x + iNaN for non-infinite x, returns NaN.
z
is 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 infinite
then
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.
isInfinite()
,
isNaN()
,
Complex moduluspublic double arg()
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 \).
Math.atan2(double, double)
public double norm()
\[ \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 infinite
then 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 than abs()
for ranking by
magnitude. If used for ranking any overflow to infinity will create an equal ranking for
values that may be still distinguished by abs()
.
isInfinite()
,
isNaN()
,
abs()
,
Absolute squarepublic boolean isNaN()
true
if either the real or imaginary component of the complex number is NaN
and the complex number is not infinite.
Note that:
true
.
Complex.equals(Object)
method.
true
if this instance contains NaN and no infinite parts.Double.isNaN(double)
,
isInfinite()
,
Complex.equals(Object)
public boolean isInfinite()
true
if 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).
true
if this instance contains an infinite value.Double.isInfinite(double)
public boolean isFinite()
true
if both real and imaginary component of the complex number are finite.true
if this instance contains finite values.Double.isFinite(double)
public Complex conj()
\[ \begin{aligned} z &= a + i b \\ \overline{z} &= a - i b \end{aligned}\]
public Complex negate()
Complex
whose 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} \]
public Complex proj()
\( 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());
isInfinite()
,
IEEE and ISO C standards: cprojpublic Complex add(Complex addend)
Complex
whose value is (this + addend)
.
Implements the formula:
\[ (a + i b) + (c + i d) = (a + c) + i (b + d) \]
addend
- Value to be added to this complex number.this + addend
.public Complex add(double addend)
Complex
whose value is (this + addend)
,
with addend
interpreted 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) \) using
add(Complex.ofCartesian(addend, 0))
since
-0.0 + 0.0 = 0.0
.
addend
- Value to be added to this complex number.this + addend
.add(Complex)
,
ofCartesian(double, double)
public Complex addImaginary(double addend)
Complex
whose value is (this + addend)
,
with addend
interpreted 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) \) using
add(Complex.ofCartesian(0, addend))
since
-0.0 + 0.0 = 0.0
.
addend
- Value to be added to this complex number.this + addend
.add(Complex)
,
ofCartesian(double, double)
public Complex subtract(Complex subtrahend)
Complex
whose value is (this - subtrahend)
.
Implements the formula:
\[ (a + i b) - (c + i d) = (a - c) + i (b - d) \]
subtrahend
- Value to be subtracted from this complex number.this - subtrahend
.public Complex subtract(double subtrahend)
Complex
whose value is (this - subtrahend)
,
with subtrahend
interpreted 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.
subtrahend
- Value to be subtracted from this complex number.this - subtrahend
.subtract(Complex)
public Complex subtractImaginary(double subtrahend)
Complex
whose value is (this - subtrahend)
,
with subtrahend
interpreted 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.
subtrahend
- Value to be subtracted from this complex number.this - subtrahend
.subtract(Complex)
public Complex subtractFrom(double minuend)
Complex
whose value is (minuend - this)
,
with minuend
interpreted 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 \) using
Complex.ofCartesian(minuend, 0).subtract(this)
since
0.0 - 0.0 = 0.0
.
minuend
- Value this complex number is to be subtracted from.minuend - this
.subtract(Complex)
,
ofCartesian(double, double)
public Complex subtractFromImaginary(double minuend)
Complex
whose value is (this - subtrahend)
,
with minuend
interpreted 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 \) using
Complex.ofCartesian(0, minuend).subtract(this)
since
0.0 - 0.0 = 0.0
.
minuend
- Value this complex number is to be subtracted from.this - subtrahend
.subtract(Complex)
,
ofCartesian(double, double)
public Complex multiply(Complex factor)
Complex
whose value is this * 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.
factor
- Value to be multiplied by this complex number.this * factor
.public Complex multiply(double factor)
Complex
whose value is this * factor
, with factor
interpreted 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 either this
complex has zeros for the real
and/or imaginary component, or if the factor is zero. The summation of signed zeros
in multiply(Complex)
may create zeros in the result that differ in sign
from the equivalent call to multiply by a real-only number.
factor
- Value to be multiplied by this complex number.this * factor
.multiply(Complex)
public Complex multiplyImaginary(double factor)
Complex
whose value is this * factor
, with factor
interpreted 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
multiply(Complex.I)
with or without negation
:
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 either this
complex has zeros for the real
and/or imaginary component, or if the factor is zero. The summation of signed zeros
in multiply(Complex)
may create zeros in the result that differ in sign
from the equivalent call to multiply by an imaginary-only number.
factor
- Value to be multiplied by this complex number.this * factor
.multiply(Complex)
public Complex divide(Complex divisor)
Complex
whose 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.
divisor
- Value by which this complex number is to be divided.this / divisor
.public Complex divide(double divisor)
Complex
whose value is (this / divisor)
,
with divisor
interpreted 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 if this
complex has zeros for the real
and/or imaginary component, or the divisor is infinite. The summation of signed zeros
in divide(Complex)
may create zeros in the result that differ in sign
from the equivalent call to divide by a real-only number.
divisor
- Value by which this complex number is to be divided.this / divisor
.divide(Complex)
public Complex divideImaginary(double divisor)
Complex
whose value is (this / divisor)
,
with divisor
interpreted 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 if this
complex has zeros for the real
and/or imaginary component, or the divisor is infinite. The summation of signed zeros
in divide(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 from
this.divide(Complex.ZERO).multiplyImaginary(1)
, however the sign
of some infinite values may be negated.
divisor
- Value by which this complex number is to be divided.this / divisor
.divide(Complex)
,
divide(double)
public Complex exp()
\[ \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()
.
z
is ±0 + i0, returns 1 + i0.
z
is x + i∞ for finite x, returns NaN + iNaN ("invalid" floating-point operation).
z
is x + iNaN for finite x, returns NaN + iNaN ("invalid" floating-point operation).
z
is +∞ + i0, returns +∞ + i0.
z
is −∞ + iy for finite y, returns +0 cis(y) (see ofCis(double)
).
z
is +∞ + iy for finite nonzero y, returns +∞ cis(y).
z
is −∞ + i∞, returns ±0 ± i0 (where the signs of the real and imaginary parts of the result are unspecified).
z
is +∞ + i∞, returns ±∞ + iNaN (where the sign of the real part of the result is unspecified; "invalid" floating-point operation).
z
is −∞ + iNaN, returns ±0 ± i0 (where the signs of the real and imaginary parts of the result are unspecified).
z
is +∞ + iNaN, returns ±∞ + iNaN (where the sign of the real part of the result is unspecified).
z
is NaN + i0, returns NaN + i0.
z
is NaN + iy for all nonzero numbers y, returns NaN + iNaN ("invalid" floating-point operation).
z
is NaN + iNaN, returns NaN + iNaN.
Implements the formula:
\[ \exp(x + iy) = e^x (\cos(y) + i \sin(y)) \]
public Complex log()
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()
.
z
is −0 + i0, returns −∞ + iπ ("divide-by-zero" floating-point operation).
z
is +0 + i0, returns −∞ + i0 ("divide-by-zero" floating-point operation).
z
is x + i∞ for finite x, returns +∞ + iπ/2.
z
is x + iNaN for finite x, returns NaN + iNaN ("invalid" floating-point operation).
z
is −∞ + iy for finite positive-signed y, returns +∞ + iπ.
z
is +∞ + iy for finite positive-signed y, returns +∞ + i0.
z
is −∞ + i∞, returns +∞ + i3π/4.
z
is +∞ + i∞, returns +∞ + iπ/4.
z
is ±∞ + iNaN, returns +∞ + iNaN.
z
is NaN + iy for finite y, returns NaN + iNaN ("invalid" floating-point operation).
z
is NaN + i∞, returns +∞ + iNaN.
z
is 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.
Math.log(double)
,
abs()
,
arg()
,
Logpublic Complex log10()
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.
Math.log10(double)
,
abs()
,
arg()
public Complex pow(Complex x)
x
.
Implements the formula:
\[ z^x = e^{x \ln(z)} \]
If this complex number is zero then this method returns zero if x
is positive
in the real component and zero in the imaginary component;
otherwise it returns NaN + iNaN.
x
- The exponent to which this complex number is to be raised.x
.log()
,
multiply(Complex)
,
exp()
,
Complex exponentiation,
Powerpublic Complex pow(double x)
x
,
with x
interpreted 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 x
is positive;
otherwise it returns NaN + iNaN.
x
- The exponent to which this complex number is to be raised.x
.log()
,
multiply(double)
,
exp()
,
pow(Complex)
,
Powerpublic Complex sqrt()
\[ \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()
.
z
is ±0 + i0, returns +0 + i0.
z
is x + i∞ for all x (including NaN), returns +∞ + i∞.
z
is x + iNaN for finite x, returns NaN + iNaN ("invalid" floating-point operation).
z
is −∞ + iy for finite positive-signed y, returns +0 + i∞.
z
is +∞ + iy for finite positive-signed y, returns +∞ + i0.
z
is −∞ + iNaN, returns NaN ± i∞ (where the sign of the imaginary part of the result is unspecified).
z
is +∞ + iNaN, returns +∞ + iNaN.
z
is NaN + iy for finite y, returns NaN + iNaN ("invalid" floating-point operation).
z
is NaN + iNaN, returns NaN + iNaN.
Implements the following algorithm to compute \( \sqrt{x + iy} \):
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.
public Complex sin()
\[ \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) \]
public Complex cos()
\[ \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) \]
public Complex tan()
\[ \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) \]public Complex asin()
\[ \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>
.
public Complex acos()
\[ \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()
.
z
is ±0 + i0, returns π/2 − i0.
z
is ±0 + iNaN, returns π/2 + iNaN.
z
is x + i∞ for finite x, returns π/2 − i∞.
z
is x + iNaN, returns NaN + iNaN ("invalid" floating-point operation).
z
is −∞ + iy for positive-signed finite y, returns π − i∞.
z
is +∞ + iy for positive-signed finite y, returns +0 − i∞.
z
is −∞ + i∞, returns 3π/4 − i∞.
z
is +∞ + i∞, returns π/4 − i∞.
z
is ±∞ + iNaN, returns NaN ± i∞ where the sign of the imaginary part of the result is unspecified.
z
is NaN + iy for finite y, returns NaN + iNaN ("invalid" floating-point operation).
z
is NaN + i∞, returns NaN − i∞.
z
is 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>
.
public Complex atan()
\[ \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) \]
public Complex sinh()
\[ \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()
.
z
is +0 + i0, returns +0 + i0.
z
is +0 + i∞, returns ±0 + iNaN (where the sign of the real part of the result is unspecified; "invalid" floating-point operation).
z
is +0 + iNaN, returns ±0 + iNaN (where the sign of the real part of the result is unspecified).
z
is x + i∞ for positive finite x, returns NaN + iNaN ("invalid" floating-point operation).
z
is x + iNaN for finite nonzero x, returns NaN + iNaN ("invalid" floating-point operation).
z
is +∞ + i0, returns +∞ + i0.
z
is +∞ + iy for positive finite y, returns +∞ cis(y) (see ofCis(double)
.
z
is +∞ + i∞, returns ±∞ + iNaN (where the sign of the real part of the result is unspecified; "invalid" floating-point operation).
z
is +∞ + iNaN, returns ±∞ + iNaN (where the sign of the real part of the result is unspecified).
z
is NaN + i0, returns NaN + i0.
z
is NaN + iy for all nonzero numbers y, returns NaN + iNaN ("invalid" floating-point operation).
z
is 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) \]
public Complex cosh()
\[ \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()
.
z
is +0 + i0, returns 1 + i0.
z
is +0 + i∞, returns NaN ± i0 (where the sign of the imaginary part of the result is unspecified; "invalid" floating-point operation).
z
is +0 + iNaN, returns NaN ± i0 (where the sign of the imaginary part of the result is unspecified).
z
is x + i∞ for finite nonzero x, returns NaN + iNaN ("invalid" floating-point operation).
z
is x + iNaN for finite nonzero x, returns NaN + iNaN ("invalid" floating-point operation).
z
is +∞ + i0, returns +∞ + i0.
z
is +∞ + iy for finite nonzero y, returns +∞ cis(y) (see ofCis(double)
).
z
is +∞ + i∞, returns ±∞ + iNaN (where the sign of the real part of the result is unspecified).
z
is +∞ + iNaN, returns +∞ + iNaN.
z
is NaN + i0, returns NaN ± i0 (where the sign of the imaginary part of the result is unspecified).
z
is NaN + iy for all nonzero numbers y, returns NaN + iNaN ("invalid" floating-point operation).
z
is 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) \]
public Complex tanh()
\[ \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 double
floating-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()
.
z
is +0 + i0, returns +0 + i0.
z
is 0 + i∞, returns 0 + iNaN.
z
is x + i∞ for finite non-zero x, returns NaN + iNaN ("invalid" floating-point operation).
z
is 0 + iNaN, returns 0 + iNAN.
z
is x + iNaN for finite non-zero x, returns NaN + iNaN ("invalid" floating-point operation).
z
is +∞ + iy for positive-signed finite y, returns 1 + i0 sin(2y).
z
is +∞ + i∞, returns 1 ± i0 (where the sign of the imaginary part of the result is unspecified).
z
is +∞ + iNaN, returns 1 ± i0 (where the sign of the imaginary part of the result is unspecified).
z
is NaN + i0, returns NaN + i0.
z
is NaN + iy for all nonzero numbers y, returns NaN + iNaN ("invalid" floating-point operation).
z
is 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 2x
and 2y
.
public Complex asinh()
\[ \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()
.
z
is +0 + i0, returns 0 + i0.
z
is x + i∞ for positive-signed finite x, returns +∞ + iπ/2.
z
is x + iNaN for finite x, returns NaN + iNaN ("invalid" floating-point operation).
z
is +∞ + iy for positive-signed finite y, returns +∞ + i0.
z
is +∞ + i∞, returns +∞ + iπ/4.
z
is +∞ + iNaN, returns +∞ + iNaN.
z
is NaN + i0, returns NaN + i0.
z
is NaN + iy for finite nonzero y, returns NaN + iNaN ("invalid" floating-point operation).
z
is NaN + i∞, returns ±∞ + iNaN (where the sign of the real part of the result is unspecified).
z
is 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) \]
public Complex acosh()
\[ \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()
.
z
is ±0 + i0, returns +0 + iπ/2.
z
is x + i∞ for finite x, returns +∞ + iπ/2.
z
is 0 + iNaN, returns NaN + iπ/2 [1].
z
is x + iNaN for finite non-zero x, returns NaN + iNaN ("invalid" floating-point operation).
z
is −∞ + iy for positive-signed finite y, returns +∞ + iπ.
z
is +∞ + iy for positive-signed finite y, returns +∞ + i0.
z
is −∞ + i∞, returns +∞ + i3π/4.
z
is +∞ + i∞, returns +∞ + iπ/4.
z
is ±∞ + iNaN, returns +∞ + iNaN.
z
is NaN + iy for finite y, returns NaN + iNaN ("invalid" floating-point operation).
z
is NaN + i∞, returns +∞ + iNaN.
z
is 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() >= 0
and compatibility with the C99 standard.
public Complex atanh()
\[ \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()
.
z
is +0 + i0, returns +0 + i0.
z
is +0 + iNaN, returns +0 + iNaN.
z
is +1 + i0, returns +∞ + i0 ("divide-by-zero" floating-point operation).
z
is x + i∞ for finite positive-signed x, returns +0 + iπ/2.
z
is x+iNaN for nonzero finite x, returns NaN+iNaN ("invalid" floating-point operation).
z
is +∞ + iy for finite positive-signed y, returns +0 + iπ/2.
z
is +∞ + i∞, returns +0 + iπ/2.
z
is +∞ + iNaN, returns +0 + iNaN.
z
is NaN+iy for finite y, returns NaN+iNaN ("invalid" floating-point operation).
z
is NaN + i∞, returns ±0 + iπ/2 (where the sign of the real part of the result is unspecified).
z
is 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>
.
public List<Complex> nthRoot(int n)
\[ 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 modulus
and
argument
of this complex number.
If one or both parts of this complex number is NaN, a list with all
all elements set to NaN + i NaN
is returned.
n
- Degree of root.n
-th roots of this complex number.IllegalArgumentException
- if n
is zero.public boolean equals(Object other)
Complex
then a
comparison is made of the real and imaginary parts; otherwise false
is returned.
If both the real and imaginary parts of two complex numbers
are exactly the same the two Complex
objects are considered to be equal.
For this purpose, two double
values are considered to be
the same if and only if the method #doubleToLongBits(double)
returns the identical long
value when applied to each.
Note that in most cases, for two instances of class
Complex
, c1
and c2
, the
value of c1.equals(c2)
is true
if and only if
c1.getReal() == c2.getReal() && c1.getImaginary() == c2.getImaginary()
also has the value true
. However, there are exceptions:
NaN
values in the same part
are considered to be equal for that part, even though Double.NaN == Double.NaN
has the value false
.
NaN
value in one part
but have different values in the other part are not considered equal.
-0.0 == 0.0
has the value true
.
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()});
equals
in class Object
other
- Object to test for equality with this instance.true
if the objects are equal, false
if object
is null
, not an instance of Complex
, or not equal to
this instance.Double.doubleToLongBits(double)
,
Arrays.equals(double[], double[])
public int hashCode()
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()})
hashCode
in class Object
Arrays.hashCode(double[])
public String toString()
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 parsed
to 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 using Double.toString(double)
.
toString
in class Object
parse(String)
,
Double.toString(double)
Copyright © 2017–2022 The Apache Software Foundation. All rights reserved.