-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix.java
105 lines (96 loc) · 3.24 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
/**
*
* @author Franco Hernández Victor Alfonso
*/
public class Matrix {
public static void show(double[][] A) {
for(int i=0; i<A.length; i++) {
System.out.print("[");
for(int j=0; j<A[0].length; j++) {
System.out.print(A[i][j]);
if(j+1 != A[0].length) System.out.print(", ");
}
System.out.println("]");
}
}
public static double[][] dot(double[][] A, double[][]B) {
int a_rows = A.length;
int a_cols = A[0].length;
int b_rows = B.length;
int b_cols = B[0].length;
if(a_cols != b_rows) {
System.err.println("Error");
System.exit(-1);
}
double[][] result = new double[a_rows][b_cols];
for(int i=0; i<a_rows; i++)
for(int j=0; j<b_cols; j++) {
result[i][j] = 0;
for(int k=0; k<a_cols; k++)
result[i][j] += A[i][k] * B[k][j];
}
return result;
}
public static double[][] hadamard(double[][] A, double[][]B) {
int a_rows = A.length;
int a_cols = A[0].length;
int b_rows = B.length;
int b_cols = B[0].length;
if(a_rows != b_rows && a_cols != b_cols) {
System.err.println("Error");
System.exit(-1);
}
double[][] result = new double[a_rows][b_cols];
for(int i=0; i<a_rows; i++)
for(int j=0; j<b_cols; j++)
result[i][j] = A[i][j] * B[i][j];
return result;
}
public static double[][] add(double[][] A, double[][] B) {
if(A.length != B.length && A[0].length != B[0].length) {
System.err.println("Error");
System.exit(-1);
}
double[][] result = new double[A.length][A[0].length];
for(int i=0; i<A.length; i++) {
for(int j=0; j<A[0].length; j++)
result[i][j] = A[i][j] + B[i][j];
}
return result;
}
public static double[][] sub(double[][] A, double[][] B) {
if(A.length != B.length && A[0].length != B[0].length) {
System.err.println("Error");
System.exit(-1);
}
double[][] result = new double[A.length][A[0].length];
for(int i=0; i<A.length; i++) {
for(int j=0; j<A[0].length; j++)
result[i][j] = A[i][j] - B[i][j];
}
return result;
}
public static double[][] scalarMul(double scalar, double[][] A) {
double[][] result = new double[A.length][A[0].length];
for(int i=0; i<A.length; i++) {
for(int j=0; j<A[0].length; j++)
result[i][j] = scalar * A[i][j];
}
return result;
}
public static double[][] transpose(double[][] A) {
double[][] result = new double[A[0].length][A.length];
for(int i=0; i<A[0].length; i++) {
for(int j=0; j<A.length; j++)
result[i][j] = A[j][i];
}
return result;
}
public static double[][] fill(int rows, int cols, double num) {
double[][] result = new double[rows][cols];
for(int i=0; i<rows; i++)
for(int j=0; j<cols; j++)
result[i][j] = num;
return result;
}
}