From 7e6b016f7a3df1dd96bcde6a5b52d219835b088b Mon Sep 17 00:00:00 2001 From: Filip Znachor Date: Thu, 6 Oct 2022 14:54:01 +0200 Subject: [PATCH] Added Matrix equals method & matrix.ts unit tests --- __tests__/matrix.ts | 33 +++++++++++++++++++++++++++++++++ matrix.ts | 10 ++++++++++ 2 files changed, 43 insertions(+) create mode 100644 __tests__/matrix.ts diff --git a/__tests__/matrix.ts b/__tests__/matrix.ts new file mode 100644 index 0000000..c0aa876 --- /dev/null +++ b/__tests__/matrix.ts @@ -0,0 +1,33 @@ +import { Matrix } from "../matrix"; + +let A = new Matrix( + [1, 2, 3], + [4, 5, 6] +); + +let B = new Matrix( + [1, 4], + [2, 5], + [3, 6] +); + +let C = new Matrix( + [ 14, 32 ], + [ 32, 77 ] +); + +test('A = B.transpose', () => { + expect(A.eq(B.transpose())).toBe(true); +}); + +test('B = A.transpose', () => { + expect(B.eq(A.transpose())).toBe(true); +}); + +test('A * B = C', () => { + expect(A.mul(B).eq(C)).toBe(true); +}); + +test('B * A != C', () => { + expect(B.mul(A).eq(C)).toBe(false); +}); \ No newline at end of file diff --git a/matrix.ts b/matrix.ts index 0f6bcb5..d4358de 100644 --- a/matrix.ts +++ b/matrix.ts @@ -94,4 +94,14 @@ export class Matrix { return new_matrix; } + eq(other_matrix: Matrix) { + if(this.rows != other_matrix.rows || this.cols != other_matrix.cols) return false; + for(let i=0; i