Avatar of mstange22

mstange22's solution

to Complex Numbers in the TypeScript Track

Published at Oct 02 2019 · 0 comments
Instructions
Test suite
Solution

A complex number is a number in the form a + b * i where a and b are real and i satisfies i^2 = -1.

a is called the real part and b is called the imaginary part of z. The conjugate of the number a + b * i is the number a - b * i. The absolute value of a complex number z = a + b * i is a real number |z| = sqrt(a^2 + b^2). The square of the absolute value |z|^2 is the result of multiplication of z by its complex conjugate.

The sum/difference of two complex numbers involves adding/subtracting their real and imaginary parts separately: (a + i * b) + (c + i * d) = (a + c) + (b + d) * i, (a + i * b) - (c + i * d) = (a - c) + (b - d) * i.

Multiplication result is by definition (a + i * b) * (c + i * d) = (a * c - b * d) + (b * c + a * d) * i.

The reciprocal of a non-zero complex number is 1 / (a + i * b) = a/(a^2 + b^2) - b/(a^2 + b^2) * i.

Dividing a complex number a + i * b by another c + i * d gives: (a + i * b) / (c + i * d) = (a * c + b * d)/(c^2 + d^2) + (b * c - a * d)/(c^2 + d^2) * i.

Raising e to a complex exponent can be expressed as e^(a + i * b) = e^a * e^(i * b), the last term of which is given by Euler's formula e^(i * b) = cos(b) + i * sin(b).

Implement the following operations:

  • addition, subtraction, multiplication and division of two complex numbers,
  • conjugate, absolute value, exponent of a given complex number.

Assume the programming language you are using does not have an implementation of complex numbers.

Setup

Go through the setup instructions for TypeScript to install the necessary dependencies:

https://exercism.io/tracks/typescript/installation

Requirements

Install assignment dependencies:

$ yarn install

Making the test suite pass

Execute the tests with:

$ yarn test

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by changing xit to it.

Source

Wikipedia https://en.wikipedia.org/wiki/Complex_number

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

complex-numbers.test.ts

import ComplexNumber from "./complex-numbers"

describe("Complex numbers", () => {
  it("Real part of a purely real number", () => {
    const expected = 1
    const actual = new ComplexNumber(1, 0).real

    expect(actual).toEqual(expected)
  })

  xit("Real part of a purely imaginary number", () => {
    const expected = 0
    const actual = new ComplexNumber(0, 1).real

    expect(actual).toEqual(expected)
  })

  xit("Real part of a number with real and imaginary part", () => {
    const expected = 1
    const actual = new ComplexNumber(1, 2).real

    expect(actual).toEqual(expected)
  })

  xit("Imaginary part of a purely real number", () => {
    const expected = 0
    const actual = new ComplexNumber(1, 0).imag

    expect(actual).toEqual(expected)
  })

  xit("Imaginary part of a purely imaginary number", () => {
    const expected = 1
    const actual = new ComplexNumber(0, 1).imag

    expect(actual).toEqual(expected)
  })

  xit("Imaginary part of a number with real and imaginary part", () => {
    const expected = 2
    const actual = new ComplexNumber(1, 2).imag

    expect(actual).toEqual(expected)
  })

  xit("Add purely real numbers", () => {
    const expected = new ComplexNumber(3, 0)
    const actual = new ComplexNumber(1, 0).add(new ComplexNumber(2, 0))

    expect(actual).toEqual(expected)
  })

  xit("Add purely imaginary numbers", () => {
    const expected = new ComplexNumber(0, 3)
    const actual = new ComplexNumber(0, 1).add(new ComplexNumber(0, 2))

    expect(actual).toEqual(expected)
  })

  xit("Add numbers with real and imaginary part", () => {
    const expected = new ComplexNumber(4, 6)
    const actual = new ComplexNumber(1, 2).add(new ComplexNumber(3, 4))

    expect(actual).toEqual(expected)
  })

  xit("Subtract purely real numbers", () => {
    const expected = new ComplexNumber(-1, 0)
    const actual = new ComplexNumber(1, 0).sub(new ComplexNumber(2, 0))

    expect(actual).toEqual(expected)
  })

  xit("Subtract purely imaginary numbers", () => {
    const expected = new ComplexNumber(0, -1)
    const actual = new ComplexNumber(0, 1).sub(new ComplexNumber(0, 2))

    expect(actual).toEqual(expected)
  })

  xit("Subtract numbers with real and imaginary part", () => {
    const expected = new ComplexNumber(-2, -2)
    const actual = new ComplexNumber(1, 2).sub(new ComplexNumber(3, 4))

    expect(actual).toEqual(expected)
  })

  xit("Multiply purely real numbers", () => {
    const expected = new ComplexNumber(2, 0)
    const actual = new ComplexNumber(1, 0).mul(new ComplexNumber(2, 0))

    expect(actual).toEqual(expected)
  })

  xit("Multiply imaginary unit", () => {
    const expected = new ComplexNumber(-1, 0)
    const actual = new ComplexNumber(0, 1).mul(new ComplexNumber(0, 1))

    expect(actual).toEqual(expected)
  })

  xit("Multiply purely imaginary numbers", () => {
    const expected = new ComplexNumber(-2, 0)
    const actual = new ComplexNumber(0, 1).mul(new ComplexNumber(0, 2))

    expect(actual).toEqual(expected)
  })

  xit("Multiply numbers with real and imaginary part", () => {
    const expected = new ComplexNumber(-5, 10)
    const actual = new ComplexNumber(1, 2).mul(new ComplexNumber(3, 4))

    expect(actual).toEqual(expected)
  })

  xit("Divide purely real numbers", () => {
    const expected = new ComplexNumber(0.5, 0)
    const actual = new ComplexNumber(1, 0).div(new ComplexNumber(2, 0))

    expect(actual).toEqual(expected)
  })

  xit("Divide purely imaginary numbers", () => {
    const expected = new ComplexNumber(0.5, 0)
    const actual = new ComplexNumber(0, 1).div(new ComplexNumber(0, 2))

    expect(actual).toEqual(expected)
  })

  xit("Divide numbers with real and imaginary part", () => {
    const expected = new ComplexNumber(0.44, 0.08)
    const actual = new ComplexNumber(1, 2).div(new ComplexNumber(3, 4))

    expect(actual).toEqual(expected)
  })

  xit("Absolute value of a positive purely real number", () => {
    const expected = 5
    const actual = new ComplexNumber(5, 0).abs

    expect(actual).toEqual(expected)
  })

  xit("Absolute value of a negative purely real number", () => {
    const expected = 5
    const actual = new ComplexNumber(-5, 0).abs

    expect(actual).toEqual(expected)
  })

  xit("Absolute value of a purely imaginary number with positive imaginary part", () => {
    const expected = 5
    const actual = new ComplexNumber(0, 5).abs

    expect(actual).toEqual(expected)
  })

  xit("Absolute value of a purely imaginary number with negative imaginary part", () => {
    const expected = 5
    const actual = new ComplexNumber(0, -5).abs

    expect(actual).toEqual(expected)
  })

  xit("Absolute value of a number with real and imaginary part", () => {
    const expected = 5
    const actual = new ComplexNumber(3, 4).abs

    expect(actual).toEqual(expected)
  })

  xit("Conjugate a purely real number", () => {
    const expected = new ComplexNumber(5, 0)
    const actual = new ComplexNumber(5, 0).conj

    expect(actual).toEqual(expected)
  })

  xit("Conjugate a purely imaginary number", () => {
    const expected = new ComplexNumber(0, -5)
    const actual = new ComplexNumber(0, 5).conj

    expect(actual).toEqual(expected)
  })

  xit("Conjugate a number with real and imaginary part", () => {
    const expected = new ComplexNumber(1, -1)
    const actual = new ComplexNumber(1, 1).conj

    expect(actual).toEqual(expected)
  })

  xit("Euler's identity/formula", () => {
    const expected = new ComplexNumber(-1, 0)
    const actual = new ComplexNumber(0, Math.PI).exp

    expect(actual.real).toBeCloseTo(expected.real)
    expect(actual.imag).toBeCloseTo(expected.imag)
  })

  xit("Exponential of 0", () => {
    const expected = new ComplexNumber(1, 0)
    const actual = new ComplexNumber(0, 0).exp

    expect(actual.real).toBeCloseTo(expected.real)
    expect(actual.imag).toBeCloseTo(expected.imag)
  })

  xit("Exponential of a purely real number", () => {
    const expected = new ComplexNumber(Math.E, 0)
    const actual = new ComplexNumber(1, 0).exp

    expect(actual.real).toBeCloseTo(expected.real)
    expect(actual.imag).toBeCloseTo(expected.imag)
  })
})
class ComplexNumber {
	real: number
	imag: number
	abs: number
	constructor(real: number, imag: number) {
		this.real = real
		this.imag = imag
		this.abs = Math.sqrt(real * real + imag * imag)
	}

	get conj (): ComplexNumber {
		return new ComplexNumber(this.real, 0 - this.imag)
	}

	get exp (): ComplexNumber {
		return new ComplexNumber(
			Math.E ** this.real * Math.cos(this.imag),
			Math.E ** this.real * Math.sin(this.imag)
		)
	}

	add(other: ComplexNumber): ComplexNumber {
		return new ComplexNumber(this.real + other.real, this.imag + other.imag)
	}

	sub(other: ComplexNumber): ComplexNumber {
		return new ComplexNumber(this.real - other.real, this.imag - other.imag)
	}

	mul(other: ComplexNumber): ComplexNumber {
		return new ComplexNumber(
			this.real * other.real - this.imag * other.imag,
			this.imag * other.real + this.real * other.imag
		)
	}

	div(other: ComplexNumber): ComplexNumber {
		return new ComplexNumber(
			(this.real * other.real + this.imag * other.imag) / (other.real ** 2 + other.imag ** 2),
			(this.imag * other.real - this.real * other.imag) / (other.real ** 2 + other.imag ** 2)
		)
	}
}

export default ComplexNumber

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?