mirror of
https://github.com/trekhleb/javascript-algorithms.git
synced 2026-03-13 08:51:02 +08:00
Add polar representation of complex numbers.
This commit is contained in:
@@ -1,5 +1,10 @@
|
||||
import radianToDegree from '../radian/radianToDegree';
|
||||
|
||||
export default class ComplexNumber {
|
||||
/**
|
||||
* z = re + im * i
|
||||
* z = radius * e^(i * phase)
|
||||
*
|
||||
* @param {number} [re]
|
||||
* @param {number} [im]
|
||||
*/
|
||||
@@ -70,4 +75,44 @@ export default class ComplexNumber {
|
||||
im: -1 * complexNumber.im,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {number}
|
||||
*/
|
||||
getRadius() {
|
||||
return Math.sqrt((this.re ** 2) + (this.im ** 2));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {boolean} [inRadians]
|
||||
* @return {number}
|
||||
*/
|
||||
getPhase(inRadians = true) {
|
||||
let phase = Math.atan(Math.abs(this.im) / Math.abs(this.re));
|
||||
|
||||
if (this.re < 0 && this.im > 0) {
|
||||
phase = Math.PI - phase;
|
||||
} else if (this.re < 0 && this.im < 0) {
|
||||
phase = -(Math.PI - phase);
|
||||
} else if (this.re > 0 && this.im < 0) {
|
||||
phase = -phase;
|
||||
}
|
||||
|
||||
if (!inRadians) {
|
||||
phase = radianToDegree(phase);
|
||||
}
|
||||
|
||||
return phase;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {boolean} [inRadians]
|
||||
* @return {{radius: number, phase: number}}
|
||||
*/
|
||||
getPolarForm(inRadians = true) {
|
||||
return {
|
||||
radius: this.getRadius(),
|
||||
phase: this.getPhase(inRadians),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user