-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix.java
206 lines (157 loc) · 5.39 KB
/
Matrix.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
package org.adi.advanced.matrix;
import java.util.Random;
/******************************************************************************
* Compilation: javac Matrix.java
* Execution: java Matrix
*
* A bare-bones collection of static methods for manipulating
* matrices.
*
******************************************************************************/
/*
Input : mat[][] = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}}
Output : Row-wise: 1 2 3 4 5 6 7 8 9
Col-wise : 1 4 7 2 5 8 3 6 9
*/
public class Matrix {
// 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];
Random random = new Random();
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
a[i][j] = random.nextFloat();
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;
}
// return c = a + b
public static double[][] add(double[][] a, double[][] b) {
int m = a.length;
int n = a[0].length;
double[][] c = new double[m][n];
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
c[i][j] = a[i][j] + b[i][j];
return c;
}
// return c = a - b
public static double[][] subtract(double[][] a, double[][] b) {
int m = a.length;
int n = a[0].length;
double[][] c = new double[m][n];
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
c[i][j] = a[i][j] - b[i][j];
return c;
}
// return c = a * b
public static double[][] multiply(double[][] a, double[][] b) {
int m1 = a.length;
int n1 = a[0].length;
int m2 = b.length;
int n2 = b[0].length;
if (n1 != m2) throw new RuntimeException("Illegal matrix dimensions.");
double[][] c = new double[m1][n2];
for (int i = 0; i < m1; i++)
for (int j = 0; j < n2; j++)
for (int k = 0; k < n1; k++)
c[i][j] += a[i][k] * b[k][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]; // result
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;
}
public static String toCSV(String[] array) {
String result = "";
if (array.length > 0) {
StringBuilder sb = new StringBuilder();
for (String s : array) {
sb.append(s).append(",");
}
result = sb.deleteCharAt(sb.length() - 1).toString();
}
return result;
}
public String toMatrixMarket(NumberFormat formatter) {
StringBuilder out = new StringBuilder();
out.append("%%MatrixMarket matrix array real general\n");
out.append(rows).append(' ').append(columns).append('\n');
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
out.append(formatter.format(get(i, j))).append('\n');
}
}
return out.toString();
}
static void printMatrix(int[][] matrix) {
for (int[] line : matrix) {
int i = 0;
StringBuilder sb = new StringBuilder(matrix.length);
for (int number : line) {
if (i != 0) {
sb.append("\t");
} else {
i++;
}
sb.append(number);
}
System.out.println(sb.toString());
}
}
static void printMatrix(Matrix matrix, int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (j != 0) {
System.out.print("\t");
}
System.out.printf("%.0f", matrix.get(i, j));
}
System.out.println("");
}
}
}