-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomplex.js
71 lines (62 loc) · 1.66 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
function complexNum(real, imaginary) {
this.real = real;
this.imaginary = imaginary;
return this;
}
function raiseNumberToComplexPower(x, c) {
var s = Math.pow(x,c.real);
var pow = c.imaginary*Math.log(x);
var num = new complexNum(Math.cos(pow),Math.sin(pow));
return scalarComplex(s, num);
}
function addComplex(c1, c2) {
var real = c1.real + c2.real;
var imaginary = c1.imaginary + c2.imaginary;
return new complexNum(real,imaginary);
}
function multComplex(c1, c2) {
var real = (c1.real * c2.real) - (c1.imaginary * c2.imaginary);
var imaginary = (c1.real * c2.imaginary) + (c2.real * c1.imaginary);
return new complexNum(real,imaginary);
}
function scalarComplex(s, c) {
return new complexNum(c.real*s, c.imaginary*s);
}
function absComplex(c) {
return new complexNum(Math.abs(c.real),Math.abs(c.imaginary));
}
function dispComplex(c) {
var sign = '';
if (c.imaginary >= 0) {
sign = '+';
}
return Math.floor(c.real*10000)/10000 + sign + Math.floor(c.imaginary*10000)/10000 + "i";
}
function getComplexModulus(c) {
return Math.sqrt((c.real * c.real) + (c.imaginary * c.imaginary));
}
function getComplexArgument(c) {
if (c.real > 0) {
if (c.imaginary > 0) {
return Math.atan(c.imaginary / c.real);
} else if (c.imaginary < 0) {
return 2*Math.PI + Math.atan(c.imaginary / c.real);
} else {
return 0;
}
} else if (c.real < 0) {
if (c.imaginary > 0) {
return (Math.PI / 2) + Math.atan(c.real / c.imaginary);
} else if (c.imaginary < 0) {
return (Math.PI) + Math.atan(c.imaginary / c.real);
} else {
return 0;
}
} else if (c.imaginary > 0) {
return PI/2;
} else if (c.imaginary < 0) {
return (3*PI)/2;
} else {
return null;
}
}