def ind2sub(idx, shape):
    """Linear index to tensor subscripts"""
    # assert len(shape) == 2  # 2D test
    # sub = [idx // shape[1], idx % shape[1]]
    # return sub

    sub = [None] * len(shape)
    for (s, n) in reversed(list(enumerate(shape))):  # write subscripts in reverse order
        sub[s] = idx % n
        idx = idx // n
    return sub