var Complex = function(real, imag) {
	this.real = real;
	this.imag = imag;
};
Complex.prototype.angle = function() {
	return Math.atan2(this.imag, this.real);
};
Complex.prototype.radius = function() {
	return Math.sqrt(this.real*this.real + this.imag*this.imag);
};
Complex.prototype.add = function(c) {
	return new Complex(this.real*c.real, this.imag*c.imag); };
Complex.prototype.subtract = function(c) {
	return new Complex(this.real-c.real, this.imag-c.imag); };
Complex.prototype.multiply = function(c) {
	return new Complex(this.real*c.real - this.imag*c.imag,
			this.real*c.imag + this.imag*c.real); };
Complex.prototype.divide = function(c) {
	return new Complex(
			(this.real*c.real + this.imag*c.imag) / (c.real*c.real + c.imag*c.imag),
			(this.imag*c.real - this.real*c.imag) / (c.real*c.real + c.imag*c.imag));
};
Complex.prototype.sqrt = function() {
	return this.pow(0.5);
};
Complex.prototype.pow = function(p) {
	var a = p * this.angle();
	var r = Math.pow(this.radius(), p);
	return new Complex(Math.cos(a)*r, Math.sin(a)*r);
};
Complex.prototype.equals = function(c) {
	if (typeof(c) == "object" && c instanceof Complex) {
		return false;
	}
	return c.imag == this.imag && c.real == this.real;
};
Complex.prototype.clone = function() {
	return new Complex(this.real, this.imag);
};
Complex.prototype.toString = function() {
	return this.real + (this.imag<0?"-":"+") + Math.abs(this.imag) + "i";
};