mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-25 22:46:20 +08:00
Add polar representation of complex numbers.
This commit is contained in:
parent
096d5a8b5b
commit
e252eb631d
@ -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),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -110,4 +110,31 @@ describe('ComplexNumber', () => {
|
||||
expect(complexNumber3.re).toBe(-7 / 41);
|
||||
expect(complexNumber3.im).toBe(22 / 41);
|
||||
});
|
||||
|
||||
it('should return complex number in polar form', () => {
|
||||
const complexNumber1 = new ComplexNumber({ re: 3, im: 3 });
|
||||
expect(complexNumber1.getPolarForm().radius).toBe(Math.sqrt((3 ** 2) + (3 ** 2)));
|
||||
expect(complexNumber1.getPolarForm().phase).toBe(Math.PI / 4);
|
||||
expect(complexNumber1.getPolarForm(false).phase).toBe(45);
|
||||
|
||||
const complexNumber2 = new ComplexNumber({ re: -3, im: 3 });
|
||||
expect(complexNumber2.getPolarForm().radius).toBe(Math.sqrt((3 ** 2) + (3 ** 2)));
|
||||
expect(complexNumber2.getPolarForm().phase).toBe(3 * (Math.PI / 4));
|
||||
expect(complexNumber2.getPolarForm(false).phase).toBe(135);
|
||||
|
||||
const complexNumber3 = new ComplexNumber({ re: -3, im: -3 });
|
||||
expect(complexNumber3.getPolarForm().radius).toBe(Math.sqrt((3 ** 2) + (3 ** 2)));
|
||||
expect(complexNumber3.getPolarForm().phase).toBe(-3 * (Math.PI / 4));
|
||||
expect(complexNumber3.getPolarForm(false).phase).toBe(-135);
|
||||
|
||||
const complexNumber4 = new ComplexNumber({ re: 3, im: -3 });
|
||||
expect(complexNumber4.getPolarForm().radius).toBe(Math.sqrt((3 ** 2) + (3 ** 2)));
|
||||
expect(complexNumber4.getPolarForm().phase).toBe(-1 * (Math.PI / 4));
|
||||
expect(complexNumber4.getPolarForm(false).phase).toBe(-45);
|
||||
|
||||
const complexNumber5 = new ComplexNumber({ re: 5, im: 7 });
|
||||
expect(complexNumber5.getPolarForm().radius).toBeCloseTo(8.60);
|
||||
expect(complexNumber5.getPolarForm().phase).toBeCloseTo(0.95);
|
||||
expect(complexNumber5.getPolarForm(false).phase).toBeCloseTo(54.46);
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user