<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def multi_index(idx, dim):
 """ Single to multi-index using graded reverse lexicographical notation. Parameters idx : int Index in interger notation dim : int The number of dimensions in the multi-index notation Returns ------- out : tuple Multi-index of `idx` with `len(out)=dim` Examples -------- (0, 0, 0) (1, 0, 0) (0, 1, 0) (0, 0, 1) (2, 0, 0) See Also -------- single_index """

    def _rec(idx, dim):
        idxn = idxm = 0
        if not dim:
            return ()

        if idx == 0:
            return (0, )*dim

        while terms(idxn, dim) <= idx:
            idxn += 1
        idx -= terms(idxn-1, dim)

        if idx == 0:
            return (idxn,) + (0,)*(dim-1)
        while terms(idxm, dim-1) <= idx:
            idxm += 1

        return (int(idxn-idxm),) + _rec(idx, dim-1)

    return _rec(idx, dim)