-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixHelper.java
More file actions
279 lines (251 loc) · 7.29 KB
/
MatrixHelper.java
File metadata and controls
279 lines (251 loc) · 7.29 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
270
271
272
273
274
275
276
277
278
279
public float[][] matmul(float[][] m1, float[][] m2) {
int[] m1dim = {m1.length, m1[0].length};
int[] m2dim = {m2.length, m2[0].length};
if (m1dim[1] != m2dim[0]) {
//If the number of columns in m1 != the number of rows in m2
return null;
}
float[][] result = new float[m1dim[0]][m2dim[1]];
for (int m1_row_index = 0; m1_row_index < m1dim[0]; m1_row_index++) {
for (int m2_column_index = 0; m2_column_index < m2dim[1]; m2_column_index++) {
//we will place the result at result[row][column]
Vector m1_row = new Vector(m1[m1_row_index]);
Vector m2_column = columnFromMatrix(m2, m2_column_index);
float value = dotProduct(m1_row, m2_column);
result[m1_row_index][m2_column_index] = value;
}
}
return result;
}
public Vector matmul(float[][] m1, Vector vec) {
float[][] columnVector = matmul(m1, vectorToColumnMatrix(vec));
Vector ret = columnFromMatrix(columnVector, 0);
return ret;
}
//This is the same thing as a (Nx1) multiplied by (1xN) vector
public float[][] matmul(Vector vec1, Vector vec2) {
float[] v1 = vec1.toArray();
float[] v2 = vec2.toArray();
int M = v1.length;
int N = v2.length;
float[][] result = new float[M][N];
for (int m = 0; m < M; m++) {
float constant = v1[m];
float[] row = multiplyByConstant(v2, constant);
result[m] = row;
}
return result;
}
public String vecToString(Vector vec) {
float[] v = vec.toArray();
String ret = "";
int max_num_spaces = 0;
for (float value : v) {
if (Float.toString(value).length() > max_num_spaces) {
max_num_spaces = Float.toString(value).length();
}
}
for (float value : v) {
int min_num_spaces = Float.toString(value).length();
String spacing = "";
for (int i = 0; i < max_num_spaces - min_num_spaces; i++) {
spacing += " ";
}
ret += "[" + spacing + Float.toString(value) + "]";
}
return ret;
}
public String matToString(float[][] matrix) {
String ret = "";
int max_num_spaces = 0;
for (float[] line : matrix) {
for (float value : line) {
if (Float.toString(value).length() > max_num_spaces) {
max_num_spaces = Float.toString(value).length();
}
}
}
for (float[] line : matrix) {
String line_text = "";
for (float value : line) {
int min_num_spaces = Float.toString(value).length();
String spacing = "";
for (int i = 0; i < max_num_spaces - min_num_spaces; i++) {
spacing += " ";
}
line_text += "[" + spacing + Float.toString(value) + "]";
}
ret += line_text + "\n";
}
return ret;
}
public float[] hadamard(float[] v1, float[] v2) {
if (v1.length != v2.length) {
return null;
}
float[] ret = new float[v1.length];
for (int i = 0; i < v1.length; i++) {
ret[i] = v1[i] * v2[i];
}
return ret;
}
public Vector hadamard(Vector v1, Vector v2) {
Vector ret = new Vector(hadamard(v1.toArray(), v2.toArray()));
return ret;
}
public float[][] hadamard(float[][] m1, float[][] m2) {
if (m1.length != m2.length || m1[0].length != m2[0].length) {
return null;
}
float[][] ret = new float[m1.length][m1[0].length];
for (int m = 0; m < m1.length; m++) {
for (int n = 0; n < m1[0].length; n++) {
ret[m][n] = m1[m][n] * m2[m][n];
}
}
return ret;
}
public float[] subtraction(float[] v1, float[] v2) {
float[] v2_neg = multiplyByConstant(v2, -1);
float[] result = addition(v1, v2_neg);
return result;
}
public Vector subtraction(Vector v1, Vector v2) {
float[] result = subtraction(v1.toArray(), v2.toArray());
Vector ret = new Vector(result);
return ret;
}
public float[][] subtraction(float[][] m1, float[][] m2) {
float[][] m2_neg = multiplyByConstant(m2, -1);
float[][] result = addition(m1, m2_neg);
return result;
}
public float[][] addition(float[][] m1, float[][] m2) {
if (m1.length != m2.length || m1[0].length != m2[0].length) {
return null;
}
float[][] ret = new float[m1.length][m1[0].length];
for (int m = 0; m < m1.length; m++) {
for (int n = 0; n < m1[0].length; n++) {
ret[m][n] = m1[m][n] + m2[m][n];
}
}
return ret;
}
public float[] addition(float[] v1, float[] v2) {
if (v1.length != v2.length) {
return null;
}
float[] ret = new float[v1.length];
for (int i = 0; i < v1.length; i++) {
ret[i] = v1[i] + v2[i];
}
return ret;
}
public float magnitude(float[] v) {
float sum = 0;
for (float value : v) {
sum += value * value;
}
float magnitude = sqrt(sum);
return magnitude;
}
public float magnitude(Vector v) {
return magnitude(v.toArray());
}
public Vector addition(Vector v1, Vector v2) {
Vector ret = new Vector(addition(v1.toArray(), v2.toArray()));
return ret;
}
public float[][] vectorToColumnMatrix(Vector vec) {
float[] values = vec.toArray();
float[][] colmat = new float[values.length][1];
for (int i = 0; i < values.length; i++) {
colmat[i][0] = values[i];
}
return colmat;
}
public float[][] multiplyByConstant(float[][] matrix, float constant) {
float[][] new_matrix = new float[matrix.length][matrix[0].length];
for (int m = 0; m < matrix.length; m++) {
for (int n = 0; n < matrix[0].length; n++) {
new_matrix[m][n] = matrix[m][n] * constant;
}
}
return new_matrix;
}
public Vector multiplyByConstant(Vector vec, float constant) {
float[] result = multiplyByConstant(vec.toArray(), constant);
Vector ret = new Vector(result);
return ret;
}
public float[] multiplyByConstant(float[] v, float constant) {
float[] result = new float[v.length];
for (int i = 0; i < v.length; i++) {
result[i] = v[i] * constant;
}
return result;
}
public float[][] transpose(float[][] matrix) {
int m = matrix.length;
int n = matrix[0].length;
float[][] transposed = new float[n][m];
for (int x = 0; x < n; x++) {
for (int y = 0; y < m; y++) {
transposed[x][y] = matrix[y][x];
}
}
return transposed;
}
public float[] toExponent(float[] v, float exponent) {
float[] ret = new float[v.length];
for (int i = 0; i < v.length; i++) {
ret[i] = pow(v[i], exponent);
}
return ret;
}
public Vector toExponent(Vector vec, float exponent) {
float[] result = toExponent(vec.toArray(), exponent);
Vector ret = new Vector(result);
return ret;
}
//Takes each value as an exponent to the constant given
//i.e. (constant)^(vector)
public Vector asExponentTo(float constant, Vector vec) {
float[] result = asExponentTo(constant, vec.toArray());
Vector ret = new Vector(result);
return ret;
}
//Takes each value as an exponent to the constant given
//i.e. (constant)^(vector)
public float[] asExponentTo(float constant, float[] v) {
float[] ret = new float[v.length];
for (int i = 0; i < v.length; i++) {
ret[i] = pow(constant, v[i]);
}
return ret;
}
public Vector columnFromMatrix(float[][] matrix, int column_index) {
if (column_index >= matrix[0].length || column_index < 0) {
return null;
}
float[] column = new float[matrix.length];
for (int i = 0; i < matrix.length; i++) {
column[i] = matrix[i][column_index];
}
Vector ret = new Vector(column);
return ret;
}
public float dotProduct(float[] v1, float[] v2) {
if (v1.length != v2.length) {
return -99999999;
}
float sum = 0;
for (int i = 0; i < v1.length; i++) {
sum += v1[i] * v2[i];
}
return sum;
}
public float dotProduct(Vector v1, Vector v2) {
return dotProduct(v1.toArray(), v2.toArray());
}