From e252eb631db97ed5c4ca8d082072aaf77c0c7657 Mon Sep 17 00:00:00 2001 From: Oleksii Trekhleb Date: Tue, 14 Aug 2018 22:56:13 +0300 Subject: [PATCH] Add polar representation of complex numbers. --- .../math/complex-number/ComplexNumber.js | 45 +++++++++++++++++++ .../__test__/ComplexNumber.test.js | 27 +++++++++++ 2 files changed, 72 insertions(+) diff --git a/src/algorithms/math/complex-number/ComplexNumber.js b/src/algorithms/math/complex-number/ComplexNumber.js index 5f33e071..801c0c37 100644 --- a/src/algorithms/math/complex-number/ComplexNumber.js +++ b/src/algorithms/math/complex-number/ComplexNumber.js @@ -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), + }; + } } diff --git a/src/algorithms/math/complex-number/__test__/ComplexNumber.test.js b/src/algorithms/math/complex-number/__test__/ComplexNumber.test.js index a25b93e0..827d2308 100644 --- a/src/algorithms/math/complex-number/__test__/ComplexNumber.test.js +++ b/src/algorithms/math/complex-number/__test__/ComplexNumber.test.js @@ -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); + }); });