package math.equation;

import math.number.NumberComplex;

/**
 * @author Daniel Weschke
 */
public class PolynomialComplex {
	private Polynomial re;
	private Polynomial im;
	
	public PolynomialComplex(){
		setRe(new Polynomial());
		im = new Polynomial();
	}
	
	public PolynomialComplex(String cpol){
		this(new Polynomial(cpol));
	}
	
	public PolynomialComplex(Polynomial c){
		int i;
		for(i=0; i<=c.degree(); i++){
			// TODO:
		}
		
    	int size = c.degree()+1;
    	double[] coefRe = new double[size];
    	double[] coefIm = new double[size];
    	for(i=0; i<size; i++){
    		if(i%2 == 0)
    			coefRe[size-i-1] = c.getCoef(i)*Math.pow(-1,i/2);
    		if(i%2 == 1)
    			coefIm[size-i-1] = c.getCoef(i)*Math.pow(-1,i/2);
    	}
    	setRe(new Polynomial(coefRe));
    	im = new Polynomial(coefIm);
    	
	}
	
	public PolynomialComplex(Polynomial re, Polynomial im){
		this.re = re;
		this.im = im;
	}
    
    /**
     * set the symbolic of all polynomials
     * @param symb symbolic
     */
    public void setSymbolic(String symb){
    	re.setSymbolic(symb);
    	im.setSymbolic(symb);
    }
	
	/**
	 * a + ib, a=Re(f(x)), b=Im(f(x))
	 * @param x
	 */
	public NumberComplex evaluate(double x){
		return new NumberComplex(re.evaluate(x), im.evaluate(x));
	}
	
	public double mod(double x){
		return evaluate(x).mod();
	}
	
	public double argd(double x){
		return evaluate(x).argd();
	}

	public Polynomial getRe() {
		return re;
	}

	public void setRe(Polynomial re) {
		this.re = re;
	}

	public Polynomial getIm() {
		return im;
	}

	public void setIm(Polynomial im) {
		this.im = im;
	}
	
	@Override
	public String toString(){
		if(getRe().degree() == 0 && im.degree() == 0){
			if(im.getCoef(0) == 0) return ""+getRe();
			if(getRe().getCoef(0) == 0) return im+"i";
			return ""+getRe()+" + "+im+"i";  
		}
		return "("+getRe()+") + ("+im+")i";
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Polynomial pol = new Polynomial("576 240 24 0");
		pol.setSymbolic("s");
		System.out.println(" G(s)  = "+pol);
		
		PolynomialComplex cpol = new PolynomialComplex(pol);
		cpol.setSymbolic("ω");
		System.out.println(" G(jω) = "+cpol);

		NumberComplex cn = cpol.evaluate(0.01);
		System.out.println(" G(2)  = "+cn);
		
		System.out.println("|G(2)| = "+cn.mod());
		System.out.println("phi(2) = "+cn.argd());
	}

}
