/
MatrixUtilities.java
291 lines (261 loc) · 7.76 KB
/
MatrixUtilities.java
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
280
281
282
283
284
285
286
287
288
289
290
291
public class MatrixUtilities {
// return a random m-by-n matrix with values between 0 and 1
public static double[][] random(int m, int n) {
double[][] a = new double[m][n];
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
a[i][j] = StdRandom.uniform(0.0, 1.0);
return a;
}
// return n-by-n identity matrix I
public static double[][] identity(int n) {
double[][] a = new double[n][n];
for (int i = 0; i < n; i++)
a[i][i] = 1;
return a;
}
// return x^T y
public static double dot(double[] x, double[] y) {
if (x.length != y.length) throw new RuntimeException("Illegal vector dimensions.");
double sum = 0.0;
for (int i = 0; i < x.length; i++)
sum += x[i] * y[i];
return sum;
}
// return B = A^T
public static double[][] transpose(double[][] a) {
int m = a.length;
int n = a[0].length;
double[][] b = new double[n][m];
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
b[j][i] = a[i][j];
return b;
}
/* Function to compute transpose of a row vector*/
public static double[][] transposeV(double[] m)
{
double[][] temp = new double[m.length][1];
for (int i = 0; i < m.length; i++)
temp[i][0] = m[i];
return temp;
}
/* A function that add matrices elements -wise*/
public static double[][] add(double[][] A, double[][] B)
{
int aRows = A.length;
int aColumns = A[0].length;
int bRows = B.length;
int bColumns = B[0].length;
if (aColumns != bColumns||aRows != bRows)
{
throw new IllegalArgumentException("A:Rows: " + aColumns + " did not match B:Columns " + bRows + ".");
}
double[][] C = new double[aRows][bColumns];
for (int i = 0; i < aRows; i++)
{
for (int j = 0; j < bColumns; j++)
{
C[i][j] = 0.00000;
}
}
for (int i = 0; i < aRows; i++)
{ // aRow
for (int j = 0; j < bColumns; j++)
{ // bColumn
C[i][j]=A[i][j]+B[i][j];
}
}
return C;
}
/* A function that add subtract matrices elements -wise*/
public static double[][] subtract(double[][] A, double[][] B)
{
int aRows = A.length;
int aColumns = A[0].length;
int bRows = B.length;
int bColumns = B[0].length;
if (aColumns != bColumns||aRows != bRows)
{
throw new IllegalArgumentException("A:Rows: " + aColumns + " did not match B:Columns " + bRows + ".");
}
double[][] C = new double[aRows][bColumns];
for (int i = 0; i < aRows; i++)
{
for (int j = 0; j < bColumns; j++)
{
C[i][j] = 0.00000;
}
}
for (int i = 0; i < aRows; i++)
{ // aRow
for (int j = 0; j < bColumns; j++)
{ // bColumn
C[i][j]=A[i][j]-B[i][j];
}
}
return C;
}
// return c = a * b
public static double[][] multiply(double[][] matrixA, double[][] matrixB){
double[][] result = new double[matrixA.length][matrixB[0].length];
for(int i = 0; i<result.length; i++){
for (int j = 0; j<result[0].length; j++){
result[i][j] = 0;
}
}
if(matrixA[0].length != matrixB.length){
System.out.println("Error: matrices dimension don't match.");
}else{
for(int i = 0; i<matrixA.length; i++){
for(int j = 0; j<matrixB[0].length; j++){
for(int k = 0; k< matrixA[0].length; k++){
result[i][j] += matrixA[i][k] * matrixB[k][j];
}
}
}
}
return result;
}
/* Element wise_multiplication*/
public static double[][] MultiplyElementWise(double[][] A, double[][] B)
{
int aRows = A.length;
int aColumns = A[0].length;
int bRows = B.length;
int bColumns = B[0].length;
if (aColumns != bColumns||aRows != bRows)
{
throw new IllegalArgumentException("A:Rows: " + aColumns + " did not match B:Columns " + bRows + ".");
}
double[][] C = new double[aRows][bColumns];
for (int i = 0; i < aRows; i++)
{
for (int j = 0; j < bColumns; j++)
{
C[i][j] = 0.00000;
}
}
for (int i = 0; i < aRows; i++)
{ // aRow
for (int j = 0; j < bColumns; j++)
{ // bColumn
C[i][j]=A[i][j]*B[i][j];
}
}
return C;
}
// matrix-vector multiplication (y = A * x)
public static double[] multiply(double[][] a, double[] x) {
int m = a.length;
int n = a[0].length;
if (x.length != n) throw new RuntimeException("Illegal matrix dimensions.");
double[] y = new double[m];
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
y[i] += a[i][j] * x[j];
return y;
}
// vector-matrix multiplication (y = x^T A)
public static double[] multiply(double[] x, double[][] a) {
int m = a.length;
int n = a[0].length;
if (x.length != m) throw new RuntimeException("Illegal matrix dimensions.");
double[] y = new double[n];
for (int j = 0; j < n; j++)
for (int i = 0; i < m; i++)
y[j] += a[i][j] * x[i];
return y;
}
//Makes an array from a n by 1 matrix
public static double[] matrixToArray(double[][] matrix){
int length = matrix.length;
double[] arrayToOutput = new double[length];
for(int i=0; i<length; i++){
arrayToOutput[i] = matrix[i][0];
}
return arrayToOutput;
}
//Makes a n by 1 matrix from an array
public static double[][] arrayToMatrix(double[] array){
int length = array.length;
double[][] matrixToOutput = new double[length][1];
for(int i=0; i<length; i++){
matrixToOutput[i][0] = array[i];
}
return matrixToOutput;
}
/* Create a vector of ones*/
public static double[] createVectorOfOnes(int size)
{
double[] result= new double[size];
for(int i=0;i<size;i++)
{
result[i]=1;
}
return result;
}
/* Create matrix of Zeros*/
public static double[][] createMatrixZeros(int row,int column)
{
double[][] result= new double[row][column];
for(int i=0;i<row;i++)
{
for(int j=0;j<column;j++)
result[i][j]=0;
}
return result;
}
/*Create matrix of ones*/
public static double[][] createMatrixOnes(int row,int column)
{
double[][] result= new double[row][column];
for(int i=0;i<row;i++)
{
for(int j=0;j<column;j++)
result[i][j]=1;
}
return result;
}
// Print a matrix
public static void printMatrix(double[][] m)
{
try{
int rows = m.length;
int columns = m[0].length;
String str = "|\t";
for(int i=0;i<rows;i++){
for(int j=0;j<columns;j++){
str += m[i][j] + "\t";
}
System.out.println(str + "|");
str = "|\t";
}
System.out.println();
}
catch(Exception e){System.out.println("Matrix is empty!!");}
}
// print an array
public static void printArray(double[] array){
System.out.print("[");
for(int i = 0; i<array.length; i++)
{
System.out.print(array[i]+" | ");
}
System.out.print("]");
System.out.println();
}
/*
* Sigmoid Function
*/
public static double[] sigmoid(double[] x)
{
int length= x.length;
double[] result = new double[length];
for(int i=0;i<length;i++)
{
result[i]=(1/( 1 + Math.pow(Math.E,(-1*x[i]))));
}
return result;
}
}