The complex packages provides a complex number type as well as complex versions of common transcendental functions and complex number formatting.
org.apache.commons.math.complex.Complex provides a complex number type that forms the basis for the complex functionality found in commons-math.
To create a complex number, simply call the constructor passing in two floating-point arguments, the first being the real part of the complex number and the second being the imaginary part:
The Complex
class provides many unary and binary
complex number operations. These operations provide the means to add,
subtract, multiple and, divide complex numbers along with other
complex number functions similar to the real number functions found in
java.math.BigDecimal
:
org.apache.commons.math.complex.ComplexMath provides
implementations of serveral transcendental functions involving complex
number arguments. These operations provide the means to compute the
log, sine, tangent and, other complex values similar to the real
number functions found in java.lang.Math
:
Complex
instances can be converted to and from strings
using the
org.apache.commons.math.complex.ComplexFormat class.
ComplexFormat
is a java.text.Format
extension and, as such, is used like other formatting objects (e.g.
java.text.SimpleDateFormat
):
To customize the formatting output, one or two
java.text.NumberFormat
instances can be used to construct
a ComplexFormat
. These number formats control the
formatting of the real and imaginary values of the complex number:
Another formatting customization provided by
ComplexFormat
is the text used for the imaginary
designation. By default, the imaginary notation is "i" but, it can be
manipulated using the setImaginaryCharacter
method.
Formatting inverse operation, parsing, can also be performed by
ComplexFormat
. Parse a complex number from a string,
simply call the parse
method: