-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathcomplex-array.js
72 lines (59 loc) · 1.63 KB
/
complex-array.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
class ComplexArray {
constructor(other, arrayType = Float32Array) {
if (other instanceof ComplexArray) {
// Copy constuctor.
this.ArrayType = other.ArrayType;
this.real = new this.ArrayType(other.real);
this.imag = new this.ArrayType(other.imag);
} else {
this.ArrayType = arrayType;
// other can be either an array or a number.
this.real = new this.ArrayType(other);
this.imag = new this.ArrayType(this.real.length);
}
this.length = this.real.length;
}
toString() {
const components = [];
this.forEach((value, i) => {
components.push(
`(${value.real.toFixed(2)}, ${value.imag.toFixed(2)})`
);
});
return `[${components.join(', ')}]`;
}
forEach(iterator) {
const n = this.length;
// For gc efficiency, re-use a single object in the iterator.
const value = Object.seal(Object.defineProperties({}, {
real: {writable: true}, imag: {writable: true},
}));
for (let i = 0; i < n; i++) {
value.real = this.real[i];
value.imag = this.imag[i];
iterator(value, i, n);
}
}
// In-place mapper.
map(mapper) {
this.forEach((value, i, n) => {
mapper(value, i, n);
this.real[i] = value.real;
this.imag[i] = value.imag;
});
return this;
}
conjugate() {
return new ComplexArray(this).map((value) => {
value.imag *= -1;
});
}
magnitude() {
const mags = new this.ArrayType(this.length);
this.forEach((value, i) => {
mags[i] = Math.sqrt(value.real*value.real + value.imag*value.imag);
})
return mags;
}
}
module.exports = ComplexArray;