Skip to content

Commit

Permalink
feat: Introduce pretty display functionality for matrix
Browse files Browse the repository at this point in the history
* Added `prettyDisplay()` methods to the `Matrix` class for enhanced visualization of matrix data.
  - `prettyDisplay()` (no arguments): Displays the entire matrix with optional column and row indices.
  - `prettyDisplay(boolean)`: Allows toggling of row and column index display.
  - `prettyDisplay(int)`: Displays a specific row of the matrix with optional row and column indices.
  - `prettyDisplay(int, boolean)`: Provides the option to specify whether to show row and column indices for a specific row.
  - `prettyDisplay(double[][])`: Static method to pretty print a 2D array, with an option to show indices.
  - `prettyDisplay(double[][], boolean)`: Static method to pretty print a 2D array with index display option.
  - `prettyDisplay(double[][], int)`: Static method to pretty print a specific row of a 2D array with optional index display.
  - `prettyDisplay(double[][], int, boolean)`: Static method to pretty print a specific row of a 2D array, with customizable index display.
* Each `prettyDisplay()` method formats the matrix or array into a neatly aligned grid, making it easier to read and interpret the data. It calculates the maximum width for each column to ensure proper alignment and provides options for including row and column indices.
* Added handling for null matrices and arrays, ensuring that `<null_matrix>` or `<null_2darray>` is displayed when appropriate.
* Implemented support for negative row indices, allowing users to specify indices relative to the end of the matrix or array.
* Improved the `prettyDisplay()` methods to raise `InvalidIndexException` for out-of-bounds indices, providing error messages when invalid indices are used.

These enhancements improve the usability and readability of matrix data, especially for debugging and data analysis purposes.
  • Loading branch information
mitsuki31 committed Sep 13, 2024
1 parent 391c7a3 commit 55a7bea
Showing 1 changed file with 214 additions and 0 deletions.
214 changes: 214 additions & 0 deletions src/main/java/com/mitsuki/jmatrix/Matrix.java
Original file line number Diff line number Diff line change
Expand Up @@ -5330,6 +5330,220 @@ final public static void display(double[ ][ ] arr, int index) {
}


final public void prettyDisplay() {
this.prettyDisplay(true);
}

final public void prettyDisplay(boolean showIndices) {
// If the matrix is null, print <null_matrix>
if (this.ENTRIES == null) {
System.out.println("<null_matrix>");
return;
}

double[][] entries = this.getEntries();
int rows = entries.length;
int cols = entries[0].length;
StringBuilder sb = new StringBuilder();

// Find the maximum width of each column
int[] widths = new int[cols];
for (double[] row : entries) {
for (int i = 0; i < row.length; i++) {
widths[i] = Math.max(widths[i], String.valueOf(row[i]).length());
}
}

// Print column numbers on top if `showIndices` is true
if (showIndices) {
sb.append(" ");
for (int i = 0; i < cols; i++) {
sb.append(
String.format("%-" + widths[i] + "s",
String.format("[%d]", i + 1)) + " "
);
}
sb.append("\n");
}

// Print the matrix with row numbers on the left if `showIndices` is true
for (int i = 0; i < rows; i++) {
if (showIndices) {
sb.append(String.format("[%d] ", (i + 1)));
}
for (int j = 0; j < cols; j++) {
sb.append(String.format("%-" + widths[j] + "s", entries[i][j]) + " ");
}
sb.append("\n");
}

// Print the whole matrix
System.out.println(sb.toString());
}

final public void prettyDisplay(int index) {
prettyDisplay(index, true);
}

final public void prettyDisplay(int index, boolean showIndices) {
// If the matrix is null, print <null_matrix>
if (this.ENTRIES == null) {
System.out.println("<null_matrix>");
return;
}

double[][] entries = this.getEntries();
int rows = entries.length;
int cols = entries[0].length;
StringBuilder sb = new StringBuilder();

index += (index < 0) ? rows: 0; // Support negative index
if (index < 0 || index >= rows) {
cause = new InvalidIndexException(
"Given row index is out of bounds: " + ((index < 0) ? (index - rows) : index)
);
}
if (cause != null) raise(cause); // Throw the exception if got one

// Find the maximum width of each column
int[] widths = new int[cols];
for (double[] row : entries) {
for (int i = 0; i < row.length; i++) {
widths[i] = Math.max(widths[i], String.valueOf(row[i]).length());
}
}

// Add the column headers on top if `showIndices` is true
if (showIndices) {
sb.append(" ");
for (int i = 0; i < cols; i++) {
sb.append(
String.format("%-" + widths[i] + "s",
String.format("[%d]", i + 1)) + " "
);
}
sb.append("\n");
}

// Add the matrix with row numbers on the left if `showIndices` is true
if (showIndices) {
sb.append(String.format("[%d] ", (index + 1)));
}
for (int j = 0; j < cols; j++) {
sb.append(String.format("%-" + widths[j] + "s", entries[index][j]) + " ");
}
sb.append("\n");

// Print the whole matrix
System.out.println(sb.toString());
}

final public static void prettyDisplay(double[ ][ ] arr) {
Matrix.prettyDisplay(arr, true);
}

final public static void prettyDisplay(double[ ][ ] arr, boolean showIndices) {
// If the two-dimensional array is null, print <null_2darray>
if (arr == null || arr.length == 0) {
System.out.println("<null_2darray>");
return;
}

int rows = arr.length;
int cols = arr[0].length;
StringBuilder sb = new StringBuilder();

// Find the maximum width of each column
int[] widths = new int[cols];
for (double[] row : arr) {
for (int i = 0; i < row.length; i++) {
widths[i] = Math.max(widths[i], String.valueOf(row[i]).length());
}
}

// Print column numbers on top if `showIndices` is true
if (showIndices) {
sb.append(" ");
for (int i = 0; i < cols; i++) {
sb.append(
String.format("%-" + widths[i] + "s",
String.format("[%d]", i + 1)) + " "
);
}
sb.append("\n");
}

// Print the matrix with row numbers on the left if `showIndices` is true
for (int i = 0; i < rows; i++) {
if (showIndices) {
sb.append(String.format("[%d] ", (i + 1)));
}
for (int j = 0; j < cols; j++) {
sb.append(String.format("%-" + widths[j] + "s", arr[i][j]) + " ");
}
sb.append("\n");
}

// Print the whole matrix
System.out.println(sb.toString());
}

final public static void prettyDisplay(double[ ][ ] arr, int index) {
Matrix.prettyDisplay(arr, index, true);
}

final public static void prettyDisplay(double[ ][ ] arr, int index, boolean showIndices) {
// If the two-dimensional array is null, print <null_2darray>
if (arr == null || arr.length == 0) {
System.out.println("<null_2darray>");
return;
}

int rows = arr.length;
int cols = arr[0].length;
StringBuilder sb = new StringBuilder();

index += (index < 0) ? rows: 0; // Support negative index
if (index < 0 || index >= rows) {
cause = new InvalidIndexException(
"Given row index is out of bounds: " + ((index < 0) ? (index - rows) : index)
);
}
if (cause != null) raise(cause); // Throw the exception if got one

// Find the maximum width of each column
int[] widths = new int[cols];
for (double[] row : arr) {
for (int i = 0; i < row.length; i++) {
widths[i] = Math.max(widths[i], String.valueOf(row[i]).length());
}
}

// Add the column headers on top if `showIndices` is true
if (showIndices) {
sb.append(" ");
for (int i = 0; i < cols; i++) {
sb.append(
String.format("%-" + widths[i] + "s",
String.format("[%d]", i + 1)) + " "
);
}
sb.append("\n");
}

// Add the matrix with row numbers on the left if `showIndices` is true
if (showIndices) {
sb.append(String.format("[%d] ", (index + 1)));
}
for (int j = 0; j < cols; j++) {
sb.append(String.format("%-" + widths[j] + "s", arr[index][j]) + " ");
}
sb.append("\n");

// Print the whole matrix
System.out.println(sb.toString());
}


/*=========================================
::
Expand Down

0 comments on commit 55a7bea

Please sign in to comment.