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.180 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.179 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.178 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.177 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.176 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.175 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.174 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.173 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.172 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.171 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.170 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.169 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.167 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.166 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.165 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.164 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.163 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.162 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.161 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.160 tracks/ecmascript/exercises/complex-numbers/example.js