class Complex16Array extends Float64Array {
constructor(...args) {
super(...args);
if (this.length % 2 !== 0) {
throw new Error('you supplied an uneven amount of elements');
}
}
get(i) {
const pos = i * 2;
this.checkBounds(pos);
return new Complex16(this.subarray(pos, pos + 2));
}
set(i, z) {
const pos = i * 2;
this.checkBounds(pos);
this[pos] = z.re;
this[pos + 1] = z.im;
}
checkBounds(pos) {
if (pos < 0 || pos > this.length - 2) {
throw new Error('index out of bounds');
}
}
get size() {
return this.length / 2;
}
toString() {
const res = [];
for (let i = 0; i < this.size; i++) {
res.push(this.get(i));
}
return `(${res.join(', ')})`;
}
}