Added Matrix equals method & matrix.ts unit tests
This commit is contained in:
parent
45929eb414
commit
7e6b016f7a
33
__tests__/matrix.ts
Normal file
33
__tests__/matrix.ts
Normal 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);
|
||||||
|
});
|
10
matrix.ts
10
matrix.ts
|
@ -94,4 +94,14 @@ export class Matrix {
|
||||||
return new_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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Reference in a new issue