Class Complex64
- All Implemented Interfaces:
Serializable
,Comparable<Complex64>
,Field<Complex64>
,Ring<Complex64>
,Semiring<Complex64>
This class models a complex number in rectangular (Cartesian) form, defined by its real and imaginary parts,
each stored as a 32-bit floating-point number (float
). Instances of Complex64
are immutable
and thread-safe.
The class provides various operations for complex arithmetic, including addition, subtraction, multiplication, division, exponentiation, logarithms, trigonometric and hyperbolic functions, as well as utilities for comparing and rounding complex numbers.
Usage Example:
Complex64 a = new Complex64(2.5f, 3.2f); // Creates a complex number 2.5 + 3.2i.
Complex64 b = new Complex64(1, -4); // Creates a complex number 1 - 4i.
Complex64 sum = a.add(b); // Sum of a and b.
Complex64 product = a.mult(b); // Product of a and b.
Special Values:
The class defines several constants for common complex numbers:
ZERO
- Complex number representing zero (0 + 0i).ONE
- Complex number representing one (1 + 0i).TWO
- Complex number representing two (2 + 0i).THREE
- Complex number representing three (3 + 0i).IMAGINARY_UNIT
- The imaginary unit (0 + 1i).INV_IMAGINARY_UNIT
- Negative imaginary unit (0 - 1i).NaN
- Complex number representing a Not-a-Number value.POSITIVE_INFINITY
- Complex number with infinite real part.NEGATIVE_INFINITY
- Complex number with negative infinite real part.
Mathematical Functions:
The class provides methods for several mathematical functions:
sqrt()
- Computes the square root.ln(Complex64)
- Computes the natural logarithm.exp(Complex64)
- Computes the exponential function.pow(Complex64, Complex64)
- Computes the power function.sin(Complex64)
- Computes the sine function.cos(Complex64)
- Computes the cosine function.tan(Complex64)
- Computes the tangent function.sinh(Complex64)
- Computes the hyperbolic sine function.cosh(Complex64)
- Computes the hyperbolic cosine function.tanh(Complex64)
- Computes the hyperbolic tangent function.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionfinal float
Imaginary component of the complex number.static final Complex64
The imaginary unit i.static final Complex64
The additive inverse of the imaginary unit, -i.static final Complex64
Complex number with real and imaginary parts equal toFloat.NaN
.static final Complex64
Complex number with real part equal toFloat.NEGATIVE_INFINITY
.static final Complex64
The complex number with zero imaginary part and one real part.static final Complex64
Complex number with real part equal toFloat.POSITIVE_INFINITY
.final float
Real component of the complex number.static final Complex64
The float value closer than any other to the square root of 3static final Complex64
The float value closer than any other to the square root of 2static final Complex64
The complex number with zero imaginary part and three real part.static final Complex64
The complex number with zero imaginary part and two real part.static final Complex64
The complex number with zero imaginary and real parts. -
Constructor Summary
ConstructorsConstructorDescriptionComplex64
(float re) Constructs a complex number with specified real component and zero imaginary component.Complex64
(float re, float im) Constructs a complex number with specified complex and real components.Constructs a complex number from a string of the form"a +/- bi"
wherea
and {b} are real values and either may be omitted. i.e. -
Method Summary
Modifier and TypeMethodDescriptionstatic Complex64
Computes the inverse cosine of a complex value.add
(float b) Sums an elements of this field with a real number.Sums two elements of this field (associative and commutative).addInv()
Computes the additive inverse for an element of this field.static float
Computes the complex argument function for a complex number.static int
Computes the index of the maximum magnitude from an array of complex numbers.static int
argmaxReal
(Complex64... values) Computes the index of the minimum real component from an array of complex numbers.static int
Computes the index of the minimum magnitude from an array of complex numbers.static int
argminReal
(Complex64... values) Computes the index of the minimum real component from an array of complex numbers.static Complex64
Computes the inverse sine of a complex value.static Complex64
Computes the inverse tangent of a complex value.static float
Computes the 2 argument arc-tangent function for a complex number.int
Compares this element of the field withb
.conj()
Computes the conjugate of this field element.static Complex64
Computes the trigonometric cosine value of a complex value.static Complex64
Computes the hyperbolic cosine of a complex value.div
(float b) Computes the quotient of an elements of this field and a real number.Computes the quotient of two elements of this field.double
Converts this complex number to an equivalent double.boolean
equals
(float b) Checks if this complex has zero imaginary part and real part equal to a double.boolean
Checks if an object is equal to this Field element.static Complex64
Computes the exponential function with the given input.static Complex64
fromPolar
(float r, float theta) Converts a complex number expressed in polar from to the rectangular form.getOne()
Gets the multiplicative identity for this semiring.getZero()
Gets the additive identity for this semiring.int
hashCode()
Generates the hashcode for this complex number.float
im()
Gets the imaginary component of this complex number.boolean
Checks if this complex number has non-zero imaginary part.boolean
isFinite()
Checks if this field element is finite in magnitude.boolean
isFloat()
Checks if this complex number is a real valued float.boolean
Checks if this complex number has zero real part.boolean
Checks if this field element is infinite in magnitude.boolean
isInt()
Checks if this complex number is a real valued integer.boolean
isNaN()
Checks if this field element is NaN in magnitude.boolean
isOne()
Checks if this value is a multiplicative identity for this semiring.boolean
isReal()
Checks if this complex number has zero imaginary part.boolean
isZero()
Checks if this value is an additive identity for this semiring.static int
Gets the length of the string representation of this complex number.static Complex64
ln
(float num) Computes the natural logarithm of a float.static Complex64
Computes the complex natural logarithm of a complex number.static Complex64
log
(float num) Computes the complex logarithm base 10 of a complex number.static Complex64
log
(float base, float num) Computes the complex logarithm, with specified base, of a complex number.static Complex64
Computes the complex logarithm, with specified base, of a complex number.static Complex64
Computes the complex logarithm base 10 of a complex number.static Complex64
Computes the complex logarithm, with specified base, of a complex number.double
mag()
Computes the magnitude of this field element.float
Squares this magnitude of this complex number.static Complex64
Computes the maximum magnitude from an array of complex numbers.static Complex64
Computes the minimum real component from an array of complex numbers.static Complex64
Computes the minimum magnitude from an array of complex numbers.static Complex64
Computes the minimum real component from an array of complex numbers.mult
(float b) Multiplies an element of this field and a real number (associative and commutative).Multiplies two elements of this field (associative and commutative).multInv()
Computes the multiplicative inverse of this complex number.static boolean
Checks if a number is near zero in magnitude.static Complex64
Compute a raised to the power of b.static Complex64
Computesa
raised to the power ofb
.static Complex64
Computesa
raised to the power ofb
.float
re()
Gets the real component of this complex number.static Complex64
Rounds both components of a complex number to the nearest respective integer.static Complex64
Rounds number to specified number of decimal places.static Complex64
roundToZero
(Complex64 n, float tol) Rounds a complex numbers to zero if its magnitude within the specified tolerance from zero.static Complex64
The complex signum function.static Complex64
Computes the trigonometric sine of a complex value.static Complex64
Computes the hyperbolic sine of a complex value.sqrt()
Computes the square root of this field element.static Complex64
sqrt
(float num) Computes the principle square root of a number.sub
(float b) Computes difference of an element of this field and a real number.Computes difference of two elements of this field.static Complex64
Computes the sum of all data of specified array.static Complex64
Computes the trigonometric tangent value of a complex value.static Complex64
Computes the hyperbolic tangent of a complex value.float[]
toPolar()
Converts a complex number to an equivalent polar from.toString()
Converts the complex number to a string representation.
-
Field Details
-
ZERO
The complex number with zero imaginary and real parts. -
ONE
The complex number with zero imaginary part and one real part. -
TWO
The complex number with zero imaginary part and two real part. -
THREE
The complex number with zero imaginary part and three real part. -
ROOT_TWO
The float value closer than any other to the square root of 2 -
ROOT_THREE
The float value closer than any other to the square root of 3 -
IMAGINARY_UNIT
The imaginary unit i. -
INV_IMAGINARY_UNIT
The additive inverse of the imaginary unit, -i. -
POSITIVE_INFINITY
Complex number with real part equal toFloat.POSITIVE_INFINITY
. -
NEGATIVE_INFINITY
Complex number with real part equal toFloat.NEGATIVE_INFINITY
. -
NaN
-
re
public final float reReal component of the complex number. -
im
public final float imImaginary component of the complex number.
-
-
Constructor Details
-
Complex64
public Complex64(float re) Constructs a complex number with specified real component and zero imaginary component.- Parameters:
re
- Real component of complex number.
-
Complex64
public Complex64(float re, float im) Constructs a complex number with specified complex and real components.- Parameters:
re
- Real component of complex number.im
- Imaginary component of complex number.
-
Complex64
Constructs a complex number from a string of the form"a +/- bi"
wherea
and {b} are real values and either may be omitted. i.e."a", "bi", "a +/- i"
, and"i"
are all also valid.- Parameters:
num
- The string representation of a complex number.
-
-
Method Details
-
equals
public boolean equals(float b) Checks if this complex has zero imaginary part and real part equal to a double.- Returns:
- True if
this.re == b && this.im == 0
. False otherwise.
-
equals
-
hashCode
-
isReal
public boolean isReal()Checks if this complex number has zero imaginary part.- Returns:
- True if this complex number has zero imaginary part. False otherwise.
- See Also:
-
isImaginary
public boolean isImaginary()Checks if this complex number has zero real part.- Returns:
- True if this complex number has zero real part. False otherwise.
- See Also:
-
isComplex
public boolean isComplex()Checks if this complex number has non-zero imaginary part.- Returns:
- True if this complex number has non-zero imaginary part. False otherwise.
- See Also:
-
add
-
add
Sums an elements of this field with a real number.- Parameters:
b
- Second element in sum.- Returns:
- The sum of this element and
b
.
-
sub
-
sub
Computes difference of an element of this field and a real number.- Parameters:
b
- Second element in difference.- Returns:
- The difference of this field element and
b
.
-
sum
-
mult
-
isZero
public boolean isZero()Checks if this value is an additive identity for this semiring.
An element 0 is an additive identity if a + 0 = a for any a in the semiring.
-
isOne
public boolean isOne()Checks if this value is a multiplicative identity for this semiring.
An element 1 is a multiplicative identity if a * 1 = a for any a in the semiring.
-
getZero
-
getOne
-
mult
Multiplies an element of this field and a real number (associative and commutative).- Parameters:
b
- Real number product.- Returns:
- The product of this field element and
b
.
-
div
-
div
Computes the quotient of an elements of this field and a real number.- Parameters:
b
- Second element in quotient.- Returns:
- The quotient of this field element and
b
.
-
mag
-
sqrt
-
magSquared
public float magSquared()Squares this magnitude of this complex number.
- Returns:
- The square of the magnitude of this complex number as a real value.
-
addInv
-
multInv
-
conj
-
pow
-
pow
-
pow
-
exp
-
ln
Computes the natural logarithm of a float. For non-negative values this function is equivalent toMath.log(double)
. If the number is negative, then it is passed on as a complex value toln(Complex64)
.- Parameters:
num
- Input to the complex natural logarithm function.- Returns:
- The principle value of the complex natural logarithm for the given input.
-
ln
Computes the complex natural logarithm of a complex number. This function is the analytic continuation of the natural logarithm, that is the log basee
.- Parameters:
num
- Input to the complex natural logarithm function.- Returns:
- The principle value of the complex natural logarithm for the given input.
-
log
Computes the complex logarithm base 10 of a complex number. Please note, this is NOT the natural logarithm. If the complex natural logarithm is desired seeln(float)
. To specify a base, seelog(float, Complex64)
orlog(Complex64, Complex64)
. If the argument is non-negative, then this function is equivalent toMath.log(double)
.- Parameters:
num
- Input to the complex logarithm base 10 function.- Returns:
- The principle value of the complex logarithm base 10 for the given input.
-
log
Computes the complex logarithm base 10 of a complex number. Please note, this is NOT the complex natural logarithm. If the complex natural logarithm is desired seeln(Complex64)
. To specify a base, seelog(float, Complex64)
orlog(Complex64, Complex64)
.- Parameters:
num
- Input to the complex logarithm base 10 function.- Returns:
- The principle value of the complex logarithm base 10 for the given input.
-
log
Computes the complex logarithm, with specified base, of a complex number.- Parameters:
base
- Base of the logarithm.num
- Input to the complex logarithm function with specified base.- Returns:
- The principle value of the complex logarithm, with specified base, for the given input.
-
log
Computes the complex logarithm, with specified base, of a complex number.- Parameters:
base
- Base of the logarithm.num
- Input to the complex logarithm function with specified base.- Returns:
- The principle value of the complex logarithm, with specified base, for the given input.
-
log
Computes the complex logarithm, with specified base, of a complex number.- Parameters:
base
- Base of the logarithm.num
- Input to the complex logarithm function with specified base.- Returns:
- The principle value of the complex logarithm, with specified base, for the given input.
-
sqrt
Computes the principle square root of a number. This method wrapsMath.sqrt(double)
and returns aComplex64
.- Parameters:
num
- Input to square root.- Returns:
- The principle square root of
num
.
-
toPolar
public float[] toPolar()Converts a complex number to an equivalent polar from.- Returns:
- An array of length 2 containing in order, the radius and angle (in radians) if the complex number.
-
fromPolar
Converts a complex number expressed in polar from to the rectangular form.- Parameters:
r
- Radius of complex number.theta
- Angle of the complex number in radians.- Returns:
- An equivalent complex number in rectangular form.
-
atan2
Computes the 2 argument arc-tangent function for a complex number. That is, for a complex number a+bi, atan2(b, a) is computed. This method wrapsMath.atan2(double, double)
.- Parameters:
num
- The input to the atan2 function.- Returns:
- The output of the atan2 function given the specified input. If the complex number is zero, then
Float.NaN
is returned.
-
arg
Computes the complex argument function for a complex number. is computed. This method is equivalent toatan2(Complex64)
.- Parameters:
num
- The input to the atan2 function.- Returns:
- The output of the atan2 function given the specified input. If the complex number is zero, then
Float.NaN
is returned.
-
sin
-
asin
-
sinh
-
cos
-
acos
-
cosh
-
tan
-
atan
-
tanh
-
sgn
The complex signum function. Please note, if the value passed to this method is zero, the function will return zero.- Parameters:
value
- Value to evaluate the signum function at.- Returns:
- If the number is zero then this function returns zero. Otherwise, returns the number divided by its magnitude.
-
round
Rounds both components of a complex number to the nearest respective integer.- Parameters:
n
- The complex number to round.- Returns:
- A complex number with integer real and imaginary components closest to the real and imaginary components of the parameter n
- Throws:
NumberFormatException
- If n isFloat.NaN
,Float.POSITIVE_INFINITY
orFloat.NEGATIVE_INFINITY
- See Also:
-
round
Rounds number to specified number of decimal places. If the number is complex, both the real and imaginary parts will be rounded.- Parameters:
n
- Number to round.decimals
- Number of decimals to round to.- Returns:
- The number rounded to the specified number of decimals.
- Throws:
IllegalArgumentException
- If decimals is less than zero.- See Also:
-
roundToZero
-
nearZero
Checks if a number is near zero in magnitude.- Parameters:
n
- Number to round.tol
- Tolerance of how close to zero is considered "near".- Returns:
- Returns
true
if magnitude of number is less than or equal totol
;false
otherwise. - Throws:
IllegalArgumentException
- If tol is less than 0.
-
compareTo
Compares this element of the field withb
.- Specified by:
compareTo
in interfaceComparable<Complex64>
- Specified by:
compareTo
in interfaceSemiring<Complex64>
- Parameters:
b
- Second element of the field.- Returns:
- An int value:
- 0 if this field element is equal to
b
in magnitude. - invalid input: '<' 0 if this field element is less than
b
in magnitude. - > 0 if this field element is greater than
b
in magnitude.
Hence, this method returns zero if and only if the two field elements are equal in magnitude, a negative value if and
only the field element it was called on is less than
b
in magnitude and positive if and only if the field element it was called on is greater thanb
in magnitude. - 0 if this field element is equal to
-
doubleValue
public double doubleValue()Converts this complex number to an equivalent double. This will be the magnitude of the complex number.- Specified by:
doubleValue
in interfaceSemiring<Complex64>
- Returns:
- A double value equivalent to this complex number. This will be the magnitude of the complex number.
-
min
-
minRe
Computes the minimum real component from an array of complex numbers. All imaginary components are ignored.- Parameters:
values
- Array of values to compute the minimum real component from.- Returns:
- The minimum magnitude from the
values array
. If the array has zero length,Float.NaN
is returned.
-
max
-
maxRe
Computes the minimum real component from an array of complex numbers. All imaginary components are ignored.- Parameters:
values
- Array of values to compute the minimum real component from.- Returns:
- The minimum magnitude from the
values array
. If the array has zero length,Float.NaN
is returned.
-
argmin
Computes the index of the minimum magnitude from an array of complex numbers.- Parameters:
values
- Array of values to compute the index of the minimum magnitude from.- Returns:
- The index of the minimum magnitude from the
values array
. If the array has zero length, then -1 is returned.
-
argminReal
Computes the index of the minimum real component from an array of complex numbers. All imaginary components are ignored.- Parameters:
values
- Array of values to compute the index of the minimum real component from.- Returns:
- The index of the minimum magnitude from the
values array
. If the array has zero length, -1 is returned.
-
argmax
Computes the index of the maximum magnitude from an array of complex numbers.- Parameters:
values
- Array of values to compute the index of the maximum magnitude from.- Returns:
- The index of the minimum magnitude from the
values array
. If the array has zero length, then -1 is returned.
-
argmaxReal
Computes the index of the minimum real component from an array of complex numbers. All imaginary components are ignored.- Parameters:
values
- Array of values to compute the index of the minimum real component from.- Returns:
- The index of the minimum magnitude from the
values array
. If the array has zero length, -1 is returned.
-
isInt
public boolean isInt()Checks if this complex number is a real valued integer.- Returns:
true
if the real component of this number is an integer and the complex component is zero;false
otherwise.
-
isFloat
public boolean isFloat()Checks if this complex number is a real valued float.- Returns:
true
if the complex component is zero;false
otherwise.
-
isFinite
public boolean isFinite()Checks if this field element is finite in magnitude.
NOTE: It may be possible for this method to return true and
mag()
to returnFloat.POSITIVE_INFINITY
,Float.NEGATIVE_INFINITY
, orFloat.NaN
if both components are finite but computing the magnitude explicitly results in an overflow. -
isInfinite
public boolean isInfinite()Checks if this field element is infinite in magnitude.- Specified by:
isInfinite
in interfaceField<Complex64>
- Returns:
- True if this field element is infinite in magnitude. False otherwise (i.e. finite, NaN, etc.).
-
isNaN
-
re
public float re()Gets the real component of this complex number.- Returns:
- The real component of this complex number.
-
im
public float im()Gets the imaginary component of this complex number.- Returns:
- The imaginary component of this complex number.
-
length
Gets the length of the string representation of this complex number.- Parameters:
a
- Complex number.- Returns:
- The length of the string representation of the number.
-
toString
-