Common Java Cookbook

Edition: 0.19

Download PDF or Read on Scribd

Download Examples (ZIP)

8.8. Arithmetic with Complex Numbers

8.8.1. Problem

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.

Expressions evaluated with the Complex object

Figure 8-5. Expressions evaluated with the Complex object


8.8.2. Solution

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

8.8.3. Discussion

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.

8.8.4. See also

For more information about the ComplexMath utility, see the Commons Math JavaDoc at http://commons.apache.org/math/apidocs/index.html.


Creative Commons License
Common Java Cookbook by Tim O'Brien is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 United States License.
Permissions beyond the scope of this license may be available at http://www.discursive.com/books/cjcook/reference/jakartackbk-PREFACE-1.html. Copyright 2009. Common Java Cookbook Chunked HTML Output. Some Rights Reserved.