Added Matrix equals method & matrix.ts unit tests

This commit is contained in:
Filip Znachor 2022-10-06 14:54:01 +02:00
parent 45929eb414
commit 7e6b016f7a
2 changed files with 43 additions and 0 deletions

33
__tests__/matrix.ts Normal file
View file

@ -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);
});

View file

@ -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<this.rows; i++) {
for(let j=0; j<this.cols; j++) {
if(this.matrix[i][j] != other_matrix.matrix[i][j]) return false;
}
}
return true;
}
}