Complex

JSvalueobjectclassexample ⟩ Complex

a class to represent complex numbers.

class Complex {

    // ⭐️ static constant
    static get zero() { return new Complex(0) }  // 0
    static get one() { return new Complex(1) }   // 1
    static get i() { return new Complex(0, 1) }  // i

    // constructor
    constructor(a = 0, b = 0) {
        this.x = a;
        this.y = b;
    }

    // -z
    negate() {
        return new Complex(-this.x, -this.y);
    }

    // conjugate (x - yi)
    conjugate() {
        return new Complex(this.x, -this.y);
    }

    // z1 + z2
    plus({ x, y }) {
        return new Complex(x + this.x, y + this.y);
    }

    // z1 - z2
    minus({ x, y }) {
        return new Complex(this.x - x, this.y - y);
    }

    // z1 * z2
    times({ x: c, y: d }) {
        const { x: a, y: b } = this;
        return new Complex(a * c - b * d, a * d + b * c);
    }

    // |z|^2
    get magnitudeSquare() {
        return this.times(this.conjugate()).x;    // real number
    }

    // 1/z
    inverse() {
        // 1/z = z' / |z|^2 (where z' is conjugate of z)
        return this.conjugate().times(new Complex(1/this.magnitudeSquare));
    }

    // z1 / z2
    dividedBy(z2) {
        return this.times(z2.inverse());
    }

    // z^n (where n is an integer)
    toPower(n) {

        let result = Complex.one;
        if (n === 0) return result;

        if (n > 0) {
            while (n > 0) {
                result = result.times(this);
                n -= 1;
            }
            return result;
        }

        if (n < 0) {
            while (n < 0) {
                result = result.dividedBy(this);
                n += 1;
            }
            return result;
        }

    }

    // complex -> string
    toString() {

        const { x, y } = this;

        return (
            y === 0 ? `${x}`           :        // x
            x === 0 ? `${hideOne(y)}i` :        // yi
            `${x} ${y < 0 ? '-' : '+'} ${hideOne(Math.abs(y))}i`  // x + yi
        );

        function hideOne(y) {
            return (
                y ===  1 ? ``  :
                y === -1 ? `-` : `${y}`
            )
        }
    }
}

module.exports = Complex;

Last updated