package jeranvier.math.util;

import java.text.DecimalFormat;

public class Complex{
	private static final double DELTA = 1e-20;
	private final double a;
	private final double b;
	private static final DecimalFormat formatter = new DecimalFormat("###0.##########");

	
	public Complex(double a, double b){
		this.a = a ;
		this.b = b;
	}
	
	public Complex(){
		this.a = 0 ;
		this.b = 0;
	}
	
	public double a(){
		return this.a;
	}
	
	public double b(){
		return this.b;
	}
	
	public double re(){
		return this.a;
	}
	
	public double im(){
		return this.b;
	}
	
	public double r(){
		return Math.sqrt(a*a+b*b);
	}
	
	public double phi(){
		return Math.atan2(b, a);
	}
	
	public boolean isReal(){
		return b*b < DELTA;
	}
	
	@Override
	public String toString(){
		return formatter.format(a)+(!isReal()?((b<0?"":"+")+formatter.format(b)+"i"):"");
	}

	public Complex substract(Complex that) {
		return new Complex(this.a-that.a, this.b-that.b);
	}

	public Complex add(Complex that) {
		return new Complex(this.a+that.a, this.b+that.b);
	}
	
	public Complex add(double a, double b) {
		return new Complex(this.a+a, this.b+b);
	}

	public Complex multiplyBy(Complex that) {
		return new Complex(this.a*that.a - this.b*that.b, this.b*that.a + this.a*that.b);	
	}

	public Complex divideBy(Complex denominator) {
		return new Complex((this.a*denominator.a + this.b*denominator.b)/(denominator.a*denominator.a + denominator.b*denominator.b)
				,(this.b*denominator.a - this.a*denominator.b)/(denominator.a*denominator.a + denominator.b*denominator.b));
	}
	
	public Complex conjugate() {
		return new Complex(a , -1*b);
	}

	public Complex add(double n) {
		return new Complex(this.a+n, this.b);
	}

	public Complex substract(double n) {
		return new Complex(this.a-n, this.b);
	}

	public Complex multiplyBy(double n) {
		return new Complex(this.a*n, this.b*n);
	}

	public Complex divideBy(double n) {
		return new Complex(this.a/n, this.b/n);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		long temp;
		temp = Double.doubleToLongBits(a);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		temp = Double.doubleToLongBits(b);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Complex other = (Complex) obj;
		if (Double.doubleToLongBits(a) != Double.doubleToLongBits(other.a))
			return false;
		if (Double.doubleToLongBits(b) != Double.doubleToLongBits(other.b))
			return false;
		return true;
	}
	
	

}

@@ -665,21 +665,75 @@
 %0A%09%7D%0A%09%0A%09p
-ublic
+rivate boolean hasIm()%7B%0A%09%09return b*b %3E DELTA;%0A%09%7D%0A%09%0A%09private
  boolean
@@ -733,22 +733,21 @@
 boolean 
-i
+ha
 sRe
-al
 ()%7B%0A%09%09re
@@ -755,13 +755,13 @@
 urn 
-b*b %3C
+a*a %3E
  DEL
@@ -816,16 +816,25 @@
 %09return 
+(hasRe()?
 formatte
@@ -848,17 +848,35 @@
 t(a)
-+(!isReal
+:(hasIm()?%22%22:%220.0%22))+(hasIm
 ()?(

fix Complex display formating once more...