def METHOD_NAME(a, factor, flux=False, trim=False):
    """Rebin a 1, 2, or 3 dimensional array by integer amounts.
    Parameters
    ----------
    a : ndarray
      Image to rebin.
    factor : int
      Rebin factor.  For `factor < 0`, shrink the image.  When
      shrinking, all axes must be an integer multiple of factor or use
      `trim`.
    flux : bool
      If `True`, preserve flux, otherwise, surface brightness is
      preserved.
    trim : bool
      If `True`, trim the shape of the input array to an integer
      multiple of factor.
    Returns
    -------
    b : ndarray
      The rebinned array.
    Example
    -------
    >>> from sbpy.imageanalysis.utils import xarray, rebin
    >>> x = xarray((10, 10))
    >>> x[0, :2].mean()
    0.5
    >>> x2 = rebin(x, -2)
    >>> x2[0, 0]
    0.5
    """
    import numpy as np
    def mini(a, factor):
        b = a[::-factor]
        for i in range(-factor - 1):
            b += a[(i + 1)::-factor]
        if not flux:
            b = b / -factor
        return b
    def magni(a, factor):
        s = np.array(a.shape)
        s[0] *= factor
        b = np.zeros(s)
        for i in range(factor):
            b[i::factor] = a
        if flux:
            b = b / factor
        return b
    if factor == 1:
        # done!
        return a
    _a = a.copy()
    if factor < 0:
        for i in range(_a.ndim):
            if trim:
                r = _a.shape[i] % abs(factor)
                if r != 0:
                    _a = np.rollaxis(np.rollaxis(_a, i)[:-r], 0, i + 1)
            if (_a.shape[i] % factor) != 0:
                raise ValueError(
                    "Axis {0} must be an integer multiple of "
                    "the minification factor.".format(i))
        f = mini
    else:
        f = magni
    b = f(_a, factor)
    for i in range(len(_a.shape) - 1):
        c = f(np.rollaxis(b, i + 1), factor)
        b = np.rollaxis(c, 0, i + 2)
    return b