-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrixProduction.java
81 lines (71 loc) · 2.46 KB
/
MatrixProduction.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
import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
public class MatrixProduction {
public int[][] readMatrix(String path){
int [][] matrix;
try{
File file = new File(path);
InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "UTF-8");
BufferedReader read = new BufferedReader(isr);
String[] arr = read.readLine().split(" ");
matrix = new int[Integer.parseInt(arr[0])][Integer.parseInt(arr[1])];
int row = 0;
String line;
while((line = read.readLine())!=null){
String [] tem =line.split(" ");
for (int i=0; i<matrix[row].length;i++){
matrix[row][i] = Integer.parseInt(tem[i]);
}
row++;
}
return matrix;
}catch (Exception e) {
e.printStackTrace();
}
return null;
}
public boolean saveMatrix(String path){
return true;
}
public int[] getVector(int[][] matrix,int flag, int index){
if (flag == 0){
return matrix[index];
}else{
int [] ans = new int[matrix.length];
for (int i = 0; i<matrix.length;i++){
ans[i] = matrix[i][index];
}
return ans;
}
}
public int Doc(int[] vector1, int[] vector2){
int ans = 0;
for (int i=0; i< vector1.length;i++){
ans += vector1[i]*vector2[i];
}
return ans;
}
public int[][] production(int[][] matrixA, int[][] matrixB){
int [][] matrixC = new int[matrixA.length][matrixB[0].length];
for (int i = 0 ; i < matrixC.length; i++){
for (int j = 0; j < matrixC[i].length ; j++){
int[] vectorRow = getVector(matrixA,0,i);
int[] vectorCol = getVector(matrixB,0,j);
matrixC[i][j] = Doc(vectorRow,vectorCol);
}
}
return matrixC;
}
public static void main(String[]args){
String apath = "A.txt";
String bpath = "B.txt";
String savepath = "C.txt";
MatrixProduction mp = new MatrixProduction();
int [][] matrixA = mp.readMatrix(apath);
int [][] matrixB = mp.readMatrix(bpath);
int [][] matrixC = mp.production(matrixA,matrixB);
System.out.println(Arrays.deepToString(matrixC));
//boolean status = mp.saveMatrix(savepath);
}
}