回答編集履歴
1
multiplyメソッドを使うコードを追加
test
CHANGED
@@ -141,3 +141,165 @@
|
|
141
141
|
}
|
142
142
|
|
143
143
|
```
|
144
|
+
|
145
|
+
**追記**
|
146
|
+
|
147
|
+
行列の掛け算をコンストラクタで実行するのではなく、
|
148
|
+
|
149
|
+
a.multiply(b) で結果の Matrix を返してくれるようにしてみました。
|
150
|
+
|
151
|
+
```Java
|
152
|
+
|
153
|
+
import java.io.*;
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
public class Matrix {
|
158
|
+
|
159
|
+
private int row, col;
|
160
|
+
|
161
|
+
private double[][] matrix;
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
public Matrix() {}
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
public Matrix(int row, int col) {
|
170
|
+
|
171
|
+
this.row = row;
|
172
|
+
|
173
|
+
this.col = col;
|
174
|
+
|
175
|
+
matrix = new double[row][col];
|
176
|
+
|
177
|
+
}
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
public void read(String filename) {
|
182
|
+
|
183
|
+
try (BufferedReader br = new BufferedReader(new FileReader(filename))){
|
184
|
+
|
185
|
+
String[] rowcol = br.readLine().split(" ");
|
186
|
+
|
187
|
+
row = Integer.parseInt(rowcol[0]);
|
188
|
+
|
189
|
+
col = Integer.parseInt(rowcol[1]);
|
190
|
+
|
191
|
+
matrix = new double[row][col];
|
192
|
+
|
193
|
+
for (int m = 0; m < row; m++) {
|
194
|
+
|
195
|
+
String[] record = br.readLine().split(" ");
|
196
|
+
|
197
|
+
for (int n = 0; n < col; n++)
|
198
|
+
|
199
|
+
matrix[m][n] = Double.parseDouble(record[n]);
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
}
|
204
|
+
|
205
|
+
catch (FileNotFoundException e) {
|
206
|
+
|
207
|
+
e.printStackTrace();
|
208
|
+
|
209
|
+
}
|
210
|
+
|
211
|
+
catch (IOException e) {
|
212
|
+
|
213
|
+
e.printStackTrace();
|
214
|
+
|
215
|
+
}
|
216
|
+
|
217
|
+
catch (NumberFormatException e) {
|
218
|
+
|
219
|
+
e.printStackTrace();
|
220
|
+
|
221
|
+
}
|
222
|
+
|
223
|
+
}
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
public void print() {
|
228
|
+
|
229
|
+
for (int i = 0; i < row; i++) {
|
230
|
+
|
231
|
+
for (int j = 0; j < col; j++)
|
232
|
+
|
233
|
+
System.out.print(" " + matrix[i][j]);
|
234
|
+
|
235
|
+
System.out.println();
|
236
|
+
|
237
|
+
}
|
238
|
+
|
239
|
+
System.out.println();
|
240
|
+
|
241
|
+
}
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
public Matrix multiply(Matrix b) {
|
246
|
+
|
247
|
+
if (col != b.row) return null;
|
248
|
+
|
249
|
+
Matrix c = new Matrix(row, b.col);
|
250
|
+
|
251
|
+
for (int i = 0; i < row; i++)
|
252
|
+
|
253
|
+
for (int j = 0; j < col; j++) {
|
254
|
+
|
255
|
+
c.matrix[i][j] = 0;
|
256
|
+
|
257
|
+
for (int k = 0; k < b.row; k++)
|
258
|
+
|
259
|
+
c.matrix[i][j] += matrix[i][k] * b.matrix[k][j];
|
260
|
+
|
261
|
+
}
|
262
|
+
|
263
|
+
return c;
|
264
|
+
|
265
|
+
}
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
public static void main(String[] args){
|
270
|
+
|
271
|
+
if (args.length != 2) {
|
272
|
+
|
273
|
+
System.out.println("need two files");
|
274
|
+
|
275
|
+
System.exit(1);
|
276
|
+
|
277
|
+
}
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
Matrix a = new Matrix();
|
282
|
+
|
283
|
+
a.read(args[0]);
|
284
|
+
|
285
|
+
a.print();
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
Matrix b = new Matrix();
|
290
|
+
|
291
|
+
b.read(args[1]);
|
292
|
+
|
293
|
+
b.print();
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
Matrix c = a.multiply(b);
|
298
|
+
|
299
|
+
c.print();
|
300
|
+
|
301
|
+
}
|
302
|
+
|
303
|
+
}
|
304
|
+
|
305
|
+
```
|