From 9f95d4d29b23eb83aa202f5de19b696f0c38bd58 Mon Sep 17 00:00:00 2001
From: Ryuu Mitsuki <117973493+mitsuki31@users.noreply.github.com>
Date: Sat, 24 Jun 2023 01:57:01 +0700
Subject: [PATCH] Remove try-catch on `transpose` methods
This changes included:
* transpose()
* transpose(double[][])
* transpose(Matrix)
---
src/main/java/com/mitsuki/jmatrix/Matrix.java | 76 ++++++++++---------
1 file changed, 41 insertions(+), 35 deletions(-)
diff --git a/src/main/java/com/mitsuki/jmatrix/Matrix.java b/src/main/java/com/mitsuki/jmatrix/Matrix.java
index e039f96..0cf7684 100644
--- a/src/main/java/com/mitsuki/jmatrix/Matrix.java
+++ b/src/main/java/com/mitsuki/jmatrix/Matrix.java
@@ -2271,34 +2271,42 @@ final public static void display(double[ ][ ] arr, int index) {
* the elements to their original position. Also can be written
* like this, (AT)T
.
*
- * @throws NullMatrixException if the entries of this matrix is {@code null}
+ * @throws NullMatrixException if the entries of this matrix is {@code null}.
*
* @since 0.2.0
* @see #transpose(Matrix)
* @see #transpose(double[][])
*/
public void transpose() {
+ // Throw the exception immediately if this matrix has null entries
+ if (this.ENTRIES == null) {
+ Options.raiseError(new NullMatrixException(
+ "This matrix is null. " +
+ "Please ensure the matrix are initialized before performing transposition."
+ ));
+ }
+
this.create( Matrix.transpose(this).getEntries() );
}
/**
- * Performs transposition for the given 2D array and produces new
+ * Performs transposition for the given two-dimensional array and produces new
* array with the transposed elements.
*
- *
If the given 2D array type are not square, then it would switches the row and column + *
If the given two-dimensional array type are not square, then it would switches the row and column
* indices of the array. Which means the array size would be switched
- * (for example, {@code 2x4 -> 4x2}).
+ * (for example, {@code 2x4 -> 4x2}).
*
*
Note:
* - *Repeating the process on the transposed 2D array returns + *
Repeating the process on the transposed two-dimensional array returns
* the elements to their original position. Also can be written
* like this, (AT)T
.
*
- * @param arr the 2D array to be transposed.
+ * @param arr the two-dimensional array to be transposed.
*
- * @return the transposed of given array.
+ * @return the transposed of given two-dimensional array.
*
* @throws NullMatrixException if the given array is {@code null} or empty.
*
@@ -2307,13 +2315,11 @@ public void transpose() {
* @see #transpose(Matrix)
*/
public static double[ ][ ] transpose(double[ ][ ] arr) {
- try {
- if (arr == null || arr.length == 0) {
- throw new NullMatrixException(
- "Given array is null. Please ensure the array has valid elements.");
- }
- } catch (final NullMatrixException nme) {
- Options.raiseError(nme);
+ if (arr == null || arr.length == 0) {
+ Options.raiseError(new NullMatrixException(
+ "Given array is null. " +
+ "Please ensure the array has valid elements before performing transposition."
+ ));
}
return Matrix.transpose(new Matrix(arr)).getEntries();
@@ -2326,7 +2332,7 @@ public void transpose() {
*
*
If the given matrix type are not square, then it would switches the row and column
* indices of the matrix. Which means the matrix size would be switched
- * (for example, {@code 2x4 -> 4x2}).
+ * (for example, {@code 2x4 -> 4x2}).
*
*
The transposed matrix always get denoted by upperscript T, t or tr, * for example: @@ -2350,6 +2356,7 @@ public void transpose() { * // which the capital "T" is to indicates the * // transposed matrix of matrix "m". * Matrix mT = Matrix.transpose(m); + * mT.display(); * * *
This code would transpose the matrix {@code m}, which means @@ -2378,41 +2385,40 @@ public void transpose() { * @see #transpose(double[][]) */ public static Matrix transpose(Matrix m) { - try { - if (m == null || m.ENTRIES == null) { - throw new NullMatrixException( - "Matrix is null. Please ensure the matrix are initialized."); - } - } catch (final NullMatrixException nme) { - Options.raiseError(nme); + // Throw the exception immediately if the given matrix has null entries + if (m == null || m.getEntries() == null) { + Options.raiseError(new NullMatrixException( + "Given matrix is null. " + + "Please ensure the matrix are initialized before performing transposition." + )); } - // Create null matrix object for the transposed matrix - Matrix transposedMatrix = new Matrix(); + // Declare new entries for the transposed matrix + double[ ][ ] transposedEntries; // Check whether the matrix is square if (m.isSquare()) { - // Initialize and create new matrix - transposedMatrix.create(m.ROWS, m.COLS); + // Initialize the entries + transposedEntries = new double[m.getSize()[0]][m.getSize()[1]]; // Iterate over elements and transpose each element - for (int i = 0; i < m.ROWS; i++) { - for (int j = 0; j < m.COLS; j++) { - transposedMatrix.ENTRIES[i][j] = m.ENTRIES[j][i]; + for (int r = 0; r < m.getSize()[0]; r++) { + for (int c = 0; c < m.getSize()[1]; c++) { + transposedEntries[r][c] = m.get(c, r); } } } else { - // Initialize and create new matrix with row and column inverted - transposedMatrix.create(m.COLS, m.ROWS); + // Initialize the entries with row and column switched + transposedEntries = new double[m.getSize()[1]][m.getSize()[0]]; - for (int i = 0; i < m.COLS; i++) { - for (int j = 0; j < m.ROWS; j++) { - transposedMatrix.ENTRIES[i][j] = m.ENTRIES[j][i]; + for (int c = 0; c < m.getSize()[1]; c++) { + for (int r = 0; r < m.getSize()[0]; r++) { + transposedEntries[c][r] = m.get(r, c); } } } - return transposedMatrix; + return new Matrix(transposedEntries); }