package at.priv.toastfreeware.jguitartuner;
/* GPL 2 or above */


/** Represents a complex number */
public class Complex {
	/** Real part of the complex number */
	public double real = 0;
	
	
	/** Imaginary part of the complex number */
	public double imag = 0;
	
	
	final static public Complex I = new Complex(0, 1);
	final static public Complex ZERO = new Complex(0, 0);
	final static public Complex ONE = new Complex(1, 0);
	
	
	/** Creates the complex number (0, 0i) */
	public Complex() {}
	
	
	/** Creates a complex number with a imaginary part of zero */
	public Complex(double real) {this.real = real;} 
	
	
	/** Creates a complex number */
	public Complex(double real, double imag) {this.real = real; this.imag = imag;}
	
	
	/** Creates a complex number by duplicating the existing one */
	public Complex(Complex c) {this.real = c.real; this.imag = c.imag;}
	
	
	/** Add the complex number */
	public void add(Complex a) {
		real += a.real;
		imag += a.imag;
	}
	
	
	/** Adds two complex numbers */
	static public Complex add(Complex a, Complex b) {
		Complex c = new Complex(a);
		c.add(b);
		return c;
	}
	
	
	/** Multiplication with a scalar */
	public void multiply(double s) {
		real *= s;
		imag *= s;
	}
	
	
	public static Complex multiply(Complex c, double s) {
		return new Complex(c.real * s, c.imag * s);
	}
	
	
	public static Complex multiply(double s, Complex c) {
		return new Complex(c.real * s, c.imag * s);
	}
	
	
	/** Multiplication of two complex numbers */
	public static Complex multiply(Complex a, Complex b) {
		return new Complex(a.real * b.real - a.imag * b.imag, a.real * b.imag + a.imag * b.real);
	}
	
	
	/** Calculates exp(c) */
	public static Complex exp(Complex c) {
		double r = java.lang.Math.exp(c.real);
		return new Complex(r * java.lang.Math.cos(c.imag), r * java.lang.Math.sin(c.imag));
	}
	
	
	@Override
	public String toString() {
		return String.format("(%f, %fi)", real, imag);
	}
	
	
	@Override
	public boolean equals(Object obj) {
		if (obj == null) return false;
		if (obj instanceof Complex) {
			Complex c = (Complex) obj;
			return c.real == real && c.imag == imag;
		} else return super.equals(obj);
	}

}
