-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomplex.js
78 lines (77 loc) · 2 KB
/
complex.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
Complex = (function() {
function Complex(x, y) {
this.x = x;
this.y = y;
}
Complex.prototype = {
toString: function() {
return this.x + " + " + this.y + "i";
}
}
Complex.add = function(c1, c2) {
return new Complex(
c1.x + c2.x,
c1.y + c2.y
);
};
Complex.multiply = function(c1, c2) {
return new Complex(
c1.x * c2.x - c1.y * c2.y,
c1.x * c2.y + c1.y * c2.x
);
};
Complex.divide = function(c1, c2) {
var d = c2.x*c2.x - c2.y*c2.y;
return new Complex(
(c1.x*c2.x + c1.y*c2.y)/d,
(c2.x*c1.y - c1.x*c2.y)/d
);
};
Complex.conj = function(c) {
return new Complex(c.x, -c.y);
};
Complex.negative = function(c) {
return new Complex(-c.x, -c.y);
};
Complex.re = function(c) {
return new Complex(c.x, 0);
};
Complex.im = function(c) {
return new Complex(c.y, 0);
};
Complex.abs = function(c) {
return new Complex(Math.sqrt(c.x*c.x + c.y*c.y), 0);
};
Complex.pow = function(c1, c2) {
if(Math.floor(c2.x) == c2.x && c2.y == 0) {
var result = new Complex(1, 0);
for(var i=0; i<c2.x; i++) {
result = Complex.multiply(result, c1);
}
return result;
}
throw NotImplemented;
}
Complex.exp = function(c) {
var ex = Math.exp(c.x);
return new Complex(
ex * Math.cos(c.y),
ex * Math.sin(c.y)
);
}
Complex.log = function(c) {
var r = Math.sqrt(c.x*c.x + c.y*c.y);
var th = Math.atan2(c.y, c.x);
return new Complex(
Math.log(r),
th
);
}
Complex.isNaN = function(c) {
return isNaN(c.x) || isNaN(c.y);
}
Complex.i = new Complex(0, 1);
Complex.pi = new Complex(Math.pi, 0);
Complex.e = new Complex(Math.exp(1), 0);
return Complex;
})();