| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,51 +14,63 @@ export default class ComplexNumber { | |||
| 14 | 14 | } | |
| 15 | 15 | ||
| 16 | 16 | /** | |
| 17 | - * @param {ComplexNumber} addend | ||
| 17 | + * @param {ComplexNumber|number} addend | ||
| 18 | 18 | * @return {ComplexNumber} | |
| 19 | 19 | */ | |
| 20 | 20 | add(addend) { | |
| 21 | + // Make sure we're dealing with complex number. | ||
| 22 | + const complexAddend = this.toComplexNumber(addend); | ||
| 23 | + | ||
| 21 | 24 | return new ComplexNumber({ | |
| 22 | - re: this.re + addend.re, | ||
| 23 | - im: this.im + addend.im, | ||
| 25 | + re: this.re + complexAddend.re, | ||
| 26 | + im: this.im + complexAddend.im, | ||
| 24 | 27 | }); | |
| 25 | 28 | } | |
| 26 | 29 | ||
| 27 | 30 | /** | |
| 28 | - * @param {ComplexNumber} subtrahend | ||
| 31 | + * @param {ComplexNumber|number} subtrahend | ||
| 29 | 32 | * @return {ComplexNumber} | |
| 30 | 33 | */ | |
| 31 | 34 | subtract(subtrahend) { | |
| 35 | + // Make sure we're dealing with complex number. | ||
| 36 | + const complexSubtrahend = this.toComplexNumber(subtrahend); | ||
| 37 | + | ||
| 32 | 38 | return new ComplexNumber({ | |
| 33 | - re: this.re - subtrahend.re, | ||
| 34 | - im: this.im - subtrahend.im, | ||
| 39 | + re: this.re - complexSubtrahend.re, | ||
| 40 | + im: this.im - complexSubtrahend.im, | ||
| 35 | 41 | }); | |
| 36 | 42 | } | |
| 37 | 43 | ||
| 38 | 44 | /** | |
| 39 | - * @param {ComplexNumber} multiplicand | ||
| 45 | + * @param {ComplexNumber|number} multiplicand | ||
| 40 | 46 | * @return {ComplexNumber} | |
| 41 | 47 | */ | |
| 42 | 48 | multiply(multiplicand) { | |
| 49 | + // Make sure we're dealing with complex number. | ||
| 50 | + const complexMultiplicand = this.toComplexNumber(multiplicand); | ||
| 51 | + | ||
| 43 | 52 | return new ComplexNumber({ | |
| 44 | - re: this.re * multiplicand.re - this.im * multiplicand.im, | ||
| 45 | - im: this.re * multiplicand.im + this.im * multiplicand.re, | ||
| 53 | + re: this.re * complexMultiplicand.re - this.im * complexMultiplicand.im, | ||
| 54 | + im: this.re * complexMultiplicand.im + this.im * complexMultiplicand.re, | ||
| 46 | 55 | }); | |
| 47 | 56 | } | |
| 48 | 57 | ||
| 49 | 58 | /** | |
| 50 | - * @param {ComplexNumber} divider | ||
| 59 | + * @param {ComplexNumber|number} divider | ||
| 51 | 60 | * @return {ComplexNumber} | |
| 52 | 61 | */ | |
| 53 | 62 | divide(divider) { | |
| 63 | + // Make sure we're dealing with complex number. | ||
| 64 | + const complexDivider = this.toComplexNumber(divider); | ||
| 65 | + | ||
| 54 | 66 | // Get divider conjugate. | |
| 55 | - const dividerConjugate = this.conjugate(divider); | ||
| 67 | + const dividerConjugate = this.conjugate(complexDivider); | ||
| 56 | 68 | ||
| 57 | 69 | // Multiply dividend by divider's conjugate. | |
| 58 | 70 | const finalDivident = this.multiply(dividerConjugate); | |
| 59 | 71 | ||
| 60 | 72 | // Calculating final divider using formula (a + bi)(a − bi) = a^2 + b^2 | |
| 61 | - const finalDivider = (divider.re ** 2) + (divider.im ** 2); | ||
| 73 | + const finalDivider = (complexDivider.re ** 2) + (complexDivider.im ** 2); | ||
| 62 | 74 | ||
| 63 | 75 | return new ComplexNumber({ | |
| 64 | 76 | re: finalDivident.re / finalDivider, | |
@@ -67,9 +79,12 @@ export default class ComplexNumber { | |||
| 67 | 79 | } | |
| 68 | 80 | ||
| 69 | 81 | /** | |
| 70 | - * @param {ComplexNumber} complexNumber | ||
| 82 | + * @param {ComplexNumber|number} number | ||
| 71 | 83 | */ | |
| 72 | - conjugate(complexNumber) { | ||
| 84 | + conjugate(number) { | ||
| 85 | + // Make sure we're dealing with complex number. | ||
| 86 | + const complexNumber = this.toComplexNumber(number); | ||
| 87 | + | ||
| 73 | 88 | return new ComplexNumber({ | |
| 74 | 89 | re: complexNumber.re, | |
| 75 | 90 | im: -1 * complexNumber.im, | |
@@ -96,6 +111,18 @@ export default class ComplexNumber { | |||
| 96 | 111 | phase = -(Math.PI - phase); | |
| 97 | 112 | } else if (this.re > 0 && this.im < 0) { | |
| 98 | 113 | phase = -phase; | |
| 114 | + } else if (this.re === 0 && this.im > 0) { | ||
| 115 | + phase = Math.PI / 2; | ||
| 116 | + } else if (this.re === 0 && this.im < 0) { | ||
| 117 | + phase = -Math.PI / 2; | ||
| 118 | + } else if (this.re < 0 && this.im === 0) { | ||
| 119 | + phase = Math.PI; | ||
| 120 | + } else if (this.re > 0 && this.im === 0) { | ||
| 121 | + phase = 0; | ||
| 122 | + } else if (this.re === 0 && this.im === 0) { | ||
| 123 | + // More correctly would be to set 'indeterminate'. | ||
| 124 | + // But just for simplicity reasons let's set zero. | ||
| 125 | + phase = 0; | ||
| 99 | 126 | } | |
| 100 | 127 | ||
| 101 | 128 | if (!inRadians) { | |
@@ -115,4 +142,19 @@ export default class ComplexNumber { | |||
| 115 | 142 | phase: this.getPhase(inRadians), | |
| 116 | 143 | }; | |
| 117 | 144 | } | |
| 145 | + | ||
| 146 | + /** | ||
| 147 | + * Convert real numbers to complex number. | ||
| 148 | + * In case if complex number is provided then lefts it as is. | ||
| 149 | + * | ||
| 150 | + * @param {ComplexNumber|number} number | ||
| 151 | + * @return {ComplexNumber} | ||
| 152 | + */ | ||
| 153 | + toComplexNumber(number) { | ||
| 154 | + if (number instanceof ComplexNumber) { | ||
| 155 | + return number; | ||
| 156 | + } | ||
| 157 | + | ||
| 158 | + return new ComplexNumber({ re: number }); | ||
| 159 | + } | ||
| 118 | 160 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -33,12 +33,16 @@ describe('ComplexNumber', () => { | |||
| 33 | 33 | ||
| 34 | 34 | const complexNumber3 = complexNumber.add(realNumber); | |
| 35 | 35 | const complexNumber4 = realNumber.add(complexNumber); | |
| 36 | + const complexNumber5 = complexNumber.add(3); | ||
| 36 | 37 | ||
| 37 | 38 | expect(complexNumber3.re).toBe(1 + 3); | |
| 38 | 39 | expect(complexNumber3.im).toBe(2); | |
| 39 | 40 | ||
| 40 | 41 | expect(complexNumber4.re).toBe(1 + 3); | |
| 41 | 42 | expect(complexNumber4.im).toBe(2); | |
| 43 | + | ||
| 44 | + expect(complexNumber5.re).toBe(1 + 3); | ||
| 45 | + expect(complexNumber5.im).toBe(2); | ||
| 42 | 46 | }); | |
| 43 | 47 | ||
| 44 | 48 | it('should subtract complex numbers', () => { | |
@@ -61,12 +65,16 @@ describe('ComplexNumber', () => { | |||
| 61 | 65 | ||
| 62 | 66 | const complexNumber3 = complexNumber.subtract(realNumber); | |
| 63 | 67 | const complexNumber4 = realNumber.subtract(complexNumber); | |
| 68 | + const complexNumber5 = complexNumber.subtract(3); | ||
| 64 | 69 | ||
| 65 | 70 | expect(complexNumber3.re).toBe(1 - 3); | |
| 66 | 71 | expect(complexNumber3.im).toBe(2); | |
| 67 | 72 | ||
| 68 | 73 | expect(complexNumber4.re).toBe(3 - 1); | |
| 69 | 74 | expect(complexNumber4.im).toBe(-2); | |
| 75 | + | ||
| 76 | + expect(complexNumber5.re).toBe(1 - 3); | ||
| 77 | + expect(complexNumber5.im).toBe(2); | ||
| 70 | 78 | }); | |
| 71 | 79 | ||
| 72 | 80 | it('should multiply complex numbers', () => { | |
@@ -75,12 +83,16 @@ describe('ComplexNumber', () => { | |||
| 75 | 83 | ||
| 76 | 84 | const complexNumber3 = complexNumber1.multiply(complexNumber2); | |
| 77 | 85 | const complexNumber4 = complexNumber2.multiply(complexNumber1); | |
| 86 | + const complexNumber5 = complexNumber1.multiply(5); | ||
| 78 | 87 | ||
| 79 | 88 | expect(complexNumber3.re).toBe(-11); | |
| 80 | 89 | expect(complexNumber3.im).toBe(23); | |
| 81 | 90 | ||
| 82 | 91 | expect(complexNumber4.re).toBe(-11); | |
| 83 | 92 | expect(complexNumber4.im).toBe(23); | |
| 93 | + | ||
| 94 | + expect(complexNumber5.re).toBe(15); | ||
| 95 | + expect(complexNumber5.im).toBe(10); | ||
| 84 | 96 | }); | |
| 85 | 97 | ||
| 86 | 98 | it('should multiply complex numbers by themselves', () => { | |
@@ -106,9 +118,13 @@ describe('ComplexNumber', () => { | |||
| 106 | 118 | const complexNumber2 = new ComplexNumber({ re: 4, im: -5 }); | |
| 107 | 119 | ||
| 108 | 120 | const complexNumber3 = complexNumber1.divide(complexNumber2); | |
| 121 | + const complexNumber4 = complexNumber1.divide(2); | ||
| 109 | 122 | ||
| 110 | 123 | expect(complexNumber3.re).toBe(-7 / 41); | |
| 111 | 124 | expect(complexNumber3.im).toBe(22 / 41); | |
| 125 | + | ||
| 126 | + expect(complexNumber4.re).toBe(1); | ||
| 127 | + expect(complexNumber4.im).toBe(1.5); | ||
| 112 | 128 | }); | |
| 113 | 129 | ||
| 114 | 130 | it('should return complex number in polar form', () => { | |
@@ -136,5 +152,30 @@ describe('ComplexNumber', () => { | |||
| 136 | 152 | expect(complexNumber5.getPolarForm().radius).toBeCloseTo(8.60); | |
| 137 | 153 | expect(complexNumber5.getPolarForm().phase).toBeCloseTo(0.95); | |
| 138 | 154 | expect(complexNumber5.getPolarForm(false).phase).toBeCloseTo(54.46); | |
| 155 | + | ||
| 156 | + const complexNumber6 = new ComplexNumber({ re: 0, im: 0.25 }); | ||
| 157 | + expect(complexNumber6.getPolarForm().radius).toBeCloseTo(0.25); | ||
| 158 | + expect(complexNumber6.getPolarForm().phase).toBeCloseTo(1.57); | ||
| 159 | + expect(complexNumber6.getPolarForm(false).phase).toBeCloseTo(90); | ||
| 160 | + | ||
| 161 | + const complexNumber7 = new ComplexNumber({ re: 0, im: -0.25 }); | ||
| 162 | + expect(complexNumber7.getPolarForm().radius).toBeCloseTo(0.25); | ||
| 163 | + expect(complexNumber7.getPolarForm().phase).toBeCloseTo(-1.57); | ||
| 164 | + expect(complexNumber7.getPolarForm(false).phase).toBeCloseTo(-90); | ||
| 165 | + | ||
| 166 | + const complexNumber8 = new ComplexNumber(); | ||
| 167 | + expect(complexNumber8.getPolarForm().radius).toBeCloseTo(0); | ||
| 168 | + expect(complexNumber8.getPolarForm().phase).toBeCloseTo(0); | ||
| 169 | + expect(complexNumber8.getPolarForm(false).phase).toBeCloseTo(0); | ||
| 170 | + | ||
| 171 | + const complexNumber9 = new ComplexNumber({ re: -0.25, im: 0 }); | ||
| 172 | + expect(complexNumber9.getPolarForm().radius).toBeCloseTo(0.25); | ||
| 173 | + expect(complexNumber9.getPolarForm().phase).toBeCloseTo(Math.PI); | ||
| 174 | + expect(complexNumber9.getPolarForm(false).phase).toBeCloseTo(180); | ||
| 175 | + | ||
| 176 | + const complexNumber10 = new ComplexNumber({ re: 0.25, im: 0 }); | ||
| 177 | + expect(complexNumber10.getPolarForm().radius).toBeCloseTo(0.25); | ||
| 178 | + expect(complexNumber10.getPolarForm().phase).toBeCloseTo(0); | ||
| 179 | + expect(complexNumber10.getPolarForm(false).phase).toBeCloseTo(0); | ||
| 139 | 180 | }); | |
| 140 | 181 | }); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments