-
Notifications
You must be signed in to change notification settings - Fork 10
/
complex.js
163 lines (140 loc) · 3.75 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/********************************************* COMPLEX NUMBERS **********************************************/
/*************************************************************************************************************
From: Coding Phyisics
email: [email protected]
github: https://github.com/CodingPhysics
*************************************************************************************************************/
/*************************************************************************************************************
- cartesian representation (re, im) and polar coordinates (abs, arg)
- basic aritmetric operations (add, sub, mul, div) with variable arguments
- operations can be chained
*************************************************************************************************************/
function Complex(a, b) {
this.re = (a instanceof Complex)? a.re : (a ? a : 0.0);
this.im = (a instanceof Complex)? a.im : (b ? b : 0.0);
this.absVal = undefined;
this.sqrVal = undefined;
this.argVal = undefined;
this.sqr = function(){
if(this.sqrVal === undefined){
this.sqrVal = this.re*this.re + this.im*this.im;
}
return this.sqrVal;
}
this.abs = function(){
if(this.absVal === undefined){
this.absVal = Math.sqrt(this.sqr());
}
return this.absVal;
}
this.arg = function(){
if(this.argVal === undefined){
this.argVal = Math.atan2(this.im, this.re)
}
return this.argVal;
}
this.clearBuffer = function(){
this.absVal = undefined;
this.sqrVal = undefined;
this.argVal = undefined;
}
this.toString = function(){
return this.re.toString()+(this.im < 0 ? '-':'+')+Math.abs(this.im).toString()+'i'
};
this.set = function(x){
if (typeof x == "number"){
this.re = x;
this.im = 0.0;
} else if(x instanceof Complex) {
this.re = x.re;
this.im = x.im;
}
this.clearBuffer();
return this;
}
this.conj = function(){
this.im = - this.im;
this.clearBuffer();
return this;
}
this.add = function(...args) {
for(let arg of args){
if (typeof arg === "number") {
this.re += arg;
} else if (arg instanceof Complex) {
this.re += arg.re;
this.im += arg.im;
}
}
this.clearBuffer();
return this;
}
this.sub = function(...args) {
for(let arg of args){
if (typeof arg === "number") {
this.re -= arg;
} else if (arg instanceof Complex){
this.re -= arg.re;
this.im -= arg.im;
}
}
this.clearBuffer();
return this;
}
this.mul = function(...args) {
for(let arg of args){
if (typeof arg === "number") {
this.re *= arg;
this.im *= arg;
} else if (arg instanceof Complex){
let a = this.re * arg.re - this.im * arg.im;
let b = this.re * arg.im + this.im * arg.re;
this.re = a;
this.im = b;
}
}
this.clearBuffer();
return this;
}
this.div = function(...args) {
for(let arg of args){
if (typeof arg === "number") {
this.re /= arg;
this.im /= arg;
} else if (arg instanceof Complex){
let a = this.re * arg.re + this.im * arg.im;
let b = this.im * arg.re - this.re * arg.im;
let c = arg.re * arg.re + arg.im * arg.im;
this.re = a / c;
this.im = b / c;
}
}
this.clearBuffer();
return this;
}
this.invert = function(){
this.re = this.re/this.sqr();
this.im = -this.im/this.sqr();
this.clearBuffer();
return this;
}
}
function conjugate(x){
if(x instanceof Complex){
return x.conj();
} else if (x instanceof Array){
let len = x.length;
for(let i = 0; i < len; i++){
conjugate(x[i]);
}
return x;
}
}
function complexDotProduct(a, b){
let dotProduct = new Complex();
let len = Math.min(a.length, b.length);
for(let i = 0; i < len; i++){
dotProduct.add((new Complex()).set(a[i]).conj().mul(b[i]));
}
return dotProduct;
}