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.159 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.158 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.157 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.156 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.155 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.154 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.153 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.152 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.151 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.150 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.149 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.148 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.147 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.146 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.145 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.144 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.143 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.142 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.141 tracks/ecmascript/exercises/complex-numbers/example.js
trackler-2.2.1.140 tracks/ecmascript/exercises/complex-numbers/example.js