-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix.pde
145 lines (123 loc) · 3.7 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
// https://github.com/CodingTrain/Toy-Neural-Network-JS/blob/master/lib/matrix.js
// https://introcs.cs.princeton.edu/java/95linear/Matrix.java.html
import java.util.function.Function;
class Matrix {
final int rows;
final int 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++) {
this.data[i][j] = 0;
}
}
}
Matrix copy() {
Matrix m = new Matrix(this.rows, this.cols);
for (int i = 0; i < this.rows; i++) {
for (int j = 0; j < this.cols; j++) {
m.data[i][j] = this.data[i][j];
}
}
return m;
}
// Should be static but Processing has a limit on this feature.
Matrix fromInputArray(float[] arr) {
return new Matrix(arr.length, 1).map((e, i, j) -> arr[i]);
}
Matrix fromArray(float[] arr) {
if (arr.length != this.rows*this.cols) throw new RuntimeException("Invalid size.");
Matrix m = new Matrix(this.rows, this.cols);
for (int i = 0; i < this.rows; i++) {
for (int j = 0; j < this.cols; j++) {
m.data[i][j] = arr[i*this.cols + j];
}
}
return m;
}
float[] toArray() {
float[] arr = new float[this.rows*this.cols];
for (int i = 0; i < this.rows; i++) {
for (int j = 0; j < this.cols; j++) {
arr[i*this.cols + j] = this.data[i][j];
}
}
return arr;
}
void randomize() {
this.selfMap((e) -> (float)(Math.random() * 2 - 1)); // [-1,1)
}
Matrix add(Matrix B) {
if (this.rows != B.rows || this.cols != B.cols) throw new RuntimeException("Invalid matrix dimensions.");
return this.map((e, i, j) -> e + B.data[i][j]);
}
// Slow implementation
Matrix multiply(Matrix B) {
if (this.cols != B.rows) throw new RuntimeException("Invalid matrix dimensions.");
Matrix C = new Matrix(this.rows, B.cols);
for (int i = 0; i < C.rows; i++) {
for (int j = 0; j < C.cols; j++) {
float dotProduct = 0;
for (int k = 0; k < this.cols; k++) {
dotProduct += this.data[i][k] * B.data[k][j];
}
C.data[i][j] = dotProduct;
}
}
return C;
}
Matrix map(TriFunction<Float, Integer, Integer, Float> func) {
Matrix C = new Matrix(this.rows, this.cols);
for (int i = 0; i < this.rows; i++) {
for (int j = 0; j < this.cols; j++) {
float val = this.data[i][j];
C.data[i][j] = func.apply(val, i, j);
}
}
return C;
}
Matrix map(Function<Float, Float> func) {
Matrix C = new Matrix(this.rows, this.cols);
for (int i = 0; i < this.rows; i++) {
for (int j = 0; j < this.cols; j++) {
float val = this.data[i][j];
C.data[i][j] = func.apply(val);
}
}
return C;
}
void selfMap(Function<Float, Float> func) {
for (int i = 0; i < this.rows; i++) {
for (int j = 0; j < this.cols; j++) {
float val = this.data[i][j];
this.data[i][j] = func.apply(val);
}
}
}
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Matrix m = (Matrix) o;
return rows == m.rows &&
cols == m.cols &&
Arrays.deepEquals(data, m.data);
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i = 0; i < this.rows; i++) {
if (i == 0) sb.append("[");
else sb.append(" [");
for (int j = 0; j < this.cols; j++) {
sb.append(this.data[i][j]);
if (j != this.cols-1) sb.append(", ");
}
if (i == this.rows - 1) sb.append("]]\n");
else sb.append("],\n");
}
return sb.toString();
}
}