-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.pde
More file actions
171 lines (156 loc) · 3.62 KB
/
Matrix.pde
File metadata and controls
171 lines (156 loc) · 3.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
float sigmoid(float x) {
return 1/(1+exp(-x));
}
Matrix toMatrix(float[] inputs) {
Matrix ip = new Matrix(inputs.length, 1);
for (int i = 0; i < ip.rows; i++) {
ip.mat[i][0] = inputs[i];
}
return ip;
}
float dsigmoid(float ysigmoided) {
return ysigmoided * (1 - ysigmoided);
}
class Matrix implements Serializable {
int rows, cols;
float[][] mat;
Matrix(int r_, int c_) {
rows = r_;
cols = c_;
mat = new float[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
mat[i][j] = 0;
}
}
}
void applySig() {
for (int i = 0; i < rows; i++) {
for (int j =0; j < cols; j++) {
mat[i][j] = sigmoid(mat[i][j]);
}
}
}
void applyDSig() {
for (int i = 0; i < rows; i++) {
for (int j =0; j < cols; j++) {
mat[i][j] = dsigmoid(mat[i][j]);
}
}
}
float[] toArray() {
int count = 0;
float[] a = new float[rows * cols];
for (int i = 0; i < rows; i++) {
for (int j =0; j < cols; j++) {
a[count] = mat[i][j];
count++;
}
}
return a;
}
void matMult(float n) {
for (int i = 0; i < rows; i++) {
for (int j =0; j < cols; j++) {
mat[i][j] *= n;
}
}
}
Matrix hadMult(Matrix m) {
Matrix res = new Matrix(rows, cols);
for (int i = 0; i < rows; i++) {
for (int j =0; j < cols; j++) {
res.mat[i][j] = mat[i][j] * m.mat[i][j];
}
}
return res;
}
void matAdd(float n) {
for (int i = 0; i < rows; i++) {
for (int j =0; j < cols; j++) {
mat[i][j] += n;
}
}
}
Matrix matSub(Matrix b) {
Matrix tmp = new Matrix(rows, cols);
for (int i = 0; i < rows; i++) {
for (int j =0; j < cols; j++) {
tmp.mat[i][j] = mat[i][j] - b.mat[i][j];
}
}
return tmp;
}
Matrix matMult(Matrix n) {
if (n.rows == cols) {
Matrix res = new Matrix(rows, n.cols);
for (int i = 0; i < res.rows; i++) {
for (int j = 0; j < res.cols; j++) {
float sum = 0;
for (int k = 0; k < n.rows; k++) {
sum += mat[i][k] * n.mat[k][j];
}
res.mat[i][j] = sum;
}
}
return res;
} else {
println("Cant multiply!!!");
return this;
}
}
Matrix matTrans() {
Matrix result = new Matrix(cols, rows);
for (int i = 0; i < rows; i++) {
for (int j =0; j < cols; j++) {
result.mat[j][i] = mat[i][j];
}
}
return result;
}
void matAdd(Matrix n) {
if (n.rows == rows && n.cols == cols) {
for (int i = 0; i < rows; i++) {
for (int j =0; j < cols; j++) {
mat[i][j] += n.mat[i][j];
}
}
}
}
void matRand() {
for (int i = 0; i < rows; i++) {
for (int j =0; j < cols; j++) {
mat[i][j] = (float) Math.random() * 2 - 1;
}
}
}
void printMat() {
for (int i = 0; i < rows; i++) {
for (int j =0; j < cols; j++) {
print(mat[i][j]);
print(" ");
}
println();
}
println();
}
void mutateMat(float mr) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (random(1) > mr) {
mat[i][j] += randomGaussian() * 0.1;
}
}
}
}
Matrix copy()
{
Matrix result = new Matrix(rows, cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result.mat[i][j] = mat[i][j];
}
}
return result;
}
}