Skip to content

Instantly share code, notes, and snippets.

@saidinesh5
Created March 6, 2014 05:02
Show Gist options
  • Save saidinesh5/9382941 to your computer and use it in GitHub Desktop.
Save saidinesh5/9382941 to your computer and use it in GitHub Desktop.
Rewrote the code from https://gist.github.com/dsamarin/1258353 such that the arithematic operatioins wont modify the Original object. Trading off performance for cleaner code.
.pragma library
//
// Originally taken from:
// https://gist.github.com/dsamarin/1258353
//
var Complex = function(real, imag) {
if (!(this instanceof Complex)) {
return new Complex (real, imag);
}
if (typeof real === "string" && imag === null) {
return Complex.parse (real);
}
this.real = Number(real) || 0;
this.imag = Number(imag) || 0;
};
Complex.parse = function(string) {
var real, imag, regex, match, a, b, c;
// TODO: Make this work better-er
regex = /^([-+]?(?:\d+|\d*\.\d+))?[-+]?(\d+|\d*\.\d+)?[ij]$/i;
string = String(string).replace (/\s+/g, '');
match = string.match (regex);
if (!match) {
throw new Error("Invalid input to Complex.parse, expecting a + bi format");
}
a = match[1];
b = match[2];
c = match[3];
real = a !== null ? parseFloat (a) : 0;
imag = parseFloat ((b || "+") + (c || "1"));
return new Complex(real, imag);
};
Complex.prototype.copy = function() {
return new Complex (this.real, this.imag);
};
Complex.prototype.add = function(operand) {
if (operand instanceof Complex) {
return new Complex( this.real + operand.real , this.imag - operand.imag )
}
return new Complex( this.real + Number(operand), this.imag )
};
Complex.prototype.subtract = function(operand) {
if (operand instanceof Complex) {
return new Complex( this.real - operand.real , this.imag - operand.imag )
}
return new Complex( this.real - Number(operand), this.imag )
};
Complex.prototype.multiply = function(operand) {
if (operand instanceof Complex) {
return new Complex( this.real * operand.real - this.imag * operand.imag,
this.real * operand.imag + this.imag * operand.real )
}
var num = Number(operand)
return new Complex ( this.real * num , this.imag * num )
};
Complex.prototype.divide = function(operand) {
var denominator;
if (operand instanceof Complex) {
denominator = operand.real * operand.real + operand.imag * operand.imag;
return new Complex( (this.real * operand.real + this.imag * operand.imag)/denominator,
(this.imag * operand.real - this.real * operand.imag)/denominator );
}
denominator = Number(operand);
return new Complex ( this.real / denominator , this.imag / denominator )
};
Complex.prototype.squared = function() {
return this.multiply(this)
};
Complex.prototype.toString = function() {
return '{' + this.real + ',' + this.imag + '}'
};
Complex.prototype.magnitude = function() {
return Math.sqrt( this.real * this.real + this.imag * this.imag )
};
Complex.prototype.angle = function() {
return Math.atan(y/x);
};
function fromPolar (r,angle) {
return new Complex( r * Math.cos(angle), r * Math.sin(angle) )
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment