| 1 | export class complex |
| 2 | { |
| 3 | /** The real component of the complex number */ |
| 4 | private _real: number; |
| 5 | /** The imaginary component of the complex number */ |
| 6 | private _img: number; |
| 7 | |
| 8 | /** |
| 9 | * Construct a new complex number from two real numbers |
| 10 | * @param real - The real component |
| 11 | * @param imaginary - The imaginary component |
| 12 | * @returns Complex number constructed from given parameters |
| 13 | */ |
| 14 | constructor(real: number, imaginary: number) |
| 15 | { |
| 16 | this._real = real; |
| 17 | this._img = imaginary; |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * Get the real component of the complex number |
| 22 | * @returns The real component - this._real |
| 23 | */ |
| 24 | get real(): number |
| 25 | { |
| 26 | return this._real; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Get the imaginary component of the complex number |
| 31 | * @returns The imaginary component - this._imaginary |
| 32 | */ |
| 33 | get img(): number |
| 34 | { |
| 35 | return this._img; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Add two complex numbers |
| 40 | * @param other - The 2nd complex number operand |
| 41 | * @returns x + other |
| 42 | */ |
| 43 | public add(other: complex): complex |
| 44 | { |
| 45 | return new complex (this._real + other.real, this._img + other.img); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Subtract two complex numbers |
| 50 | * @param other - The 2nd complex number operand |
| 51 | * @returns x - other |
| 52 | */ |
| 53 | public sub(other: complex): complex |
| 54 | { |
| 55 | return new complex (this._real - other.real, this._img - other.img); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Multiply two complex numbers |
| 60 | * @param other - The 2nd complex number operand |