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