Added matrix diagonal method and upper/lower triangular form check
This commit is contained in:
parent
d6af343db1
commit
d0e27ea7da
|
@ -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);
|
||||
});
|
22
matrix.ts
22
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<Math.min(this.rows, this.cols); i++) {
|
||||
diagonal.push(this.matrix[i][i]);
|
||||
}
|
||||
return diagonal;
|
||||
}
|
||||
|
||||
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++) {
|
||||
|
@ -108,7 +118,7 @@ export class Matrix {
|
|||
return true;
|
||||
}
|
||||
|
||||
in_stepped_shape() {
|
||||
is_in_elchelon_form() {
|
||||
let pivot_index = -1;
|
||||
for(let i=0; i<this.rows; i++) {
|
||||
for(let j=0; j<this.cols; j++) {
|
||||
|
@ -122,6 +132,16 @@ export class Matrix {
|
|||
return true;
|
||||
}
|
||||
|
||||
is_triangular(form: TriangularForm = TriangularForm.UPPER) {
|
||||
for(let i=0; i<Math.min(this.rows, this.cols); i++) {
|
||||
for(let j=0; j<i; j++) {
|
||||
if(this.matrix[i][j] !== 0 && form == TriangularForm.UPPER) return false;
|
||||
if(this.matrix[j][i] !== 0 && form == TriangularForm.LOWER) return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
pivots() {
|
||||
let pivots: number[] = [];
|
||||
for(let i=0; i<this.rows; i++) {
|
||||
|
|
Reference in a new issue