Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e252eb6

Browse files
committed
Add polar representation of complex numbers.
1 parent 096d5a8 commit e252eb6

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

‎src/algorithms/math/complex-number/ComplexNumber.js‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
import radianToDegree from '../radian/radianToDegree';
2+
13
export default class ComplexNumber {
24
/**
5+
* z = re + im * i
6+
* z = radius * e^(i * phase)
7+
*
38
* @param {number} [re]
49
* @param {number} [im]
510
*/
@@ -70,4 +75,44 @@ export default class ComplexNumber {
7075
im: -1 * complexNumber.im,
7176
});
7277
}
78+
79+
/**
80+
* @return {number}
81+
*/
82+
getRadius() {
83+
return Math.sqrt((this.re ** 2) + (this.im ** 2));
84+
}
85+
86+
/**
87+
* @param {boolean} [inRadians]
88+
* @return {number}
89+
*/
90+
getPhase(inRadians = true) {
91+
let phase = Math.atan(Math.abs(this.im) / Math.abs(this.re));
92+
93+
if (this.re < 0 && this.im > 0) {
94+
phase = Math.PI - phase;
95+
} else if (this.re < 0 && this.im < 0) {
96+
phase = -(Math.PI - phase);
97+
} else if (this.re > 0 && this.im < 0) {
98+
phase = -phase;
99+
}
100+
101+
if (!inRadians) {
102+
phase = radianToDegree(phase);
103+
}
104+
105+
return phase;
106+
}
107+
108+
/**
109+
* @param {boolean} [inRadians]
110+
* @return {{radius: number, phase: number}}
111+
*/
112+
getPolarForm(inRadians = true) {
113+
return {
114+
radius: this.getRadius(),
115+
phase: this.getPhase(inRadians),
116+
};
117+
}
73118
}

‎src/algorithms/math/complex-number/__test__/ComplexNumber.test.js‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,31 @@ describe('ComplexNumber', () => {
110110
expect(complexNumber3.re).toBe(-7 / 41);
111111
expect(complexNumber3.im).toBe(22 / 41);
112112
});
113+
114+
it('should return complex number in polar form', () => {
115+
const complexNumber1 = new ComplexNumber({ re: 3, im: 3 });
116+
expect(complexNumber1.getPolarForm().radius).toBe(Math.sqrt((3 ** 2) + (3 ** 2)));
117+
expect(complexNumber1.getPolarForm().phase).toBe(Math.PI / 4);
118+
expect(complexNumber1.getPolarForm(false).phase).toBe(45);
119+
120+
const complexNumber2 = new ComplexNumber({ re: -3, im: 3 });
121+
expect(complexNumber2.getPolarForm().radius).toBe(Math.sqrt((3 ** 2) + (3 ** 2)));
122+
expect(complexNumber2.getPolarForm().phase).toBe(3 * (Math.PI / 4));
123+
expect(complexNumber2.getPolarForm(false).phase).toBe(135);
124+
125+
const complexNumber3 = new ComplexNumber({ re: -3, im: -3 });
126+
expect(complexNumber3.getPolarForm().radius).toBe(Math.sqrt((3 ** 2) + (3 ** 2)));
127+
expect(complexNumber3.getPolarForm().phase).toBe(-3 * (Math.PI / 4));
128+
expect(complexNumber3.getPolarForm(false).phase).toBe(-135);
129+
130+
const complexNumber4 = new ComplexNumber({ re: 3, im: -3 });
131+
expect(complexNumber4.getPolarForm().radius).toBe(Math.sqrt((3 ** 2) + (3 ** 2)));
132+
expect(complexNumber4.getPolarForm().phase).toBe(-1 * (Math.PI / 4));
133+
expect(complexNumber4.getPolarForm(false).phase).toBe(-45);
134+
135+
const complexNumber5 = new ComplexNumber({ re: 5, im: 7 });
136+
expect(complexNumber5.getPolarForm().radius).toBeCloseTo(8.60);
137+
expect(complexNumber5.getPolarForm().phase).toBeCloseTo(0.95);
138+
expect(complexNumber5.getPolarForm(false).phase).toBeCloseTo(54.46);
139+
});
113140
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /