Example Complex Math

From Efficient Java Matrix Library
Revision as of 18:53, 31 March 2015 by Peter (talk | contribs)

Jump to: navigation, search

The Complex64F data type stores a single complex number. Inside the ComplexMath64F class are functions for performing standard math operations on Complex64F, such as addition and division. The example below demonstrates how to perform these operations.

Code on GitHub: ExampleComplexMath

Example

/**
 * Demonstration of different operations that can be performed on complex numbers.
 *
 * @author Peter Abeles
 */
public class ExampleComplexMath {

    public static void main( String []args ) {
        Complex64F a = new Complex64F(1,2);
        Complex64F b = new Complex64F(-1,-0.6);
        Complex64F c = new Complex64F();
        ComplexPolar64F polarC = new ComplexPolar64F();

        System.out.println("a = "+a);
        System.out.println("b = "+b);
        System.out.println("------------------");

        ComplexMath64F.plus(a, b, c);
        System.out.println("a + b = "+c);
        ComplexMath64F.minus(a, b, c);
        System.out.println("a - b = "+c);
        ComplexMath64F.multiply(a, b, c);
        System.out.println("a * b = "+c);
        ComplexMath64F.divide(a, b, c);
        System.out.println("a / b = "+c);

        System.out.println("------------------");
        ComplexPolar64F polarA = new ComplexPolar64F();
        ComplexMath64F.convert(a, polarA);
        System.out.println("polar notation of a = "+polarA);
        ComplexMath64F.pow(polarA, 3, polarC);
        System.out.println("a ** 3 = "+polarC);
        ComplexMath64F.convert(polarC, c);
        System.out.println("a ** 3 = "+c);
    }
}