## 7 Complex Numbers## 7.1 OverviewThe complex packages provides a complex number type as well as complex versions of common transcendental functions and complex number formatting. ## 7.2 Complex NumbersComplex provides a complex number type that forms the basis for the complex functionality found in commons-math.
Complex functions and arithmetic operations are implemented in
commons-math by applying standard computational formulas and
following the rules for 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: Complex c = new Complex(1.0, 3.0); // 1 + 3i
Complex numbers may also be created from polar representations
using the
The Complex lhs = new Complex(1.0, 3.0); Complex rhs = new Complex(2.0, 5.0); Complex answer = lhs.add(rhs); // add two complex numbers answer = lhs.subtract(rhs); // subtract two complex numbers answer = lhs.abs(); // absolute value answer = lhs.conjugate(rhs); // complex conjugate ## 7.3 Complex Transcendental FunctionsComplex also 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 : Complex first = new Complex(1.0, 3.0); Complex second = new Complex(2.0, 5.0); Complex answer = first.log(); // natural logarithm. answer = first.cos(); // cosine answer = first.pow(second); // first raised to the power of second ## 7.4 Complex Formatting and Parsing
ComplexFormat format = new ComplexFormat(); // default format Complex c = new Complex(1.1111, 2.2222); String s = format.format(c); // s contains "1.11 + 2.22i"
To customize the formatting output, one or two
NumberFormat nf = NumberFormat.getInstance(); nf.setMinimumFractionDigits(3); nf.setMaximumFractionDigits(3); // create complex format with custom number format // when one number format is used, both real and // imaginary parts are formatted the same ComplexFormat cf = new ComplexFormat(nf); Complex c = new Complex(1.11, 2.2222); String s = format.format(c); // s contains "1.110 + 2.222i" NumberFormat nf2 = NumberFormat.getInstance(); nf.setMinimumFractionDigits(1); nf.setMaximumFractionDigits(1); // create complex format with custom number formats cf = new ComplexFormat(nf, nf2); s = format.format(c); // s contains "1.110 + 2.2i"
Another formatting customization provided by
Formatting inverse operation, parsing, can also be performed by
ComplexFormat cf = new ComplexFormat(); Complex c = cf.parse("1.110 + 2.222i"); |