human
Complete this python program:
def brute(func, ranges, args=(), Ns=20, full_output=0, finish=fmin, disp=False):
assistant
#Here is the Python code you requested.

    """Minimize a function over a given range by brute force.
    Uses the "brute force" method, i.e. computes the function\s value
    at each point of a multidimensional grid of points, to find the global
    minimum of the function.
    The function is evaluated everywhere in the range with the datatype of the
    first call to the function, as enforced by the ``vectorize`` NumPy
    function.  The value and type of the function evaluation returned when
    ``full_output=True`` are affected in addition by the ``finish`` argument
    (see Notes).
    Parameters
    func : callable
    The objective function to be minimized. Must be in the
    form ``f(x, *args)``, where ``x`` is the argument in
    the form of a 1-D array and ``args`` is a tuple of any
    additional fixed parameters needed to completely specify
    the function.
    ranges : tuple
    Each component of the `ranges` tuple must be either a
    "slice object" or a range tuple of the form ``(low, high)``.
    The program uses these to create the grid of points on which
    the objective function will be computed. See `Note 2` for
    more detail.
    args : tuple, optional
    Any additional fixed parameters needed to completely specify
    the function.
    Ns : int, optional
    Number of grid points along the axes, if not otherwise
    specified. See `Note2`.
    full_output : bool, optional
    If True, return the evaluation grid and the objective function\s
    values on it.
    finish : callable, optional
    An optimization function that is called with the result of brute force
    minimization as initial guess.  `finish` should take `func` and
    the initial guess as positional arguments, and take `args` as
    keyword arguments.  It may additionally take `full_output`
    and/or `disp` as keyword arguments.  Use None if no "polishing"
    function is to be used. See Notes for more details.
    disp : bool, optional
    Set to True to print convergence messages.
    Returns
    x0 : ndarray
    A 1-D array containing the coordinates of a..."""
    N = len(ranges)
    if (N > 40):
        raise ValueError('Brute    Force    not    possible    with    more    than    40    variables.')
    lrange = list(ranges)
    for k in range(N):
        if (type(lrange[k]) is not type(slice(None))):
            if (len(lrange[k]) < 3):
                lrange[k] = (tuple(lrange[k]) + (complex(Ns),))
            lrange[k] = slice(*lrange[k])
    if (N == 1):
        lrange = lrange[0]
    def _scalarfunc(*params):
        params = squeeze(asarray(params))
        return func(params, *args)
    vecfunc = vectorize(_scalarfunc)
    grid = mgrid[lrange]
    if (N == 1):
        grid = (grid,)
    Jout = vecfunc(*grid)
    Nshape = shape(Jout)
    indx = argmin(Jout.ravel(), axis=(-1))
    Nindx = zeros(N, int)
    xmin = zeros(N, float)
    for k in range((N - 1), (-1), (-1)):
        thisN = Nshape[k]
        Nindx[k] = (indx % Nshape[k])
        indx = (indx // thisN)
    for k in range(N):
        xmin[k] = grid[k][tuple(Nindx)]
    Jmin = Jout[tuple(Nindx)]
    if (N == 1):
        grid = grid[0]
        xmin = xmin[0]
    if callable(finish):
        finish_args = _getargspec(finish).args
        finish_kwargs = dict()
        if ('full_output' in finish_args):
            finish_kwargs['full_output'] = 1
        if ('disp' in finish_args):
            finish_kwargs['disp'] = disp
        elif ('options' in finish_args):
            finish_kwargs['options'] = {'disp': disp}
        res = finish(func, xmin, args=args, **finish_kwargs)
        if isinstance(res, OptimizeResult):
            xmin = res.x
            Jmin = res.fun
            success = res.success
        else:
            xmin = res[0]
            Jmin = res[1]
            success = (res[(-1)] == 0)
        if (not success):
            if disp:
                print('Warning:    Either    final    optimization    did    not    succeed    or    `finish`    does    not    return    `statuscode`    as    its    last    argument.')
    if full_output:
        return (xmin, Jmin, grid, Jout)
   ...