-
Notifications
You must be signed in to change notification settings - Fork 1
/
matrix.js
272 lines (256 loc) · 10 KB
/
matrix.js
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
class Matrix {
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//constructor
constructor(r, c) {
this.rows = r;
this.cols = c;
this.matrix = [];
for (var i = 0; i < r; i++) {
this.matrix.push([]);
for (var j = 0; j < c; j++) {
this.matrix[i].push(0);
}
}
// console.log(this.matrix);
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//constructor from 2D array
static matrixFrom2D(m) {
this.matrix = m;
this.cols = m.length;
this.rows = m[0].length;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//print matrix
output() {
for (var i = 0; i < this.rows; i++) {
for (var j = 0; j < this.cols; j++) {
print(this.matrix[i][j] + " ");
}
println(" ");
}
println();
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//multiply by scalar
multiply(n) {
for (var i = 0; i < this.rows; i++) {
for (var j = 0; j < this.cols; j++) {
this.matrix[i][j] *= n;
}
}
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//return a matrix which is this matrix dot product parameter matrix
dot(n) {
var result = new Matrix(this.rows, n.cols);
if (this.cols == n.rows) {
//for each spot in the new matrix
for (var i = 0; i < this.rows; i++) {
for (var j = 0; j < n.cols; j++) {
var sum = 0;
for (var k = 0; k < this.cols; k++) {
sum += this.matrix[i][k] * n.matrix[k][j];
}
result.matrix[i][j] = sum;
}
}
}
return result;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//set the matrix to random ints between -1 and 1
randomize() {
for (var i = 0; i < this.rows; i++) {
for (var j = 0; j < this.cols; j++) {
var param = random();
this.matrix[i][j] = param * -2 + 1;
}
}
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//add a scalar to the matrix
Add(n) {
for (var i = 0; i < this.rows; i++) {
for (var j = 0; j < this.cols; j++) {
this.matrix[i][j] += n;
}
}
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
///return a matrix which is this matrix + parameter matrix
add(n) {
var newMatrix = new Matrix(rows, cols);
if (this.cols == n.cols && this.rows == n.rows) {
for (var i = 0; i < this.rows; i++) {
for (var j = 0; j < this.cols; j++) {
newMatrix.matrix[i][j] = this.matrix[i][j] + n.matrix[i][j];
}
}
}
return newMatrix;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//return a matrix which is this matrix - parameter matrix
subtract(n) {
var newMatrix = new Matrix(cols, rows);
if (this.cols == n.cols && this.rows == n.rows) {
for (var i = 0; i < this.rows; i++) {
for (var j = 0; j < this.cols; j++) {
newMatrix.matrix[i][j] = this.matrix[i][j] - n.matrix[i][j];
}
}
}
return newMatrix;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//return a matrix which is this matrix * parameter matrix (element wise multiplication)
multiply(n) {
var newMatrix = new Matrix(rows, cols);
if (this.cols == n.cols && this.rows == n.rows) {
for (var i = 0; i < this.rows; i++) {
for (var j = 0; j < this.cols; j++) {
newMatrix.matrix[i][j] = this.matrix[i][j] * n.matrix[i][j];
}
}
}
return newMatrix;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//return a matrix which is the transpose of this matrix
transpose() {
var n = new Matrix(cols, rows);
for (var i = 0; i < this.rows; i++) {
for (var j = 0; j < this.cols; j++) {
n.matrix[j][i] = this.matrix[i][j];
}
}
return n;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//Creates a single column array from the parameter array
singleColumnMatrixFromArray(arr) {
var n = new Matrix(arr.length, 1);
for (var i = 0; i < arr.length; i++) {
n.matrix[i][0] = arr[i];
}
return n;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//sets this matrix from an array
fromArray(arr) {
for (var i = 0; i < this.rows; i++) {
for (var j = 0; j < this.cols; j++) {
this.matrix[i][j] = arr[j + i * this.cols];
}
}
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//returns an array which represents this matrix
toArray() {
var arr = new Array(this.rows * this.cols);
for (var i = 0; i < this.rows; i++) {
for (var j = 0; j < this.cols; j++) {
arr[j + i * this.cols] = this.matrix[i][j];
}
}
return arr;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//for ix1 matrixes adds one to the bottom
addBias() {
var n = new Matrix(this.rows + 1, 1);
for (var i = 0; i < this.rows; i++) {
n.matrix[i][0] = this.matrix[i][0];
}
n.matrix[this.rows][0] = 1;
return n;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//applies the activation function(sigmoid) to each element of the matrix
activate() {
var n = new Matrix(this.rows, this.cols);
for (var i = 0; i < this.rows; i++) {
for (var j = 0; j < this.cols; j++) {
n.matrix[i][j] = this.sigmoid(this.matrix[i][j]);
}
}
return n;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//sigmoid activation function
sigmoid(x) {
var y = 1 / (1 + pow(Math.E, -x));
return y;
}
//returns the matrix that is the derived sigmoid function of the current matrix
sigmoidDerived() {
var n = new Matrix(this.rows, this.cols);
for (var i = 0; i < this.rows; i++) {
for (var j = 0; j < this.cols; j++) {
n.matrix[i][j] = (this.matrix[i][j] * (1 - this.matrix[i][j]));
}
}
return n;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//returns the matrix which is this matrix with the bottom layer removed
removeBottomLayer() {
var n = new Matrix(this.rows - 1, this.cols);
for (var i = 0; i < n.rows; i++) {
for (var j = 0; j < this.cols; j++) {
n.matrix[i][j] = this.matrix[i][j];
}
}
return n;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//Mutation function for genetic algorithm
mutate(mutationRate) {
//for each element in the matrix
for (var i = 0; i < this.rows; i++) {
for (var j = 0; j < this.cols; j++) {
var rand = random();
if (rand < mutationRate) { //if chosen to be mutated
this.matrix[i][j] += randomGaussian() / 5; //add a random value to it(can be negative)
//set the boundaries to 1 and -1
if (this.matrix[i][j] > 1) {
this.matrix[i][j] = 1;
}
if (this.matrix[i][j] < -1) {
this.matrix[i][j] = -1;
}
}
}
}
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//returns a matrix which has a random number of values from this matrix and the rest from the parameter matrix
crossover(partner) {
var child = new Matrix(this.rows, this.cols);
//pick a random point in the matrix
var randC = floor(random(this.cols));
var randR = floor(random(this.rows));
for (var i = 0; i < this.rows; i++) {
for (var j = 0; j < this.cols; j++) {
if ((i < randR) || (i == randR && j <= randC)) { //if before the random point then copy from this matric
child.matrix[i][j] = this.matrix[i][j];
} else { //if after the random point then copy from the parameter array
child.matrix[i][j] = partner.matrix[i][j];
}
}
}
return child;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//return a copy of this matrix
clone() {
var clone = new Matrix(this.rows, this.cols);
for (var i = 0; i < this.rows; i++) {
for (var j = 0; j < this.cols; j++) {
clone.matrix[i][j] = this.matrix[i][j];
}
}
return clone;
}
}