-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.java
More file actions
269 lines (238 loc) · 7.22 KB
/
Matrix.java
File metadata and controls
269 lines (238 loc) · 7.22 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
public class Matrix {
private double[][] data;
private int cols;
private int rows;
public Matrix(int rows, int cols) {
this.rows = rows;
this.cols = cols;
this.data = new double[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
this.data[i][j] = 0;
}
}
}
public int getCols() {
return cols;
}
public int getRows() {
return rows;
}
public double[][] getData() {
return data;
}
public double getData(int x, int y) {
return data[y][x];
}
public void setData(double data, int x, int y) {
this.data[y][x] = data;
}
public void randomize() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
data[i][j] = (double) (Math.random() * 2 - 1);
}
}
}
public static Matrix transpose(Matrix m) {
Matrix result = new Matrix(m.cols, m.rows);
for (int i = 0; i < result.rows; i++) {
for (int j = 0; j < result.cols; j++) {
result.data[i][j] = m.data[j][i];
}
}
return result;
}
public void add(double n) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
data[i][j] += n;
}
}
}
public static Matrix subtract(Matrix m1, Matrix m2) {
if (m1.cols == m2.cols && m1.rows == m2.rows) {
Matrix result = new Matrix(m1.rows, m1.cols);
for (int i = 0; i < result.rows; i++) {
for (int j = 0; j < result.cols; j++) {
result.data[i][j] = m1.data[i][j] - m2.data[i][j];
}
}
return result;
}
return null;
}
public void add(Matrix m) {
if (this.cols == m.cols && this.rows == m.rows) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
data[i][j] += m.data[i][j];
}
}
}
}
public static Matrix add(Matrix m, int n) {
Matrix res = new Matrix(m.rows, m.cols);
for (int i = 0; i < m.rows; i++) {
for (int j = 0; j < m.cols; j++) {
res.data[i][j] = m.data[i][j] + n;
}
}
return res;
}
public void multiply(double n) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
data[i][j] *= n;
}
}
}
public static Matrix multiply(Matrix m, double n) {
Matrix result = new Matrix(m.rows, m.cols);
for (int i = 0; i < m.rows; i++) {
for (int j = 0; j < m.cols; j++) {
result.data[i][j] = m.data[i][j] * n;
}
}
return result;
}
public static Matrix add(Matrix a, Matrix b) {
Matrix res;
if (a.cols == b.cols && a.rows == b.rows) {
res = new Matrix(a.rows, a.cols);
for (int i = 0; i < a.rows; i++) {
for (int j = 0; j < a.cols; j++) {
res.data[i][j] = a.data[i][j] + b.data[i][j];
}
}
return res;
}
return null;
}
public static Matrix multiply(Matrix a, Matrix b) {
if (a.cols == b.rows) {
Matrix res = new Matrix(a.rows, b.cols);
for (int i = 0; i < a.rows; i++) {
for (int j = 0; j < b.cols; j++) {
double sum = 0;
for (int k = 0; k < a.cols; k++) {
sum += a.data[i][k] * b.data[k][j];
}
res.data[i][j] = sum;
}
}
return res;
}
return null;
}
public void hadamardMult(Matrix a) {
if (a.cols == this.cols && a.rows == this.rows) {
for (int i = 0; i < a.rows; i++) {
for (int j = 0; j < a.cols; j++) {
this.data[i][j] *= a.data[i][j];
}
}
}
}
public void elementwiseSqrt() {
for (int i = 0; i < this.rows; i++) {
for (int j = 0; j < this.cols; j++) {
this.data[i][j] = Math.sqrt(this.data[i][j]);
}
}
}
public void elementwisePower(int n) {
for (int i = 0; i < this.rows; i++) {
for (int j = 0; j < this.cols; j++) {
this.data[i][j] = Math.pow(this.data[i][j], n);
}
}
}
public static Matrix elementwisePower(Matrix m, int n) {
Matrix m2 = new Matrix(m.rows, m.cols);
for (int i = 0; i < m.rows; i++) {
for (int j = 0; j < m.cols; j++) {
m2.data[i][j] = Math.pow(m.data[i][j], n);
}
}
return m2;
}
public void elementwiseInverse() {
for (int i = 0; i < this.rows; i++) {
for (int j = 0; j < this.cols; j++) {
this.data[i][j] = 1.0 / this.data[i][j];
}
}
}
public static Matrix fromArray(double[] arr) {
Matrix m = new Matrix(arr.length, 1);
for (int i = 0; i < arr.length; i++) {
m.data[i][0] = arr[i];
}
return m;
}
public double[] toArray() {
double[] arr = new double[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;
}
public void sigmoid() {
for (int i = 0; i < this.rows; i++) {
for (int j = 0; j < this.cols; j++) {
this.data[i][j] = (double) (1 / (1 + Math.exp(-1 * this.data[i][j])));
}
}
}
public void dsigmoid() {
for (int i = 0; i < this.rows; i++) {
for (int j = 0; j < this.cols; j++) {
double sig = (1 / (1 + Math.exp(-1 * this.data[i][j])));
this.data[i][j] = sig * (1 - sig);
}
}
}
public static Matrix dsigmoid(Matrix m) {
Matrix temp = new Matrix(m.rows, m.cols);
for (int i = 0; i < m.rows; i++) {
for (int j = 0; j < m.cols; j++) {
temp.data[i][j] = m.data[i][j] * (1 - m.data[i][j]);
}
}
return temp;
}
public void softmax() {
if (this.cols != 1) return;
double sum = 0.0;
for (int i = 0; i < this.rows; i++) {
sum += this.data[i][0];
}
for (int i = 0; i < this.rows; i++) {
this.data[i][0] /= sum;
}
}
@Override
public String toString() {
String str = "";
for (int i = 0; i < this.rows; i++) {
for (int j = 0; j < this.cols; j++) {
str += this.data[i][j] + " ";
}
str += "\n";
}
return str;
}
@Override
public Matrix clone() {
Matrix n = new Matrix(this.rows, this.cols);
for (int i = 0; i < this.rows; i++) {
for (int j = 0; j < this.cols; j++) {
n.data = this.getData().clone();
}
}
return n;
}
}