-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomplex.js
269 lines (231 loc) · 7.41 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/**
* @file Contains the Complex class.
*
* @copyright Oscar Litorell 2019
*/
/**
* Represents a complex number.
*/
class Complex {
/**
* @param {number} [re] - The real component of the complex number.
* @param {number} [im] - The imaginary component of the complex number.
*/
constructor(re=0, im=0) {
this.re = Number(re);
this.im = Number(im);
}
/**
* Formats a complex number nicely. Rounds the number to the given length.
* @param {number} [decimals] - Number of decimals to use.
*/
print(decimals=null) {
let re;
let im;
if (decimals !== null) {
let multiplier = Math.pow(10, decimals);
re = Math.round(this.re * multiplier) / multiplier;
im = Math.round(this.im * multiplier) / multiplier;
} else {
re = this.re;
im = this.im;
}
return `${re} ${(im >= 0) ? "+" : "-"} ${Math.abs(im)}i`;
}
/**
* Create a complex number from polar coordinates.
* @param {number} r - Distance from 0 (absolute value).
* @param {number} theta - Angle from the number to 0 + 0i to 1 + 0i.
* @returns {Complex}
*/
static fromPolar(r, theta) {
let re = r * Math.cos(theta);
let im = r * Math.sin(theta);
return new Complex(re, im);
}
/**
* Add two numbers.
* @param {number|Complex} num1
* @param {number|Complex} num2
* @returns {Complex}
*/
static add(num1, num2) {
if (num1.constructor !== Complex) num1 = new Complex(num1);
if (num2.constructor !== Complex) num2 = new Complex(num2);
let out = new Complex();
out.re = num1.re + num2.re;
out.im = num1.im + num2.im;
return out;
}
/**
* Subtract one number from another.
* @param {number|Complex} num1
* @param {number|Complex} num2
* @returns {Complex}
*/
static subtract(num1, num2) {
if (num1.constructor !== Complex) num1 = new Complex(num1);
if (num2.constructor !== Complex) num2 = new Complex(num2);
let out = new Complex();
out.re = num1.re - num2.re;
out.im = num1.im - num2.im;
return out;
}
/**
* Multiply two numbers.
* @param {number|Complex} num1
* @param {number|Complex} num2
* @returns {Complex}
*/
static multiply(in1, in2) {
if (in1.constructor !== Complex) in1 = new Complex(in1);
if (in2.constructor !== Complex) in2 = new Complex(in2);
let num1 = in1.re * in2.re;
let num2 = in1.im * in2.re;
let num3 = in1.re * in2.im;
let num4 = in1.im * in2.im;
let out = new Complex();
out.re = num1 - num4;
out.im = num2 + num3;
return out;
}
/**
* Divide one number with another.
* @param {number|Complex} num1
* @param {number|Complex} num2
* @returns {Complex}
*/
static divide(num1, num2) {
if (num1.constructor !== Complex) num1 = new Complex(num1);
if (num2.constructor !== Complex) num2 = new Complex(num2);
return Complex.multiply(num1, Complex.invert(num2));
}
/**
* The mulitplicative inverse for a number.
* @param {number|Complex} num
* @returns {Complex}
*/
static invert(num) {
if (num.constructor !== Complex) num = new Complex(num);
let a = num.re;
let b = num.im;
let absSquared = 1 / (Math.pow(a, 2) + Math.pow(b, 2));
return new Complex(a * absSquared, -b * absSquared)
}
/**
* Natural logarithm.
* @param {number|Complex} num1
* @returns {Complex}
*/
static ln(num1) {
if (num1.constructor !== Complex) num1 = new Complex(num1);
let polar = Complex.toPolar(num1);
return new Complex(Math.log(polar.r), polar.theta);
}
/**
* Sine.
* @param {number|Complex} num1
* @returns {Complex}
*/
static sin(num1) {
if (num1.constructor !== Complex) num1 = new Complex(num1);
let e = new Complex(Math.E);
let c = Complex;
// (e^(-ix) - e^(ix)) * 0.5i
return c.multiply(c.subtract(c.raise(e, c.multiply(new c(0, -1), num1)), c.raise(e, c.multiply(new c(0, 1), num1))), new c(0, 0.5));
}
/**
* Cosine.
* @param {number|Complex} num1
* @returns {Complex}
*/
static cos(num1) {
if (num1.constructor !== Complex) num1 = new Complex(num1);
let e = new Complex(Math.E);
let c = Complex;
// (e^(ix) + e^(-ix)) * 0.5
return c.multiply(c.add(c.raise(e, c.multiply(new c(0, 1), num1)), c.raise(e, c.multiply(new c(0, -1), num1))), 0.5);
}
/**
* Tangent.
* @param {number|Complex} num1
* @returns {Complex}
*/
static tan(num1) {
if (num1.constructor !== Complex) num1 = new Complex(num1);
return Complex.divide(Complex.sin(num1), Complex.cos(num1));
}
/**
* Inverse sine / arcsin.
* @param {number|Complex} num1
* @returns {Complex}
*/
static asin(num1) {
if (num1.constructor !== Complex) num1 = new Complex(num1);
let c = Complex;
// -i * ln(ix + sqrt(1 - x^2))
return c.multiply(new c(0, -1), c.ln(c.add(c.multiply(num1, new c(0, 1)), c.raise(c.subtract(1, c.raise(num1, 2)), 0.5))));
}
/**
* Inverse cosine / arccos.
* @param {number|Complex} num1
* @returns {Complex}
*/
static acos(num1) {
if (num1.constructor !== Complex) num1 = new Complex(num1);
return Complex.subtract(new Complex(Math.PI / 2), Complex.asin(num1));
}
/**
* Inverse tangent / arctan.
* @param {number|Complex} num1
* @returns {Complex}
*/
static atan(num1) {
if (num1.constructor !== Complex) num1 = new Complex(num1);
let c = Complex;
let iz = c.multiply(new Complex(0, 1), num1);
// i/2*ln((1-i*z)/(1+i*z))
return c.multiply(new c(0, 0.5), c.ln(c.divide(c.subtract(1, iz), c.add(1, iz))));
}
/**
* Raise one number to another.
* @param {number|Complex} num1
* @param {number|Complex} num2
* @returns {Complex}
*/
static raise(num1, num2) {
if (num1.constructor !== Complex) num1 = new Complex(num1);
if (num2.constructor !== Complex) num2 = new Complex(num2);
if (Complex.abs(num1).re === 0 && Complex.abs(num2).re !== 0) {
return new Complex(0);
}
let num1Polar = Complex.toPolar(num1);
// Absolute value and argument of base
let absB = num1Polar.r;
let argB = num1Polar.theta;
let out = Complex.fromPolar(Math.exp(num2.re * Math.log(absB) - num2.im * argB), num2.im * Math.log(absB) + num2.re * argB);
return out;
}
/**
* The absolute value of a complex number.
* @param {Complex} num1
* @returns {Complex}
*/
static abs(num) {
if (num.constructor !== Complex) return new Complex(Math.abs(num));
return new Complex(Math.pow(Math.pow(num.re, 2) + Math.pow(num.im, 2), 0.5));
}
/**
* To Polar coordinates (radius and angle).
* @param {Complex} num
* @returns {Object} {"r": r, "theta": theta}
*/
static toPolar(num) {
let r = Complex.abs(num).re;
let theta = Math.atan2(num.im, num.re);
return {
r: r,
theta: theta
};
}
}