This repository has been archived on 2022-10-20. You can view files and clone it, but cannot push or open issues or pull requests.
Math/__tests__/matrix.ts

118 lines
2 KiB
TypeScript

import { GaussianElimination, Matrix, TriangularForm } 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('Transpose (A, B)', () => {
expect(A.eq(B.transpose())).toBe(true);
});
test('Transpose (B, A)', () => {
expect(B.eq(A.transpose())).toBe(true);
});
test('Multiply (A, B, C)', () => {
expect(A.mul(B).eq(C)).toBe(true);
});
test('Multiply (B, A, C)', () => {
expect(B.mul(A).eq(C)).toBe(false);
});
let D = new Matrix(
[1, 2, -1, 1, 2],
[2, 5, 0, 1, 5],
[3, 7, -1, 3, 9],
[1, 3, 1, 1, 5]
);
let D_solved = new Matrix(
[1, 2, -1, 1, 2],
[0, 1, 2, -1, 1],
[0, 0, 0, 1, 2],
[0, 0, 0, 0, 0]
);
test('Gaussian elimination (D)', () => {
expect(GaussianElimination.ge(D).eq(D_solved)).toBe(true);
});
let E = new Matrix(
[1, 1, 3],
[2, -1, 4]
);
let E_solved = new Matrix(
[1, 1, 3],
[0, -3, -2]
);
test('Gaussian elimination (E)', () => {
expect(GaussianElimination.ge(E).eq(E_solved)).toBe(true);
});
let F = new Matrix(
[2, 1, -1, 8],
[-3, -1, 2, -11],
[-2, 1, 2, -3]
);
let F_solved = new Matrix(
[2, 0, 0, 4],
[0, 0.5, 0, 1.5],
[0, 0, -1, 1]
);
let F_results = GaussianElimination.solve(F);
test('Gauss-Jordan elimination (F)', () => {
expect(GaussianElimination.gje(F).eq(F_solved)).toBe(true);
});
test('Only one result of Gauss-Jordan elimination (F)', () => {
expect(F_results instanceof Matrix).toBe(false);
});
test('Results of Gauss-Jordan elimination (F)', () => {
[2, 3, -1].forEach((result, i) => {
if(F_results instanceof Matrix) throw("Result is matrix!");
expect(result == F_results[i]).toBe(true);
});
});
let G = new Matrix(
[1, 1, 1],
[0, 2, 4],
[0, 0, 5],
);
let I = new Matrix(
[1, 0, 0],
[5, 2, 0],
[4, 8, 5],
);
test('Upper triangular matrix (F)', () => {
expect(G.is_triangular()).toBe(true);
});
test('Upper triangular matrix (A)', () => {
expect(A.is_triangular()).toBe(false);
});
test('Lower triangular matrix (G)', () => {
expect(I.is_triangular(TriangularForm.LOWER)).toBe(true);
});