-
Notifications
You must be signed in to change notification settings - Fork 1
/
Matrix.pde
106 lines (92 loc) · 1.99 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
class Matrix {
// Implementation von Matrizen
int rows;
int cols;
float[][] m;
Matrix(int r, int c) {
rows = r;
cols = c;
m = new float[r][c];
}
void mult(Matrix m1, Matrix m2) {
assert(m1.cols == m2.rows);
for(int i=0; i<m1.rows; i++){
for(int j=0; j<m2.cols; j++){
this.set(i, j, mult(m1.getRow(i), m2.getCol(j)));
}
}
}
float mult(float[] i, float[] j) {
float result = 0;
for (int x = 0; x < i.length; x++) {
result += i[x] * j[x];
}
return result;
}
float get(int r, int c) {
return m[r][c];
}
void set(int r, int c, float v) {
m[r][c] = v;
}
void set(float[][] v) {
m = v;
}
void setRandom(float v1, float v2){
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
m[i][j] = random(v1,v2);
}
}
}
void printMatrix(){
print("[");
for(int i=0; i<this.rows; i++){
if(!(i==0)){
print(" [");
} else {
print("[");
}
for(int j=0; j<this.cols; j++){
print(" " + m[i][j] + " ");
}
if(!(i==this.rows-1)){
println("]");
} else {
print("]");
}
}
println("]");
}
void copyM(Matrix m2){
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
m[i][j] = m2.m[i][j];
}
}
}
float[] getRow(int r) {
return m[r];
}
float[] getCol(int j) {
float[] result = new float[rows];
for (int i=0; i<rows; i++) {
result[i] = m[i][j];
}
return result;
}
void sigmoid() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
m[i][j] = Activationfunction.sigmoid(m[i][j]);
}
}
}
void tanh() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
m[i][j] = Activationfunction.tanh(m[i][j]);
}
}
}
}