Skip to content

Commit

Permalink
isEqualTo at Complex class
Browse files Browse the repository at this point in the history
  • Loading branch information
robertrypula committed May 1, 2018
1 parent 86dde5e commit 05b578b
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/common/simple-math/simple-math.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ interface ISimpleMath {
random(): number;
max(a: number, b: number): number;
min(a: number, b: number): number;
abs(x: number): number;
}

interface ISimpleMathStatic {
Expand Down
5 changes: 5 additions & 0 deletions src/common/simple-math/simple-math.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,9 @@ describe('List', () => {
expect(simpleMath.max(0.123456, 0.654321)).toBeCloseTo(0.654321, precisionDigits);
expect(simpleMath.min(0.123456, 0.654321)).toBeCloseTo(0.123456, precisionDigits);
});

it('should properly return absolute value', () => {
expect(simpleMath.abs(0.123456)).toBeCloseTo(0.123456, precisionDigits);
expect(simpleMath.abs(-0.123456)).toBeCloseTo(0.123456, precisionDigits);
});
});
4 changes: 4 additions & 0 deletions src/common/simple-math/simple-math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,8 @@ export class SimpleMath implements ISimpleMath {
public min(a: number, b: number): number {
return Math.min(a, b);
}

public abs(x: number): number {
return Math.abs(x);
}
}
1 change: 1 addition & 0 deletions src/dsp/complex/complex-dependency-bag.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ISimpleMath } from './../../common';
interface IComplexDependencyBag {
simpleMath: ISimpleMath;
precisionDigits: number;
epsilon: number;
}

interface IComplexDependencyBagStatic {
Expand Down
1 change: 1 addition & 0 deletions src/dsp/complex/complex-dependency-bag.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ describe('ComplexDependencyBag', () => {
expect(complexDependencyBag).toBeInstanceOf(ComplexDependencyBag);
expect(complexDependencyBag.simpleMath).toBeInstanceOf(SimpleMath);
expect(complexDependencyBag.precisionDigits).toBe(precisionDigits);
expect(complexDependencyBag.epsilon).toBe(0.000001);
});
});
3 changes: 3 additions & 0 deletions src/dsp/complex/complex-dependency-bag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ export class ComplexDependencyBag implements IComplexDependencyBag {
PRECISION_DIGITS
];

public epsilon: number;

constructor(
public simpleMath: ISimpleMath,
public precisionDigits: number
) {
this.epsilon = simpleMath.pow(10, -precisionDigits);
}
}
1 change: 1 addition & 0 deletions src/dsp/complex/complex.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ interface IComplex {
getMagnitude(): number;
getUnitAngle(): number;
normalize(): IComplex;
isEqualTo(b: IComplex): boolean;
toDto(): IComplexDto;
toRawIQ(): number[];
}
Expand Down
12 changes: 12 additions & 0 deletions src/dsp/complex/complex.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,18 @@ describe('Complex', () => {
expect(complex).toBe(complexNormalized);
});

it('should properly indicate that two complex numbers are equal', () => {
const complexA = new Complex(complexDependencyBag, REAL, IMAGINARY);
const complexB = new Complex(
complexDependencyBag,
REAL + 0.000001,
IMAGINARY + 0.000001
);

expect(complex.isEqualTo(complexA)).toBe(true);
expect(complex.isEqualTo(complexB)).toBe(false);
});

it('should properly return DTO', () => {
const complexDto: IComplexDto = complex.toDto();

Expand Down
8 changes: 8 additions & 0 deletions src/dsp/complex/complex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,14 @@ export class Complex implements IComplex {
return this;
}

public isEqualTo(b: IComplex): boolean {
const simpleMath: ISimpleMath = this.complexDependencyBag.simpleMath;
const epsilon: number = this.complexDependencyBag.epsilon;

return (simpleMath.abs(this.real - b.getReal()) < epsilon) &&
(simpleMath.abs(this.imaginary - b.getImaginary()) < epsilon);
}

public toDto(): IComplexDto {
/* tslint:disable:object-literal-sort-keys */
return {
Expand Down

0 comments on commit 05b578b

Please sign in to comment.