-
Notifications
You must be signed in to change notification settings - Fork 0
/
JSClass.js
46 lines (44 loc) · 1.46 KB
/
JSClass.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
function Complex(real, imaginary){
if(isNaN(real) || isNaN(imaginary)){
throw new TypeError();
}
this.r = real;
this.i = imaginary;
}
Complex.prototype.add = function(that){
return new Complex(this.r+that.r, this.i+that.i);
};
Complex.prototype.mul = function(that){
return new Complex(this.r * that.r - this.i * that.i,
this.r * that.i + this.i * that.r);
};
Complex.prototype.mag = function(){
return Math.sqrt(this.r * this.r + this.i * this.i);
};
Complex.prototype.neg = function(){
return new Complex(-this.r, -this.i);
};
Complex.prototype.toString = function(){
return "{" + this.r + "," + this.i + "}";
};
Complex.prototype.equals = function(that){
return that != null &&
that.constructor === Complex &&
this.r === that.r && this.i === that.i;
};
Complex.ZERO = new Complex(0, 0);
Complex.ONE = new Complex(1, 0);
Complex.I = new Complex(0, 1);
Complex.parse = function(s){
try{
var m = Complex._format.exec(s);
return new Complex(parseFloat(m[1]), parseFloat(m[2]));
}catch (x){
throw new TypeError();
}
}
Complex._format = /^\{([^,]+),([^}]+)\}$/;
var c = new Complex(2, 3);
var d = new Complex(c.i, c.r);
c.add(d).toString();
Complex.parse(c.toString()).add(c.neg()).equals(Complex.ZERO);