-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix.java
198 lines (148 loc) · 3.39 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
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
public class Matrix {
private int[][] A;
private int[] dimensions;
public Matrix(int[][] matrix) {
A = matrix;
dimensions = new int[2];
dimensions[0] = A.length; // # of rows
dimensions[1] = A[0].length; // # of columns
}
public int[] getDimensions() {
return dimensions;
}
public int[][] multiply(Matrix B) {
int[] dim = B.getDimensions();
if (dim[0] != dimensions[1] && dim[1] != dimensions[0]) {
System.out.println("Invalid matrix dimensions.");
System.out.println("A matrix dimensions -- Rows " + dimensions[0] + " Columns " + dimensions[1]);
System.out.println("B matrix dimensions -- Rows " + dim[0] + " Columns " + dim[1]);
return null;
}
else {
int[][] fin = new int[dimensions[0]][dim[1]];
System.out.println("Final dimensions: " + fin.length + " x " + fin[0].length);
for (int i = 0; i < fin[0].length; i++) { // i == B column
for (int j = 0; j < fin.length; j++) { // j == A row
int[] product = multiplyVectors(A, B.A, j, i);
int dot = sumArr(product);
fin[j][i] = dot;
}
}
return fin;
}
}
private int[] multiplyVectors(int[][] A, int[][] B, int row, int col) {
int[] r = A[row];
int[] c = new int[B.length];
for (int i = 0; i < c.length; i++) {
c[i] = B[i][col];
}
if (r.length != c.length) {
System.out.println("Row " + r.length + " Column " + c.length);
return null;
}
int[] ret = new int[r.length];
for (int i = 0; i < ret.length; i++) {
ret[i] = (r[i] * c[i]);
}
return ret;
}
private int sumArr(int[] arr) {
int sum = 0;
if (arr == null) {
return -1;
}
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}
public static void main(String args[]) {
/*
3x2
B == [[1, 2],
[3, 4],
[5, 6]]
2x3
A == [[1, 2, 3],
[4, 5, 6]]
== [[22, 30],
[49, 64]]
*/
// EXAMPLE #1
int[][] A, B;
A = new int[2][3];
B = new int[3][2];
A[0][0] = 1;
A[0][1] = 2;
A[0][2] = 3;
A[1][0] = 4;
A[1][1] = 5;
A[1][2] = 6;
B[0][0] = 1;
B[0][1] = 2;
B[1][0] = 3;
B[1][1] = 4;
B[2][0] = 5;
B[2][1] = 6;
Matrix A_matrix = new Matrix(A);
Matrix B_matrix = new Matrix(B);
int[][] fin = A_matrix.multiply(B_matrix);
for (int i = 0; i < fin.length; i++) {
for (int j = 0; j < fin[i].length; j++) {
System.out.print(fin[i][j] + " ");
}
System.out.println();
}
System.out.println();
// EXAMPLE #2
A = new int[2][3];
A[0][0] = 1;
A[0][1] = 1;
A[0][2] = 1;
A[1][0] = 2;
A[1][1] = 2;
A[1][2] = 2;
B = new int[3][2];
B[0][0] = 1;
B[0][1] = 1;
B[1][0] = 2;
B[1][1] = 2;
B[2][0] = 3;
B[2][1] = 3;
A_matrix = new Matrix(A);
B_matrix = new Matrix(B);
fin = B_matrix.multiply(A_matrix);
for (int i = 0; i < fin.length; i++) {
for (int j = 0; j < fin[i].length; j++) {
System.out.print(fin[i][j] + " ");
}
System.out.println();
}
System.out.println();
// EXAMPLE #3
A = new int[3][3];
A[0][0] = 6;
A[0][1] = 2;
A[0][2] = 4;
A[1][0] = -1;
A[1][1] = 4;
A[1][2] = 3;
A[2][0] = -2;
A[2][1] = 9;
A[2][2] = 3;
B = new int[3][1];
B[0][0] = 4;
B[1][0] = -2;
B[2][0] = 1;
A_matrix = new Matrix(A);
B_matrix = new Matrix(B);
fin = A_matrix.multiply(B_matrix);
for (int i = 0; i < fin.length; i++) {
for (int j = 0; j < fin[i].length; j++) {
System.out.print(fin[i][j] + " ");
}
System.out.println();
}
}
}