Package dev.nm.number.complex
Class Complex
- java.lang.Object
-
- java.lang.Number
-
- dev.nm.number.complex.Complex
-
- All Implemented Interfaces:
AbelianGroup<Complex>
,Field<Complex>
,Monoid<Complex>
,Ring<Complex>
,NumberUtils.Comparable<Complex>
,Serializable
public class Complex extends Number implements Field<Complex>, NumberUtils.Comparable<Complex>
A complex number is a number consisting of a real number part and an imaginary number part. It is normally written in the form a + bi, where a and b are real numbers, and i is the square root of minus one. This class is immutable.- See Also:
- Wikipedia: Complex number, Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface dev.nm.algebra.structure.Field
Field.InverseNonExistent
-
-
Field Summary
Fields Modifier and Type Field Description static Complex
I
a number representing 0.0 + 1.0i, the square root of -1static Complex
NaN
a number representing the complex Not-a-Number (NaN
)static Complex
NEGATIVE_INFINITY
a number representing -∞ + -∞istatic Complex
ONE
a number representing 1.0 + 0.0istatic Complex
POSITIVE_INFINITY
a number representing +∞ + ∞istatic Complex
ZERO
a number representing 0.0 + 0.0i
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description Complex
add(Complex that)
+ : G × G → Gdouble
arg()
Get the θ of the complex number in polar representation.int
compare(Number that, double epsilon)
Comparethis
andthat
numbers up to a precision.Complex
conjugate()
Get the conjugate of the complex number, namely, (a - bi).Complex
divide(Complex that)
Compute the quotient of this complex number divided by another complex number.double
doubleValue()
boolean
equals(Object obj)
float
floatValue()
static Complex
fromPolar(double r, double theta)
Factory method to construct a complex number from the polar form: (r, θ).int
hashCode()
double
imaginary()
Get the imaginary part of this complex number.int
intValue()
Deprecated.Invalid operation.Complex
inverse()
For each a in F, there exists an element b in F such that a × b = b × a = 1.static boolean
isInfinite(Complex z)
Check if a complex number is an infinity; i.e., either the real or the imaginary part is infinite, c.f.,Double.isInfinite()
, and the number is not aNaN
.static boolean
isNaN(Complex z)
Check if a complex number is anNaN
; i.e., either the real or the imaginary part is anNaN
.static boolean
isReal(Complex z)
Check if this complex number is a real number; i.e., the imaginary part is 0.long
longValue()
Deprecated.Invalid operation.Complex
minus(Complex that)
- : G × G → Gdouble
modulus()
Get the modulus.Complex
multiply(Complex that)
Compute the product of this complex number and that complex number.Complex
ONE()
Get one - the number representing 1.0 + 0.0i.Complex
opposite()
For each a in G, there exists an element b in G such that a + b = b + a = 0.double
real()
Get the real part of this complex number.Double
toDouble()
Cast the complex number to aDouble
if it is a real number.String
toString()
Complex
ZERO()
Get zero - the number representing 0.0 + 0.0i.-
Methods inherited from class java.lang.Number
byteValue, shortValue
-
-
-
-
Field Detail
-
I
public static final Complex I
a number representing 0.0 + 1.0i, the square root of -1
-
ZERO
public static final Complex ZERO
a number representing 0.0 + 0.0i
-
ONE
public static final Complex ONE
a number representing 1.0 + 0.0i
-
POSITIVE_INFINITY
public static final Complex POSITIVE_INFINITY
a number representing +∞ + ∞i
-
NEGATIVE_INFINITY
public static final Complex NEGATIVE_INFINITY
a number representing -∞ + -∞i
-
NaN
public static final Complex NaN
a number representing the complex Not-a-Number (NaN
)
-
-
Method Detail
-
fromPolar
public static Complex fromPolar(double r, double theta)
Factory method to construct a complex number from the polar form: (r, θ).- Parameters:
r
- a radiustheta
- an angle- Returns:
- a complex number equivalent to the polar form (r, θ)
-
real
public double real()
Get the real part of this complex number.- Returns:
- the real part
-
imaginary
public double imaginary()
Get the imaginary part of this complex number.- Returns:
- the imaginary part
-
isReal
public static boolean isReal(Complex z)
Check if this complex number is a real number; i.e., the imaginary part is 0.- Parameters:
z
- a complex number- Returns:
true
if the imaginary part is 0
-
isNaN
public static boolean isNaN(Complex z)
Check if a complex number is anNaN
; i.e., either the real or the imaginary part is anNaN
.- Parameters:
z
- a complex number- Returns:
true
if either the real or the imaginary part is aNaN
-
isInfinite
public static boolean isInfinite(Complex z)
Check if a complex number is an infinity; i.e., either the real or the imaginary part is infinite, c.f.,Double.isInfinite()
, and the number is not aNaN
.- Parameters:
z
- a complex number- Returns:
true
if either the real or the imaginary part is infinite
-
modulus
public double modulus()
Get the modulus. The modulus is the square root of itself multiplied by its conjugate, namelythis.modulus() * this.modulus() = this.multiply(this.conjugate())
- Returns:
- the modulus
- See Also:
Math.hypot(double, double)
-
arg
public double arg()
Get the θ of the complex number in polar representation.- Returns:
- θ as in the polar form (r, θ)
-
toDouble
public Double toDouble()
Cast the complex number to aDouble
if it is a real number.- Returns:
- the real part if this complex number is a real number
- Throws:
IllegalArgumentException
- if this complex number is not a real number
-
conjugate
public Complex conjugate()
Get the conjugate of the complex number, namely, (a - bi).- Returns:
- the conjugate
-
intValue
@Deprecated public int intValue()
Deprecated.Invalid operation.
-
longValue
@Deprecated public long longValue()
Deprecated.Invalid operation.
-
floatValue
public float floatValue()
- Specified by:
floatValue
in classNumber
-
doubleValue
public double doubleValue()
- Specified by:
doubleValue
in classNumber
-
add
public Complex add(Complex that)
Description copied from interface:AbelianGroup
+ : G × G → G- Specified by:
add
in interfaceAbelianGroup<Complex>
- Parameters:
that
- the object to be added- Returns:
- this + that
-
minus
public Complex minus(Complex that)
Description copied from interface:AbelianGroup
- : G × G → G The operation "-" is not in the definition of of an additive group but can be deduced. This function is provided for convenience purpose. It is equivalent to
.this.add(that.opposite())
- Specified by:
minus
in interfaceAbelianGroup<Complex>
- Parameters:
that
- the object to be subtracted (subtrahend)- Returns:
- this - that
-
opposite
public Complex opposite()
Description copied from interface:AbelianGroup
For each a in G, there exists an element b in G such that a + b = b + a = 0. That is, it is the object such asthis.add(this.opposite()) == this.ZERO
- Specified by:
opposite
in interfaceAbelianGroup<Complex>
- Returns:
- -this, the additive opposite
- See Also:
- Wikipedia: Additive inverse
-
inverse
public Complex inverse() throws Field.InverseNonExistent
Description copied from interface:Field
For each a in F, there exists an element b in F such that a × b = b × a = 1. That is, it is the object such asthis.multiply(this.inverse()) == this.ONE
- Specified by:
inverse
in interfaceField<Complex>
- Returns:
- 1 / this if it exists
- Throws:
Field.InverseNonExistent
- if the inverse does not exist- See Also:
- Wikipedia: Multiplicative inverse
-
divide
public Complex divide(Complex that)
Compute the quotient of this complex number divided by another complex number. \[ \frac{a + bi}{c + di} = \frac{ac + bd + (bc - ad)i}{c^2 + d^2} \] ABSTRACTWe develop a simple method for scaling to avoid overflow and harmful underflow in complex division. The method guarantees that no overflow will occur unless at least one component of the quotient must overflow, otherwise the normwise error in the computed result is at most a few units in the last place. Moreover, the scaling requires only four floating point multiplications and a small amount of integer arithmetic to compute the scale factor. Thus, on many modern CPUs, our method is both safer and faster than Smith's widely used algorithm.
- Specified by:
divide
in interfaceField<Complex>
- Parameters:
that
- the denominator- Returns:
- this / that
- Throws:
ArithmeticException
- if division by zero happens
-
multiply
public Complex multiply(Complex that)
Compute the product of this complex number and that complex number.(a + bi)(c + di) = (ac - bd) + (ad + bc)i
This implementation is more efficient by doing 1 less multiplication:(a + bi)(c + di) = (ac - bd) + ((a + b)(c + d) - ac - bd)i
-
ZERO
public Complex ZERO()
Get zero - the number representing 0.0 + 0.0i.- Specified by:
ZERO
in interfaceAbelianGroup<Complex>
- Returns:
- ZERO
-
ONE
public Complex ONE()
Get one - the number representing 1.0 + 0.0i.
-
compare
public int compare(Number that, double epsilon)
Description copied from interface:NumberUtils.Comparable
Comparethis
andthat
numbers up to a precision.- Specified by:
compare
in interfaceNumberUtils.Comparable<Complex>
- Parameters:
that
- aNumber
. As a number can be represented in multiple ways, e.g., 0 = 0 + 0i, the implementation may need to checkObject
type.epsilon
- a precision parameter: when a number |x| ≤ ε, it is considered 0- Returns:
- 0 if both numbers are close enough; +1 if
this
is bigger; -1 ifthat
is bigger
-
-