Skip to content

Commit

Permalink
feat: Add support for negative indices in display methods
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
mitsuki31 committed Sep 13, 2024
1 parent 55a7bea commit a3560d6
Showing 1 changed file with 22 additions and 30 deletions.
52 changes: 22 additions & 30 deletions src/main/java/com/mitsuki/jmatrix/Matrix.java
Original file line number Diff line number Diff line change
Expand Up @@ -5237,25 +5237,20 @@ final public void display() {
* @see #getEntries()
*/
final public void display(int index) {
if (this.ENTRIES != null) {
// Check for negative index and throw the exception
if (index < 0) {
cause = new InvalidIndexException(
"Invalid given index. Index cannot be a negative value.");
}
// Check if the given index greater than number of rows this matrix
else if (index > this.ROWS - 1) {
cause = new InvalidIndexException(
"Invalid given index. Index cannot be larger than number of rows.");
}

// Throw the exception if got one
if (cause != null) raise(cause);

System.out.println(Arrays.toString(this.ENTRIES[index]));
} else {
if (this.ENTRIES == null) {
System.out.println("<null_matrix>");
return;
}

int rows = this.getNumRows();
index += (index < 0) ? rows: 0; // Support negative index
if (index < 0 || index >= rows) {
raise(new InvalidIndexException("Given row index is out of bounds: " +
((index < 0) ? (index - rows) : index)
));
}

System.out.println(Arrays.toString(this.ENTRIES[index]));
}


Expand Down Expand Up @@ -5312,21 +5307,18 @@ final public static void display(double[ ][ ] arr) {
final public static void display(double[ ][ ] arr, int index) {
if (arr == null || arr.length == 0) {
System.out.println("<null_2darray>");
} else {
// Checking index value
if (index < 0) {
cause = new InvalidIndexException(
"Invalid given index. Index cannot be a negative value.");
} else if (index > arr.length - 1) {
cause = new InvalidIndexException(
"Invalid given index. Index cannot be larger than number of rows.");
}

// Throw the exception if got one
if (cause != null) raise(cause);
return;
}

System.out.println(Arrays.toString(arr[index]));
int rows = arr.length;
index += (index < 0) ? rows: 0; // Support negative index
if (index < 0 || index >= rows) {
raise(new InvalidIndexException("Given row index is out of bounds: " +
((index < 0) ? (index - rows) : index)
));
}

System.out.println(Arrays.toString(arr[index]));
}


Expand Down

0 comments on commit a3560d6

Please sign in to comment.