You need to perform arithmetic with complex numbers. For example, given the complex
numbers A, B, C,
and E and two equations shown in
Figure 8-5, you need to find the real
part of F and the imaginary part of
D.
Use Commons Math Complex and
ComplexMath classes to represent
complex numbers and perform arithmetic using complex numbers. Use the
ComplexFormat class to print the real and imaginary parts of a complex
number. The following example demonstrates the use of the Complex class to calculate D and F
from Figure 8-5 using arbitrary values
for A, B, C,
and E:
import org.apache.commons.math.complex.Complex;
import org.apache.commons.math.complex.ComplexFormat;
Complex a = new Complex(2, 3);
Complex b = new Complex(4, 5);
Complex c = new Complex(0.3, 2);
Complex e = new Complex(4, 4);
Complex sum = a.add( b );
Complex d = c.divide( sum );
Complex f = e.multiply( d.conjugate( ) );
System.out.println( "D is: " + ComplexFormat.formatComplex( d ) );
System.out.println( "F is: " + ComplexFormat.formatComplex( f ) );
double realF = f.getReal( );
double imD = d.getImaginary( );
double answer = realF / imD;
System.out.println( "Answer Re(F)/Im(D): " +
NumberFormat.getInstance( ).format( answer ) );The variables a, b, c, and
e are created using arbitrary values,
and an intermediate Complex object
sum is calculated by adding b to a--a.add(b). d is calculated by dividing this intermediate
sum by c: c.divide(sum). f is calculated by multiplying e times the complex conjugate of d: e.multiply(
d.conjugate( ) ). The final answer is calculated by taking the
real part of f (f.getReal( )) and dividing that by the
imaginary part of d: d.getImaginary( ). The previous example
performs complex arithmetic and prints the following to the
console:
D is: 0.18 + 0.1i F is: 1.1 + 0.33i Answer Re(F)/Im(D): 11.417
The previous example used the ComplexFormat class to create a String representation of a Complex object. This class allows you to print
out the complex number N in the
format Re(N) + Im(N)i. This class
also has a constructor that takes a String to use instead of "i." In electrical
engineering, where "i" is frequently used to refer to current, complex
impedance is represented using a "j" instead of an "i." To print a
complex number using a "j", write the following code:
Complex impedance = new Complex( 1.0, 2.0 );
ComplexFormat format = new ComplexFormat("j");
System.out.println( "Impedance: " + format.format( impedance ) );The previous code prints the following output to the console:
Impedance: 1.0 + 2.0j
The Complex object contains
simple arithmetic methods such as add(
), subtract( ), multiply( ), divide(), conjugate(
), and negate( ). More
advanced methods are available as static methods on the ComplexMath class. ComplexMath includes trigonometric methods
such as sin( ), sinh( ), cos(), and tan(
), as well as methods to calculate logarithms and to take the
square root of a Complex
object.
For more information about the ComplexMath utility, see the Commons Math
JavaDoc at http://commons.apache.org/math/apidocs/index.html.
