-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.pde
More file actions
271 lines (252 loc) · 9.59 KB
/
Matrix.pde
File metadata and controls
271 lines (252 loc) · 9.59 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
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
class Matrix {
//local variables
int rows;
int cols;
float[][] matrix;
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//constructor
Matrix(int r, int c) {
rows = r;
cols = c;
matrix = new float[rows][cols];
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//constructor from 2D array
Matrix(float[][] m) {
matrix = m;
cols = m.length;
rows = m[0].length;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//print matrix
void output() {
for (int i =0; i<rows; i++) {
for (int j = 0; j<cols; j++) {
print(matrix[i][j] + " ");
}
println(" ");
}
println();
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//multiply by scalar
void multiply(float n ) {
for (int i =0; i<rows; i++) {
for (int j = 0; j<cols; j++) {
matrix[i][j] *= n;
}
}
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//return a matrix which is this matrix dot product parameter matrix
Matrix dot(Matrix n) {
Matrix result = new Matrix(rows, n.cols);
if (cols == n.rows) {
//for each spot in the new matrix
for (int i =0; i<rows; i++) {
for (int j = 0; j<n.cols; j++) {
float sum = 0;
for (int k = 0; k<cols; k++) {
sum+= matrix[i][k]*n.matrix[k][j];
}
result.matrix[i][j] = sum;
}
}
}
return result;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//set the matrix to random ints between -1 and 1
void randomize() {
for (int i =0; i<rows; i++) {
for (int j = 0; j<cols; j++) {
matrix[i][j] = random(-1, 1);
}
}
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//add a scalar to the matrix
void Add(float n ) {
for (int i =0; i<rows; i++) {
for (int j = 0; j<cols; j++) {
matrix[i][j] += n;
}
}
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
///return a matrix which is this matrix + parameter matrix
Matrix add(Matrix n ) {
Matrix newMatrix = new Matrix(rows, cols);
if (cols == n.cols && rows == n.rows) {
for (int i =0; i<rows; i++) {
for (int j = 0; j<cols; j++) {
newMatrix.matrix[i][j] = matrix[i][j] + n.matrix[i][j];
}
}
}
return newMatrix;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//return a matrix which is this matrix - parameter matrix
Matrix subtract(Matrix n ) {
Matrix newMatrix = new Matrix(cols, rows);
if (cols == n.cols && rows == n.rows) {
for (int i =0; i<rows; i++) {
for (int j = 0; j<cols; j++) {
newMatrix.matrix[i][j] = matrix[i][j] - n.matrix[i][j];
}
}
}
return newMatrix;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//return a matrix which is this matrix * parameter matrix (element wise multiplication)
Matrix multiply(Matrix n ) {
Matrix newMatrix = new Matrix(rows, cols);
if (cols == n.cols && rows == n.rows) {
for (int i =0; i<rows; i++) {
for (int j = 0; j<cols; j++) {
newMatrix.matrix[i][j] = matrix[i][j] * n.matrix[i][j];
}
}
}
return newMatrix;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//return a matrix which is the transpose of this matrix
Matrix transpose() {
Matrix n = new Matrix(cols, rows);
for (int i =0; i<rows; i++) {
for (int j = 0; j<cols; j++) {
n.matrix[j][i] = matrix[i][j];
}
}
return n;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//Creates a single column array from the parameter array
Matrix singleColumnMatrixFromArray(float[] arr) {
Matrix n = new Matrix(arr.length, 1);
for (int i = 0; i< arr.length; i++) {
n.matrix[i][0] = arr[i];
}
return n;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//sets this matrix from an array
void fromArray(float[] arr) {
for (int i = 0; i< rows; i++) {
for (int j = 0; j< cols; j++) {
matrix[i][j] = arr[j+i*cols];
}
}
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//returns an array which represents this matrix
float[] toArray() {
float[] arr = new float[rows*cols];
for (int i = 0; i< rows; i++) {
for (int j = 0; j< cols; j++) {
arr[j+i*cols] = matrix[i][j];
}
}
return arr;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//for ix1 matrixes adds one to the bottom
Matrix addBias() {
Matrix n = new Matrix(rows+1, 1);
for (int i =0; i<rows; i++) {
n.matrix[i][0] = matrix[i][0];
}
n.matrix[rows][0] = 1;
return n;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//applies the activation function(sigmoid) to each element of the matrix
Matrix activate() {
Matrix n = new Matrix(rows, cols);
for (int i =0; i<rows; i++) {
for (int j = 0; j<cols; j++) {
n.matrix[i][j] = sigmoid(matrix[i][j]);
}
}
return n;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//sigmoid activation function
float sigmoid(float x) {
float y = 1 / (1 + pow((float)Math.E, -x));
return y;
}
//returns the matrix that is the derived sigmoid function of the current matrix
Matrix sigmoidDerived() {
Matrix n = new Matrix(rows, cols);
for (int i =0; i<rows; i++) {
for (int j = 0; j<cols; j++) {
n.matrix[i][j] = (matrix[i][j] * (1- matrix[i][j]));
}
}
return n;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//returns the matrix which is this matrix with the bottom layer removed
Matrix removeBottomLayer() {
Matrix n = new Matrix(rows-1, cols);
for (int i =0; i<n.rows; i++) {
for (int j = 0; j<cols; j++) {
n.matrix[i][j] = matrix[i][j];
}
}
return n;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//Mutation function for genetic algorithm
void mutate(float mutationRate) {
//for each element in the matrix
for (int i =0; i<rows; i++) {
for (int j = 0; j<cols; j++) {
float rand = random(1);
if (rand<mutationRate) {//if chosen to be mutated
matrix[i][j] += randomGaussian()/5;//add a random value to it(can be negative)
//set the boundaries to 1 and -1
if (matrix[i][j]>1) {
matrix[i][j] = 1;
}
if (matrix[i][j] <-1) {
matrix[i][j] = -1;
}
}
}
}
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//returns a matrix which has a random number of values from this matrix and the rest from the parameter matrix
Matrix crossover(Matrix partner) {
Matrix child = new Matrix(rows, cols);
//pick a random point in the matrix
int randC = floor(random(cols));
int randR = floor(random(rows));
for (int i =0; i<rows; i++) {
for (int j = 0; j<cols; j++) {
if ((i< randR)|| (i==randR && j<=randC)) { //if before the random point then copy from this matric
child.matrix[i][j] = 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
Matrix clone() {
Matrix clone = new Matrix(rows, cols);
for (int i =0; i<rows; i++) {
for (int j = 0; j<cols; j++) {
clone.matrix[i][j] = matrix[i][j];
}
}
return clone;
}
}