Complex = {
let complex = class Complex {
constructor(a, b) {
if (b) {
this.re = a;
this.im = b;
} else if (a) {
this.re = a;
this.im = 0;
} else {
this.re = 0;
this.im = 0;
}
}
abs2() {
return this.re ** 2 + this.im ** 2;
}
abs() {
return Math.sqrt(this.abs2());
}
squared() {
return new Complex(this.re ** 2 - this.im ** 2, 2 * this.re * this.im);
}
plus(z) {
if (z instanceof Complex) {
return new Complex(this.re + z.re, this.im + z.im);
} else if (typeof z == "number") {
return new Complex(this.re + z, this.im);
} else {
return NaN;
}
}
minus(z) {
if (z instanceof Complex) {
return new Complex(this.re - z.re, this.im - z.im);
} else if (typeof z == "number") {
return new Complex(this.re - z, this.im);
} else {
return NaN;
}
}
times(z) {
if (z instanceof Complex) {
return new Complex(
this.re * z.re - this.im * z.im,
this.re * z.im + this.im * z.re
);
} else if (typeof z == "number") {
return new Complex(z * this.re, z * this.im);
} else {
return NaN;
}
}
almost_equal(z, tolerance = 10 ** -20) {
return this.minus(z).abs2() < tolerance;
}
indexOf(A, tolerance = 10 ** -10) {
let i = A.length - 1;
while (i >= 0) {
let z = A[i];
if (this.almost_equal(z, tolerance)) {
break;
}
i--;
}
if (i == 0) {
i = -1;
}
return i;
}
};
return complex;
}