-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.ts
162 lines (129 loc) · 3.92 KB
/
matrix.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
export default class Matrix {
public rows: number
public columns: number
private data: number[][] = []
constructor(rows: number, columns: number) {
this.rows = rows
this.columns = columns
this.data = new Array(rows).fill(new Array(columns).fill(0))
}
private checkRef(ref: [number, number]) {
if (ref[0] < 0 || ref[1] < 0 || ref[0] >= this.rows || ref[1] >= this.columns) {
throw new Error("Reference is out of bounds of matrix")
}
}
public get(ref: [number, number]) {
this.checkRef(ref)
return this.data[ref[0]][ref[1]]
}
public equals(data: number[][]): boolean {
if (data.length != this.rows) return false
if (Math.max(...data.map((r) => r.length)) != Math.min(...data.map((r) => r.length)))
return false
if (data[0].length != this.columns) return false
for (let r = 0; r < this.rows; r++) {
for (let c = 0; c < this.columns; c++) {
if (this.get([r, c]) != data[r][c]) return false
}
}
return true
}
public equalsMat(matrix: Matrix) {
if (this.rows != matrix.rows) return false
if (this.columns != matrix.columns) return false
for (let r = 0; r < this.rows; r++) {
for (let c = 0; c < this.columns; c++) {
if (this.get([r, c]) != matrix.get([r, c])) return false
}
}
return true
}
public setItem(ref: [number, number], value: number) {
this.checkRef(ref)
let temp = [...this.data[ref[0]]]
temp[ref[1]] = value
this.data[ref[0]] = temp
return this
}
public set(data: number[][]) {
for (let r = 0; r < data.length; r++) {
for (let c = 0; c < data[r].length; c++) {
this.setItem([r, c], data[r][c])
}
}
return this
}
public getMinor(ref: [number, number]) {
return new Matrix(this.rows - 1, this.columns - 1).set(
this.data.filter((_, ri) => ri != ref[0]).map((r) => r.filter((_, ci) => ci != ref[1]))
)
}
get det() {
if (this.rows != this.columns) {
throw new Error("Cannot find determinant of this matrix")
}
if(this.rows == 1) return this.get([0,0])
if (this.rows == 2) {
return this.get([0, 0]) * this.get([1, 1]) - this.get([0, 1]) * this.get([1, 0])
}
let det = 0
for (let i = 0; i < this.columns; i++) {
det += Math.pow(-1, i) * this.get([0, i]) * this.getMinor([0, i]).det
}
return det
}
public scale(scalar: number) {
for (let r = 0; r < this.rows; r++) {
for (let c = 0; c < this.columns; c++) {
this.setItem([r, c], this.get([r, c]) * scalar)
}
}
return this
}
public add(matrix: Matrix) {
if (this.rows != matrix.rows || this.columns != matrix.columns) {
throw new Error("Matricies must be the same size to add them")
}
let temp = new Matrix(this.rows, this.columns)
for (let r = 0; r < this.rows; r++) {
for (let c = 0; c < this.columns; c++) {
temp.setItem([r, c], this.get([r, c]) + matrix.get([r, c]))
}
}
return temp
}
public subtract(matrix: Matrix) {
return this.add(matrix.scale(-1))
}
private pairMultiply(matrix1: Matrix, matrix2: Matrix, mat1row: number, mat2colum: number) {
let temp = 0
for (let r = 0; r < matrix1.columns; r++) {
temp += matrix1.get([mat1row, r]) * matrix2.get([r, mat2colum])
}
return temp
}
public multiply(matrix: Matrix) {
if (this.columns != matrix.rows) {
throw new Error("Matricies must be multiplicatively comfortable")
}
let temp = new Matrix(this.rows, matrix.columns)
for (let r = 0; r < this.rows; r++) {
for (let c = 0; c < matrix.columns; c++) {
temp.setItem([r, c], this.pairMultiply(this, matrix, r, c))
}
}
return temp
}
public get inverse() {
if(this.det == 0) {
throw new Error("Cannot inverse a singular matrix")
}
const result = new Matrix(this.rows, this.columns)
for(let r = 0; r < this.rows; r++) {
for(let c = 0; c < this.columns; c++) {
result.setItem([c,r], this.getMinor([r,c]).det * Math.pow(-1,c) * Math.pow(-1, r))
}
}
return result.scale(1 / this.det)
}
}