Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add pretty display functionality and negative index support #133

Merged
merged 2 commits into from
Sep 13, 2024

Conversation

mitsuki31
Copy link
Owner

Overview

This pull request introduces enhancements to the Matrix class by adding functionality for pretty displaying matrix data and supporting negative indices in matrix display methods. These changes are aimed at improving the readability and usability of matrix and array representations, particularly for debugging and data analysis.

Details

Pretty Display Functionality

New Methods Added

  • prettyDisplay(): Displays the entire matrix with optional column and row indices.

    public void prettyDisplay();
  • prettyDisplay(boolean): Allows toggling the display of row and column indices.

    public void prettyDisplay(boolean showIndices);
  • prettyDisplay(int): Displays a specific row of the matrix with optional row and column indices.

    public void prettyDisplay(int index);
  • prettyDisplay(int, boolean): Provides the option to show row and column indices for a specific row.

    public void prettyDisplay(int index, boolean showIndices);
  • prettyDisplay(double[][]): Static method to pretty print a 2D array with optional index display.

    public static void prettyDisplay(double[][] arr);
  • prettyDisplay(double[][], boolean): Static method to pretty print a 2D array with index display option.

    public static void prettyDisplay(double[][] arr, boolean showIndices);
  • prettyDisplay(double[][], int): Static method to pretty print a specific row of a 2D array with optional index display.

    public static void prettyDisplay(double[][] arr, int index);
  • prettyDisplay(double[][], int, boolean): Static method to pretty print a specific row of a 2D array with customizable index display.

    public static void prettyDisplay(double[][] arr, int index, boolean showIndices);

Features and Enhancements

  • Formats the matrix or array into a neatly aligned grid.
  • Calculates the maximum width for each column to ensure proper alignment.
  • Provides options to include row and column indices for better data interpretation.
  • Handles null matrices and arrays gracefully, displaying <null_matrix> or <null_2darray> as appropriate.
  • Implements support for negative row indices, allowing indices relative to the end of the matrix or array.
  • Raises InvalidIndexException for out-of-bounds indices, with clear error messages.

Support for Negative Indices

  • Enhancements:
    • Modified the display(int) method to support negative indices. Negative indices are interpreted as counting from the end of the matrix (e.g., -1 for the last row, -2 for the second-to-last row).
    • Adjusted the index calculation to convert negative indices into valid positive indices.
    • Added checks to ensure that the adjusted index is within the valid range of matrix rows.
    • Improved error handling by raising an InvalidIndexException with a descriptive message if the index is out of bounds.
    • Updated the static display(double[][], int) method to support negative indices similarly.

Impact

Functionality

  • The new prettyDisplay() methods provide enhanced visualization of matrix data, making it easier to read and analyze.
  • Support for negative indices improves flexibility in accessing matrix rows and aligns with common conventions.

Backward Compatibility

  • These changes are backward compatible. Existing functionality remains unaffected while new features are added.

Examples

Example Code

Here's how you might use the new prettyDisplay() and updated display() methods in practice:

import com.mitsuki.jmatrix.Matrix;

public class MatrixExample {
    public static void main(String[] args) {
        // Create a sample matrix
        double[][] data = {
            {1.234, 56.78, 1234.5},
            {7.89, 0.12, 345.67},
            {123.45, 678.9, 10.11}
        };
        
        // Initialize the matrix
        Matrix matrix = new Matrix(data);

        // Pretty display the entire matrix with indices
        System.out.println("Pretty display with indices:");
        matrix.prettyDisplay(true);
        
        // Pretty display the entire matrix without indices
        System.out.println("\nPretty display without indices:");
        matrix.prettyDisplay(false);
        
        // Pretty display a specific row (index 1) with indices
        System.out.println("\nPretty display of row 2 with indices:");
        matrix.prettyDisplay(1, true);
        
        // Pretty display a specific row (index 1) without indices
        System.out.println("\nPretty display of row 2 without indices:");
        matrix.prettyDisplay(1, false);

        // Pretty display the entire array (static method) with indices
        System.out.println("\nStatic pretty display of the entire array with indices:");
        Matrix.prettyDisplay(data, true);
        
        // Pretty display a specific row of the array (index 0) with indices
        System.out.println("\nStatic pretty display of row 1 with indices:");
        Matrix.prettyDisplay(data, 0, true);
        
        // Using negative indices
        System.out.println("\nPretty display of the last row using negative index:");
        matrix.prettyDisplay(-1, true);

        // Using invalid index (e.g., index out of bounds)
        System.out.println("\nTrying invalid index (4):");
        try {
            matrix.prettyDisplay(4, true);
        } catch (InvalidIndexException e) {
            System.out.println(e.getMessage());
        }

        // Display a specific row using the display method with negative index
        System.out.println("\nDisplay the second-to-last row using negative index:");
        matrix.display(-2);
    }
}

Example Output

Given the example matrix data, the output would look like this:

Pretty display with indices:
     [1]     [2]    [3]   
[1]  1.234   56.78  1234.5  
[2]  7.89    0.12   345.67  
[3]  123.45  678.9  10.11   

Pretty display without indices:
1.234   56.78  1234.5  
7.89    0.12   345.67  
123.45  678.9  10.11  

Pretty display of row 2 with indices:
     [1]   [2]   [3]   
[2]  7.89  0.12  345.67  

Pretty display of row 2 without indices:
7.89  0.12  345.67  

Static pretty display of the entire array with indices:
     [1]     [2]    [3]   
[1]  1.234   56.78  1234.5  
[2]  7.89    0.12   345.67  
[3]  123.45  678.9  10.11   

Static pretty display of row 1 with indices:
     [1]    [2]    [3]   
[1]  1.234  56.78  1234.5  

Pretty display of the last row using negative index:
     [1]     [2]    [3]   
[3]  123.45  678.9  10.11  

Trying invalid index (4):
Given row index is out of bounds: 4

Display the second-to-last row using negative index:
[2.89, 0.12, 345.67]

Notes

  • The output shows the matrix with properly aligned columns and optional row and column indices.
  • For negative index examples, the matrix displays the rows counted from the end.
  • The error handling for invalid indices demonstrates appropriate exception messages.

* 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.
* Enhanced the `display(int index)` method in the `Matrix` class to support negative indices. Negative indices are now interpreted as counting from the end of the matrix. For instance, an index of `-1` refers to the last row, `-2` refers to the second-to-last row, and so on.
* Adjusted the index calculation to convert negative indices into valid positive indices.
* Added checks to ensure that the adjusted index is within valid bounds for the matrix rows.
* Improved error handling to raise an `InvalidIndexException` with a clear message if the adjusted index is out of bounds.
* Updated the static `display(double[][] arr, int index)` method to support negative indices similarly.

These updates provide greater flexibility for accessing rows in matrices and 2D arrays using negative indices, aligning with common conventions and improving usability.
@mitsuki31 mitsuki31 self-assigned this Sep 13, 2024
@mitsuki31 mitsuki31 added this to the v1.5.0 milestone Sep 13, 2024
@github-actions github-actions bot added feature Add new features to improve the project lang:java Some changes on Java code labels Sep 13, 2024
@mitsuki31 mitsuki31 added the minor Minor update label Sep 13, 2024
@mitsuki31 mitsuki31 merged commit 4f63fbe into master Sep 13, 2024
34 checks passed
@mitsuki31 mitsuki31 deleted the feature/introduce-matrix-pretty-displayer branch September 13, 2024 09:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Add new features to improve the project lang:java Some changes on Java code minor Minor update
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

1 participant