human
Complete this python program:
def approx_derivative(fun, x0, method='3-point', rel_step=None, f0=None, bounds=((- np.inf), np.inf), sparsity=None, args=(), kwargs={}):
assistant
#Here is the Python code you requested.

    """Compute finite difference approximation of the derivatives of a
    vector-valued function.
    If a function maps from R^n to R^m, its derivatives form m-by-n matrix
    called the Jacobian, where an element (i, j) is a partial derivative of
    f[i] with respect to x[j].
    Parameters
    fun : callable
    Function of which to estimate the derivatives. The argument x
    passed to this function is ndarray of shape (n,) (never a scalar
    even if n=1). It must return 1-d array_like of shape (m,) or a scalar.
    x0 : array_like of shape (n,) or float
    Point at which to estimate the derivatives. Float will be converted
    to a 1-d array.
    method : {\3-point\, \2-point\}, optional
    Finite difference method to use:
    - \2-point\ - use the fist order accuracy forward or backward
    difference.
    - \3-point\ - use central difference in interior points and the
    second order accuracy forward or backward difference
    near the boundary.
    - \cs\ - use a complex-step finite difference scheme. This assumes
    that the user function is real-valued and can be
    analytically continued to the complex plane. Otherwise,
    produces bogus results.
    rel_step : None or array_like, optional
    Relative step size to use. The absolute step size is computed as
    ``h = rel_step * sign(x0) * max(1, abs(x0))``, possibly adjusted to
    fit into the bounds. For ``method=\3-point\`` the sign of `h` is
    ignored. If None (default) then step is selected automatically,
    see Notes.
    f0 : None or array_like, optional
    If not None it is assumed to be equal to ``fun(x0)``, in  this case
    the ``fun(x0)`` is not called. Default is None.
    bounds : tuple of array_like, optional
    Lower and upper bounds on independent variables. Defaults to no bounds.
    Each bound must match the size of `x0` or be a scalar, in the latter
    case the bound will be the same for all variables. Use it to limit the
    range of function evaluation.
    sparsity : {None, array_like, sparse matrix, 2-tuple},..."""
    if (method not in ['2-point', '3-point', 'cs']):
        raise ValueError(("Unknown    method    '%s'.    " % method))
    x0 = np.atleast_1d(x0)
    if (x0.ndim > 1):
        raise ValueError('`x0`    must    have    at    most    1    dimension.')
    (lb, ub) = _prepare_bounds(bounds, x0)
    if ((lb.shape != x0.shape) or (ub.shape != x0.shape)):
        raise ValueError('Inconsistent    shapes    between    bounds    and    `x0`.')
    def fun_wrapped(x):
        f = np.atleast_1d(fun(x, *args, **kwargs))
        if (f.ndim > 1):
            raise RuntimeError('`fun`    return    value    has    more    than    1    dimension.')
        return f
    if (f0 is None):
        f0 = fun_wrapped(x0)
    else:
        f0 = np.atleast_1d(f0)
        if (f0.ndim > 1):
            raise ValueError('`f0`    passed    has    more    than    1    dimension.')
    if np.any(((x0 < lb) | (x0 > ub))):
        raise ValueError('`x0`    violates    bound    constraints.')
    h = _compute_absolute_step(rel_step, x0, method)
    if (method == '2-point'):
        (h, use_one_sided) = _adjust_scheme_to_bounds(x0, h, 1, '1-sided', lb, ub)
    elif (method == '3-point'):
        (h, use_one_sided) = _adjust_scheme_to_bounds(x0, h, 1, '2-sided', lb, ub)
    elif (method == 'cs'):
        use_one_sided = False
    if (sparsity is None):
        return _dense_difference(fun_wrapped, x0, f0, h, use_one_sided, method)
    else:
        if ((not issparse(sparsity)) and (len(sparsity) == 2)):
            (structure, groups) = sparsity
        else:
            structure = sparsity
            groups = group_columns(sparsity)
        if issparse(structure):
            structure = csc_matrix(structure)
        else:
            structure = np.atleast_2d(structure)
        groups = np.atleast_1d(groups)
        return _sparse_difference(fun_wrapped, x0, f0, h, use_one_sided, structure, groups, method)