<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def cartesian(shape):
 r"""Cartesian indexing. Returns a sequence of n-tuples indexing each element of a hypothetical matrix of the given shape. Args: shape (tuple):
 tuple of dimensions. Returns: array_like: indices. Example ------- .. doctest:: [[0 0] [0 1] [0 2] [1 0] [1 1] [1 2]] Reference: [1] http://stackoverflow.com/a/27286794 """

    n = len(shape)
    idx = [slice(0, s) for s in shape]
    g = rollaxis(mgrid[idx], 0, n + 1)
    return g.reshape((prod(shape), n))