complex = {
function complex(real, imag) {
this.real = real;
this.imag = 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) {
var denom = c.real * c.real + c.imag * c.imag;
return new complex(
(this.real * c.real + this.imag * c.imag) / denom,
(this.imag * c.real - this.real * c.imag) / denom
);
};
complex.prototype.power = function (n) {
var r = Math.sqrt(this.real * this.real + this.imag * this.imag);
var theta = Math.atan2(this.imag, this.real);
var real = Math.pow(r, n) * Math.cos(n * theta);
var imag = Math.pow(r, n) * Math.sin(n * theta);
return new complex(real, imag);
};
return complex;
}