Sha256: 2a1ce3b87c8a62308bc283b3c26388d08ed0c0ca6096e63492a63e78c1a0b6f5

Contents?: true

Size: 1.09 KB

Versions: 121

Compression:

Stored size: 1.09 KB

Contents

export default class ComplexNumber {

  constructor(real, imag) {
    this.real = real;
    this.imag = imag;
  }

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

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

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

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

  get abs() {
    return Math.sqrt((this.real * this.real) + (this.imag * this.imag));
  }

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

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

Version data entries

121 entries across 121 versions & 1 rubygems

Version Path
trackler-2.2.1.139 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.138 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.137 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.136 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.135 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.134 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.133 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.132 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.131 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.130 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.129 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.128 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.127 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.126 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.125 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.124 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.123 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.122 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.121 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.120 tracks/ecmascript/exercises/complex-numbers/example.js