-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix.js
More file actions
184 lines (165 loc) · 4.62 KB
/
matrix.js
File metadata and controls
184 lines (165 loc) · 4.62 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
class Matrix{
constructor(rows,cols)
{
this.rows = rows;
this.cols = cols;
this.data = Array(this.rows).fill().map(() => Array(this.cols).fill(0));
}
map(f)
{
for(let i=0;i<this.rows;i++)
{
for(let j=0;j<this.cols;j++)
{
let e = this.data[i][j];
this.data[i][j] = f(e,i,j);
}
}
return this;
}
get copy()
{
return new Matrix(this.rows,this.cols).map((e,i,j)=>this.data[i][j])
}
get str()
{
console.table(this.data);
return this;
}
get T()
{
return new Matrix(this.cols,this.rows).map((e,i,j)=> this.data[j][i]);
}
randInit()
{
return this.map(() => Math.random()*2-1);
}
static arr(a)
{
if(a[0][0] === undefined) // Vector instead of 2D Array
return new Matrix(a.length,1).map((e,i) => a[i]);
else
return new Matrix(a.length,a[0].length).map((e,i,j) => a[i][j]);
}
static eye(n)
{
return new Matrix(n,n).map((e,i,j) => (i===j)?1:0);
}
static dot(a,b)
{
if (a.cols !== b.rows)
{
console.log('Can\'t multiply this!');
return;
}
return new Matrix(a.rows,b.cols).map((e,i,j) => {
let sum = 0;
for(let k=0;k<a.cols;k++)
sum += a.data[i][k]*b.data[k][j]; //Dot Product, or Matrix Multiplication
return sum;
})
}
// Destructive edits, create copies beforehand
mul(m)
{
if (m instanceof Matrix)
{
if (this.rows !== m.rows || this.cols !== m.cols)
{
console.log('Can\'t do a Hadamard product on this!');
return;
}
return this.map((e,i,j) => m.data[i][j]*e); //Hadamard Product
}
else
{
return this.map(e => m*e);
}
}
add(m)
{
if (m instanceof Matrix)
{
if (this.rows !== m.rows || this.cols !== m.cols)
{
console.log('Can\'t do addition on this!');
return;
}
return this.map((e,i,j) => m.data[i][j] + e);
}
else
{
return this.map(e => m + e);
}
}
subtract(m)
{
if (m instanceof Matrix)
{
if (this.rows !== m.rows || this.cols !== m.cols)
{
console.log('Can\'t do subtraction on this!');
return;
}
return this.map((e,i,j) => e - m.data[i][j]);
}
else
{
return this.map(e => e - m);
}
}
get inv() // Gaussian Elimination
{
if(this.rows !== this.cols)
return;
let n = this.rows;
let M = this.copy;
let I = Matrix.eye(n);
//Check if diagonal has zeros
for(let i=0;i<n;i++)
{
if(M.data[i][i] === 0)
{
//Swap rows with one after current row that has a non-zero element in the same column
for(let k=i;k<n;k++)
{
if(M.data[k][i] !== 0)
{
for(let j=0;j<n;j++)
{
let temp = M.data[i][j]
M.data[i][j] = M.data[k][j];
M.data[k][j] = temp;
temp = I.data[i][j];
I.data[i][j] = I.data[k][j];
I.data[k][j] = temp;
}
}
break;
}
if(M.data[i][i] === 0)
return; // Not invertible
}
let diag = M.data[i][i];
for(let j=0;j<n;j++)
{
M.data[i][j]/=diag;
I.data[i][j]/=diag;
}
//Echelon form
for(let k=0;k<n;k++)
{
if (k!==i)
{
let temp = M.data[k][i];
for(let j=0;j<n;j++)
{
M.data[k][j] -= temp*M.data[i][j];
I.data[k][j] -= temp*I.data[i][j];
}
}
}
}
return I;
}
}