Package org.flag4j.complex_numbers
Class CNumber
java.lang.Object
java.lang.Number
org.flag4j.complex_numbers.CNumber
- All Implemented Interfaces:
Serializable
A complex number stored in rectangular form with both the real and imaginary components stored as a 64-bit floats.
A CNumber
is immutable.
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionstatic final CNumber
The real double value closer to the mathematical constant e than any other.final double
Imaginary component of the complex number.static final CNumber
The imaginary unit i.static final CNumber
The additive inverse of the imaginary unit, -i.static final CNumber
The maximum real double value 1.7976931348623157E308.static final CNumber
The minimum real double value 4.9E-324static final CNumber
The smallest possible real normal double 2.2250738585072014E-308.static final CNumber
Complex number with real and imaginary parts equal toDouble.NaN
.static final CNumber
Complex number with real part equal toDouble.NEGATIVE_INFINITY
.static final CNumber
The complex number with zero imaginary part and negative one real part.static final CNumber
The complex number with zero imaginary part and one real part.static final CNumber
The real double value closer to pi than any other.static final CNumber
Complex number with real part equal toDouble.POSITIVE_INFINITY
.final double
Real component of the complex number.static final CNumber
The double value closer than any other to the square root of 3static final CNumber
The double value closer than any other to the square root of 2static final CNumber
The complex number with zero imaginary part and two real part.static final CNumber
The complex number with zero imaginary and real parts. -
Constructor Summary
ConstructorDescriptionCNumber
(double re) Constructs a complex number with specified real component and zero imaginary component.CNumber
(double re, double 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. -
Method Summary
Modifier and TypeMethodDescriptiondouble
abs()
Computes the absolute value / magnitude of a complex number.static CNumber
acos
(double num) Computes the inverse cosine of a value.static CNumber
Computes the inverse cosine of a complex value.add
(double b) Adds a double to a complex number.Adds two complex numbers.addInv()
Computes the additive inverse of this complex number.static double
Computes the complex argument function for a complex number.static CNumber
argAsCNumber
(CNumber num) 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
(CNumber... 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
(CNumber... values) Computes the index of the minimum real component from an array of complex numbers.static CNumber
asin
(double num) Computes the inverse sine of a value.static CNumber
Computes the inverse sine of a complex value.static CNumber
atan
(double num) Computes the inverse tangent of a value.static CNumber
Computes the inverse tangent of a complex value.static double
Computes the 2 argument arc-tangent function for a complex number.static CNumber
atan2AsCNumber
(CNumber num) Computes the 2 argument arc-tangent function for a complex number.int
Compares the size of two complex numbers (magnitudes).int
compareToReal
(double b) Compares the real value of two numbers.int
Compares the real value of two numbers.conj()
Computes the complex conjugate of this complex number.static CNumber
cos
(double num) Computes the trigonometric cosine of a value.static CNumber
Computes the trigonometric cosine value of a complex value.static CNumber
cosh
(double num) Computes the hyperbolic cosine of a value.static CNumber
Computes the hyperbolic cosine of a complex value.div
(double b) Computes the division of a complex numbers with a double value.Computes the division of two complex numbers.double
Gets the double value of the imaginary component of this complex number.double
Gets the value of the specified number as adouble
.boolean
equals
(double b) Checks if a complex number is numerically equal to some double value.boolean
Checks if two complex numbers are equal.boolean
Checks if aNumber
is numerically equal to this complex number.static CNumber
exp
(double exponent) Computes the exponential function with the given input.static CNumber
Computes the exponential function with the given input.float
Note: This method may result in loss of accuracyfloat
Gets the value of the specified number as afloat
.static CNumber
fromPolar
(double r, double theta) Converts a complex number expressed in polar from to the rectangular form.int
hashCode()
Generates the hashcode for this CNumber.double
im()
Gets the imaginary component of this complex number.int
Note: This method may result in loss of accuracyint
intValue()
Gets the value of the specified number as anint
.boolean
Checks if this complex number has non-zero imaginary part.boolean
isDouble()
Checks if this complex number is a real valued double.boolean
isFinite()
Checks that both components of this complex number are finite valued.boolean
Checks if this complex number has zero real part.boolean
Checks if either component of this complex number is infinitely large in absolute value.boolean
isInt()
Checks if this complex number is a real valued integer.boolean
isNaN()
Checks if either component of this complex number is NaN.boolean
isReal()
Checks if this complex number has zero imaginary part.static int
Gets the length of the string representation of this complex number.static CNumber
ln
(double num) Computes the natural logarithm of a double.static CNumber
Computes the complex natural logarithm of a complex number.static CNumber
log
(double num) Computes the complex logarithm base 10 of a complex number.static CNumber
log
(double base, double num) Computes the complex logarithm, with specified base, of a complex number.static CNumber
Computes the complex logarithm, with specified base, of a complex number.static CNumber
Computes the complex logarithm base 10 of a complex number.static CNumber
Computes the complex logarithm, with specified base, of a complex number.long
Note: This method may result in loss of accuracylong
Gets the value of the specified number as along
.double
mag()
Computes the magnitude value of a complex number as a double.double
Squares this magnitude of this complex number.static CNumber
Computes the maximum magnitude from an array of complex numbers.static CNumber
Computes the minimum real component from an array of complex numbers.static CNumber
Computes the minimum magnitude from an array of complex numbers.static CNumber
Computes the minimum real component from an array of complex numbers.mult
(double b) Computes the multiplication of a complex number and a double.Computes the multiplication of two complex numbers.multInv()
Computes the multiplicative inverse of this complex number.static boolean
Checks if a number is near zero in magnitude.static CNumber
pow
(double a, double b) Compute a raised to the power of b.static CNumber
Compute a raised to the power of b.static CNumber
Computesa
raised to the power ofb
.static CNumber
Compute a raised to the power of b.double
re()
Gets the real component of this complex number.static CNumber
Rounds both components of a complex number to the nearest respective integer.static CNumber
Rounds number to specified number of decimal places.static CNumber
roundToZero
(CNumber n, double tol) Rounds a complex numbers to zero if its magnitude within the specified tolerance from zero.static CNumber
The complex signum function.static CNumber
sin
(double num) Computes the trigonometric sine of a value.static CNumber
Computes the trigonometric sine of a complex value.static CNumber
sinh
(double num) Computes the hyperbolic sine of a value.static CNumber
Computes the hyperbolic sine of a complex value.static CNumber
sqrt
(double num) Computes the principle square root of a number.static CNumber
Computes the principle square root of a number.sub
(double b) subtracts a double from a complex number.Subtracting two complex numbers.static CNumber
Sums an array of complex numbers.static CNumber
tan
(double num) Computes the trigonometric tangent of a value.static CNumber
Computes the trigonometric tangent value of a complex value.static CNumber
tanh
(double num) Computes the hyperbolic tangent of a value.static CNumber
Computes the hyperbolic tangent of a complex value.double[]
toPolar()
Converts a complex number to an equivalent polar from.toString()
Converts the complex number to a string representation.Methods inherited from class java.lang.Number
byteValue, shortValue
-
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. -
NEGATIVE_ONE
The complex number with zero imaginary part and negative one real part. -
PI
The real double value closer to pi than any other. -
E
The real double value closer to the mathematical constant e than any other. -
ROOT_TWO
The double value closer than any other to the square root of 2 -
ROOT_THREE
The double 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. -
MAX_REAL
The maximum real double value 1.7976931348623157E308. -
MIN_REAL
The minimum real double value 4.9E-324 -
MIN_REAL_NORMAL
The smallest possible real normal double 2.2250738585072014E-308. -
POSITIVE_INFINITY
Complex number with real part equal toDouble.POSITIVE_INFINITY
. -
NEGATIVE_INFINITY
Complex number with real part equal toDouble.NEGATIVE_INFINITY
. -
NaN
Complex number with real and imaginary parts equal toDouble.NaN
. -
re
public final double reReal component of the complex number. -
im
public final double imImaginary component of the complex number.
-
-
Constructor Details
-
CNumber
public CNumber(double re) Constructs a complex number with specified real component and zero imaginary component.- Parameters:
re
- Real component of complex number.
-
CNumber
public CNumber(double re, double im) Constructs a complex number with specified complex and real components.- Parameters:
re
- Real component of complex number.im
- Imaginary component of complex number.
-
CNumber
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
-
equalsNumber
-
equals
public boolean equals(double b) Checks if a complex number is numerically equal to some double value. That is, if the real component of this complex number is zero and the real component is equivalent to the double parameter.- Parameters:
b
- The double to compare.- Returns:
- True if b is a complex number and is equivalent to this complex number in both the real and imaginary components. False, otherwise.
-
hashCode
-
intValue
-
longValue
-
floatValue
public float floatValue()Gets the value of the specified number as afloat
. This will be calculated only with the real component of thisCNumber
.- Specified by:
floatValue
in classNumber
- Returns:
- the numeric value represented by this object after conversion
to type
float
.
-
doubleValue
public double doubleValue()Gets the value of the specified number as adouble
. This will be calculated only with the real component of thisCNumber
.- Specified by:
doubleValue
in classNumber
- Returns:
- the numeric value represented by this object after conversion
to type
double
.
-
doubleImaginaryValue
public double doubleImaginaryValue()Gets the double value of the imaginary component of this complex number.- Returns:
- imaginary part of given Number as double
-
floatImaginaryValue
public float floatImaginaryValue()Note: This method may result in loss of accuracy- Returns:
- imaginary part of given Number as float
-
intImaginaryValue
public int intImaginaryValue()Note: This method may result in loss of accuracy- Returns:
- imaginary part of given Number as int
-
longImaginaryValue
public long longImaginaryValue()Note: This method may result in loss of accuracy- Returns:
- imaginary part of given Number as long
-
add
-
add
Adds a double to a complex number.- Parameters:
b
- The double value to add to this complex number.- Returns:
- The result of adding b to this complex number.
-
sub
-
sub
subtracts a double from a complex number.- Parameters:
b
- The double value to subtract from this complex number.- Returns:
- The result of subtracting b from this complex number.
-
sum
-
mult
-
mult
Computes the multiplication of a complex number and a double.- Parameters:
b
- Second complex number in the product.- Returns:
- Product of this complex number with b.
-
div
-
div
-
abs
public double abs()Computes the absolute value / magnitude of a complex number.
Note: This method is the same asmag()
.- Returns:
- The absolute value/magnitude of this complex number.
-
mag
public double mag()Computes the magnitude value of a complex number as a double.- Returns:
- The absolute value/magnitude of this complex number as a double.
-
addInv
Computes the additive inverse of this complex number.- Returns:
- The additive inverse of this complex number.
-
multInv
Computes the multiplicative inverse of this complex number.- Returns:
- The multiplicative inverse of this complex number.
-
conj
Computes the complex conjugate of this complex number.- Returns:
- The complex conjugate of this complex number.
-
pow
Compute a raised to the power of b. This method wrapsMath.pow(double, double)
and returns aCNumber
.- Parameters:
a
- The base.b
- The exponent.- Returns:
- a raised to the power of b.
-
pow
-
pow
-
pow
-
magSquared
public double magSquared()Squares this magnitude of this complex number.- Returns:
- The square of the magnitude of this complex number as a real value.
-
exp
Computes the exponential function with the given input. This method simply wraps theMath.exp(double)
method and returns aCNumber
.- Parameters:
exponent
- Input to the exponential function.- Returns:
- The output of the exponential function.
-
exp
-
ln
Computes the natural logarithm of a double. 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(CNumber)
.- 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(double)
. To specify a base, seelog(double, CNumber)
orlog(CNumber, CNumber)
. 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(CNumber)
. To specify a base, seelog(double, CNumber)
orlog(CNumber, CNumber)
.- 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 aCNumber
.- Parameters:
num
- Input to square root.- Returns:
- The principle square root of
num
.
-
sqrt
-
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.
-
toPolar
public double[] 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)
.
To get the result as anCNumber
seeatan2AsCNumber(CNumber)
.- 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
Double.NaN
is returned.
-
arg
Computes the complex argument function for a complex number. is computed. This method is equivalent toatan2(CNumber)
.
To get the result as anCNumber
seeargAsCNumber(CNumber)
.- 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
Double.NaN
is returned.
-
argAsCNumber
Computes the complex argument function for a complex number. is computed. This method wraps is equivalent toCNumber
.
To get the result as a double seearg(CNumber)
.- 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
Double.NaN
is returned.
-
atan2AsCNumber
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)
.
To get the result as a double seeatan2(CNumber)
.- 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
Double.NaN
is returned.
-
sin
Computes the trigonometric sine of a value.
Note, this method wrapsMath.sin(double)
and returns aCNumber
.- Parameters:
num
- Input angle in radians.- Returns:
- The trigonometric sine function evaluated at the specified value.
-
sin
-
asin
Computes the inverse sine of a value.
Note, this method wrapsMath.asin(double)
and returnsCNumber
.- Parameters:
num
- Input to the inverse sine function.- Returns:
- The inverse sine of the input value. That is, the angle whose sine value is num.
-
asin
-
sinh
Computes the hyperbolic sine of a value.
Note, this method wrapsMath.sinh(double)
and returns aCNumber
.- Parameters:
num
- Input to the hyperbolic sine function.- Returns:
- The hyperbolic sine of the input value.
-
sinh
-
cos
Computes the trigonometric cosine of a value.
Note, this method wrapsMath.cos(double)
and returns aCNumber
.- Parameters:
num
- Input angle in radians.- Returns:
- The trigonometric cosine function evaluated at the specified value.
-
cos
-
acos
Computes the inverse cosine of a value.
Note, this method wrapsMath.acos(double)
and returnsCNumber
.- Parameters:
num
- Input to the inverse cosine function.- Returns:
- The inverse cosine of the input value. That is, the angle whose cosine value is num.
-
acos
-
cosh
Computes the hyperbolic cosine of a value.
Note, this method wrapsMath.cosh(double)
and returns aCNumber
.- Parameters:
num
- Input to the hyperbolic cosine function.- Returns:
- The hyperbolic cosine of the input value.
-
cosh
-
tan
Computes the trigonometric tangent of a value.
Note, this method wrapsMath.tan(double)
and returns aCNumber
.- Parameters:
num
- Input angle in radians.- Returns:
- The trigonometric tangent function evaluated at the specified value.
-
tan
-
atan
Computes the inverse tangent of a value.
Note, this method wrapsMath.atan(double)
and returnsCNumber
.- Parameters:
num
- Input to the inverse tangent function.- Returns:
- The inverse tangent of the input value. That is, the angle whose tangent value is num.
-
atan
-
tanh
Computes the hyperbolic tangent of a value.
Note, this method wrapsMath.tanh(double)
and returns aCNumber
.- Parameters:
num
- Input to the hyperbolic tangent function.- Returns:
- The hyperbolic tangent of the input value.
-
tanh
-
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:
-
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 isDouble.NaN
,Double.POSITIVE_INFINITY
orDouble.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
n
rounded to the specified number of decimals. - Throws:
IllegalArgumentException
- If decimals is less than zero.NumberFormatException
- If n isDouble.NaN
,Double.POSITIVE_INFINITY
orDouble.NEGATIVE_INFINITY
- 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 to
tol
. Otherwise, returns false. - Throws:
IllegalArgumentException
- If tol is less than 0.
-
compareTo
Compares the size of two complex numbers (magnitudes).- Parameters:
b
- Number to compare to this number.- Returns:
- - If the magnitude of this number is equal to that of
b
, then this method will return 0.
- If the magnitude of this number is less than that ofb
, then this method will return a negative number.
- If the magnitude of this number is greater than that ofb
, then this method will return a positive number.
-
compareToReal
Compares the real value of two numbers.- Parameters:
b
- Number to compare to this number.- Returns:
- - If the real value of this number is equal to that of
b
, then this method will return 0.
- If the real value of this number is less than that ofb
, then this method will return a negative number.
- If the real value of this number is greater than that ofb
, then this method will return a positive number.
-
compareToReal
public int compareToReal(double b) Compares the real value of two numbers. This method wrapsDouble.compareTo(Double)
which is computed with the real component of this complex number.- Parameters:
b
- Number to compare to this number.- Returns:
- - If the real value of this number is equal to that of
b
, then this method will return 0.
- If the real value of this number is less than that ofb
, then this method will return a negative number.
- If the real value of this number is greater than that ofb
, then this method will return a positive 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,Double.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,Double.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. Otherwise, returns false.
-
isDouble
public boolean isDouble()Checks if this complex number is a real valued double.- Returns:
- True if the complex component is zero. Otherwise, returns false.
-
isNaN
public boolean isNaN()Checks if either component of this complex number is NaN.- Returns:
- True if either component is NaN. Otherwise, returns false.
-
isFinite
public boolean isFinite()Checks that both components of this complex number are finite valued.- Returns:
- True if both components are finite. Otherwise, returns false (including NaN).
-
isInfinite
public boolean isInfinite()Checks if either component of this complex number is infinitely large in absolute value.- Returns:
- True if either components are infinite. Otherwise, returns false.
-
re
public double re()Gets the real component of this complex number.- Returns:
- The real component of this complex number.
-
im
public double 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
-