def square_to_condensed(i: int, j: int, n: int):
    """Convert a square matrix position (i, j) to a condensed distance matrix index.

    Args:
        i: Index i.
        j: Index j.
        n: The dimension of the matrix.

    See Also:
        https://stackoverflow.com/questions/13079563/how-does-condensed-distance-matrix-work-pdist

    Returns:
        Condensed index.
    """
    assert i != j, 'No diagonal elements in condensed matrix'
    if i < j:
        i, j = j, i
    return n * j - j * (j + 1) / 2 + i - 1 - j