Working with complex numbers

TypeScript:
Working with complex numbers

How to:

Handling complex numbers in TypeScript needs a dedicated class. Let’s create one and work through addition and multiplication.

class Complex {
    constructor(public re: number, public im: number) {}

    add(other: Complex): Complex {
        return new Complex(this.re + other.re, this.im + other.im);
    }

    multiply(other: Complex): Complex {
        return new Complex(
            this.re * other.re - this.im * other.im,
            this.re * other.im + this.im * other.re
        );
    }

    toString(): string {
        return `${this.re} + ${this.im}i`;
    }
}

let num1 = new Complex(1, 2);
let num2 = new Complex(3, 4);
let sum = num1.add(num2);
let product = num1.multiply(num2);

console.log(`Sum: ${sum.toString()}`); // Output: Sum: 4 + 6i
console.log(`Product: ${product.toString()}`); // Output: Product: -5 + 10i

Deep Dive

Historically, complex numbers were controversial - even coined as ‘imaginary’ to express initial skepticism. Now, they’re foundational in modern mathematics and science.

Alternatives to our simple class might involve using existing libraries such as math.js or complex.js, detailed with additional features like trigonometric functions, exponentiation, and complex conjugation.

Our TypeScript implementation details boil down to defining arithmetic operations. The add method simply adds corresponding parts. multiply applies the FOIL method used in algebra, remembering that i^2 = -1.

See Also

For further reading and resources on complex numbers and their use in programming, check out: