-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.java
More file actions
196 lines (160 loc) · 5.75 KB
/
Matrix.java
File metadata and controls
196 lines (160 loc) · 5.75 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
import java.util.Random;
import java.lang.Math;
public class Matrix {
int rows, cols;
double[][] m;
String size;
Matrix(int rows, int cols){
this.rows = rows;
this.cols = cols;
this.m = new double[rows][cols];
for(int r = 0; r < this.rows; r++){
for(int c = 0; c < this.cols; c++){
this.m[r][c] = 0;
}
}
this.size = "[" + this.rows + ", " + this.cols + "]";
}
@Override
public String toString(){
String res = "";
for(int r = 0; r < this.rows; r++){
res += "[";
for(int c = 0; c < this.cols; c++){
if(c != this.cols - 1){
res += Double.toString(this.m[r][c]) + "\t";
}
else{
res += Double.toString(this.m[r][c]);
}
}
res += "]\n";
}
return res;
}
public void update(){
this.rows = this.m.length;
this.cols = this.m[0].length;
}
public static Matrix transpose(Matrix m){
Matrix res = new Matrix(m.cols, m.rows);
for(int i = 0; i < m.rows; i++){
for(int j = 0; j < m.cols; j++){
res.m[j][i] = m.m[i][j];
}
}
return res;
}
public void randomise(){
Random rn = new Random();
// rn.setSeed(0);
for(int r = 0; r < this.rows; r++){
for(int c = 0; c < this.cols; c++){
double res = rn.nextDouble();
this.m[r][c] = res;
}
}
}
public static Matrix add(Matrix a, Matrix b){
Matrix res = new Matrix(a.rows, a.cols);
for(int i = 0; i < a.rows; i++){
for(int j = 0; j < a.cols; j++){
res.m[i][j] = a.m[i][j] + b.m[i][j];
}
}
return res;
}
public static Matrix sub(Matrix a, Matrix b){
Matrix res = new Matrix(a.rows, a.cols);
for(int i = 0; i < a.rows; i++){
for(int j = 0; j < a.cols; j++){
res.m[i][j] = a.m[i][j] - b.m[i][j];
}
}
return res;
}
public static Matrix scMul(Matrix a, double n){
Matrix res = new Matrix(a.rows, a.cols);
for(int i = 0; i < a.rows; i++){
for(int j = 0; j < a.cols; j++){
res.m[i][j] = a.m[i][j] * n;
}
}
return res;
}
public static Matrix dot(Matrix a, Matrix b){
if(a.cols != b.rows){
System.out.println("Cannot multiply [" + Integer.toString(a.rows) + ", " + Integer.toString(a.cols) + "] with " + "[" + Integer.toString(b.rows) + ", " + Integer.toString(b.cols) + "]");
return new Matrix(1,1);
}
else{
Matrix res = new Matrix(a.rows, b.cols);
for(int i = 0; i < res.rows; i++){
for(int j = 0; j < res.cols; j++){
double sum = 0;
for(int k = 0; k < a.cols; k++){
sum += a.m[i][k] * b.m[k][j];
}
res.m[i][j] = sum;
}
}
return res;
}
}
public static Matrix sigmoid(Matrix m, boolean deriv){
Matrix res = new Matrix(m.rows, m.cols);
for(int i = 0; i < m.rows; i++){
for(int j = 0; j < m.cols; j++){
if(!deriv){
res.m[i][j] = 1/(1 + Math.pow(Math.E, - 1 * m.m[i][j]));
}
else{
res.m[i][j] = (1 - m.m[i][j]) * m.m[i][j];
}
}
}
return res;
}
public static Matrix tanh(Matrix m, boolean deriv){
Matrix res = new Matrix(m.rows, m.cols);
for(int i = 0; i < m.rows; i++){
for(int j = 0; j < m.cols; j++){
if(!deriv){
res.m[i][j] = Math.tanh(m.m[i][j]);
}
else{
res.m[i][j] = (1 - m.m[i][j] * m.m[i][j]);
}
}
}
return res;
}
public static Matrix RelU(Matrix m, boolean deriv){
Matrix res = new Matrix(m.rows, m.cols);
for(int i = 0; i < m.rows; i++){
for(int j = 0; j < m.cols; j++){
if(!deriv){
res.m[i][j] = Math.max(0, m.m[i][j]);
}
else{
if(m.m[i][j] >= 0){
res.m[i][j] = 1;
}
else{
res.m[i][j] = 0;
}
}
}
}
return res;
}
public static Matrix schur(Matrix a, Matrix b){
Matrix res = new Matrix(a.rows, a.cols);
for(int i = 0; i < a.rows; i++){
for(int j = 0; j < a.cols; j++){
res.m[i][j] = a.m[i][j] * b.m[i][j];
}
}
return res;
}
}