From d0e27ea7da8769418459a7f07f16d53f7218f8c5 Mon Sep 17 00:00:00 2001 From: Filip Znachor Date: Thu, 13 Oct 2022 18:57:40 +0200 Subject: [PATCH] Added matrix diagonal method and upper/lower triangular form check --- __tests__/matrix.ts | 38 +++++++++++++++++++++++++++++++------- matrix.ts | 22 +++++++++++++++++++++- 2 files changed, 52 insertions(+), 8 deletions(-) diff --git a/__tests__/matrix.ts b/__tests__/matrix.ts index e048765..5b1f4de 100644 --- a/__tests__/matrix.ts +++ b/__tests__/matrix.ts @@ -1,4 +1,4 @@ -import { GaussianElimination, Matrix } from "../matrix"; +import { GaussianElimination, Matrix, TriangularForm } from "../matrix"; let A = new Matrix( [1, 2, 3], @@ -16,19 +16,19 @@ let C = new Matrix( [ 32, 77 ] ); -test('A = B.transpose', () => { +test('Transpose (A, B)', () => { expect(A.eq(B.transpose())).toBe(true); }); -test('B = A.transpose', () => { +test('Transpose (B, A)', () => { expect(B.eq(A.transpose())).toBe(true); }); -test('A * B = C', () => { +test('Multiply (A, B, C)', () => { expect(A.mul(B).eq(C)).toBe(true); }); -test('B * A != C', () => { +test('Multiply (B, A, C)', () => { expect(B.mul(A).eq(C)).toBe(false); }); @@ -48,7 +48,7 @@ let D_solved = new Matrix( [0, 0, 0, 0, 0] ); -test('D - Gaussian elimination', () => { +test('Gaussian elimination (D)', () => { expect(D.solve().eq(D_solved)).toBe(true); }); @@ -64,6 +64,30 @@ let E_solved = new Matrix( [0, -3, -2] ); -test('E - Gaussian elimination', () => { +test('Gaussian elimination (E)', () => { expect(E.solve().eq(E_solved)).toBe(true); +}); + +let F = new Matrix( + [1, 1, 1], + [0, 2, 4], + [0, 0, 5], +); + +let G = new Matrix( + [1, 0, 0], + [5, 2, 0], + [4, 8, 5], +); + +test('Upper triangular matrix (F)', () => { + expect(F.is_triangular()).toBe(true); +}); + +test('Upper triangular matrix (A)', () => { + expect(A.is_triangular()).toBe(false); +}); + +test('Lower triangular matrix (G)', () => { + expect(G.is_triangular(TriangularForm.LOWER)).toBe(true); }); \ No newline at end of file diff --git a/matrix.ts b/matrix.ts index d679d3a..fdec8d6 100644 --- a/matrix.ts +++ b/matrix.ts @@ -1,6 +1,8 @@ export type RawMatrix = number[][]; +export enum TriangularForm {UPPER, LOWER}; + export class Matrix { rows!: number; @@ -98,6 +100,14 @@ export class Matrix { return new_matrix; } + diagonal() { + let diagonal: number[] = []; + for(let i=0; i