From 96dd404e7d8298369bb53d516eb04ed299747803 Mon Sep 17 00:00:00 2001 From: Filip Znachor Date: Thu, 20 Oct 2022 17:35:21 +0200 Subject: [PATCH] Added invertible matrix method --- matrix.ts | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/matrix.ts b/matrix.ts index cac67bb..ce1219b 100644 --- a/matrix.ts +++ b/matrix.ts @@ -80,8 +80,9 @@ export class Matrix { } pow(power: number) { - for(let i=0; i<(power-1); i++) - this.set(...this.mul(this).matrix); + if(power > 0) + for(let i=0; i<(power-1); i++) + this.set(...this.mul(this).matrix); return this; } @@ -159,6 +160,22 @@ export class Matrix { m.splice(row, 1); this.set(...m); } + invertible() { + let matrix = Matrix.zero(this.rows, this.cols*2); + this.matrix.forEach((row, i) => { + matrix.matrix[i][this.cols+i] = 1; + row.forEach((col, j) => { + matrix.matrix[i][j] = col; + }); + }); + let gje = GaussianElimination.gje(matrix).matrix.map(GaussianElimination.make_pivot_1); + let result = Matrix.zero(this.rows, this.cols); + result.set(...result.matrix.map((row, i) => { + return row.map((col, j) => gje[i][this.cols+j]); + })); + return result; + } + }