-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.java
More file actions
77 lines (61 loc) · 1.98 KB
/
Matrix.java
File metadata and controls
77 lines (61 loc) · 1.98 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
public class Matrix {
public static void main(String[] args) {
StdOut.println("Matrix");
}
public static double dot(double [] a, double [] b) {
double sum = 0;
for (int i = 0; i < a.length; i++) {
sum += a[i] * b[i];
}
return sum;
}
public static double[][] multiply(double[][] a, double[][] b) {
double[][] c = new double[a.length][b[0].length];
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < b[i].length; j++) {
// calculate c[i][j]
double sum = 0;
for (int m = 0; m < a[i].length;) {
sum += a[i][m] * b[m][j];
}
c[i][j] = sum;
}
}
return c;
}
// public static double[] multiply(double[] a, double[] b) {
// int m = a.length();
// int n = b.length();
// double[][] c = new double[x][y];
// for (int i = 0; i < m; i++) {
// // calculate c[i][j]
// double sum = 0;
// for (int j = 0; j < a[i].length; j++) {
// sum += a[i][x] * b[x];
// }
// c[i] = sum;
// }
// return c;
// }
// public static double [][] transpose(double [][] a) {
// int m = a.length;
// int n = a[0].length;
// double [][] b = new double[m][n];
// for (int i = 0; i < m; i++) {
// for (int j = 0; j < n; j++) {
// b[i][j] = a[j][i];
// }
// }
// }
// public static void [][] transposeInPlace(double [][] a) {
// int m = a.length;
// int n = a[0].length;
// double [][] b = new double[m][n];
// for (int i = 0; i < m; i++) {
// for (int j = 0; j < n; j++) {
// double temp = a[j][i];
// a[j][i] = a[j][i];
// }
// }
// }
}