def derivative_listing_to_counts(derivatives, gdim):
    """Convert a derivative listing tuple to a derivative count tuple.

    The derivative d^3 / dy^2 dz is represented
    in counting form as (0, 2, 1) meaning (dx^0, dy^2, dz^1)
    and in listing form as (1, 1, 2) meaning (dy, dy, dz).
    """
    derivative_counts = [0] * gdim
    for d in derivatives:
        derivative_counts[d] += 1
    return tuple(derivative_counts)
