Complex numbers (Java)

From LiteratePrograms

Jump to: navigation, search
Other implementations: C++ | Java | Ruby | Scala

Java does not support standard complex number library. However, it is not difficult to write a complex number class. Also, Apache Commons Math has a Complex class.

Contents

Data

A complex number is composed of two part - a real part re and an imaginary part im. We use IEEE floating-point type double for both part.

<<Data>>=
  private double re;
  private double im;

Constructors

There are three constructors.

  1. Without any argument, it initializes a zero complex number.
  2. Otherwise, it initializes a complex number for given real part and imaginary part.
  3. We also copy a given complex number.
<<Constructor>>=
  public Complex () {
    this.re = 0;
    this.im = 0;
  }
  public Complex(double re, double im) {
    this.re = re;
    this.im = im;
  }
  public Complex(Complex input) {
    this.re = input.getRe();
    this.im = input.getIm();
  }

Getter and Setter

The implementation of both getter and setter are quite straightforward.

<<Setter>>=
  public void setRe(double re) {
    this.re = re;
  }
  public void setIm(double im) {
    this.im = im;
  }   
<<Getter>>=
  public double getRe() {
    return this.re;
  }
  public double getIm() {
    return this.im;
  }

Complex Conjugate

The getConjugate method returns the complex conjugate of a given complex number.

<<Complex_Conjugate>>=
  public Complex getConjugate() {
    return new Complex(this.re, this.im * (-1));
  }

Four Basic Arithmetic Operations

Addition, Subtraction, multiplication, and division are four basic arithmetic operations.

<<Four_Basic_Arithmetic_Operations>>=
  public Complex add(Complex op) {
    Complex result = new Complex();
    result.setRe(this.re + op.getRe());
    result.setIm(this.im + op.getIm());
    return result;
  }
  public Complex sub(Complex op) {
    Complex result = new Complex();
    result.setRe(this.re - op.getRe());
    result.setIm(this.im - op.getIm());
    return result;
  }
  public Complex mul(Complex op) {
    Complex result = new Complex();
    result.setRe(this.re * op.getRe() - this.im * op.getIm());
    result.setIm(this.re * op.getIm() + this.im * op.getRe());
    return result;
  }
  public Complex div(Complex op) {
    Complex result = new Complex(this);
    result = result.mul(op.getConjugate());
   double opNormSq = op.getRe()*op.getRe()+op.getIm()*op.getIm();
    result.setRe(result.getRe() / opNormSq);
    result.setIm(result.getIm() / opNormSq);
    return result;
  }

The division method (div) is the only method which is non-trivial. We can combine complex multiplication, division, and the complex conjugate and norm operations to perform division of arbitrary complex numbers using the following formula:

Polar Form

<<Polar_Form>>=
  public Complex fromPolar(double magnitude, double angle) {
    Complex result = new Complex();
    result.setRe(magnitude * Math.cos(angle));
    result.setIm(magnitude * Math.sin(angle));
    return result;
  }
  public double getNorm() {
    return Math.sqrt(this.re * this.re + this.im * this.im);
  }
  public double getAngle() {
    return Math.atan2(this.im, this.re);
  }

String Representation

This chunk returns string representation of a given complex number. We use i for the imaginary unit. If one part of a given complex number is zero, we omit it without printing "0." We also take the negative imaginary part into consideration.

<<String_Representation>>=
  public String toString() {
    if (this.re == 0) {
      if (this.im == 0) {
        return "0";
      } else {
        return (this.im + "i");
      }
    } else {
      if (this.im == 0) {
        return String.valueOf(this.re);
      } else if (this.im < 0) {
        return(this.re + " " + this.im + "i");
      } else {
        return(this.re + " +" + this.im + "i");
      }
    }
  }

Test

This chunk is used to test.

<<Test>>=
  public static void main(String argv[]) {
    Complex a = new Complex(3, 4);
    Complex b = new Complex(1, -100);
    System.out.println(a.getNorm());
    System.out.println(b.getAngle());
    System.out.println(a.mul(b));
    System.out.println(a.div(b));
    System.out.println(a.div(b).mul(b));
  }

The Whole Class

<<Complex.java>>=
public class Complex {
  Constructor
  Getter
  Setter
  Four_Basic_Arithmetic_Operations
  Polar_Form
  Complex_Conjugate  
  String_Representation
  Data
  Test
}
Download code
Views