-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.java
More file actions
203 lines (172 loc) · 6.79 KB
/
Matrix.java
File metadata and controls
203 lines (172 loc) · 6.79 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
/* *****************************************************************************
* Compilation: javac Matrix.java
* Execution: java Matrix file1.txt file2.txt
* Dependencies: In.java StdOut.java
*
* % java Matrix matrix1.txt matrix2.txt
*
**************************************************************************** */
import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.StdOut;
public class Matrix {
public static void main(String[] args) {
// unit test for vector x vector
// String[] data1 = new In(args[0]).readAllStrings();
// String[] data2 = new In(args[1]).readAllStrings();
// int N = Integer.parseInt(data1[0]);
// int M = Integer.parseInt(data2[0]);
//
// if (N != M) throw new IllegalArgumentException("Two vectors must have equal sizes.");
//
// double[] firstVector = new double[N];
// for (int i = 0; i < N; i++)
// firstVector[i] = Double.parseDouble(data1[i + 1]);
//
// double[] secondVector = new double[N];
// for (int i = 0; i < N; i++)
// secondVector[i] = Double.parseDouble(data2[i + 1]);
//
// StdOut.println(dot(firstVector, secondVector));
// unit test for matrix x matrix
// String[] data1 = new In(args[0]).readAllStrings();
// String[] data2 = new In(args[1]).readAllStrings();
//
// int N1 = Integer.parseInt(data1[0]);
// int M1 = Integer.parseInt(data1[1]);
// int N2 = Integer.parseInt(data2[0]);
// int M2 = Integer.parseInt(data2[1]);
//
// if (M1 != N2) throw new IllegalArgumentException("Two matrices don`t match");
//
// double[][] firstMatrix = new double[N1][M1];
// for (int i = 0; i < N1; i++)
// for (int j = 0; j < M1; j++)
// firstMatrix[i][j] = Double.parseDouble(data1[i * M1 + j + 2]);
//
// double[][] secondMatrix = new double[N2][M2];
// for (int i = 0; i < N2; i++)
// for (int j = 0; j < M2; j++)
// secondMatrix[i][j] = Double.parseDouble(data2[i * M2 + j + 2]);
//
// double[][] result = mult(firstMatrix, secondMatrix);
//
// for (int i = 0; i < result.length; i++)
// for (int j = 0; j < result[0].length; j++) {
// StdOut.print(result[i][j] + " ");
// if (j == result[0].length - 1)
// StdOut.println();
// }
// unit test for transpose
// String[] data = new In(args[0]).readAllStrings();
//
// int N = Integer.parseInt(data[0]);
// int M = Integer.parseInt(data[1]);
// double[][] matrix = new double[N][M];
// for (int i = 0; i < N; i++)
// for (int j = 0; j < M; j++)
// matrix[i][j] = Double.parseDouble(data[i * M + j + 2]);
//
// double[][] result = transpose(matrix);
// for (int i = 0; i < M; i++)
// for (int j = 0; j < N; j++) {
// StdOut.print(result[i][j] + " ");
// if (j == N - 1)
// StdOut.println();
// }
// unit test for matrix x vector
// String[] data1 = new In(args[0]).readAllStrings();
// String[] data2 = new In(args[1]).readAllStrings();
//
// int N1 = Integer.parseInt(data1[0]);
// int M1 = Integer.parseInt(data1[1]);
// int N2 = Integer.parseInt(data2[0]);
//
// if (M1 != N2) throw new IllegalArgumentException("com.lixing.algorithm.matrix.Matrix and vector are not matched");
//
// double[][] matrix = new double[N1][M1];
// for (int i = 0; i < N1; i++)
// for (int j = 0; j < M1; j++)
// matrix[i][j] = Double.parseDouble(data1[i * M1 + j + 2]);
//
// double[] vector = new double[N2];
// for (int i = 0; i < N2; i++)
// vector[i] = Double.parseDouble(data2[i + 1]);
//
// double[] result = mult(matrix, vector);
// for (int i = 0; i < result.length; i++)
// StdOut.println(result[i]);
// unit test for vector x matrix
String[] data1 = new In(args[0]).readAllStrings();
String[] data2 = new In(args[1]).readAllStrings();
int M1 = Integer.parseInt(data1[0]);
int N2 = Integer.parseInt(data2[0]);
int M2 = Integer.parseInt(data2[1]);
if (M1 != N2) throw new IllegalArgumentException("Vector and matrix don`t match");
double[] vector = new double[M1];
for (int i = 0; i < M1; i++)
vector[i] = Double.parseDouble(data1[i + 1]);
double[][] matrix = new double[N2][M2];
for (int i = 0; i < N2; i++)
for (int j = 0; j < M2; j++)
matrix[i][j] = Double.parseDouble(data2[i * M2 + j + 2]);
double[] result = mult(vector, matrix);
for (int i = 0; i < M2; i++) {
StdOut.print(result[i] + " ");
if (i == M2 - 1)
StdOut.println();
}
}
// dot product of two vectors
public static double dot(double[] a, double[] b) {
double result = 0.0;
for (int i = 0; i < a.length; i++) {
result += a[i] * b[i];
}
return result;
}
// multiplication between two matrices
public static double[][] mult(double[][] a, double[][] b) {
int N = a.length;
int M = b[0].length;
int K = a[0].length;
double[][] result = new double[N][M];
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
for (int k = 0; k < K; k++) {
result[i][j] += a[i][k] * b[k][j];
}
}
}
return result;
}
// transpose of matrix
public static double[][] transpose(double[][] a) {
int N = a.length;
int M = a[0].length;
double[][] result = new double[M][N];
for (int i = 0; i < M; i++)
for (int j = 0; j < N; j++)
result[i][j] = a[j][i];
return result;
}
// multiplication between matrix and vector
public static double[] mult(double[][] a, double[] b) {
int N = a.length;
int M = a[0].length;
double[] result = new double[N];
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
result[i] += a[i][j] * b[j];
return result;
}
// multiplication between vector and matrix
public static double[] mult(double[] a, double[][] b) {
int M1 = a.length;
int M2 = b[0].length;
double[] result = new double[M1];
for (int i = 0; i < M2; i++)
for (int j = 0; j < M1; j++)
result[i] += a[j] * b[j][i];
return result;
}
}