import { Complex } from "@masabando/quantum-gates"The Complex class represents complex numbers and provides methods for complex number operations.
new Complex(realPart: number, imaginaryPart: number) : ComplexCreates a new complex number with the specified real (realPart) and imaginary (imaginaryPart) parts.
add(other: Complex) : ComplexAdds this complex number to another complex number (other) and returns the result as a new Complex instance.
const a = new Complex(1, 2); // 1 + 2iconst b = new Complex(3, 4); // 3 + 4iconst result = a.add(b); // result is 4 + 6imultiply(other: Complex) : ComplexMultiplies this complex number by another complex number (other) and returns the result as a new Complex instance.
const a = new Complex(1, 2); // 1 + 2iconst b = new Complex(3, 4); // 3 + 4iconst result = a.multiply(b); // result is -5 + 10iscale(scalar: number) : ComplexScales this complex number by a real number (scalar) and returns the result as a new Complex instance.
const a = new Complex(1, 2); // 1 + 2iconst result = a.scale(3); // result is 3 + 6itoString(digits?: number) : stringReturns a string representation of the complex number. If digits is provided, the real and imaginary parts are formatted to the specified number of decimal places.
const a = new Complex(1.23456, 2.34567);console.log(a.toString()); // "1.23456 + 2.34567i"console.log(a.toString(2)); // "1.23 + 2.35i"magnitude() : numberReturns the magnitude (absolute value) of the complex number.
const a = new Complex(3, 4);console.log(a.magnitude()); // 5conjugate() : ComplexReturns the complex conjugate of this complex number as a new Complex instance.
const a = new Complex(1, 2); // 1 + 2iconst result = a.conjugate(); // result is 1 - 2iThe methods of the Complex class can be chained together to perform multiple operations in a single expression.
const a = new Complex(1, 2); // 1 + 2iconst b = new Complex(3, 4); // 3 + 4iconst result = a.add(b).scale(2).conjugate().toString(); // result is "-8 - 12i"/* You can also break it down for clarity */const anotherResult = a.add(b) .scale(2) .conjugate() .toString();