-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix.pde
243 lines (219 loc) · 5.8 KB
/
Matrix.pde
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
static class Matrix extends PApplet {
int rows, cols;
float[][] data;
Matrix(int rows, int cols) {
this.rows = rows;
this.cols = cols;
this.data = new float[rows][cols];
for (int i = 0; i < this.rows; i++) {
for (int j = 0; j < this.cols; j++) {
data[i][j] = 0;
}
}
}
Matrix(float[][] m) {
this.rows = m.length;
this.cols = m[0].length;
this.data = m;
}
static Matrix scale(Matrix a, float s) {
Matrix m = new Matrix(a.rows, a.cols);;
for (int i = 0; i < m.rows; i++) {
for (int j = 0; j < m.cols; j++) {
m.data[i][j] = a.data[i][j] * s;
}
}
return m;
}
static Matrix product(Matrix a, Matrix b) {
Matrix m = new Matrix(a.rows, a.cols);
if (a.rows == b.rows && a.cols == b.cols) {
for (int i = 0; i < a.rows; i++) {
for (int j = 0; j < a.cols; j++) {
m.data[i][j] = a.data[i][j] * b.data[i][j];
}
}
return m;
} else {
throw new IllegalArgumentException("Number of rows and columns of each matrix must be equivalent.");
}
}
static Matrix add(Matrix a, int s) {
Matrix m = new Matrix(a.rows, a.cols);
for (int i = 0; i < a.rows; i++) {
for (int j = 0; j < a.cols; j++) {
m.data[i][j] = a.data[i][j] + s;
}
}
return m;
}
Matrix add(int s) {
Matrix a = this;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
a.data[i][j] = a.data[i][j] + s;
}
}
return a;
}
static Matrix add(Matrix a, Matrix b) {
Matrix m = new Matrix(a.rows, a.cols);
if (a.rows == b.rows && a.cols == b.cols) {
for (int i = 0; i < a.rows; i++) {
for (int j = 0; j < a.cols; j++) {
m.data[i][j] = a.data[i][j] + b.data[i][j];
}
}
return m;
} else {
throw new IllegalArgumentException("Number of rows and columns of each matrix must be equivalent.");
}
}
Matrix add(Matrix b) {
Matrix a = this;
if (a.rows == b.rows && a.cols == b.cols) {
for (int i = 0; i < a.rows; i++) {
for (int j = 0; j < a.cols; j++) {
a.data[i][j] = a.data[i][j] + b.data[i][j];
}
}
return this;
} else {
throw new IllegalArgumentException("Number of rows and columns of each matrix must be equivalent.");
}
}
static Matrix subtract(Matrix a, Matrix b) {
Matrix m = new Matrix(a.rows, a.cols);
if (a.rows == b.rows && a.cols == b.cols) {
for (int i = 0; i < a.rows; i++) {
for (int j = 0; j < a.cols; j++) {
m.data[i][j] = a.data[i][j] - b.data[i][j];
}
}
return m;
} else {
throw new IllegalArgumentException("Number of rows and columns of each matrix must be equivalent.");
}
}
Matrix subtract(Matrix b) {
Matrix a = this;
if (a.rows == b.rows && a.cols == b.cols) {
for (int i = 0; i < a.rows; i++) {
for (int j = 0; j < a.cols; j++) {
a.data[i][j] = a.data[i][j] - b.data[i][j];
}
}
return this;
} else {
throw new IllegalArgumentException("Number of rows and columns of each matrix must be equivalent.");
}
}
static Matrix multiply(Matrix a, Matrix b) {
Matrix m;
if (a.cols == b.rows) {
if (b.cols == 1) {
m = new Matrix(a.rows, 1);
} else {
m = new Matrix(a.rows, b.cols);
}
for (int i = 0; i < m.rows; i++) {
for (int j = 0; j < m.cols; j++) {
for (int k = 0; k < a.cols; k++) {
m.data[i][j] += (a.data[i][k] * b.data[k][j]);
}
}
}
return m;
} else {
throw new IllegalArgumentException("Number of rows for left matrix and columns for right matrix must be equivalent.");
}
}
static Matrix sigmoidF(Matrix a) {
Matrix m = new Matrix(a.rows, a.cols);
for (int i = 0; i < a.rows; i++) {
for (int j = 0; j < a.cols; j++) {
m.data[i][j] = sigmoidF(a.data[i][j]);
}
}
return m;
}
Matrix sigmoidF() {
Matrix a = this;
for (int i = 0; i < a.rows; i++) {
for (int j = 0; j < a.cols; j++) {
a.data[i][j] = sigmoidF(a.data[i][j]);
}
}
return this;
}
static Matrix dSigmoidF(Matrix a) {
Matrix m = new Matrix(a.rows, a.cols);
for (int i = 0; i < a.rows; i++) {
for (int j = 0; j < a.cols; j++) {
m.data[i][j] = a.data[i][j] * (1 - a.data[i][j]);;
}
}
return m;
}
Matrix dSigmoidF() {
Matrix a = this;
for (int i = 0; i < a.rows; i++) {
for (int j = 0; j < a.cols; j++) {
a.data[i][j] = a.data[i][j] * (1 - a.data[i][j]);;
}
}
return a;
}
static Matrix transpose(Matrix a) {
Matrix m = new Matrix(a.cols, a.rows);
for (int i = 0; i < a.cols; i++) {
for (int j = 0; j < a.rows; j++) {
m.data[i][j] = a.data[j][i];
}
}
return m;
}
Matrix transpose() {
Matrix a = this;
for (int i = 0; i < a.cols; i++) {
for (int j = 0; j < a.rows; j++) {
a.data[i][j] = a.data[j][i];
}
}
return this;
}
void randomize(float s) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
data[i][j] = random(0, s);
}
}
}
void randomize(int s) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
data[i][j] = (float) Math.floor(random(0, s));
}
}
}
void randomize() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
data[i][j] = random(0, 1);
}
}
}
static float sigmoidF(float input) {
return 1/(1+exp(-input));
}
String toString() {
String s = new String();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
s += data[i][j] + " ";
}
s += "\n";
}
return s;
}
}