Complex number

avatar

export class ComplexNumber {

r : number;

i : number;

constructor(real: number, imaginary: number) {

this.r = real;

this.i = imaginary;

}

public get real(): number {

return this.r;

}

public get imag(): number {

return this.i;

}

public add(other: ComplexNumber): ComplexNumber {

return new ComplexNumber((this.r+other.r),(this.i+other.i));

}

public sub(other: ComplexNumber): ComplexNumber {

return new ComplexNumber((this.r-other.r),(this.i-other.i));

}

public mul(other: ComplexNumber): ComplexNumber {

return new ComplexNumber((this.r\*other.r-this.i\*other.i),(this.i\*other.r+this.r\*other.i));

}

public div(other: ComplexNumber): ComplexNumber {

 return new ComplexNumber((this.r\*other.r+this.i\*other.i)/(other.r\*other.r+other.i\*other.i),(this.i\*other.r-this.r\*other.i)/(other.r\*other.r+other.i\*other.i));

}

public get abs(): number {

   return Math.sqrt(this.i\*this.i+this.r\*this.r);

}

public get conj(): ComplexNumber {

 return new ComplexNumber(this.real, this.imag ? this.imag \* (-1) : 0);

}

public get exp(): ComplexNumber {

return new ComplexNumber(Math.exp(this.r)\*Math.cos(this.i), Math.exp(this.r)\*Math.sin(this.i));

}

}



0
0
0.000
1 comments
avatar

Thank you for your witness vote!
Have a !BEER on me!
To Opt-Out of my witness beer program just comment STOP below

0
0
0.000