Refactor complex numbers.

This commit is contained in:
Oleksii Trekhleb 2018-08-14 21:30:57 +03:00
parent 7d6854489e
commit b3315966e5
2 changed files with 61 additions and 61 deletions

View File

@ -1,11 +1,11 @@
export default class ComplexNumber {
/**
* @param {number} [real]
* @param {number} [imaginary]
* @param {number} [re]
* @param {number} [im]
*/
constructor({ real = 0, imaginary = 0 } = {}) {
this.real = real;
this.imaginary = imaginary;
constructor({ re = 0, im = 0 } = {}) {
this.re = re;
this.im = im;
}
/**
@ -14,8 +14,8 @@ export default class ComplexNumber {
*/
add(addend) {
return new ComplexNumber({
real: this.real + addend.real,
imaginary: this.imaginary + addend.imaginary,
re: this.re + addend.re,
im: this.im + addend.im,
});
}
@ -25,8 +25,8 @@ export default class ComplexNumber {
*/
subtract(subtrahend) {
return new ComplexNumber({
real: this.real - subtrahend.real,
imaginary: this.imaginary - subtrahend.imaginary,
re: this.re - subtrahend.re,
im: this.im - subtrahend.im,
});
}
@ -36,8 +36,8 @@ export default class ComplexNumber {
*/
multiply(multiplicand) {
return new ComplexNumber({
real: this.real * multiplicand.real - this.imaginary * multiplicand.imaginary,
imaginary: this.real * multiplicand.imaginary + this.imaginary * multiplicand.real,
re: this.re * multiplicand.re - this.im * multiplicand.im,
im: this.re * multiplicand.im + this.im * multiplicand.re,
});
}
@ -53,11 +53,11 @@ export default class ComplexNumber {
const finalDivident = this.multiply(dividerConjugate);
// Calculating final divider using formula (a + bi)(a bi) = a^2 + b^2
const finalDivider = (divider.real ** 2) + (divider.imaginary ** 2);
const finalDivider = (divider.re ** 2) + (divider.im ** 2);
return new ComplexNumber({
real: finalDivident.real / finalDivider,
imaginary: finalDivident.imaginary / finalDivider,
re: finalDivident.re / finalDivider,
im: finalDivident.im / finalDivider,
});
}
@ -66,8 +66,8 @@ export default class ComplexNumber {
*/
conjugate(complexNumber) {
return new ComplexNumber({
real: complexNumber.real,
imaginary: -1 * complexNumber.imaginary,
re: complexNumber.re,
im: -1 * complexNumber.im,
});
}
}

View File

@ -2,112 +2,112 @@ import ComplexNumber from '../ComplexNumber';
describe('ComplexNumber', () => {
it('should create complex numbers', () => {
const complexNumber = new ComplexNumber({ real: 1, imaginary: 2 });
const complexNumber = new ComplexNumber({ re: 1, im: 2 });
expect(complexNumber).toBeDefined();
expect(complexNumber.real).toBe(1);
expect(complexNumber.imaginary).toBe(2);
expect(complexNumber.re).toBe(1);
expect(complexNumber.im).toBe(2);
const defaultComplexNumber = new ComplexNumber();
expect(defaultComplexNumber.real).toBe(0);
expect(defaultComplexNumber.imaginary).toBe(0);
expect(defaultComplexNumber.re).toBe(0);
expect(defaultComplexNumber.im).toBe(0);
});
it('should add complex numbers', () => {
const complexNumber1 = new ComplexNumber({ real: 1, imaginary: 2 });
const complexNumber2 = new ComplexNumber({ real: 3, imaginary: 8 });
const complexNumber1 = new ComplexNumber({ re: 1, im: 2 });
const complexNumber2 = new ComplexNumber({ re: 3, im: 8 });
const complexNumber3 = complexNumber1.add(complexNumber2);
const complexNumber4 = complexNumber2.add(complexNumber1);
expect(complexNumber3.real).toBe(1 + 3);
expect(complexNumber3.imaginary).toBe(2 + 8);
expect(complexNumber3.re).toBe(1 + 3);
expect(complexNumber3.im).toBe(2 + 8);
expect(complexNumber4.real).toBe(1 + 3);
expect(complexNumber4.imaginary).toBe(2 + 8);
expect(complexNumber4.re).toBe(1 + 3);
expect(complexNumber4.im).toBe(2 + 8);
});
it('should add complex and natural numbers', () => {
const complexNumber = new ComplexNumber({ real: 1, imaginary: 2 });
const realNumber = new ComplexNumber({ real: 3 });
const complexNumber = new ComplexNumber({ re: 1, im: 2 });
const realNumber = new ComplexNumber({ re: 3 });
const complexNumber3 = complexNumber.add(realNumber);
const complexNumber4 = realNumber.add(complexNumber);
expect(complexNumber3.real).toBe(1 + 3);
expect(complexNumber3.imaginary).toBe(2);
expect(complexNumber3.re).toBe(1 + 3);
expect(complexNumber3.im).toBe(2);
expect(complexNumber4.real).toBe(1 + 3);
expect(complexNumber4.imaginary).toBe(2);
expect(complexNumber4.re).toBe(1 + 3);
expect(complexNumber4.im).toBe(2);
});
it('should subtract complex numbers', () => {
const complexNumber1 = new ComplexNumber({ real: 1, imaginary: 2 });
const complexNumber2 = new ComplexNumber({ real: 3, imaginary: 8 });
const complexNumber1 = new ComplexNumber({ re: 1, im: 2 });
const complexNumber2 = new ComplexNumber({ re: 3, im: 8 });
const complexNumber3 = complexNumber1.subtract(complexNumber2);
const complexNumber4 = complexNumber2.subtract(complexNumber1);
expect(complexNumber3.real).toBe(1 - 3);
expect(complexNumber3.imaginary).toBe(2 - 8);
expect(complexNumber3.re).toBe(1 - 3);
expect(complexNumber3.im).toBe(2 - 8);
expect(complexNumber4.real).toBe(3 - 1);
expect(complexNumber4.imaginary).toBe(8 - 2);
expect(complexNumber4.re).toBe(3 - 1);
expect(complexNumber4.im).toBe(8 - 2);
});
it('should subtract complex and natural numbers', () => {
const complexNumber = new ComplexNumber({ real: 1, imaginary: 2 });
const realNumber = new ComplexNumber({ real: 3 });
const complexNumber = new ComplexNumber({ re: 1, im: 2 });
const realNumber = new ComplexNumber({ re: 3 });
const complexNumber3 = complexNumber.subtract(realNumber);
const complexNumber4 = realNumber.subtract(complexNumber);
expect(complexNumber3.real).toBe(1 - 3);
expect(complexNumber3.imaginary).toBe(2);
expect(complexNumber3.re).toBe(1 - 3);
expect(complexNumber3.im).toBe(2);
expect(complexNumber4.real).toBe(3 - 1);
expect(complexNumber4.imaginary).toBe(-2);
expect(complexNumber4.re).toBe(3 - 1);
expect(complexNumber4.im).toBe(-2);
});
it('should multiply complex numbers', () => {
const complexNumber1 = new ComplexNumber({ real: 3, imaginary: 2 });
const complexNumber2 = new ComplexNumber({ real: 1, imaginary: 7 });
const complexNumber1 = new ComplexNumber({ re: 3, im: 2 });
const complexNumber2 = new ComplexNumber({ re: 1, im: 7 });
const complexNumber3 = complexNumber1.multiply(complexNumber2);
const complexNumber4 = complexNumber2.multiply(complexNumber1);
expect(complexNumber3.real).toBe(-11);
expect(complexNumber3.imaginary).toBe(23);
expect(complexNumber3.re).toBe(-11);
expect(complexNumber3.im).toBe(23);
expect(complexNumber4.real).toBe(-11);
expect(complexNumber4.imaginary).toBe(23);
expect(complexNumber4.re).toBe(-11);
expect(complexNumber4.im).toBe(23);
});
it('should multiply complex numbers by themselves', () => {
const complexNumber = new ComplexNumber({ real: 1, imaginary: 1 });
const complexNumber = new ComplexNumber({ re: 1, im: 1 });
const result = complexNumber.multiply(complexNumber);
expect(result.real).toBe(0);
expect(result.imaginary).toBe(2);
expect(result.re).toBe(0);
expect(result.im).toBe(2);
});
it('should calculate i in power of two', () => {
const complexNumber = new ComplexNumber({ real: 0, imaginary: 1 });
const complexNumber = new ComplexNumber({ re: 0, im: 1 });
const result = complexNumber.multiply(complexNumber);
expect(result.real).toBe(-1);
expect(result.imaginary).toBe(0);
expect(result.re).toBe(-1);
expect(result.im).toBe(0);
});
it('should divide complex numbers', () => {
const complexNumber1 = new ComplexNumber({ real: 2, imaginary: 3 });
const complexNumber2 = new ComplexNumber({ real: 4, imaginary: -5 });
const complexNumber1 = new ComplexNumber({ re: 2, im: 3 });
const complexNumber2 = new ComplexNumber({ re: 4, im: -5 });
const complexNumber3 = complexNumber1.divide(complexNumber2);
expect(complexNumber3.real).toBe(-7 / 41);
expect(complexNumber3.imaginary).toBe(22 / 41);
expect(complexNumber3.re).toBe(-7 / 41);
expect(complexNumber3.im).toBe(22 / 41);
});
});