Skip to content

Commit fd0e86b

Browse files
committed
Pretty print matrix.
1 parent de6bdcc commit fd0e86b

File tree

3 files changed

+106
-11
lines changed

3 files changed

+106
-11
lines changed

src/utils/MatrixPrinter.java

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,50 @@
11
/**
2-
* Copyright 2020 jingedawang
2+
* Copyright 2022 jingedawang
33
*/
44
package utils;
55

66
import matrix.Matrix;
77

88
/**
9-
* <h3>Matrix printer</h3>
9+
* Matrix printer.
1010
*/
1111
public class MatrixPrinter {
1212

1313
/**
1414
* Print the matrix in a readable format.
1515
*
16-
* @param m The matrix to be printed.
16+
* @param matrix The matrix to be printed.
1717
*/
18-
public static void print(Matrix m) {
19-
System.out.print("[");
20-
for (int i = 0; i < m.rows(); i++) {
21-
for (int j = 0; j < m.columns() - 1; j++) {
22-
System.out.print(m.value()[i][j] + ",");
18+
public static <T extends Number> void print(Matrix<T> matrix) {
19+
double maxValue = MatrixUtils.max(matrix).doubleValue();
20+
double minValue = MatrixUtils.min(matrix).doubleValue();
21+
double maxAbsValue = Math.max(Math.abs(maxValue), Math.abs(minValue));
22+
int digitNumberBeforePoint = String.format("%.0f", maxAbsValue).length();
23+
24+
for (int i = 0; i < matrix.rows(); i++) {
25+
if (i == 0) {
26+
System.out.print('[');
27+
}
28+
else {
29+
System.out.print(' ');
30+
}
31+
for (int j = 0; j < matrix.columns(); j++) {
32+
if (matrix.get(i, j) instanceof Integer) {
33+
System.out.printf("%" + digitNumberBeforePoint + "d", matrix.get(i, j));
34+
}
35+
else {
36+
System.out.printf("%" + (digitNumberBeforePoint + 3) + ".2f", matrix.get(i, j));
37+
}
38+
if (j != matrix.columns() - 1) {
39+
System.out.print(", ");
40+
}
41+
}
42+
if (i != matrix.rows() - 1) {
43+
System.out.println();
44+
}
45+
else {
46+
System.out.println(']');
2347
}
24-
System.out.println(m.value()[i][m.columns() - 1] + (i == m.rows() - 1 ? "]" : ""));
2548
}
2649
}
2750

src/utils/MatrixUtils.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* Copyright 2022 jingedawang
3+
*/
4+
package utils;
5+
6+
import matrix.Matrix;
7+
8+
/**
9+
* Utility class for {@link Matrix}.
10+
*/
11+
public class MatrixUtils {
12+
13+
/**
14+
* Get the maximum value in the matrix.
15+
*
16+
* @param matrix The matrix object.
17+
* @param <T> The type of the underlying elements of the matrix.
18+
* @return The maximum value in the matrix.
19+
*/
20+
public static <T extends Number> T max(Matrix<T> matrix) {
21+
Number max;
22+
if (matrix.get(0, 0) instanceof Integer) {
23+
max = Integer.MIN_VALUE;
24+
}
25+
else {
26+
max = Double.MIN_VALUE;
27+
}
28+
for (int i = 0; i < matrix.rows(); i++) {
29+
for (int j = 0; j < matrix.columns(); j++) {
30+
if (matrix.get(0, 0) instanceof Integer) {
31+
max = Math.max((Integer) max, (Integer) matrix.get(i, j));
32+
}
33+
else {
34+
max = Math.max((Double) max, (Double) matrix.get(i, j));
35+
}
36+
}
37+
}
38+
return (T) max;
39+
}
40+
41+
/**
42+
* Get the minimum value in the matrix.
43+
*
44+
* @param matrix The matrix object.
45+
* @param <T> The type of the underlying elements of the matrix.
46+
* @return The minimum value in the matrix.
47+
*/
48+
public static <T extends Number> T min(Matrix<T> matrix) {
49+
Number min;
50+
if (matrix.get(0, 0) instanceof Integer) {
51+
min = Integer.MAX_VALUE;
52+
}
53+
else {
54+
min = Double.MAX_VALUE;
55+
}
56+
for (int i = 0; i < matrix.rows(); i++) {
57+
for (int j = 0; j < matrix.columns(); j++) {
58+
if (matrix.get(0, 0) instanceof Integer) {
59+
min = Math.min((Integer) min, (Integer) matrix.get(i, j));
60+
}
61+
else {
62+
min = Math.min((Double) min, (Double) matrix.get(i, j));
63+
}
64+
}
65+
}
66+
return (T) min;
67+
}
68+
69+
}

test/MatrixPrinterTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ public class MatrixPrinterTest {
1414

1515
@Test
1616
void print() {
17-
Matrix<Double> matrix = MatrixGenerator.generateRandomDoubleMatrix(4, 5);
18-
MatrixPrinter.print(matrix);
17+
Matrix<Integer> integerMatrix = MatrixGenerator.generateRandomIntegerMatrix(4, 5, 50);
18+
MatrixPrinter.print(integerMatrix);
19+
20+
Matrix<Double> doubleMatrix = MatrixGenerator.generateRandomDoubleMatrix(4, 5, 100);
21+
MatrixPrinter.print(doubleMatrix);
1922
}
2023

2124
}

0 commit comments

Comments
 (0)