Sha256: d0f022cf3c5ca39cb1c524da12eaa103993c5f1818317ae24d56f383a8707cf8
Contents?: true
Size: 1.3 KB
Versions: 49
Compression:
Stored size: 1.3 KB
Contents
export default class ComplexNumber { real: number imag: number constructor(real: number, imag: number) { this.real = real this.imag = imag } add(other: ComplexNumber) { return new ComplexNumber(this.real + other.real, this.imag + other.imag) } sub(other: ComplexNumber) { return new ComplexNumber(this.real - other.real, this.imag - other.imag) } mul(other: ComplexNumber) { return new ComplexNumber( (this.real * other.real) - (this.imag * other.imag), (this.imag * other.real) + (this.real * other.imag)) } div(other: ComplexNumber) { 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
49 entries across 49 versions & 1 rubygems