function Complex(a, b)
{
	this.real = a;
	this.imag = b;
}

function add(a, b)
{
	c = new Complex;
	
	c.real = a.real + b.real;
	c.imag = a.imag + b.imag;
	
	return c;
}

function subtract(a, b)
{
	c = new Complex;
	
	c.real = a.real - b.real;
	c.imag = a.imag - b.imag;
	
	return c;
}

function multiply(a, b)
{
	c = new Complex;
	
	c.real = a.real * b.real - a.imag * b.imag;
	c.imag = a.real * b.imag + b.real * a.imag;
	
	return c;
}

function divide(a, b)
{
	c = new Complex;
	
	denom = b.real * b.real + b.imag * b.imag;
	c.real = (b.imag * a.imag + b.real * a.real) / denom;
	c.imag = (b.real * a.imag - b.imag * a.real) / denom;
	
	return c;
}

function modulus(a)
{
	return Math.sqrt(a.real * a.real + a.imag * a.imag);
}
	
function mod(a) //does not take sqrt
{
	return (a.real * a.real + a.imag * a.imag);
	//use simplify radicals page in JS file to simplify it, but keep it in radical form.
}

function arg(a)
{
	return Math.atan(a.imag / a.real);
	//again think about keeping in non-decimal format.
}

function dmt(z, power) //does DMT and returns as Cartesian.
{
	//alert(toComplexString(z));
	r = modulus(z);
	r = Math.pow(r,power);
	angle = arg(z);
	angle *= power;
	
	//alert(r + "\n" + angle);
	
	w = new Complex();
	w.real = r * Math.cos(angle);
	w.imag = r * Math.sin(angle);
	return w;
	//converts to polar, then applies De Moirve's Theorem
}

//************************** TO STRING METHODS ***************************

//need a method to check if a number is an integer, then can ignore rounding.

function toComplexRoundedString(z, rounding)
{
	if(z.real == 0 && z.imag == 0)
	{
		return 0;
	}
	else if(z.real == 0)
	{
		if(z.imag == 1)
		{
			return "<i>i</i>";
		}
		else if(z.imag == -1)
		{
			return "-<i>i</i>";
		}
		else
		{
			return z.imag.toFixed(rounding) + "<i>i</i>";
		}
	}
	else if(z.imag == 0)
	{
		return z.real.toFixed(rounding);
	}
	else if(z.imag == 1)
	{
		return z.real.toFixed(rounding) + " + <i>i</i>";
	}
	else if(z.imag == -1)
	{
		return z.real.toFixed(rounding) + " - <i>i</i>";
	}
	else if(z.imag < 0)
	{
		return z.real.toFixed(rounding) + " - " + Math.abs(z.imag).toFixed(rounding) + "<i>i</i>";
	}
	else
	{
		return z.real.toFixed(rounding) + " + " + z.imag.toFixed(rounding) + "<i>i</i>";
	}
}	
	
function toComplexString(z)
{
	if(z.real == 0 && z.imag == 0)
	{
		return 0;
	}
	else if(z.real == 0)
	{
		if(z.imag == 1)
		{
			return "<i>i</i>";
		}
		else if(z.imag == -1)
		{
			return "-<i>i</i>";
		}
		else
		{
			return z.imag + "<i>i</i>";
		}
	}
	else if(z.imag == 0)
	{
		return z.real;
	}
	else if(z.imag == 1)
	{
		return z.real + " + <i>i</i>";
	}
	else if(z.imag == -1)
	{
		return z.real + " - <i>i</i>";
	}
	else if(z.imag < 0)
	{
		return z.real + " - " + Math.abs(z.imag) + "<i>i</i>";
	}
	else
	{
		return z.real + " + " + z.imag + "<i>i</i>";
	}
}

function toPolarString(z)
{
	/*
	function PerfectSquare(x)
	{
		//returns true if number is a perfect square
		var num = Math.sqrt(x);
		var newnum = Math.floor(num);
		if(newnum * newnum == x)
			{return true;}
		else 
			{return false;}
	}	*/

	//need to check value of cost and sint - if zero, remove.
	//make "exact" using coeff of PI and fractions etc
	
	//need both radical simplification and decimal -> fractions to do this well.
	//can apply this to the other uses of radians throughout site if successful.
	//consider writing angle as multiple of Pi, can this be done - it seems it can!
	
	//may therefore need to add both frac convert and radical stuff to JS files.
	
	//
	
	//alert(coeff);
	
	r = mod(z);
	t = arg(z);

	var coeff = Math.PI / t;
	if(t == 0)
	{coeff = 0;}	
	//coeff now needs to be expressed as a fraction.
	
	//var fracCoeff = convert(coeff);
	//alert(fracCoeff);
	if(coeff != 0)
	{var angleStr = "(cos <span class='pi'>&pi;</span>/" + coeff.toFixed(2) + " + <i>i</i>sin <span class='pi'>&pi;</span>/" + coeff.toFixed(2) + ")"}
	else
	{var angleStr = "(cos " + 0 + " + <i>i</i>sin " + 0 + ")";}
	
	if(r == 0)
	{
		return 0;	
	}	
	else if(r == 1)
	{
		return angleStr;
	}
	else
	{
		return "sqrt(" + r.toFixed(0) + ")" + angleStr;
	}
	
}

function toPolarRoundedString(z, rounding)
{
	//need to check value of cost and sint - if zero, remove.

	r = mod(z);
	t = arg(z);

	var angleStr = "(cos " + t.toFixed(rounding) +" +  <i>i</i>sin " + t.toFixed(rounding) + ")"
	
	if(r == 0)
	{
		return 0;	
	}	
	else if(r == 1)
	{
	
		return angleStr;
	}
	else
	{
		return Math.sqrt(r).toFixed(rounding) + angleStr;
	}
	
}

function PolarToCartesian(radius, angle)
{
	var real = radius * Math.cos(angle);
	var imag = radius * Math.sin(angle);

	return new Complex(real, imag);
}
