Repository navigation
Expand file tree
/
Copy pathMatrix.java
More file actions
379 lines (343 loc) · 9.89 KB
/
Copy pathMatrix.java
File metadata and controls
379 lines (343 loc) · 9.89 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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
* Amer Hesson
* University of Toronto
*
* Sept. 26, 2012
*
* Class Matrix creates a 2 dimensional array of values that can be manipulated
* in useful ways, including elementary row operations and transforming the
* matrix to reduced row echelon form.
*/
import java.text.DecimalFormat;
import java.awt.Point;
import java.util.*;
public class Matrix {
private int numRows;
private int numCols;
private int rrefPivotRow;
private double[][] entries;
public Matrix(int numRows, int numCols, double[][] entries) {
this.numRows = numRows;
this.numCols = numCols;
this.entries = new double[numRows][numCols];
for (int m = 0; m < numRows; m++) {
for (int n = 0; n < numCols; n++) {
this.entries[m][n] = entries[m][n];
}
}
this.rrefPivotRow = -1;
}
public Matrix(Matrix other) {
this.numRows = other.numRows;
this.numCols = other.numCols;
this.entries = new double[numRows][numCols];
for (int m = 0; m < numRows; m++) {
for (int n = 0; n < numCols; n++) {
this.entries[m][n] = other.entries[m][n];
}
}
this.rrefPivotRow = -1;
}
public Matrix reduceRowEchelon() {
if (this.isReduced()) {
return this;
} else {
adjustEntries();
//System.out.println(this);
Point pivot = this.getPivot();
if (pivot != null) {
int pivotRow = (int)pivot.getX();
int pivotCol = (int)pivot.getY();
double pivotEntry = entries[pivotRow][pivotCol];
if (pivotRow != 0 && this.isZeroRow(pivotRow - 1)) {
this.exchange(pivotRow, pivotRow - 1);
}
this.multiplyRow(pivotRow, 1/pivotEntry);
//System.out.println(this);
for (int m = 0; m < numRows; m++) {
if (m != pivotRow) {
this.addMultiple(m, pivotRow, -entries[m][pivotCol]);
//System.out.println(this);
}
}
} else {
return this;
}
}
return this.reduceRowEchelon();
}
public boolean isReduced() {
double[] firstNonZeroEntry = new double[numRows];
int[] firstNonZeroEntryCol = new int[numRows];
for (int m = 0; m < numRows; m++) {
firstNonZeroEntry[m] = 0;
}
for (int m = 0; m < numRows; m++) {
for (int n = 0; n < numCols; n++) {
if (entries[m][n] != 0) {
firstNonZeroEntry[m] = entries[m][n];
firstNonZeroEntryCol[m] = n;
break;
}
}
}
//Check that all zero rows are at the end, and all leading ones are to the right of the leading ones in the rows above it.
boolean isAfterZeroRow = false;
int prevLeadingOneEntryCol = -1;
for (int m = 0; m < numRows; m++) {
int curNonZeroEntryCol = firstNonZeroEntryCol[m];
if (firstNonZeroEntry[m] == 0) {
isAfterZeroRow = true;
} else if (firstNonZeroEntry[m] != 1 || isAfterZeroRow || prevLeadingOneEntryCol >= curNonZeroEntryCol) {
return false;
}
//Check that non-zero entry is the only such entry in its column.
if (firstNonZeroEntry[m] == 1) {
for (int r = 0; r < numRows; r++) {
if (r != m && entries[r][curNonZeroEntryCol] != 0) {
return false;
}
}
prevLeadingOneEntryCol = firstNonZeroEntryCol[m];
}
}
return true;
}
private boolean isValidPivot(int row, int col) {
boolean isReducedCol = isReducedCol(col);
if (entries[row][col] == 0) {
return false;
}
if (isReducedCol) {
reorderReducedCol(col);
rrefPivotRow = row;
return false;
}
//if (entries[row][col] == 0) {
// return false;
//}
for (int n = 0; n < col; n++) {
if (isReducedCol(n) && entries[row][n] == 1) {
return false;
}
}
if (rrefPivotRow + 1 < numRows) {
this.exchange(row, rrefPivotRow + 1);
}
rrefPivotRow = row;
return true;
}
private boolean isReducedCol(int col) {
boolean existsOneInCol = false;
boolean isReducedCol = false;
for (int m = 0; m < numRows; m++) {
double curEntry = entries[m][col];
if (curEntry == 1 && existsOneInCol) {
return false;
} else if (curEntry == 1) {
existsOneInCol = true;
} else if (curEntry != 0) {
return false;
}
}
return true;
}
private boolean isZeroRow(int row) {
for (int n = 0; n < numCols; n++) {
if (entries[row][n] != 0) {
return false;
}
}
return true;
}
private Point getPivot() {
for (int n = 0; n < numCols; n++) {
for (int m = 0; m < numRows; m++) {
if (isValidPivot(m, n)) {
double curEntry = entries[m][n];
if (curEntry != 0) {
return new Point(m, n);
}
}
}
}
return null;
}
private void reorderReducedCol(int col) {
int oneRowNum = -1;
for (int m = 0; m < numRows; m++) {
if (entries[m][col] == 1) {
oneRowNum = m;
}
}
for (int m = 0; m < numRows; m++) {
for (int n = 0; n < numCols; n++) {
if (entries[m][n] == 1 && isReducedCol(n)) {
if (n > col && m < oneRowNum) {
this.exchange(m, oneRowNum);
}
}
}
}
}
private void exchange(int row1, int row2) {
double[] bufferRow = new double[numCols];
for (int n = 0; n < numCols; n++) {
bufferRow[n] = entries[row1][n];
entries[row1][n] = entries[row2][n];
entries[row2][n] = bufferRow[n];
}
}
private void multiplyRow(int row, double factor) {
for (int n = 0; n < numCols; n++) {
entries[row][n] *= factor;
}
}
//row1 = row1 + factor*row2
private void addMultiple(int row1, int row2, double factor) {
for (int n = 0; n < numCols; n++) {
entries[row1][n] += factor * entries[row2][n];
}
}
private void adjustEntries() {
for (int m = 0; m < numRows; m++) {
for (int n = 0; n < numCols; n++) {
if (Math.abs(entries[m][n]) <= 0.00001) {
entries[m][n] = 0;
}
}
}
}
public String toString() {
String strMatrix = "";
DecimalFormat formatter = new DecimalFormat("#.##");
for (int m = 0; m < numRows; m++) {
strMatrix += " ";
for (int n = 0; n < numCols; n++) {
if (entries[m][n] == 0) {
entries[m][n] = 0;
}
String strEntry = formatter.format(entries[m][n]);
int strEntryLength = strEntry.length();
for (int x = 0; x < 4 - strEntryLength; x++) {
strEntry += " ";
}
strMatrix+= strEntry + " ";
}
strMatrix += "\n";
}
return strMatrix;
}
public Double determinant() {
if (numRows != numCols) {
return null;
} else if (numRows == 2) {
return entries[0][0] * entries[1][1] - entries[0][1] * entries[1][0];
} else {
Double det = new Double(0);
for (int col = 0; col < numCols; col++) {
double[][] newEntries = new double[numRows - 1][numCols - 1];
int newRow = 0, newCol = 0;
for (int r = 1; r < numRows; r++) {
for (int c = 0; c < numCols; c++) {
if (c == col) continue;
newEntries[newRow][newCol] = entries[r][c];
newCol++;
}
newRow++;
newCol = 0;
}
int multiplier = col % 2 == 0 ? 1 : -1;
Matrix cofactorMatrix = new Matrix(numRows - 1, numCols - 1, newEntries);
det += multiplier * entries[0][col] * cofactorMatrix.determinant();
}
return det;
}
}
public Matrix transpose() {
double[][] newEntries = new double[numCols][numRows];
for (int r = 0; r < numRows; r++) {
for (int c = 0; c < numCols; c++) {
newEntries[c][r] = entries[r][c];
}
}
return new Matrix(numCols, numRows, newEntries);
}
public double cofactorAt (int i, int j) {
double[][] newEntries = new double[numRows - 1][numCols - 1];
int newRow = 0, newCol = 0;
for (int r = 0; r < numRows; r++) {
if (r == i) continue;
for (int c = 0; c < numCols; c++) {
if (c == j) continue;
newEntries[newRow][newCol] = entries[r][c];
newCol++;
}
newRow++;
newCol = 0;
}
int multiplier = (i + j) % 2 == 0 ? 1 : -1;
Matrix smaller = new Matrix(numRows - 1, numCols - 1, newEntries);
return multiplier * smaller.determinant();
}
public Matrix cofactor() {
if (numRows != numCols) {
return null;
}
double[][] newEntries = new double[numRows][numCols];
for (int r = 0; r < numRows; r++) {
for (int c = 0; c < numCols; c++) {
newEntries[r][c] = cofactorAt(r, c);
}
}
return new Matrix(numRows, numCols, newEntries);
}
public Matrix adjoint() {
Matrix cofactor = cofactor();
if (cofactor != null) return cofactor.transpose();
return null;
}
public static Matrix dot(Matrix a, Matrix b) {
if (a.numCols != b.numRows) {
return null;
}
double[][] newEntries = new double[a.numRows][b.numCols];
double dotSum = 0;
for (int ra = 0; ra < a.numRows; ra++) {
for (int cb = 0; cb < b.numCols; cb++) {
for (int ca = 0; ca < a.numCols; ca++) {
dotSum += a.entries[ra][ca] * b.entries[ca][cb];
}
newEntries[ra][cb] = dotSum;
dotSum = 0;
}
}
return new Matrix(a.numRows, b.numCols, newEntries);
}
public boolean isIdentity() {
if (numCols == numRows) {
for (int r = 0; r < numRows; r++) {
for (int c = 0; c < numCols; c++) {
if ((r == c && this.entries[r][c] != 1.0) || (r != c && this.entries[r][c] != 0.0))
return false;
}
}
}
return true;
}
/*
public double[] eigenvalues() {
Matrix rref = this.reduceRowEchelon();
}
*/
/*
private double[][] concatEntries(double[][] entriesA, double[][] entriesB) {
int rowsEntriesA = entriesA.length;
int rowsEntriesB = entriesB.length;
double[][] entriesC = new double[rowsEntriesA + rowsEntriesB][entriesA[0].length];
System.arraycopy(entriesA, 0, entriesC, 0, rowsEntriesA);
System.arraycopy(entriesB, 0, entriesC, rowsEntriesA, rowsEntriesB);
return entriesC;
}
*/
}