Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/maths/MatrixRank.java (Java)

Code before:
        if (matrix == null) {
            throw new IllegalArgumentException("The input matrix cannot be null");
        }
        if (matrix.length == 0) {
            throw new IllegalArgumentException("The input matrix cannot be empty");
        }
        if (!hasValidRows(matrix)) {
            throw new IllegalArgumentException("The input matrix cannot have null or empty rows");
        }
        if (isJaggedMatrix(matrix)) {
            throw new IllegalArgumentException("The input matrix cannot be jagged");
        }
    }

    private static boolean hasValidRows(double[][] matrix) {
        for (double[] row : matrix) {
            if (row == null || row.length == 0) {
                return false;
            }
        }
        return true;
    }

    /**
     * @brief Checks if the input matrix is a jagged matrix.
     * Jaggged matrix is a matrix where the number of columns in each row is not the same.
     *
     * @param matrix The input matrix
     * @return True if the input matrix is a jagged matrix, false otherwise
     */
    private static boolean isJaggedMatrix(double[][] matrix) {
        int numColumns = matrix[0].length;
        for (double[] row : matrix) {
            if (row.length != numColumns) {
                return true;
            }
        }
        return false;
    }

    /**
     * @brief The pivot row is the row in the matrix that is used to eliminate other rows and reduce the matrix to its row echelon form.
     * The pivot row is selected as the first row (from top to bottom) where the value in the current column (the pivot column) is not zero.
     * This row is then used to "eliminate" other rows, by subtracting multiples of the pivot row from them, so that all other entries in the pivot column become zero.
     * This process is repeated for each column, each time selecting a new pivot row, until the matrix is in row echelon form.
     * The number of pivot rows (rows with a leading entry, or pivot) then gives the rank of the matrix.
     *
     * @param matrix The input matrix
     * @param rowMarked An array indicating which rows have been marked
     * @param colIndex The column index
     * @return The pivot row index, or the number of rows if no suitable pivot row was found

Review comment (suggestion):
```suggestion
     * Jagged matrix is a matrix where the number of columns in each row is not the same.
```

Code after:
        if (matrix == null) {
            throw new IllegalArgumentException("The input matrix cannot be null");
        }
        if (matrix.length == 0) {
            throw new IllegalArgumentException("The input matrix cannot be empty");
        }
        if (!hasValidRows(matrix)) {
            throw new IllegalArgumentException("The input matrix cannot have null or empty rows");
        }
        if (isJaggedMatrix(matrix)) {
            throw new IllegalArgumentException("The input matrix cannot be jagged");
        }
    }

    private static boolean hasValidRows(double[][] matrix) {
        for (double[] row : matrix) {
            if (row == null || row.length == 0) {
                return false;
            }
        }
        return true;
    }

    /**
     * @brief Checks if the input matrix is a jagged matrix.
     * Jagged matrix is a matrix where the number of columns in each row is not the same.
     *
     * @param matrix The input matrix
     * @return True if the input matrix is a jagged matrix, false otherwise
     */
    private static boolean isJaggedMatrix(double[][] matrix) {
        int numColumns = matrix[0].length;
        for (double[] row : matrix) {
            if (row.length != numColumns) {
                return true;
            }
        }
        return false;
    }

    /**
     * @brief The pivot row is the row in the matrix that is used to eliminate other rows and reduce the matrix to its row echelon form.
     * The pivot row is selected as the first row (from top to bottom) where the value in the current column (the pivot column) is not zero.
     * This row is then used to "eliminate" other rows, by subtracting multiples of the pivot row from them, so that all other entries in the pivot column become zero.
     * This process is repeated for each column, each time selecting a new pivot row, until the matrix is in row echelon form.
     * The number of pivot rows (rows with a leading entry, or pivot) then gives the rank of the matrix.
     *
     * @param matrix The input matrix
     * @param rowMarked An array indicating which rows have been marked
     * @param colIndex The column index
     * @return The pivot row index, or the number of rows if no suitable pivot row was found