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

Code before:
 *
 * @author Anup Omkar
 */
public class MatrixRank {

    // Small value to handle precision issues with floating point numbers
    private final static double EPSILON = 1e-10;

    /**
     * Returns rank of matrix
     *
     * @param matrix matrix whose rank is to be found
     * @return rank of matrix
     */
    public static int computeRank(double[][] matrix) {
        int numRows = matrix.length;
        int numColumns = matrix[0].length;
        int rank = 0;

        boolean[] rowMarked = new boolean[numRows];
        for (int colIndex = 0; colIndex < numColumns; ++colIndex) {
            int pivotRow;
            for (pivotRow = 0; pivotRow < numRows; ++pivotRow) {
                if (!rowMarked[pivotRow] && Math.abs(matrix[pivotRow][colIndex]) > EPSILON) {
                    break;
                }
            }
            if (pivotRow != numRows) {
                ++rank;
                rowMarked[pivotRow] = true;
                for (int nextCol = colIndex + 1; nextCol < numColumns; ++nextCol) {
                    matrix[pivotRow][nextCol] /= matrix[pivotRow][colIndex];
                }
                for (int otherRow = 0; otherRow < numRows; ++otherRow) {
                    if (otherRow != pivotRow && Math.abs(matrix[otherRow][colIndex]) > EPSILON) {
                        for (int col2 = colIndex + 1; col2 < numColumns; ++col2) {
                            matrix[otherRow][col2] -= matrix[pivotRow][col2] * matrix[otherRow][colIndex];
                        }
                    }
                }
            }
        }
        return rank;
    }
}


Review comment (suggestion):
This code is very complex. Do you see some change to cut it into smaller pieces? The first step it to try to write comments what happens in some of the blocks.

Code after:
    private MatrixRank() {
    }

    private static final double EPSILON = 1e-10;

    /**
     * @brief Computes the rank of the input matrix
     *
     * @param matrix The input matrix
     * @return The rank of the input matrix
     */
    public static int computeRank(double[][] matrix) {
        validateInputMatrix(matrix);

        int numRows = matrix.length;
        int numColumns = matrix[0].length;
        int rank = 0;

        boolean[] rowMarked = new boolean[numRows];

        double[][] matrixCopy = deepCopy(matrix);

        for (int colIndex = 0; colIndex < numColumns; ++colIndex) {
            int pivotRow = findPivotRow(matrixCopy, rowMarked, colIndex);
            if (pivotRow != numRows) {
                ++rank;
                rowMarked[pivotRow] = true;
                normalizePivotRow(matrixCopy, pivotRow, colIndex);
                eliminateRows(matrixCopy, pivotRow, colIndex);
            }
        }
        return rank;
    }

    private static boolean isZero(double value) {
        return Math.abs(value) < EPSILON;
    }

    private static double[][] deepCopy(double[][] matrix) {
        int numRows = matrix.length;
        int numColumns = matrix[0].length;
        double[][] matrixCopy = new double[numRows][numColumns];
        for (int rowIndex = 0; rowIndex < numRows; ++rowIndex) {
            System.arraycopy(matrix[rowIndex], 0, matrixCopy[rowIndex], 0, numColumns);
        }
        return matrixCopy;
    }

    private static void validateInputMatrix(double[][] matrix) {
        if (matrix == null) {
            throw new IllegalArgumentException("The input matrix cannot be null");