Complex Numbers
//
// This is only a SKELETON file for the 'Complex Numbers' exercise. It's been provided as a
// convenience to get you started writing code faster.
//
export class ComplexNumber {
constructor(real, imag) {
this.real = real;
this.imag = imag;
}
add(n) {
this.real += n.real;
this.imag += n.imag;
return this;
}
sub(n) {
this.real -= n.real;
this.imag -= n.imag;
return this;
}
div(n) {
const real = (this.realn.real+this.imagn.imag)/(n.realn.real+n.imagn.imag);
this.imag = (this.imagn.real- this.realn.imag)/(n.realn.real+n.imagn.imag);
this.real = real;
return this;
}
mul(n) {
const real = this.realn.real-this.imagn.imag;
this.imag = this.imagn.real+this.realn.imag;
this.real = real;
return this;
}
get abs() {
return Math.sqrt(this.imagthis.imag+this.realthis.real);
}
get conj() {
return new ComplexNumber(this.real, this.imag ? -this.imag : 0)
}
get exp() {
const real = Math.exp(this.real);
return new ComplexNumber(realMath.cos(this.imag), realMath.sin(this.imag));
}
}