public static BigInteger determinant(Matrix matrix) throws NoSquareException {
        if (!matrix.isSquare())
            throw new NoSquareException("matrix need to be square.");
        if (matrix.size() == 1){
            return matrix.getValueAt(0, 0);
        }

        if (matrix.size()==2) {
            return (matrix.getValueAt(0, 0).multiply(matrix.getValueAt(1, 1))).subtract(matrix.getValueAt(0, 1).multiply(matrix.getValueAt(1, 0)));
        }
        BigInteger sum = new BigInteger("0");
        for (int i=0; i<matrix.getNcols(); i++) {
            sum = sum.add(BigInteger.valueOf(changeSign(i)).multiply(matrix.getValueAt(0, i)).multiply(determinant(createSubMatrix(matrix, 0, i))));
        }
        return sum;
    }