def differential_evolution(func, bounds, args=(), strategy='<STR_LIT>',<EOL>maxiter=None, popsize=<NUM_LIT:15>, tol=<NUM_LIT>,<EOL>mutation=(<NUM_LIT:0.5>, <NUM_LIT:1>), recombination=<NUM_LIT>, seed=None,<EOL>callback=None, disp=False, polish=True,<EOL>init='<STR_LIT>'):

solver = DifferentialEvolutionSolver(func, bounds, args=args,<EOL>strategy=strategy, maxiter=maxiter,<EOL>popsize=popsize, tol=tol,<EOL>mutation=mutation,<EOL>recombination=recombination,<EOL>seed=seed, polish=polish,<EOL>callback=callback,<EOL>disp=disp,<EOL>init=init)<EOL>return solver.solve()<EOL>

Finds the global minimum of a multivariate function.
    Differential Evolution is stochastic in nature (does not use gradient
    methods) to find the minimium, and can search large areas of candidate
    space, but often requires larger numbers of function evaluations than
    conventional gradient based techniques.

    The algorithm is due to Storn and Price [1]_.

    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.
    bounds : sequence
        Bounds for variables.  ``(min, max)`` pairs for each element in ``x``,
        defining the lower and upper bounds for the optimizing argument of
        `func`. It is required to have ``len(bounds) == len(x)``.
        ``len(bounds)`` is used to determine the number of parameters in ``x``.
    args : tuple, optional
        Any additional fixed parameters needed to
        completely specify the objective function.
    strategy : str, optional
        The differential evolution strategy to use. Should be one of:

            - 'best1bin'
            - 'best1exp'
            - 'rand1exp'
            - 'randtobest1exp'
            - 'best2exp'
            - 'rand2exp'
            - 'randtobest1bin'
            - 'best2bin'
            - 'rand2bin'
            - 'rand1bin'

        The default is 'best1bin'.
    maxiter : int, optional
        The maximum number of times the entire population is evolved.
        The maximum number of function evaluations is:
        ``maxiter * popsize * len(x)``
    popsize : int, optional
        A multiplier for setting the total population size.  The population has
        ``popsize * len(x)`` individuals.
    tol : float, optional
        When the mean of the population energies, multiplied by tol,
        divided by the standard deviation of the population energies
        is greater than 1 the solving process terminates:
        ``convergence = mean(pop) * tol / stdev(pop) > 1``
    mutation : float or tuple(float, float), optional
        The mutation constant.
        If specified as a float it should be in the range [0, 2].
        If specified as a tuple ``(min, max)`` dithering is employed. Dithering
        randomly changes the mutation constant on a generation by generation
        basis. The mutation constant for that generation is taken from
        ``U[min, max)``. Dithering can help speed convergence significantly.
        Increasing the mutation constant increases the search radius, but will
        slow down convergence.
    recombination : float, optional
        The recombination constant, should be in the range [0, 1]. Increasing
        this value allows a larger number of mutants to progress into the next
        generation, but at the risk of population stability.
    seed : int or `np.random.RandomState`, optional
        If `seed` is not specified the `np.RandomState` singleton is used.
        If `seed` is an int, a new `np.random.RandomState` instance is used,
        seeded with seed.
        If `seed` is already a `np.random.RandomState instance`, then that
        `np.random.RandomState` instance is used.
        Specify `seed` for repeatable minimizations.
    disp : bool, optional
        Display status messages
    callback : callable, `callback(xk, convergence=val)`, optional:
        A function to follow the progress of the minimization. ``xk`` is
        the current value of ``x0``. ``val`` represents the fractional
        value of the population convergence.  When ``val`` is greater than one
        the function halts. If callback returns `True`, then the minimization
        is halted (any polishing is still carried out).
    polish : bool, optional
        If True (default), then `scipy.optimize.minimize` with the `L-BFGS-B`
        method is used to polish the best population member at the end, which
        can improve the minimization slightly.
    init : string, optional
        Specify how the population initialization is performed. Should be
        one of:

            - 'latinhypercube'
            - 'random'

        The default is 'latinhypercube'. Latin Hypercube sampling tries to
        maximize coverage of the available parameter space. 'random' initializes
        the population randomly - this has the drawback that clustering can
        occur, preventing the whole of parameter space being covered.

    Returns
    -------
    res : OptimizeResult
        The optimization result represented as a `OptimizeResult` object.
        Important attributes are: ``x`` the solution array, ``success`` a
        Boolean flag indicating if the optimizer exited successfully and
        ``message`` which describes the cause of the termination. See
        `OptimizeResult` for a description of other attributes. If `polish`
        was employed, then OptimizeResult also contains the `jac` attribute.

    Notes
    -----
    Differential evolution is a stochastic population based method that is
    useful for global optimization problems. At each pass through the population
    the algorithm mutates each candidate solution by mixing with other candidate
    solutions to create a trial candidate. There are several strategies [2]_ for
    creating trial candidates, which suit some problems more than others. The
    'best1bin' strategy is a good starting point for many systems. In this
    strategy two members of the population are randomly chosen. Their difference
    is used to mutate the best member (the `best` in `best1bin`), :math:`b_0`,
    so far:

    .. math::

        b' = b_0 + mutation * (population[rand0] - population[rand1])

    A trial vector is then constructed. Starting with a randomly chosen 'i'th
    parameter the trial is sequentially filled (in modulo) with parameters from
    `b'` or the original candidate. The choice of whether to use `b'` or the
    original candidate is made with a binomial distribution (the 'bin' in
    'best1bin') - a random number in [0, 1) is generated.  If this number is
    less than the `recombination` constant then the parameter is loaded from
    `b'`, otherwise it is loaded from the original candidate.  The final
    parameter is always loaded from `b'`.  Once the trial candidate is built
    its fitness is assessed. If the trial is better than the original candidate
    then it takes its place. If it is also better than the best overall
    candidate it also replaces that.
    To improve your chances of finding a global minimum use higher `popsize`
    values, with higher `mutation` and (dithering), but lower `recombination`
    values. This has the effect of widening the search radius, but slowing
    convergence.

    .. versionadded:: 0.15.0

    Examples
    --------
    Let us consider the problem of minimizing the Rosenbrock function. This
    function is implemented in `rosen` in `scipy.optimize`.

    >>> from scipy.optimize import rosen, differential_evolution
    >>> bounds = [(0,2), (0, 2), (0, 2), (0, 2), (0, 2)]
    >>> result = differential_evolution(rosen, bounds)
    >>> result.x, result.fun
    (array([1., 1., 1., 1., 1.]), 1.9216496320061384e-19)

    Next find the minimum of the Ackley function
    (http://en.wikipedia.org/wiki/Test_functions_for_optimization).

    >>> from scipy.optimize import differential_evolution
    >>> import numpy as np
    >>> def ackley(x):
    ...     arg1 = -0.2 * np.sqrt(0.5 * (x[0] ** 2 + x[1] ** 2))
    ...     arg2 = 0.5 * (np.cos(2. * np.pi * x[0]) + np.cos(2. * np.pi * x[1]))
    ...     return -20. * np.exp(arg1) - np.exp(arg2) + 20. + np.e
    >>> bounds = [(-5, 5), (-5, 5)]
    >>> result = differential_evolution(ackley, bounds)
    >>> result.x, result.fun
    (array([ 0.,  0.]), 4.4408920985006262e-16)

    References
    ----------
    .. [1] Storn, R and Price, K, Differential Evolution - a Simple and
           Efficient Heuristic for Global Optimization over Continuous Spaces,
           Journal of Global Optimization, 1997, 11, 341 - 359.
    .. [2] http://www1.icsi.berkeley.edu/~storn/code.html
    .. [3] http://en.wikipedia.org/wiki/Differential_evolution


if seed is None or seed is np.random:<EOL><INDENT>return np.random.mtrand._rand<EOL><DEDENT>if isinstance(seed, (numbers.Integral, np.integer)):<EOL><INDENT>return np.random.RandomState(seed)<EOL><DEDENT>if isinstance(seed, np.random.RandomState):<EOL><INDENT>return seed<EOL><DEDENT>raise ValueError('<STR_LIT>'<EOL>'<STR_LIT>' % seed)<EOL>

Turn seed into a np.random.RandomState instance

    If seed is None, return the RandomState singleton used by np.random.
    If seed is an int, return a new RandomState instance seeded with seed.
    If seed is already a RandomState instance, return it.
    Otherwise raise ValueError.


samples = np.size(self.population, <NUM_LIT:0>)<EOL>N = np.size(self.population, <NUM_LIT:1>)<EOL>rng = self.random_number_generator<EOL>segsize = <NUM_LIT:1.0> / samples<EOL>rdrange = rng.rand(samples, N) * segsize<EOL>rdrange += np.atleast_2d(np.arange(<NUM_LIT:0.>, <NUM_LIT:1.>, segsize)).T<EOL>self.population = np.zeros_like(rdrange)<EOL>for j in range(N):<EOL><INDENT>order = rng.permutation(range(samples))<EOL>self.population[:, j] = rdrange[order, j]<EOL><DEDENT>

Initializes the population with Latin Hypercube Sampling
Latin Hypercube Sampling ensures that the sampling of parameter space
is maximised.


rng = self.random_number_generator<EOL>self.population = rng.random_sample(self.population.shape)<EOL>

Initialises the population at random.  This type of initialization
can possess clustering, Latin Hypercube sampling is generally better.


return self._scale_parameters(self.population[<NUM_LIT:0>])<EOL>

The best solution from the solver

Returns
-------
x - ndarray
    The best solution from the solver.


nfev, nit, warning_flag = <NUM_LIT:0>, <NUM_LIT:0>, False<EOL>status_message = _status_message['<STR_LIT:success>']<EOL>for index, candidate in enumerate(self.population):<EOL><INDENT>parameters = self._scale_parameters(candidate)<EOL>self.population_energies[index] = self.func(parameters,<EOL>*self.args)<EOL>nfev += <NUM_LIT:1><EOL>if nfev > self.maxfun:<EOL><INDENT>warning_flag = True<EOL>status_message = _status_message['<STR_LIT>']<EOL>break<EOL><DEDENT><DEDENT>minval = np.argmin(self.population_energies)<EOL>lowest_energy = self.population_energies[minval]<EOL>self.population_energies[minval] = self.population_energies[<NUM_LIT:0>]<EOL>self.population_energies[<NUM_LIT:0>] = lowest_energy<EOL>self.population[[<NUM_LIT:0>, minval], :] = self.population[[minval, <NUM_LIT:0>], :]<EOL>if warning_flag:<EOL><INDENT>return OptimizeResult(<EOL>x=self.x,<EOL>fun=self.population_energies[<NUM_LIT:0>],<EOL>nfev=nfev,<EOL>nit=nit,<EOL>message=status_message,<EOL>success=(warning_flag != True))<EOL><DEDENT>for nit in range(<NUM_LIT:1>, self.maxiter + <NUM_LIT:1>):<EOL><INDENT>if self.dither is not None:<EOL><INDENT>self.scale = self.random_number_generator.rand(<EOL>) * (self.dither[<NUM_LIT:1>] - self.dither[<NUM_LIT:0>]) + self.dither[<NUM_LIT:0>]<EOL><DEDENT>for candidate in range(np.size(self.population, <NUM_LIT:0>)):<EOL><INDENT>if nfev > self.maxfun:<EOL><INDENT>warning_flag = True<EOL>status_message = _status_message['<STR_LIT>']<EOL>break<EOL><DEDENT>trial = self._mutate(candidate)<EOL>self._ensure_constraint(trial)<EOL>parameters = self._scale_parameters(trial)<EOL>energy = self.func(parameters, *self.args)<EOL>nfev += <NUM_LIT:1><EOL>if energy < self.population_energies[candidate]:<EOL><INDENT>self.population[candidate] = trial<EOL>self.population_energies[candidate] = energy<EOL>if energy < self.population_energies[<NUM_LIT:0>]:<EOL><INDENT>self.population_energies[<NUM_LIT:0>] = energy<EOL>self.population[<NUM_LIT:0>] = trial<EOL><DEDENT><DEDENT><DEDENT>convergence = (np.std(self.population_energies) /<EOL>np.abs(np.mean(self.population_energies) +<EOL>_MACHEPS))<EOL>if self.disp:<EOL><INDENT>print("<STR_LIT>"<EOL>% (nit,<EOL>self.population_energies[<NUM_LIT:0>]))<EOL><DEDENT>if (self.callback and<EOL>self.callback(self._scale_parameters(self.population[<NUM_LIT:0>]),<EOL>convergence=self.tol / convergence) is True):<EOL><INDENT>warning_flag = True<EOL>status_message = ('<STR_LIT>'<EOL>'<STR_LIT>')<EOL>break<EOL><DEDENT>if convergence < self.tol or warning_flag:<EOL><INDENT>break<EOL><DEDENT><DEDENT>else:<EOL><INDENT>status_message = _status_message['<STR_LIT>']<EOL>warning_flag = True<EOL><DEDENT>DE_result = OptimizeResult(<EOL>x=self.x,<EOL>fun=self.population_energies[<NUM_LIT:0>],<EOL>nfev=nfev,<EOL>nit=nit,<EOL>message=status_message,<EOL>success=(warning_flag != True))<EOL>if self.polish:<EOL><INDENT>result = minimize(self.func,<EOL>np.copy(DE_result.x),<EOL>method='<STR_LIT>',<EOL>bounds=self.limits.T,<EOL>args=self.args)<EOL>nfev += result.nfev<EOL>DE_result.nfev = nfev<EOL>if result.fun < DE_result.fun:<EOL><INDENT>DE_result.fun = result.fun<EOL>DE_result.x = result.x<EOL>DE_result.jac = result.jac<EOL>self.population_energies[<NUM_LIT:0>] = result.fun<EOL>self.population[<NUM_LIT:0>] = self._unscale_parameters(result.x)<EOL><DEDENT><DEDENT>return DE_result<EOL>

Runs the DifferentialEvolutionSolver.

Returns
-------
res : OptimizeResult
    The optimization result represented as a ``OptimizeResult`` object.
    Important attributes are: ``x`` the solution array, ``success`` a
    Boolean flag indicating if the optimizer exited successfully and
    ``message`` which describes the cause of the termination. See
    `OptimizeResult` for a description of other attributes. If polish
    was employed, then OptimizeResult also contains the ``hess_inv`` and
    ``jac`` attributes.


return self.__scale_arg1 + (trial - <NUM_LIT:0.5>) * self.__scale_arg2<EOL>

scale from a number between 0 and 1 to parameters


return (parameters - self.__scale_arg1) / self.__scale_arg2 + <NUM_LIT:0.5><EOL>

scale from parameters to a number between 0 and 1.


for index, param in enumerate(trial):<EOL><INDENT>if param > <NUM_LIT:1> or param < <NUM_LIT:0>:<EOL><INDENT>trial[index] = self.random_number_generator.rand()<EOL><DEDENT><DEDENT>

make sure the parameters lie between the limits


trial = np.copy(self.population[candidate])<EOL>parameter_count = np.size(trial, <NUM_LIT:0>)<EOL>fill_point = self.random_number_generator.randint(<NUM_LIT:0>, parameter_count)<EOL>if (self.strategy == '<STR_LIT>'<EOL>or self.strategy == '<STR_LIT>'):<EOL><INDENT>bprime = self.mutation_func(candidate,<EOL>self._select_samples(candidate, <NUM_LIT:5>))<EOL><DEDENT>else:<EOL><INDENT>bprime = self.mutation_func(self._select_samples(candidate, <NUM_LIT:5>))<EOL><DEDENT>if self.strategy in self._binomial:<EOL><INDENT>crossovers = self.random_number_generator.rand(parameter_count)<EOL>crossovers = crossovers < self.cross_over_probability<EOL>crossovers[fill_point] = True<EOL>trial = np.where(crossovers, bprime, trial)<EOL>return trial<EOL><DEDENT>elif self.strategy in self._exponential:<EOL><INDENT>i = <NUM_LIT:0><EOL>while (i < parameter_count and<EOL>self.random_number_generator.rand() <<EOL>self.cross_over_probability):<EOL><INDENT>trial[fill_point] = bprime[fill_point]<EOL>fill_point = (fill_point + <NUM_LIT:1>) % parameter_count<EOL>i += <NUM_LIT:1><EOL><DEDENT>return trial<EOL><DEDENT>

create a trial vector based on a mutation strategy


r0, r1 = samples[:<NUM_LIT:2>]<EOL>return (self.population[<NUM_LIT:0>] + self.scale *<EOL>(self.population[r0] - self.population[r1]))<EOL>


r0, r1, r2 = samples[:<NUM_LIT:3>]<EOL>return (self.population[r0] + self.scale *<EOL>(self.population[r1] - self.population[r2]))<EOL>


def _randtobest1(self, candidate, samples):

r0, r1 = samples[:<NUM_LIT:2>]<EOL>bprime = np.copy(self.population[candidate])<EOL>bprime += self.scale * (self.population[<NUM_LIT:0>] - bprime)<EOL>bprime += self.scale * (self.population[r0] -<EOL>self.population[r1])<EOL>return bprime<EOL>


r0, r1, r2, r3 = samples[:<NUM_LIT:4>]<EOL>bprime = (self.population[<NUM_LIT:0>] + self.scale *<EOL>(self.population[r0] + self.population[r1]<EOL>- self.population[r2] - self.population[r3]))<EOL>return bprime<EOL>


r0, r1, r2, r3, r4 = samples<EOL>bprime = (self.population[r0] + self.scale *<EOL>(self.population[r1] + self.population[r2] -<EOL>self.population[r3] - self.population[r4]))<EOL>return bprime<EOL>


def _select_samples(self, candidate, number_samples):

idxs = list(range(np.size(self.population, <NUM_LIT:0>)))<EOL>idxs.remove(candidate)<EOL>self.random_number_generator.shuffle(idxs)<EOL>idxs = idxs[:number_samples]<EOL>return idxs<EOL>

obtain random integers from range(np.size(self.population, 0)),
without replacement.  You can't have the original candidate either.


def fsolve(func, x0, args=(), fprime=None, full_output=<NUM_LIT:0>,<EOL>col_deriv=<NUM_LIT:0>, xtol=<NUM_LIT>, maxfev=<NUM_LIT:0>, band=None,<EOL>epsfcn=None, factor=<NUM_LIT:100>, diag=None):

options = {'<STR_LIT>': col_deriv,<EOL>'<STR_LIT>': xtol,<EOL>'<STR_LIT>': maxfev,<EOL>'<STR_LIT>': band,<EOL>'<STR_LIT>': epsfcn,<EOL>'<STR_LIT>': factor,<EOL>'<STR_LIT>': diag,<EOL>'<STR_LIT>': full_output}<EOL>res = _root_hybr(func, x0, args, jac=fprime, **options)<EOL>if full_output:<EOL><INDENT>x = res['<STR_LIT:x>']<EOL>info = dict((k, res.get(k))<EOL>for k in ('<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT:r>', '<STR_LIT>') if k in res)<EOL>info['<STR_LIT>'] = res['<STR_LIT>']<EOL>return x, info, res['<STR_LIT:status>'], res['<STR_LIT:message>']<EOL><DEDENT>else:<EOL><INDENT>return res['<STR_LIT:x>']<EOL><DEDENT>

Find the roots of a function.

Return the roots of the (non-linear) equations defined by
``func(x) = 0`` given a starting estimate.

Parameters
----------
func : callable ``f(x, *args)``
    A function that takes at least one (possibly vector) argument.
x0 : ndarray
    The starting estimate for the roots of ``func(x) = 0``.
args : tuple, optional
    Any extra arguments to `func`.
fprime : callable(x), optional
    A function to compute the Jacobian of `func` with derivatives
    across the rows. By default, the Jacobian will be estimated.
full_output : bool, optional
    If True, return optional outputs.
col_deriv : bool, optional
    Specify whether the Jacobian function computes derivatives down
    the columns (faster, because there is no transpose operation).
xtol : float
    The calculation will terminate if the relative error between two
    consecutive iterates is at most `xtol`.
maxfev : int, optional
    The maximum number of calls to the function. If zero, then
    ``100*(N+1)`` is the maximum where N is the number of elements
    in `x0`.
band : tuple, optional
    If set to a two-sequence containing the number of sub- and
    super-diagonals within the band of the Jacobi matrix, the
    Jacobi matrix is considered banded (only for ``fprime=None``).
epsfcn : float, optional
    A suitable step length for the forward-difference
    approximation of the Jacobian (for ``fprime=None``). If
    `epsfcn` is less than the machine precision, it is assumed
    that the relative errors in the functions are of the order of
    the machine precision.
factor : float, optional
    A parameter determining the initial step bound
    (``factor * || diag * x||``).  Should be in the interval
    ``(0.1, 100)``.
diag : sequence, optional
    N positive entries that serve as a scale factors for the
    variables.

Returns
-------
x : ndarray
    The solution (or the result of the last iteration for
    an unsuccessful call).
infodict : dict
    A dictionary of optional outputs with the keys:

    ``nfev``
        number of function calls
    ``njev``
        number of Jacobian calls
    ``fvec``
        function evaluated at the output
    ``fjac``
        the orthogonal matrix, q, produced by the QR
        factorization of the final approximate Jacobian
        matrix, stored column wise
    ``r``
        upper triangular matrix produced by QR factorization
        of the same matrix
    ``qtf``
        the vector ``(transpose(q) * fvec)``

ier : int
    An integer flag.  Set to 1 if a solution was found, otherwise refer
    to `mesg` for more information.
mesg : str
    If no solution is found, `mesg` details the cause of failure.

See Also
--------
root : Interface to root finding algorithms for multivariate
functions. See the 'hybr' `method` in particular.

Notes
-----
``fsolve`` is a wrapper around MINPACK's hybrd and hybrj algorithms.


def _root_hybr(func, x0, args=(), jac=None,<EOL>col_deriv=<NUM_LIT:0>, xtol=<NUM_LIT>, maxfev=<NUM_LIT:0>, band=None, eps=None,<EOL>factor=<NUM_LIT:100>, diag=None, full_output=<NUM_LIT:0>, **unknown_options):

_check_unknown_options(unknown_options)<EOL>epsfcn = eps<EOL>x0 = asarray(x0).flatten()<EOL>n = len(x0)<EOL>if not isinstance(args, tuple):<EOL><INDENT>args = (args,)<EOL><DEDENT>shape, dtype = _check_func('<STR_LIT>', '<STR_LIT>', func, x0, args, n, (n,))<EOL>if epsfcn is None:<EOL><INDENT>epsfcn = finfo(dtype).eps<EOL><DEDENT>Dfun = jac<EOL>if Dfun is None:<EOL><INDENT>if band is None:<EOL><INDENT>ml, mu = -<NUM_LIT:10>, -<NUM_LIT:10><EOL><DEDENT>else:<EOL><INDENT>ml, mu = band[:<NUM_LIT:2>]<EOL><DEDENT>if maxfev == <NUM_LIT:0>:<EOL><INDENT>maxfev = <NUM_LIT:200> * (n + <NUM_LIT:1>)<EOL><DEDENT>retval = _minpack._hybrd(func, x0, args, <NUM_LIT:1>, xtol, maxfev,<EOL>ml, mu, epsfcn, factor, diag)<EOL><DEDENT>else:<EOL><INDENT>_check_func('<STR_LIT>', '<STR_LIT>', Dfun, x0, args, n, (n, n))<EOL>if (maxfev == <NUM_LIT:0>):<EOL><INDENT>maxfev = <NUM_LIT:100> * (n + <NUM_LIT:1>)<EOL><DEDENT>retval = _minpack._hybrj(func, Dfun, x0, args, <NUM_LIT:1>,<EOL>col_deriv, xtol, maxfev, factor, diag)<EOL><DEDENT>x, status = retval[<NUM_LIT:0>], retval[-<NUM_LIT:1>]<EOL>errors = {<NUM_LIT:0>: ["<STR_LIT>", TypeError],<EOL><NUM_LIT:1>: ["<STR_LIT>", None],<EOL><NUM_LIT:2>: ["<STR_LIT>"<EOL>"<STR_LIT>" % maxfev, ValueError],<EOL><NUM_LIT:3>: ["<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>" % xtol, ValueError],<EOL><NUM_LIT:4>: ["<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>", ValueError],<EOL><NUM_LIT:5>: ["<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>", ValueError],<EOL>'<STR_LIT>': ["<STR_LIT>", TypeError]}<EOL>if status != <NUM_LIT:1> and not full_output:<EOL><INDENT>if status in [<NUM_LIT:2>, <NUM_LIT:3>, <NUM_LIT:4>, <NUM_LIT:5>]:<EOL><INDENT>msg = errors[status][<NUM_LIT:0>]<EOL>warnings.warn(msg, RuntimeWarning)<EOL><DEDENT>else:<EOL><INDENT>try:<EOL><INDENT>raise errors[status][<NUM_LIT:1>](errors[status][<NUM_LIT:0>])<EOL><DEDENT>except KeyError:<EOL><INDENT>raise errors['<STR_LIT>'][<NUM_LIT:1>](errors['<STR_LIT>'][<NUM_LIT:0>])<EOL><DEDENT><DEDENT><DEDENT>info = retval[<NUM_LIT:1>]<EOL>info['<STR_LIT>'] = info.pop('<STR_LIT>')<EOL>sol = OptimizeResult(x=x, success=(status == <NUM_LIT:1>), status=status)<EOL>sol.update(info)<EOL>try:<EOL><INDENT>sol['<STR_LIT:message>'] = errors[status][<NUM_LIT:0>]<EOL><DEDENT>except KeyError:<EOL><INDENT>info['<STR_LIT:message>'] = errors['<STR_LIT>'][<NUM_LIT:0>]<EOL><DEDENT>return sol<EOL>

Find the roots of a multivariate function using MINPACK's hybrd and
hybrj routines (modified Powell method).

Options for the hybrd algorithm are:
    col_deriv : bool
        Specify whether the Jacobian function computes derivatives down
        the columns (faster, because there is no transpose operation).
    xtol : float
        The calculation will terminate if the relative error between two
        consecutive iterates is at most `xtol`.
    maxfev : int
        The maximum number of calls to the function. If zero, then
        ``100*(N+1)`` is the maximum where N is the number of elements
        in `x0`.
    band : tuple
        If set to a two-sequence containing the number of sub- and
        super-diagonals within the band of the Jacobi matrix, the
        Jacobi matrix is considered banded (only for ``fprime=None``).
    eps : float
        A suitable step length for the forward-difference
        approximation of the Jacobian (for ``fprime=None``). If
        `eps` is less than the machine precision, it is assumed
        that the relative errors in the functions are of the order of
        the machine precision.
    factor : float
        A parameter determining the initial step bound
        (``factor * || diag * x||``).  Should be in the interval
        ``(0.1, 100)``.
    diag : sequence
        N positive entries that serve as a scale factors for the
        variables.

This function is called by the `root` function with `method=hybr`. It
is not supposed to be called directly.


def leastsq(func, x0, args=(), Dfun=None, full_output=<NUM_LIT:0>,<EOL>col_deriv=<NUM_LIT:0>, ftol=<NUM_LIT>, xtol=<NUM_LIT>,<EOL>gtol=<NUM_LIT:0.0>, maxfev=<NUM_LIT:0>, epsfcn=None, factor=<NUM_LIT:100>, diag=None):

x0 = asarray(x0).flatten()<EOL>n = len(x0)<EOL>if not isinstance(args, tuple):<EOL><INDENT>args = (args,)<EOL><DEDENT>shape, dtype = _check_func('<STR_LIT>', '<STR_LIT>', func, x0, args, n)<EOL>m = shape[<NUM_LIT:0>]<EOL>if n > m:<EOL><INDENT>raise TypeError('<STR_LIT>' % (n, m))<EOL><DEDENT>if epsfcn is None:<EOL><INDENT>epsfcn = finfo(dtype).eps<EOL><DEDENT>if Dfun is None:<EOL><INDENT>if maxfev == <NUM_LIT:0>:<EOL><INDENT>maxfev = <NUM_LIT:200>*(n + <NUM_LIT:1>)<EOL><DEDENT>retval = _minpack._lmdif(func, x0, args, full_output, ftol, xtol,<EOL>gtol, maxfev, epsfcn, factor, diag)<EOL><DEDENT>else:<EOL><INDENT>if col_deriv:<EOL><INDENT>_check_func('<STR_LIT>', '<STR_LIT>', Dfun, x0, args, n, (n, m))<EOL><DEDENT>else:<EOL><INDENT>_check_func('<STR_LIT>', '<STR_LIT>', Dfun, x0, args, n, (m, n))<EOL><DEDENT>if maxfev == <NUM_LIT:0>:<EOL><INDENT>maxfev = <NUM_LIT:100> * (n + <NUM_LIT:1>)<EOL><DEDENT>retval = _minpack._lmder(func, Dfun, x0, args, full_output, col_deriv,<EOL>ftol, xtol, gtol, maxfev, factor, diag)<EOL><DEDENT>errors = {<NUM_LIT:0>: ["<STR_LIT>", TypeError],<EOL><NUM_LIT:1>: ["<STR_LIT>"<EOL>"<STR_LIT>" % ftol, None],<EOL><NUM_LIT:2>: ["<STR_LIT>"<EOL>"<STR_LIT>" % xtol, None],<EOL><NUM_LIT:3>: ["<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>" % (ftol, xtol), None],<EOL><NUM_LIT:4>: ["<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>" % gtol, None],<EOL><NUM_LIT:5>: ["<STR_LIT>"<EOL>"<STR_LIT>" % maxfev, ValueError],<EOL><NUM_LIT:6>: ["<STR_LIT>"<EOL>"<STR_LIT>""<STR_LIT>" % ftol,<EOL>ValueError],<EOL><NUM_LIT:7>: ["<STR_LIT>"<EOL>"<STR_LIT>" % xtol,<EOL>ValueError],<EOL><NUM_LIT:8>: ["<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>" % gtol, ValueError],<EOL>'<STR_LIT>': ["<STR_LIT>", TypeError]}<EOL>info = retval[-<NUM_LIT:1>]    <EOL>if info not in [<NUM_LIT:1>, <NUM_LIT:2>, <NUM_LIT:3>, <NUM_LIT:4>] and not full_output:<EOL><INDENT>if info in [<NUM_LIT:5>, <NUM_LIT:6>, <NUM_LIT:7>, <NUM_LIT:8>]:<EOL><INDENT>warnings.warn(errors[info][<NUM_LIT:0>], RuntimeWarning)<EOL><DEDENT>else:<EOL><INDENT>try:<EOL><INDENT>raise errors[info][<NUM_LIT:1>](errors[info][<NUM_LIT:0>])<EOL><DEDENT>except KeyError:<EOL><INDENT>raise errors['<STR_LIT>'][<NUM_LIT:1>](errors['<STR_LIT>'][<NUM_LIT:0>])<EOL><DEDENT><DEDENT><DEDENT>mesg = errors[info][<NUM_LIT:0>]<EOL>if full_output:<EOL><INDENT>cov_x = None<EOL>if info in [<NUM_LIT:1>, <NUM_LIT:2>, <NUM_LIT:3>, <NUM_LIT:4>]:<EOL><INDENT>from numpy.dual import inv<EOL>from numpy.linalg import LinAlgError<EOL>perm = take(eye(n), retval[<NUM_LIT:1>]['<STR_LIT>'] - <NUM_LIT:1>, <NUM_LIT:0>)<EOL>r = triu(transpose(retval[<NUM_LIT:1>]['<STR_LIT>'])[:n, :])<EOL>R = dot(r, perm)<EOL>try:<EOL><INDENT>cov_x = inv(dot(transpose(R), R))<EOL><DEDENT>except (LinAlgError, ValueError):<EOL><INDENT>pass<EOL><DEDENT><DEDENT>return (retval[<NUM_LIT:0>], cov_x) + retval[<NUM_LIT:1>:-<NUM_LIT:1>] + (mesg, info)<EOL><DEDENT>else:<EOL><INDENT>return (retval[<NUM_LIT:0>], info)<EOL><DEDENT>

Minimize the sum of squares of a set of equations.

::

    x = arg min(sum(func(y)**2,axis=0))
             y

Parameters
----------
func : callable
    should take at least one (possibly length N vector) argument and
    returns M floating point numbers. It must not return NaNs or
    fitting might fail.
x0 : ndarray
    The starting estimate for the minimization.
args : tuple
    Any extra arguments to func are placed in this tuple.
Dfun : callable
    A function or method to compute the Jacobian of func with derivatives
    across the rows. If this is None, the Jacobian will be estimated.
full_output : bool
    non-zero to return all optional outputs.
col_deriv : bool
    non-zero to specify that the Jacobian function computes derivatives
    down the columns (faster, because there is no transpose operation).
ftol : float
    Relative error desired in the sum of squares.
xtol : float
    Relative error desired in the approximate solution.
gtol : float
    Orthogonality desired between the function vector and the columns of
    the Jacobian.
maxfev : int
    The maximum number of calls to the function. If zero, then 100*(N+1) is
    the maximum where N is the number of elements in x0.
epsfcn : float
    A variable used in determining a suitable step length for the forward-
    difference approximation of the Jacobian (for Dfun=None).
    Normally the actual step length will be sqrt(epsfcn)*x
    If epsfcn is less than the machine precision, it is assumed that the
    relative errors are of the order of the machine precision.
factor : float
    A parameter determining the initial step bound
    (``factor * || diag * x||``). Should be in interval ``(0.1, 100)``.
diag : sequence
    N positive entries that serve as a scale factors for the variables.

Returns
-------
x : ndarray
    The solution (or the result of the last iteration for an unsuccessful
    call).
cov_x : ndarray
    Uses the fjac and ipvt optional outputs to construct an
    estimate of the jacobian around the solution. None if a
    singular matrix encountered (indicates very flat curvature in
    some direction).  This matrix must be multiplied by the
    residual variance to get the covariance of the
    parameter estimates -- see curve_fit.
infodict : dict
    a dictionary of optional outputs with the key s:

    ``nfev``
        The number of function calls
    ``fvec``
        The function evaluated at the output
    ``fjac``
        A permutation of the R matrix of a QR
        factorization of the final approximate
        Jacobian matrix, stored column wise.
        Together with ipvt, the covariance of the
        estimate can be approximated.
    ``ipvt``
        An integer array of length N which defines
        a permutation matrix, p, such that
        fjac*p = q*r, where r is upper triangular
        with diagonal elements of nonincreasing
        magnitude. Column j of p is column ipvt(j)
        of the identity matrix.
    ``qtf``
        The vector (transpose(q) * fvec).

mesg : str
    A string message giving information about the cause of failure.
ier : int
    An integer flag.  If it is equal to 1, 2, 3 or 4, the solution was
    found.  Otherwise, the solution was not found. In either case, the
    optional output variable 'mesg' gives more information.

Notes
-----
"leastsq" is a wrapper around MINPACK's lmdif and lmder algorithms.

cov_x is a Jacobian approximation to the Hessian of the least squares
objective function.
This approximation assumes that the objective function is based on the
difference between some observed target data (ydata) and a (non-linear)
function of the parameters `f(xdata, params)` ::

       func(params) = ydata - f(xdata, params)

so that the objective function is ::

       min   sum((ydata - f(xdata, params))**2, axis=0)
     params


def curve_fit(f, xdata, ydata, p0=None, sigma=None, absolute_sigma=False,<EOL>check_finite=True, **kw):

if p0 is None:<EOL><INDENT>import inspect<EOL>args, varargs, varkw, defaults = inspect.getargspec(f)<EOL>if len(args) < <NUM_LIT:2>:<EOL><INDENT>msg = "<STR_LIT>"<EOL>raise ValueError(msg)<EOL><DEDENT>if '<STR_LIT>' in args:<EOL><INDENT>p0 = [<NUM_LIT:1.0>] * (len(args)-<NUM_LIT:2>)<EOL><DEDENT>else:<EOL><INDENT>p0 = [<NUM_LIT:1.0>] * (len(args)-<NUM_LIT:1>)<EOL><DEDENT><DEDENT>if isscalar(p0):<EOL><INDENT>p0 = array([p0])<EOL><DEDENT>if check_finite:<EOL><INDENT>ydata = np.asarray_chkfinite(ydata)<EOL><DEDENT>else:<EOL><INDENT>ydata = np.asarray(ydata)<EOL><DEDENT>if isinstance(xdata, (list, tuple, np.ndarray)):<EOL><INDENT>if check_finite:<EOL><INDENT>xdata = np.asarray_chkfinite(xdata)<EOL><DEDENT>else:<EOL><INDENT>xdata = np.asarray(xdata)<EOL><DEDENT><DEDENT>args = (xdata, ydata, f)<EOL>if sigma is None:<EOL><INDENT>func = _general_function<EOL><DEDENT>else:<EOL><INDENT>func = _weighted_general_function<EOL>args += (<NUM_LIT:1.0> / asarray(sigma),)<EOL><DEDENT>return_full = kw.pop('<STR_LIT>', False)<EOL>res = leastsq(func, p0, args=args, full_output=<NUM_LIT:1>, **kw)<EOL>(popt, pcov, infodict, errmsg, ier) = res<EOL>if ier not in [<NUM_LIT:1>, <NUM_LIT:2>, <NUM_LIT:3>, <NUM_LIT:4>]:<EOL><INDENT>msg = "<STR_LIT>" + errmsg<EOL>raise RuntimeError(msg)<EOL><DEDENT>warn_cov = False<EOL>if pcov is None:<EOL><INDENT>pcov = zeros((len(popt), len(popt)), dtype=float)<EOL>pcov.fill(inf)<EOL>warn_cov = True<EOL><DEDENT>elif not absolute_sigma:<EOL><INDENT>if len(ydata) > len(p0):<EOL><INDENT>s_sq = (asarray(func(popt, *args))**<NUM_LIT:2>).sum() / (len(ydata) - len(p0))<EOL>pcov = pcov * s_sq<EOL><DEDENT>else:<EOL><INDENT>pcov.fill(inf)<EOL>warn_cov = True<EOL><DEDENT><DEDENT>if warn_cov:<EOL><INDENT>warnings.warn('<STR_LIT>',<EOL>category=OptimizeWarning)<EOL><DEDENT>if return_full:<EOL><INDENT>return popt, pcov, infodict, errmsg, ier<EOL><DEDENT>else:<EOL><INDENT>return popt, pcov<EOL><DEDENT>

Use non-linear least squares to fit a function, f, to data.

Assumes ``ydata = f(xdata, *params) + eps``

Parameters
----------
f : callable
    The model function, f(x, ...).  It must take the independent
    variable as the first argument and the parameters to fit as
    separate remaining arguments.
xdata : An M-length sequence or an (k,M)-shaped array
    for functions with k predictors.
    The independent variable where the data is measured.
ydata : M-length sequence
    The dependent data --- nominally f(xdata, ...)
p0 : None, scalar, or N-length sequence
    Initial guess for the parameters.  If None, then the initial
    values will all be 1 (if the number of parameters for the function
    can be determined using introspection, otherwise a ValueError
    is raised).
sigma : None or M-length sequence, optional
    If not None, the uncertainties in the ydata array. These are used as
    weights in the least-squares problem
    i.e. minimising ``np.sum( ((f(xdata, *popt) - ydata) / sigma)**2 )``
    If None, the uncertainties are assumed to be 1.
absolute_sigma : bool, optional
    If False, `sigma` denotes relative weights of the data points.
    The returned covariance matrix `pcov` is based on *estimated*
    errors in the data, and is not affected by the overall
    magnitude of the values in `sigma`. Only the relative
    magnitudes of the `sigma` values matter.

    If True, `sigma` describes one standard deviation errors of
    the input data points. The estimated covariance in `pcov` is
    based on these values.
check_finite : bool, optional
    If True, check that the input arrays do not contain nans of infs,
    and raise a ValueError if they do. Setting this parameter to
    False may silently produce nonsensical results if the input arrays
    do contain nans.
    Default is True.

Returns
-------
popt : array
    Optimal values for the parameters so that the sum of the squared error
    of ``f(xdata, *popt) - ydata`` is minimized
pcov : 2d array
    The estimated covariance of popt. The diagonals provide the variance
    of the parameter estimate. To compute one standard deviation errors
    on the parameters use ``perr = np.sqrt(np.diag(pcov))``.

    How the `sigma` parameter affects the estimated covariance
    depends on `absolute_sigma` argument, as described above.

Raises
------
OptimizeWarning
    if covariance of the parameters can not be estimated.

ValueError
    if ydata and xdata contain NaNs.

See Also
--------
leastsq

Notes
-----
The algorithm uses the Levenberg-Marquardt algorithm through `leastsq`.
Additional keyword arguments are passed directly to that algorithm.

Examples
--------
>>> import numpy as np
>>> from scipy.optimize import curve_fit
>>> def func(x, a, b, c):
...     return a * np.exp(-b * x) + c

>>> xdata = np.linspace(0, 4, 50)
>>> y = func(xdata, 2.5, 1.3, 0.5)
>>> ydata = y + 0.2 * np.random.normal(size=len(xdata))

>>> popt, pcov = curve_fit(func, xdata, ydata)


def check_gradient(fcn, Dfcn, x0, args=(), col_deriv=<NUM_LIT:0>):

x = atleast_1d(x0)<EOL>n = len(x)<EOL>x = x.reshape((n,))<EOL>fvec = atleast_1d(fcn(x, *args))<EOL>m = len(fvec)<EOL>fvec = fvec.reshape((m,))<EOL>ldfjac = m<EOL>fjac = atleast_1d(Dfcn(x, *args))<EOL>fjac = fjac.reshape((m, n))<EOL>if col_deriv == <NUM_LIT:0>:<EOL><INDENT>fjac = transpose(fjac)<EOL><DEDENT>xp = zeros((n,), float)<EOL>err = zeros((m,), float)<EOL>fvecp = None<EOL>_minpack._chkder(m, n, x, fvec, fjac, ldfjac, xp, fvecp, <NUM_LIT:1>, err)<EOL>fvecp = atleast_1d(fcn(xp, *args))<EOL>fvecp = fvecp.reshape((m,))<EOL>_minpack._chkder(m, n, x, fvec, fjac, ldfjac, xp, fvecp, <NUM_LIT:2>, err)<EOL>good = (product(greater(err, <NUM_LIT:0.5>), axis=<NUM_LIT:0>))<EOL>return (good, err)<EOL>

Perform a simple check on the gradient for correctness.


def fixed_point(func, x0, args=(), xtol=<NUM_LIT>, maxiter=<NUM_LIT>):

if not isscalar(x0):<EOL><INDENT>x0 = asarray(x0)<EOL>p0 = x0<EOL>for iter in range(maxiter):<EOL><INDENT>p1 = func(p0, *args)<EOL>p2 = func(p1, *args)<EOL>d = p2 - <NUM_LIT> * p1 + p0<EOL>p = where(d == <NUM_LIT:0>, p2, p0 - (p1 - p0)*(p1 - p0) / d)<EOL>relerr = where(p0 == <NUM_LIT:0>, p, (p-p0)/p0)<EOL>if all(abs(relerr) < xtol):<EOL><INDENT>return p<EOL><DEDENT>p0 = p<EOL><DEDENT><DEDENT>else:<EOL><INDENT>p0 = x0<EOL>for iter in range(maxiter):<EOL><INDENT>p1 = func(p0, *args)<EOL>p2 = func(p1, *args)<EOL>d = p2 - <NUM_LIT> * p1 + p0<EOL>if d == <NUM_LIT:0.0>:<EOL><INDENT>return p2<EOL><DEDENT>else:<EOL><INDENT>p = p0 - (p1 - p0)*(p1 - p0) / d<EOL><DEDENT>if p0 == <NUM_LIT:0>:<EOL><INDENT>relerr = p<EOL><DEDENT>else:<EOL><INDENT>relerr = (p - p0)/p0<EOL><DEDENT>if abs(relerr) < xtol:<EOL><INDENT>return p<EOL><DEDENT>p0 = p<EOL><DEDENT><DEDENT>msg = "<STR_LIT>" % (maxiter, p)<EOL>raise RuntimeError(msg)<EOL>

Find a fixed point of the function.

Given a function of one or more variables and a starting point, find a
fixed-point of the function: i.e. where ``func(x0) == x0``.

Parameters
----------
func : function
    Function to evaluate.
x0 : array_like
    Fixed point of function.
args : tuple, optional
    Extra arguments to `func`.
xtol : float, optional
    Convergence tolerance, defaults to 1e-08.
maxiter : int, optional
    Maximum number of iterations, defaults to 500.

Notes
-----
Uses Steffensen's Method using Aitken's ``Del^2`` convergence acceleration.
See Burden, Faires, "Numerical Analysis", 5th edition, pg. 80

Examples
--------
>>> from scipy import optimize
>>> def func(x, c1, c2):
....    return np.sqrt(c1/(x+c2))
>>> c1 = np.array([10,12.])
>>> c2 = np.array([3, 5.])
>>> optimize.fixed_point(func, [1.2, 1.3], args=(c1,c2))
array([ 1.4920333 ,  1.37228132])


def fmin_tnc(func, x0, fprime=None, args=(), approx_grad=<NUM_LIT:0>,<EOL>bounds=None, epsilon=<NUM_LIT>, scale=None, offset=None,<EOL>messages=MSG_ALL, maxCGit=-<NUM_LIT:1>, maxfun=None, eta=-<NUM_LIT:1>,<EOL>stepmx=<NUM_LIT:0>, accuracy=<NUM_LIT:0>, fmin=<NUM_LIT:0>, ftol=-<NUM_LIT:1>, xtol=-<NUM_LIT:1>, pgtol=-<NUM_LIT:1>,<EOL>rescale=-<NUM_LIT:1>, disp=None, callback=None):

<EOL>if approx_grad:<EOL><INDENT>fun = func<EOL>jac = None<EOL><DEDENT>elif fprime is None:<EOL><INDENT>fun = MemoizeJac(func)<EOL>jac = fun.derivative<EOL><DEDENT>else:<EOL><INDENT>fun = func<EOL>jac = fprime<EOL><DEDENT>if disp is not None:  <EOL><INDENT>mesg_num = disp<EOL><DEDENT>else:<EOL><INDENT>mesg_num = {<NUM_LIT:0>:MSG_NONE, <NUM_LIT:1>:MSG_ITER, <NUM_LIT:2>:MSG_INFO, <NUM_LIT:3>:MSG_VERS,<EOL><NUM_LIT:4>:MSG_EXIT, <NUM_LIT:5>:MSG_ALL}.get(messages, MSG_ALL)<EOL><DEDENT>opts = {'<STR_LIT>': epsilon,<EOL>'<STR_LIT>': scale,<EOL>'<STR_LIT>': offset,<EOL>'<STR_LIT>': mesg_num,<EOL>'<STR_LIT>': maxCGit,<EOL>'<STR_LIT>': maxfun,<EOL>'<STR_LIT>': eta,<EOL>'<STR_LIT>': stepmx,<EOL>'<STR_LIT>': accuracy,<EOL>'<STR_LIT>': fmin,<EOL>'<STR_LIT>': ftol,<EOL>'<STR_LIT>': xtol,<EOL>'<STR_LIT>': pgtol,<EOL>'<STR_LIT>': rescale,<EOL>'<STR_LIT>': False}<EOL>res = _minimize_tnc(fun, x0, args, jac, bounds, callback=callback, **opts)<EOL>return res['<STR_LIT:x>'], res['<STR_LIT>'], res['<STR_LIT:status>']<EOL>

Minimize a function with variables subject to bounds, using
gradient information in a truncated Newton algorithm. This
method wraps a C implementation of the algorithm.

Parameters
----------
func : callable ``func(x, *args)``
    Function to minimize.  Must do one of:

    1. Return f and g, where f is the value of the function and g its
       gradient (a list of floats).

    2. Return the function value but supply gradient function
       separately as `fprime`.

    3. Return the function value and set ``approx_grad=True``.

    If the function returns None, the minimization
    is aborted.
x0 : array_like
    Initial estimate of minimum.
fprime : callable ``fprime(x, *args)``
    Gradient of `func`. If None, then either `func` must return the
    function value and the gradient (``f,g = func(x, *args)``)
    or `approx_grad` must be True.
args : tuple
    Arguments to pass to function.
approx_grad : bool
    If true, approximate the gradient numerically.
bounds : list
    (min, max) pairs for each element in x0, defining the
    bounds on that parameter. Use None or +/-inf for one of
    min or max when there is no bound in that direction.
epsilon : float
    Used if approx_grad is True. The stepsize in a finite
    difference approximation for fprime.
scale : array_like
    Scaling factors to apply to each variable.  If None, the
    factors are up-low for interval bounded variables and
    1+|x| for the others.  Defaults to None.
offset : array_like
    Value to subtract from each variable.  If None, the
    offsets are (up+low)/2 for interval bounded variables
    and x for the others.
messages :
    Bit mask used to select messages display during
    minimization values defined in the MSGS dict.  Defaults to
    MGS_ALL.
disp : int
    Integer interface to messages.  0 = no message, 5 = all messages
maxCGit : int
    Maximum number of hessian*vector evaluations per main
    iteration.  If maxCGit == 0, the direction chosen is
    -gradient if maxCGit < 0, maxCGit is set to
    max(1,min(50,n/2)).  Defaults to -1.
maxfun : int
    Maximum number of function evaluation.  if None, maxfun is
    set to max(100, 10*len(x0)).  Defaults to None.
eta : float
    Severity of the line search. if < 0 or > 1, set to 0.25.
    Defaults to -1.
stepmx : float
    Maximum step for the line search.  May be increased during
    call.  If too small, it will be set to 10.0.  Defaults to 0.
accuracy : float
    Relative precision for finite difference calculations.  If
    <= machine_precision, set to sqrt(machine_precision).
    Defaults to 0.
fmin : float
    Minimum function value estimate.  Defaults to 0.
ftol : float
    Precision goal for the value of f in the stoping criterion.
    If ftol < 0.0, ftol is set to 0.0 defaults to -1.
xtol : float
    Precision goal for the value of x in the stopping
    criterion (after applying x scaling factors).  If xtol <
    0.0, xtol is set to sqrt(machine_precision).  Defaults to
    -1.
pgtol : float
    Precision goal for the value of the projected gradient in
    the stopping criterion (after applying x scaling factors).
    If pgtol < 0.0, pgtol is set to 1e-2 * sqrt(accuracy).
    Setting it to 0.0 is not recommended.  Defaults to -1.
rescale : float
    Scaling factor (in log10) used to trigger f value
    rescaling.  If 0, rescale at each iteration.  If a large
    value, never rescale.  If < 0, rescale is set to 1.3.
callback : callable, optional
    Called after each iteration, as callback(xk), where xk is the
    current parameter vector.

Returns
-------
x : ndarray
    The solution.
nfeval : int
    The number of function evaluations.
rc : int
    Return code as defined in the RCSTRINGS dict.

See also
--------
minimize: Interface to minimization algorithms for multivariate
    functions. See the 'TNC' `method` in particular.

Notes
-----
The underlying algorithm is truncated Newton, also called
Newton Conjugate-Gradient. This method differs from
scipy.optimize.fmin_ncg in that

1. It wraps a C implementation of the algorithm
2. It allows each variable to be given an upper and lower bound.

The algorithm incoporates the bound constraints by determining
the descent direction as in an unconstrained truncated Newton,
but never taking a step-size large enough to leave the space
of feasible x's. The algorithm keeps track of a set of
currently active constraints, and ignores them when computing
the minimum allowable step size. (The x's associated with the
active constraint are kept fixed.) If the maximum allowable
step size is zero then a new constraint is added. At the end
of each iteration one of the constraints may be deemed no
longer active and removed. A constraint is considered
no longer active is if it is currently active
but the gradient for that variable points inward from the
constraint. The specific constraint removed is the one
associated with the variable of largest index whose
constraint is no longer active.

References
----------
Wright S., Nocedal J. (2006), 'Numerical Optimization'

Nash S.G. (1984), "Newton-Type Minimization Via the Lanczos Method",
SIAM Journal of Numerical Analysis 21, pp. 770-778


def _minimize_tnc(fun, x0, args=(), jac=None, bounds=None,<EOL>eps=<NUM_LIT>, scale=None, offset=None, mesg_num=None,<EOL>maxCGit=-<NUM_LIT:1>, maxiter=None, eta=-<NUM_LIT:1>, stepmx=<NUM_LIT:0>, accuracy=<NUM_LIT:0>,<EOL>minfev=<NUM_LIT:0>, ftol=-<NUM_LIT:1>, xtol=-<NUM_LIT:1>, gtol=-<NUM_LIT:1>, rescale=-<NUM_LIT:1>, disp=False,<EOL>callback=None, **unknown_options):

_check_unknown_options(unknown_options)<EOL>epsilon = eps<EOL>maxfun = maxiter<EOL>fmin = minfev<EOL>pgtol = gtol<EOL>x0 = asfarray(x0).flatten()<EOL>n = len(x0)<EOL>if bounds is None:<EOL><INDENT>bounds = [(None,None)] * n<EOL><DEDENT>if len(bounds) != n:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>if mesg_num is not None:<EOL><INDENT>messages = {<NUM_LIT:0>:MSG_NONE, <NUM_LIT:1>:MSG_ITER, <NUM_LIT:2>:MSG_INFO, <NUM_LIT:3>:MSG_VERS,<EOL><NUM_LIT:4>:MSG_EXIT, <NUM_LIT:5>:MSG_ALL}.get(mesg_num, MSG_ALL)<EOL><DEDENT>elif disp:<EOL><INDENT>messages = MSG_ALL<EOL><DEDENT>else:<EOL><INDENT>messages = MSG_NONE<EOL><DEDENT>if jac is None:<EOL><INDENT>def func_and_grad(x):<EOL><INDENT>f = fun(x, *args)<EOL>g = approx_fprime(x, fun, epsilon, *args)<EOL>return f, g<EOL><DEDENT><DEDENT>else:<EOL><INDENT>def func_and_grad(x):<EOL><INDENT>f = fun(x, *args)<EOL>g = jac(x, *args)<EOL>return f, g<EOL><DEDENT><DEDENT>"""<STR_LIT>"""<EOL>low = zeros(n)<EOL>up = zeros(n)<EOL>for i in range(n):<EOL><INDENT>if bounds[i] is None:<EOL><INDENT>l, u = -inf, inf<EOL><DEDENT>else:<EOL><INDENT>l,u = bounds[i]<EOL>if l is None:<EOL><INDENT>low[i] = -inf<EOL><DEDENT>else:<EOL><INDENT>low[i] = l<EOL><DEDENT>if u is None:<EOL><INDENT>up[i] = inf<EOL><DEDENT>else:<EOL><INDENT>up[i] = u<EOL><DEDENT><DEDENT><DEDENT>if scale is None:<EOL><INDENT>scale = array([])<EOL><DEDENT>if offset is None:<EOL><INDENT>offset = array([])<EOL><DEDENT>if maxfun is None:<EOL><INDENT>maxfun = max(<NUM_LIT:100>, <NUM_LIT:10>*len(x0))<EOL><DEDENT>rc, nf, nit, x = moduleTNC.minimize(func_and_grad, x0, low, up, scale,<EOL>offset, messages, maxCGit, maxfun,<EOL>eta, stepmx, accuracy, fmin, ftol,<EOL>xtol, pgtol, rescale, callback)<EOL>funv, jacv = func_and_grad(x)<EOL>return OptimizeResult(x=x, fun=funv, jac=jacv, nfev=nf, nit=nit, status=rc,<EOL>message=RCSTRINGS[rc], success=(-<NUM_LIT:1> < rc < <NUM_LIT:3>))<EOL>

Minimize a scalar function of one or more variables using a truncated
Newton (TNC) algorithm.

Options for the TNC algorithm are:
    eps : float
        Step size used for numerical approximation of the jacobian.
    scale : list of floats
        Scaling factors to apply to each variable.  If None, the
        factors are up-low for interval bounded variables and
        1+|x] fo the others.  Defaults to None
    offset : float
        Value to subtract from each variable.  If None, the
        offsets are (up+low)/2 for interval bounded variables
        and x for the others.
    disp : bool
       Set to True to print convergence messages.
    maxCGit : int
        Maximum number of hessian*vector evaluations per main
        iteration.  If maxCGit == 0, the direction chosen is
        -gradient if maxCGit < 0, maxCGit is set to
        max(1,min(50,n/2)).  Defaults to -1.
    maxiter : int
        Maximum number of function evaluation.  if None, `maxiter` is
        set to max(100, 10*len(x0)).  Defaults to None.
    eta : float
        Severity of the line search. if < 0 or > 1, set to 0.25.
        Defaults to -1.
    stepmx : float
        Maximum step for the line search.  May be increased during
        call.  If too small, it will be set to 10.0.  Defaults to 0.
    accuracy : float
        Relative precision for finite difference calculations.  If
        <= machine_precision, set to sqrt(machine_precision).
        Defaults to 0.
    minfev : float
        Minimum function value estimate.  Defaults to 0.
    ftol : float
        Precision goal for the value of f in the stoping criterion.
        If ftol < 0.0, ftol is set to 0.0 defaults to -1.
    xtol : float
        Precision goal for the value of x in the stopping
        criterion (after applying x scaling factors).  If xtol <
        0.0, xtol is set to sqrt(machine_precision).  Defaults to
        -1.
    gtol : float
        Precision goal for the value of the projected gradient in
        the stopping criterion (after applying x scaling factors).
        If gtol < 0.0, gtol is set to 1e-2 * sqrt(accuracy).
        Setting it to 0.0 is not recommended.  Defaults to -1.
    rescale : float
        Scaling factor (in log10) used to trigger f value
        rescaling.  If 0, rescale at each iteration.  If a large
        value, never rescale.  If < 0, rescale is set to 1.3.

This function is called by the `minimize` function with `method=TNC`.
It is not supposed to be called directly.


def _minimize_dogleg(fun, x0, args=(), jac=None, hess=None,<EOL>**trust_region_options):

if jac is None:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>if hess is None:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>return _minimize_trust_region(fun, x0, args=args, jac=jac, hess=hess,<EOL>subproblem=DoglegSubproblem,<EOL>**trust_region_options)<EOL>

Minimization of scalar function of one or more variables using
the dog-leg trust-region algorithm.

This function is called by the `minimize` function.
It is not supposed to be called directly.


if self._cauchy_point is None:<EOL><INDENT>g = self.jac<EOL>Bg = self.hessp(g)<EOL>self._cauchy_point = -(np.dot(g, g) / np.dot(g, Bg)) * g<EOL><DEDENT>return self._cauchy_point<EOL>

The Cauchy point is minimal along the direction of steepest descent.


if self._newton_point is None:<EOL><INDENT>g = self.jac<EOL>B = self.hess<EOL>cho_info = scipy.linalg.cho_factor(B)<EOL>self._newton_point = -scipy.linalg.cho_solve(cho_info, g)<EOL><DEDENT>return self._newton_point<EOL>

The Newton point is a global minimum of the approximate function.


<EOL>p_best = self.newton_point()<EOL>if scipy.linalg.norm(p_best) < trust_radius:<EOL><INDENT>hits_boundary = False<EOL>return p_best, hits_boundary<EOL><DEDENT>p_u = self.cauchy_point()<EOL>p_u_norm = scipy.linalg.norm(p_u)<EOL>if p_u_norm >= trust_radius:<EOL><INDENT>p_boundary = p_u * (trust_radius / p_u_norm)<EOL>hits_boundary = True<EOL>return p_boundary, hits_boundary<EOL><DEDENT>_, tb = self.get_boundaries_intersections(p_u, p_best - p_u,<EOL>trust_radius)<EOL>p_boundary = p_u + tb * (p_best - p_u)<EOL>hits_boundary = True<EOL>return p_boundary, hits_boundary<EOL>

Minimize a function using the dog-leg trust-region algorithm.

This algorithm requires function values and first and second derivatives.
It also performs a costly Hessian decomposition for most iterations,
and the Hessian is required to be positive definite.

Parameters
----------
trust_radius : float
    We are allowed to wander only this far away from the origin.

Returns
-------
p : ndarray
    The proposed step.
hits_boundary : bool
    True if the proposed step is on the boundary of the trust region.

Notes
-----
The Hessian is required to be positive definite.

References
----------
.. [1] Jorge Nocedal and Stephen Wright,
       Numerical Optimization, second edition,
       Springer-Verlag, 2006, page 73.


def _minimize_trust_ncg(fun, x0, args=(), jac=None, hess=None, hessp=None,<EOL>**trust_region_options):

if jac is None:<EOL><INDENT>raise ValueError('<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT>if hess is None and hessp is None:<EOL><INDENT>raise ValueError('<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT>return _minimize_trust_region(fun, x0, args=args, jac=jac, hess=hess,<EOL>hessp=hessp, subproblem=CGSteihaugSubproblem,<EOL>**trust_region_options)<EOL>

Minimization of scalar function of one or more variables using
the Newton conjugate gradient trust-region algorithm.

This function is called by the `minimize` function.
It is not supposed to be called directly.


<EOL>p_origin = np.zeros_like(self.jac)<EOL>tolerance = min(<NUM_LIT:0.5>, math.sqrt(self.jac_mag)) * self.jac_mag<EOL>if self.jac_mag < tolerance:<EOL><INDENT>hits_boundary = False<EOL>return p_origin, hits_boundary<EOL><DEDENT>z = p_origin<EOL>r = self.jac<EOL>d = -r<EOL>while True:<EOL><INDENT>Bd = self.hessp(d)<EOL>dBd = np.dot(d, Bd)<EOL>if dBd <= <NUM_LIT:0>:<EOL><INDENT>ta, tb = self.get_boundaries_intersections(z, d, trust_radius)<EOL>pa = z + ta * d<EOL>pb = z + tb * d<EOL>if self(pa) < self(pb):<EOL><INDENT>p_boundary = pa<EOL><DEDENT>else:<EOL><INDENT>p_boundary = pb<EOL><DEDENT>hits_boundary = True<EOL>return p_boundary, hits_boundary<EOL><DEDENT>r_squared = np.dot(r, r)<EOL>alpha = r_squared / dBd<EOL>z_next = z + alpha * d<EOL>if scipy.linalg.norm(z_next) >= trust_radius:<EOL><INDENT>ta, tb = self.get_boundaries_intersections(z, d, trust_radius)<EOL>p_boundary = z + tb * d<EOL>hits_boundary = True<EOL>return p_boundary, hits_boundary<EOL><DEDENT>r_next = r + alpha * Bd<EOL>r_next_squared = np.dot(r_next, r_next)<EOL>if math.sqrt(r_next_squared) < tolerance:<EOL><INDENT>hits_boundary = False<EOL>return z_next, hits_boundary<EOL><DEDENT>beta_next = r_next_squared / r_squared<EOL>d_next = -r_next + beta_next * d<EOL>z = z_next<EOL>r = r_next<EOL>d = d_next<EOL><DEDENT>

Solve the subproblem using a conjugate gradient method.

Parameters
----------
trust_radius : float
    We are allowed to wander only this far away from the origin.

Returns
-------
p : ndarray
    The proposed step.
hits_boundary : bool
    True if the proposed step is on the boundary of the trust region.

Notes
-----
This is algorithm (7.2) of Nocedal and Wright 2nd edition.
Only the function that computes the Hessian-vector product is required.
The Hessian itself is not required, and the Hessian does
not need to be positive semidefinite.


x0 = asfarray(x)<EOL>f0 = atleast_1d(func(*((x0,)+args)))<EOL>jac = zeros([len(x0),len(f0)])<EOL>dx = zeros(len(x0))<EOL>for i in range(len(x0)):<EOL><INDENT>dx[i] = epsilon<EOL>jac[i] = (func(*((x0+dx,)+args)) - f0)/epsilon<EOL>dx[i] = <NUM_LIT:0.0><EOL><DEDENT>return jac.transpose()<EOL>

Approximate the Jacobian matrix of a callable function.

Parameters
----------
x : array_like
    The state vector at which to compute the Jacobian matrix.
func : callable f(x,*args)
    The vector-valued function.
epsilon : float
    The perturbation used to determine the partial derivatives.
args : sequence
    Additional arguments passed to func.

Returns
-------
An array of dimensions ``(lenf, lenx)`` where ``lenf`` is the length
of the outputs of `func`, and ``lenx`` is the number of elements in
`x`.

Notes
-----
The approximation is done using forward differences.


def fmin_slsqp(func, x0, eqcons=(), f_eqcons=None, ieqcons=(), f_ieqcons=None,<EOL>bounds=(), fprime=None, fprime_eqcons=None,<EOL>fprime_ieqcons=None, args=(), iter=<NUM_LIT:100>, acc=<NUM_LIT>,<EOL>iprint=<NUM_LIT:1>, disp=None, full_output=<NUM_LIT:0>, epsilon=_epsilon,<EOL>callback=None):

if disp is not None:<EOL><INDENT>iprint = disp<EOL><DEDENT>opts = {'<STR_LIT>': iter,<EOL>'<STR_LIT>': acc,<EOL>'<STR_LIT>': iprint,<EOL>'<STR_LIT>': iprint != <NUM_LIT:0>,<EOL>'<STR_LIT>': epsilon,<EOL>'<STR_LIT>': callback}<EOL>cons = ()<EOL>cons += tuple({'<STR_LIT:type>': '<STR_LIT>', '<STR_LIT>': c, '<STR_LIT:args>': args} for c in eqcons)<EOL>cons += tuple({'<STR_LIT:type>': '<STR_LIT>', '<STR_LIT>': c, '<STR_LIT:args>': args} for c in ieqcons)<EOL>if f_eqcons:<EOL><INDENT>cons += ({'<STR_LIT:type>': '<STR_LIT>', '<STR_LIT>': f_eqcons, '<STR_LIT>': fprime_eqcons,<EOL>'<STR_LIT:args>': args}, )<EOL><DEDENT>if f_ieqcons:<EOL><INDENT>cons += ({'<STR_LIT:type>': '<STR_LIT>', '<STR_LIT>': f_ieqcons, '<STR_LIT>': fprime_ieqcons,<EOL>'<STR_LIT:args>': args}, )<EOL><DEDENT>res = _minimize_slsqp(func, x0, args, jac=fprime, bounds=bounds,<EOL>constraints=cons, **opts)<EOL>if full_output:<EOL><INDENT>return res['<STR_LIT:x>'], res['<STR_LIT>'], res['<STR_LIT>'], res['<STR_LIT:status>'], res['<STR_LIT:message>']<EOL><DEDENT>else:<EOL><INDENT>return res['<STR_LIT:x>']<EOL><DEDENT>

Minimize a function using Sequential Least SQuares Programming

Python interface function for the SLSQP Optimization subroutine
originally implemented by Dieter Kraft.

Parameters
----------
func : callable f(x,*args)
    Objective function.
x0 : 1-D ndarray of float
    Initial guess for the independent variable(s).
eqcons : list
    A list of functions of length n such that
    eqcons[j](x,*args) == 0.0 in a successfully optimized
    problem.
f_eqcons : callable f(x,*args)
    Returns a 1-D array in which each element must equal 0.0 in a
    successfully optimized problem.  If f_eqcons is specified,
    eqcons is ignored.
ieqcons : list
    A list of functions of length n such that
    ieqcons[j](x,*args) >= 0.0 in a successfully optimized
    problem.
f_ieqcons : callable f(x,*args)
    Returns a 1-D ndarray in which each element must be greater or
    equal to 0.0 in a successfully optimized problem.  If
    f_ieqcons is specified, ieqcons is ignored.
bounds : list
    A list of tuples specifying the lower and upper bound
    for each independent variable [(xl0, xu0),(xl1, xu1),...]
    Infinite values will be interpreted as large floating values.
fprime : callable `f(x,*args)`
    A function that evaluates the partial derivatives of func.
fprime_eqcons : callable `f(x,*args)`
    A function of the form `f(x, *args)` that returns the m by n
    array of equality constraint normals.  If not provided,
    the normals will be approximated. The array returned by
    fprime_eqcons should be sized as ( len(eqcons), len(x0) ).
fprime_ieqcons : callable `f(x,*args)`
    A function of the form `f(x, *args)` that returns the m by n
    array of inequality constraint normals.  If not provided,
    the normals will be approximated. The array returned by
    fprime_ieqcons should be sized as ( len(ieqcons), len(x0) ).
args : sequence
    Additional arguments passed to func and fprime.
iter : int
    The maximum number of iterations.
acc : float
    Requested accuracy.
iprint : int
    The verbosity of fmin_slsqp :

    * iprint <= 0 : Silent operation
    * iprint == 1 : Print summary upon completion (default)
    * iprint >= 2 : Print status of each iterate and summary
disp : int
    Over-rides the iprint interface (preferred).
full_output : bool
    If False, return only the minimizer of func (default).
    Otherwise, output final objective function and summary
    information.
epsilon : float
    The step size for finite-difference derivative estimates.
callback : callable, optional
    Called after each iteration, as ``callback(x)``, where ``x`` is the
    current parameter vector.

Returns
-------
out : ndarray of float
    The final minimizer of func.
fx : ndarray of float, if full_output is true
    The final value of the objective function.
its : int, if full_output is true
    The number of iterations.
imode : int, if full_output is true
    The exit mode from the optimizer (see below).
smode : string, if full_output is true
    Message describing the exit mode from the optimizer.

See also
--------
minimize: Interface to minimization algorithms for multivariate
    functions. See the 'SLSQP' `method` in particular.

Notes
-----
Exit modes are defined as follows ::

    -1 : Gradient evaluation required (g & a)
     0 : Optimization terminated successfully.
     1 : Function evaluation required (f & c)
     2 : More equality constraints than independent variables
     3 : More than 3*n iterations in LSQ subproblem
     4 : Inequality constraints incompatible
     5 : Singular matrix E in LSQ subproblem
     6 : Singular matrix C in LSQ subproblem
     7 : Rank-deficient equality constraint subproblem HFTI
     8 : Positive directional derivative for linesearch
     9 : Iteration limit exceeded

Examples
--------
Examples are given :ref:`in the tutorial <tutorial-sqlsp>`.


def _minimize_slsqp(func, x0, args=(), jac=None, bounds=None,<EOL>constraints=(),<EOL>maxiter=<NUM_LIT:100>, ftol=<NUM_LIT>, iprint=<NUM_LIT:1>, disp=False,<EOL>eps=_epsilon, callback=None,<EOL>**unknown_options):

_check_unknown_options(unknown_options)<EOL>fprime = jac<EOL>iter = maxiter<EOL>acc = ftol<EOL>epsilon = eps<EOL>if not disp:<EOL><INDENT>iprint = <NUM_LIT:0><EOL><DEDENT>if isinstance(constraints, dict):<EOL><INDENT>constraints = (constraints, )<EOL><DEDENT>cons = {'<STR_LIT>': (), '<STR_LIT>': ()}<EOL>for ic, con in enumerate(constraints):<EOL><INDENT>try:<EOL><INDENT>ctype = con['<STR_LIT:type>'].lower()<EOL><DEDENT>except KeyError:<EOL><INDENT>raise KeyError('<STR_LIT>' % ic)<EOL><DEDENT>except TypeError:<EOL><INDENT>raise TypeError('<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT>except AttributeError:<EOL><INDENT>raise TypeError("<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>if ctype not in ['<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>raise ValueError("<STR_LIT>" % con['<STR_LIT:type>'])<EOL><DEDENT><DEDENT>if '<STR_LIT>' not in con:<EOL><INDENT>raise ValueError('<STR_LIT>' % ic)<EOL><DEDENT>cjac = con.get('<STR_LIT>')<EOL>if cjac is None:<EOL><INDENT>def cjac_factory(fun):<EOL><INDENT>def cjac(x, *args):<EOL><INDENT>return approx_jacobian(x, fun, epsilon, *args)<EOL><DEDENT>return cjac<EOL><DEDENT>cjac = cjac_factory(con['<STR_LIT>'])<EOL><DEDENT>cons[ctype] += ({'<STR_LIT>': con['<STR_LIT>'],<EOL>'<STR_LIT>': cjac,<EOL>'<STR_LIT:args>': con.get('<STR_LIT:args>', ())}, )<EOL><DEDENT>exit_modes = {-<NUM_LIT:1>: "<STR_LIT>",<EOL><NUM_LIT:0>: "<STR_LIT>",<EOL><NUM_LIT:1>: "<STR_LIT>",<EOL><NUM_LIT:2>: "<STR_LIT>",<EOL><NUM_LIT:3>: "<STR_LIT>",<EOL><NUM_LIT:4>: "<STR_LIT>",<EOL><NUM_LIT:5>: "<STR_LIT>",<EOL><NUM_LIT:6>: "<STR_LIT>",<EOL><NUM_LIT:7>: "<STR_LIT>",<EOL><NUM_LIT:8>: "<STR_LIT>",<EOL><NUM_LIT:9>: "<STR_LIT>"}<EOL>feval, func = wrap_function(func, args)<EOL>if fprime:<EOL><INDENT>geval, fprime = wrap_function(fprime, args)<EOL><DEDENT>else:<EOL><INDENT>geval, fprime = wrap_function(approx_jacobian, (func, epsilon))<EOL><DEDENT>x = asfarray(x0).flatten()<EOL>meq = sum(map(len, [atleast_1d(c['<STR_LIT>'](x, *c['<STR_LIT:args>'])) for c in cons['<STR_LIT>']]))<EOL>mieq = sum(map(len, [atleast_1d(c['<STR_LIT>'](x, *c['<STR_LIT:args>'])) for c in cons['<STR_LIT>']]))<EOL>m = meq + mieq<EOL>la = array([<NUM_LIT:1>, m]).max()<EOL>n = len(x)<EOL>n1 = n + <NUM_LIT:1><EOL>mineq = m - meq + n1 + n1<EOL>len_w = (<NUM_LIT:3>*n1+m)*(n1+<NUM_LIT:1>)+(n1-meq+<NUM_LIT:1>)*(mineq+<NUM_LIT:2>) + <NUM_LIT:2>*mineq+(n1+mineq)*(n1-meq)+ <NUM_LIT:2>*meq + n1 + ((n+<NUM_LIT:1>)*n)//<NUM_LIT:2> + <NUM_LIT:2>*m + <NUM_LIT:3>*n + <NUM_LIT:3>*n1 + <NUM_LIT:1><EOL>len_jw = mineq<EOL>w = zeros(len_w)<EOL>jw = zeros(len_jw)<EOL>if bounds is None or len(bounds) == <NUM_LIT:0>:<EOL><INDENT>xl, xu = array([-<NUM_LIT>]*n), array([<NUM_LIT>]*n)<EOL><DEDENT>else:<EOL><INDENT>bnds = array(bounds, float)<EOL>if bnds.shape[<NUM_LIT:0>] != n:<EOL><INDENT>raise IndexError('<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT>bnderr = where(bnds[:, <NUM_LIT:0>] > bnds[:, <NUM_LIT:1>])[<NUM_LIT:0>]<EOL>if bnderr.any():<EOL><INDENT>raise ValueError('<STR_LIT>' %<EOL>'<STR_LIT:U+002CU+0020>'.join(str(b) for b in bnderr))<EOL><DEDENT>xl, xu = bnds[:, <NUM_LIT:0>], bnds[:, <NUM_LIT:1>]<EOL>infbnd = ~isfinite(bnds)<EOL>xl[infbnd[:, <NUM_LIT:0>]] = -<NUM_LIT><EOL>xu[infbnd[:, <NUM_LIT:1>]] = <NUM_LIT><EOL><DEDENT>mode = array(<NUM_LIT:0>,int)<EOL>acc = array(acc,float)<EOL>majiter = array(iter,int)<EOL>majiter_prev = <NUM_LIT:0><EOL>if iprint >= <NUM_LIT:2>:<EOL><INDENT>print("<STR_LIT>" % ("<STR_LIT>","<STR_LIT>","<STR_LIT>","<STR_LIT>"))<EOL><DEDENT>while <NUM_LIT:1>:<EOL><INDENT>if mode == <NUM_LIT:0> or mode == <NUM_LIT:1>:  <EOL><INDENT>fx = func(x)<EOL>if cons['<STR_LIT>']:<EOL><INDENT>c_eq = concatenate([atleast_1d(con['<STR_LIT>'](x, *con['<STR_LIT:args>']))<EOL>for con in cons['<STR_LIT>']])<EOL><DEDENT>else:<EOL><INDENT>c_eq = zeros(<NUM_LIT:0>)<EOL><DEDENT>if cons['<STR_LIT>']:<EOL><INDENT>c_ieq = concatenate([atleast_1d(con['<STR_LIT>'](x, *con['<STR_LIT:args>']))<EOL>for con in cons['<STR_LIT>']])<EOL><DEDENT>else:<EOL><INDENT>c_ieq = zeros(<NUM_LIT:0>)<EOL><DEDENT>c = concatenate((c_eq, c_ieq))<EOL><DEDENT>if mode == <NUM_LIT:0> or mode == -<NUM_LIT:1>:  <EOL><INDENT>g = append(fprime(x),<NUM_LIT:0.0>)<EOL>if cons['<STR_LIT>']:<EOL><INDENT>a_eq = vstack([con['<STR_LIT>'](x, *con['<STR_LIT:args>'])<EOL>for con in cons['<STR_LIT>']])<EOL><DEDENT>else:  <EOL><INDENT>a_eq = zeros((meq, n))<EOL><DEDENT>if cons['<STR_LIT>']:<EOL><INDENT>a_ieq = vstack([con['<STR_LIT>'](x, *con['<STR_LIT:args>'])<EOL>for con in cons['<STR_LIT>']])<EOL><DEDENT>else:  <EOL><INDENT>a_ieq = zeros((mieq, n))<EOL><DEDENT>if m == <NUM_LIT:0>:  <EOL><INDENT>a = zeros((la, n))<EOL><DEDENT>else:<EOL><INDENT>a = vstack((a_eq, a_ieq))<EOL><DEDENT>a = concatenate((a,zeros([la,<NUM_LIT:1>])),<NUM_LIT:1>)<EOL><DEDENT>slsqp(m, meq, x, xl, xu, fx, c, g, a, acc, majiter, mode, w, jw)<EOL>if callback is not None and majiter > majiter_prev:<EOL><INDENT>callback(x)<EOL><DEDENT>if iprint >= <NUM_LIT:2> and majiter > majiter_prev:<EOL><INDENT>print("<STR_LIT>" % (majiter,feval[<NUM_LIT:0>],<EOL>fx,linalg.norm(g)))<EOL><DEDENT>if abs(mode) != <NUM_LIT:1>:<EOL><INDENT>break<EOL><DEDENT>majiter_prev = int(majiter)<EOL><DEDENT>if iprint >= <NUM_LIT:1>:<EOL><INDENT>print(exit_modes[int(mode)] + "<STR_LIT>" + str(mode) + '<STR_LIT:)>')<EOL>print("<STR_LIT>", fx)<EOL>print("<STR_LIT>", majiter)<EOL>print("<STR_LIT>", feval[<NUM_LIT:0>])<EOL>print("<STR_LIT>", geval[<NUM_LIT:0>])<EOL><DEDENT>return OptimizeResult(x=x, fun=fx, jac=g, nit=int(majiter), nfev=feval[<NUM_LIT:0>],<EOL>njev=geval[<NUM_LIT:0>], status=int(mode),<EOL>message=exit_modes[int(mode)], success=(mode == <NUM_LIT:0>))<EOL>

Minimize a scalar function of one or more variables using Sequential
Least SQuares Programming (SLSQP).

Options for the SLSQP algorithm are:
    ftol : float
        Precision goal for the value of f in the stopping criterion.
    eps : float
        Step size used for numerical approximation of the jacobian.
    disp : bool
        Set to True to print convergence messages. If False,
        `verbosity` is ignored and set to 0.
    maxiter : int
        Maximum number of iterations.

This function is called by the `minimize` function with
`method=SLSQP`. It is not supposed to be called directly.


def fourier_gaussian(input, sigma, n=-<NUM_LIT:1>, axis=-<NUM_LIT:1>, output=None):

input = numpy.asarray(input)<EOL>output, return_value = _get_output_fourier(output, input)<EOL>axis = _ni_support._check_axis(axis, input.ndim)<EOL>sigmas = _ni_support._normalize_sequence(sigma, input.ndim)<EOL>sigmas = numpy.asarray(sigmas, dtype=numpy.float64)<EOL>if not sigmas.flags.contiguous:<EOL><INDENT>sigmas = sigmas.copy()<EOL><DEDENT>_nd_image.fourier_filter(input, sigmas, n, axis, output, <NUM_LIT:0>)<EOL>return return_value<EOL>

Multi-dimensional Gaussian fourier filter.

The array is multiplied with the fourier transform of a Gaussian
kernel.

Parameters
----------
input : array_like
    The input array.
sigma : float or sequence
    The sigma of the Gaussian kernel. If a float, `sigma` is the same for
    all axes. If a sequence, `sigma` has to contain one value for each
    axis.
n : int, optional
    If `n` is negative (default), then the input is assumed to be the
    result of a complex fft.
    If `n` is larger than or equal to zero, the input is assumed to be the
    result of a real fft, and `n` gives the length of the array before
    transformation along the real transform direction.
axis : int, optional
    The axis of the real transform.
output : ndarray, optional
    If given, the result of filtering the input is placed in this array.
    None is returned in this case.

Returns
-------
fourier_gaussian : ndarray or None
    The filtered input. If `output` is given as a parameter, None is
    returned.


def fourier_uniform(input, size, n=-<NUM_LIT:1>, axis=-<NUM_LIT:1>, output=None):

input = numpy.asarray(input)<EOL>output, return_value = _get_output_fourier(output, input)<EOL>axis = _ni_support._check_axis(axis, input.ndim)<EOL>sizes = _ni_support._normalize_sequence(size, input.ndim)<EOL>sizes = numpy.asarray(sizes, dtype=numpy.float64)<EOL>if not sizes.flags.contiguous:<EOL><INDENT>sizes = sizes.copy()<EOL><DEDENT>_nd_image.fourier_filter(input, sizes, n, axis, output, <NUM_LIT:1>)<EOL>return return_value<EOL>

Multi-dimensional uniform fourier filter.

The array is multiplied with the fourier transform of a box of given
size.

Parameters
----------
input : array_like
    The input array.
size : float or sequence
    The size of the box used for filtering.
    If a float, `size` is the same for all axes. If a sequence, `size` has
    to contain one value for each axis.
n : int, optional
    If `n` is negative (default), then the input is assumed to be the
    result of a complex fft.
    If `n` is larger than or equal to zero, the input is assumed to be the
    result of a real fft, and `n` gives the length of the array before
    transformation along the real transform direction.
axis : int, optional
    The axis of the real transform.
output : ndarray, optional
    If given, the result of filtering the input is placed in this array.
    None is returned in this case.

Returns
-------
fourier_uniform : ndarray or None
    The filtered input. If `output` is given as a parameter, None is
    returned.


def fourier_ellipsoid(input, size, n=-<NUM_LIT:1>, axis=-<NUM_LIT:1>, output=None):

input = numpy.asarray(input)<EOL>output, return_value = _get_output_fourier(output, input)<EOL>axis = _ni_support._check_axis(axis, input.ndim)<EOL>sizes = _ni_support._normalize_sequence(size, input.ndim)<EOL>sizes = numpy.asarray(sizes, dtype=numpy.float64)<EOL>if not sizes.flags.contiguous:<EOL><INDENT>sizes = sizes.copy()<EOL><DEDENT>_nd_image.fourier_filter(input, sizes, n, axis, output, <NUM_LIT:2>)<EOL>return return_value<EOL>

Multi-dimensional ellipsoid fourier filter.

The array is multiplied with the fourier transform of a ellipsoid of
given sizes.

Parameters
----------
input : array_like
    The input array.
size : float or sequence
    The size of the box used for filtering.
    If a float, `size` is the same for all axes. If a sequence, `size` has
    to contain one value for each axis.
n : int, optional
    If `n` is negative (default), then the input is assumed to be the
    result of a complex fft.
    If `n` is larger than or equal to zero, the input is assumed to be the
    result of a real fft, and `n` gives the length of the array before
    transformation along the real transform direction.
axis : int, optional
    The axis of the real transform.
output : ndarray, optional
    If given, the result of filtering the input is placed in this array.
    None is returned in this case.

Returns
-------
fourier_ellipsoid : ndarray or None
    The filtered input. If `output` is given as a parameter, None is
    returned.

Notes
-----
This function is implemented for arrays of rank 1, 2, or 3.


def fourier_shift(input, shift, n=-<NUM_LIT:1>, axis=-<NUM_LIT:1>, output=None):

input = numpy.asarray(input)<EOL>output, return_value = _get_output_fourier_complex(output, input)<EOL>axis = _ni_support._check_axis(axis, input.ndim)<EOL>shifts = _ni_support._normalize_sequence(shift, input.ndim)<EOL>shifts = numpy.asarray(shifts, dtype=numpy.float64)<EOL>if not shifts.flags.contiguous:<EOL><INDENT>shifts = shifts.copy()<EOL><DEDENT>_nd_image.fourier_shift(input, shifts, n, axis, output)<EOL>return return_value<EOL>

Multi-dimensional fourier shift filter.

The array is multiplied with the fourier transform of a shift operation.

Parameters
----------
input : array_like
    The input array.
shift : float or sequence
    The size of the box used for filtering.
    If a float, `shift` is the same for all axes. If a sequence, `shift`
    has to contain one value for each axis.
n : int, optional
    If `n` is negative (default), then the input is assumed to be the
    result of a complex fft.
    If `n` is larger than or equal to zero, the input is assumed to be the
    result of a real fft, and `n` gives the length of the array before
    transformation along the real transform direction.
axis : int, optional
    The axis of the real transform.
output : ndarray, optional
    If given, the result of shifting the input is placed in this array.
    None is returned in this case.

Returns
-------
fourier_shift : ndarray or None
    The shifted input. If `output` is given as a parameter, None is
    returned.


def label(input, structure=None, output=None):

input = numpy.asarray(input)<EOL>if numpy.iscomplexobj(input):<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>if structure is None:<EOL><INDENT>structure = morphology.generate_binary_structure(input.ndim, <NUM_LIT:1>)<EOL><DEDENT>structure = numpy.asarray(structure, dtype=bool)<EOL>if structure.ndim != input.ndim:<EOL><INDENT>raise RuntimeError('<STR_LIT>')<EOL><DEDENT>for ii in structure.shape:<EOL><INDENT>if ii != <NUM_LIT:3>:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT><DEDENT>need_64bits = input.size >= (<NUM_LIT:2>**<NUM_LIT> - <NUM_LIT:2>)<EOL>if isinstance(output, numpy.ndarray):<EOL><INDENT>if output.shape != input.shape:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>caller_provided_output = True<EOL><DEDENT>else:<EOL><INDENT>caller_provided_output = False<EOL>if output is None:<EOL><INDENT>output = np.empty(input.shape, np.intp if need_64bits else np.int32)<EOL><DEDENT>else:<EOL><INDENT>output = np.empty(input.shape, output)<EOL><DEDENT><DEDENT>if input.ndim == <NUM_LIT:0> or input.size == <NUM_LIT:0>:<EOL><INDENT>if input.ndim == <NUM_LIT:0>:<EOL><INDENT>maxlabel = <NUM_LIT:1> if (input != <NUM_LIT:0>) else <NUM_LIT:0><EOL>output[...] = maxlabel<EOL><DEDENT>else:<EOL><INDENT>maxlabel = <NUM_LIT:0><EOL><DEDENT>if caller_provided_output:<EOL><INDENT>return maxlabel<EOL><DEDENT>else:<EOL><INDENT>return output, maxlabel<EOL><DEDENT><DEDENT>try:<EOL><INDENT>max_label = _ni_label._label(input, structure, output)<EOL><DEDENT>except _ni_label.NeedMoreBits:<EOL><INDENT>tmp_output = np.empty(input.shape, np.intp if need_64bits else np.int32)<EOL>max_label = _ni_label._label(input, structure, tmp_output)<EOL>output[...] = tmp_output[...]<EOL>if not np.all(output == tmp_output):<EOL><INDENT>raise RuntimeError("<STR_LIT>")<EOL><DEDENT><DEDENT>if caller_provided_output:<EOL><INDENT>return max_label<EOL><DEDENT>else:<EOL><INDENT>return output, max_label<EOL><DEDENT>

Label features in an array.

Parameters
----------
input : array_like
    An array-like object to be labeled.  Any non-zero values in `input` are
    counted as features and zero values are considered the background.
structure : array_like, optional
    A structuring element that defines feature connections.
    `structure` must be symmetric.  If no structuring element is provided,
    one is automatically generated with a squared connectivity equal to
    one.  That is, for a 2-D `input` array, the default structuring element
    is::

        [[0,1,0],
         [1,1,1],
         [0,1,0]]

output : (None, data-type, array_like), optional
    If `output` is a data type, it specifies the type of the resulting
    labeled feature array
    If `output` is an array-like object, then `output` will be updated
    with the labeled features from this function.  This function can
    operate in-place, by passing output=input.
    Note that the output must be able to store the largest label, or this
    function will raise an Exception.

Returns
-------
label : ndarray or int
    An integer ndarray where each unique feature in `input` has a unique
    label in the returned array.
num_features : int
    How many objects were found.

    If `output` is None, this function returns a tuple of
    (`labeled_array`, `num_features`).

    If `output` is a ndarray, then it will be updated with values in
    `labeled_array` and only `num_features` will be returned by this
    function.

See Also
--------
find_objects : generate a list of slices for the labeled features (or
               objects); useful for finding features' position or
               dimensions

Examples
--------
Create an image with some features, then label it using the default
(cross-shaped) structuring element:

>>> a = np.array([[0,0,1,1,0,0],
...               [0,0,0,1,0,0],
...               [1,1,0,0,1,0],
...               [0,0,0,1,0,0]])
>>> labeled_array, num_features = label(a)

Each of the 4 features are labeled with a different integer:

>>> print(num_features)
4
>>> print(labeled_array)
array([[0, 0, 1, 1, 0, 0],
       [0, 0, 0, 1, 0, 0],
       [2, 2, 0, 0, 3, 0],
       [0, 0, 0, 4, 0, 0]])

Generate a structuring element that will consider features connected even
if they touch diagonally:

>>> s = generate_binary_structure(2,2)

or,

>>> s = [[1,1,1],
         [1,1,1],
         [1,1,1]]

Label the image using the new structuring element:

>>> labeled_array, num_features = label(a, structure=s)

Show the 2 labeled features (note that features 1, 3, and 4 from above are
now considered a single feature):

>>> print(num_features)
2
>>> print(labeled_array)
array([[0, 0, 1, 1, 0, 0],
       [0, 0, 0, 1, 0, 0],
       [2, 2, 0, 0, 1, 0],
       [0, 0, 0, 1, 0, 0]])


input = numpy.asarray(input)<EOL>if numpy.iscomplexobj(input):<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>if max_label < <NUM_LIT:1>:<EOL><INDENT>max_label = input.max()<EOL><DEDENT>return _nd_image.find_objects(input, max_label)<EOL>

Find objects in a labeled array.

Parameters
----------
input : ndarray of ints
    Array containing objects defined by different labels. Labels with
    value 0 are ignored.
max_label : int, optional
    Maximum label to be searched for in `input`. If max_label is not
    given, the positions of all objects are returned.

Returns
-------
object_slices : list of tuples
    A list of tuples, with each tuple containing N slices (with N the
    dimension of the input array).  Slices correspond to the minimal
    parallelepiped that contains the object. If a number is missing,
    None is returned instead of a slice.

See Also
--------
label, center_of_mass

Notes
-----
This function is very useful for isolating a volume of interest inside
a 3-D array, that cannot be "seen through".

Examples
--------
>>> a = np.zeros((6,6), dtype=np.int)
>>> a[2:4, 2:4] = 1
>>> a[4, 4] = 1
>>> a[:2, :3] = 2
>>> a[0, 5] = 3
>>> a
array([[2, 2, 2, 0, 0, 3],
       [2, 2, 2, 0, 0, 0],
       [0, 0, 1, 1, 0, 0],
       [0, 0, 1, 1, 0, 0],
       [0, 0, 0, 0, 1, 0],
       [0, 0, 0, 0, 0, 0]])
>>> ndimage.find_objects(a)
[(slice(2, 5, None), slice(2, 5, None)), (slice(0, 2, None), slice(0, 3, None)), (slice(0, 1, None), slice(5, 6, None))]
>>> ndimage.find_objects(a, max_label=2)
[(slice(2, 5, None), slice(2, 5, None)), (slice(0, 2, None), slice(0, 3, None))]
>>> ndimage.find_objects(a == 1, max_label=2)
[(slice(2, 5, None), slice(2, 5, None)), None]

>>> loc = ndimage.find_objects(a)[0]
>>> a[loc]
array([[1, 1, 0]
       [1, 1, 0]
       [0, 0, 1]])


def labeled_comprehension(input, labels, index, func, out_dtype, default, pass_positions=False):

as_scalar = numpy.isscalar(index)<EOL>input = numpy.asarray(input)<EOL>if pass_positions:<EOL><INDENT>positions = numpy.arange(input.size).reshape(input.shape)<EOL><DEDENT>if labels is None:<EOL><INDENT>if index is not None:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if not pass_positions:<EOL><INDENT>return func(input.ravel())<EOL><DEDENT>else:<EOL><INDENT>return func(input.ravel(), positions.ravel())<EOL><DEDENT><DEDENT>try:<EOL><INDENT>input, labels = numpy.broadcast_arrays(input, labels)<EOL><DEDENT>except ValueError:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>if index is None:<EOL><INDENT>if not pass_positions:<EOL><INDENT>return func(input[labels > <NUM_LIT:0>])<EOL><DEDENT>else:<EOL><INDENT>return func(input[labels > <NUM_LIT:0>], positions[labels > <NUM_LIT:0>])<EOL><DEDENT><DEDENT>index = numpy.atleast_1d(index)<EOL>if np.any(index.astype(labels.dtype).astype(index.dtype) != index):<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>" %<EOL>(index.dtype, labels.dtype))<EOL><DEDENT>index = index.astype(labels.dtype)<EOL>lo = index.min()<EOL>hi = index.max()<EOL>mask = (labels >= lo) & (labels <= hi)<EOL>labels = labels[mask]<EOL>input = input[mask]<EOL>if pass_positions:<EOL><INDENT>positions = positions[mask]<EOL><DEDENT>label_order = labels.argsort()<EOL>labels = labels[label_order]<EOL>input = input[label_order]<EOL>if pass_positions:<EOL><INDENT>positions = positions[label_order]<EOL><DEDENT>index_order = index.argsort()<EOL>sorted_index = index[index_order]<EOL>def do_map(inputs, output):<EOL><INDENT>"""<STR_LIT>"""<EOL>nidx = sorted_index.size<EOL>lo = numpy.searchsorted(labels, sorted_index, side='<STR_LIT:left>')<EOL>hi = numpy.searchsorted(labels, sorted_index, side='<STR_LIT:right>')<EOL>for i, l, h in zip(range(nidx), lo, hi):<EOL><INDENT>if l == h:<EOL><INDENT>continue<EOL><DEDENT>output[i] = func(*[inp[l:h] for inp in inputs])<EOL><DEDENT><DEDENT>temp = numpy.empty(index.shape, out_dtype)<EOL>temp[:] = default<EOL>if not pass_positions:<EOL><INDENT>do_map([input], temp)<EOL><DEDENT>else:<EOL><INDENT>do_map([input, positions], temp)<EOL><DEDENT>output = numpy.zeros(index.shape, out_dtype)<EOL>output[index_order] = temp<EOL>if as_scalar:<EOL><INDENT>output = output[<NUM_LIT:0>]<EOL><DEDENT>return output<EOL>

Roughly equivalent to [func(input[labels == i]) for i in index].

Sequentially applies an arbitrary function (that works on array_like input)
to subsets of an n-D image array specified by `labels` and `index`.
The option exists to provide the function with positional parameters as the
second argument.

Parameters
----------
input : array_like
    Data from which to select `labels` to process.
labels : array_like or None
    Labels to objects in `input`.
    If not None, array must be same shape as `input`.
    If None, `func` is applied to raveled `input`.
index : int, sequence of ints or None
    Subset of `labels` to which to apply `func`.
    If a scalar, a single value is returned.
    If None, `func` is applied to all non-zero values of `labels`.
func : callable
    Python function to apply to `labels` from `input`.
out_dtype : dtype
    Dtype to use for `result`.
default : int, float or None
    Default return value when a element of `index` does not exist
    in `labels`.
pass_positions : bool, optional
    If True, pass linear indices to `func` as a second argument.
    Default is False.

Returns
-------
result : ndarray
    Result of applying `func` to each of `labels` to `input` in `index`.

Examples
--------
>>> a = np.array([[1, 2, 0, 0],
                  [5, 3, 0, 4],
                  [0, 0, 0, 7],
                  [9, 3, 0, 0]])
>>> from scipy import ndimage
>>> lbl, nlbl = ndimage.label(a)
>>> lbls = np.arange(1, nlbl+1)
>>> ndimage.labeled_comprehension(a, lbl, lbls, np.mean, float, 0)
array([ 2.75,  5.5 ,  6.  ])

Falling back to `default`:

>>> lbls = np.arange(1, nlbl+2)
>>> ndimage.labeled_comprehension(a, lbl, lbls, np.mean, float, -1)
array([ 2.75,  5.5 ,  6.  , -1.  ])

Passing positions:

>>> def fn(val, pos):
...     print("fn says: %s : %s" % (val, pos))
...     return (val.sum()) if (pos.sum() % 2 == 0) else (-val.sum())
...
>>> ndimage.labeled_comprehension(a, lbl, lbls, fn, float, 0, True)
fn says: [1 2 5 3] : [0 1 4 5]
fn says: [4 7] : [7 11]
fn says: [9 3] : [12 13]
array([ 11.,  11., -12.])


int_size = np.dtype(int).itemsize<EOL>safe = ((np.issubdtype(dt, int) and dt.itemsize <= int_size) or<EOL>(np.issubdtype(dt, np.unsignedinteger) and dt.itemsize < int_size))<EOL>return safe<EOL>

Test whether the numpy data type `dt` can be safely cast to an int.


def _stats(input, labels=None, index=None, centered=False):

def single_group(vals):<EOL><INDENT>if centered:<EOL><INDENT>vals_c = vals - vals.mean()<EOL>return vals.size, vals.sum(), (vals_c * vals_c.conjugate()).sum()<EOL><DEDENT>else:<EOL><INDENT>return vals.size, vals.sum()<EOL><DEDENT><DEDENT>if labels is None:<EOL><INDENT>return single_group(input)<EOL><DEDENT>input, labels = numpy.broadcast_arrays(input, labels)<EOL>if index is None:<EOL><INDENT>return single_group(input[labels > <NUM_LIT:0>])<EOL><DEDENT>if numpy.isscalar(index):<EOL><INDENT>return single_group(input[labels == index])<EOL><DEDENT>def _sum_centered(labels):<EOL><INDENT>means = sums / counts<EOL>centered_input = input - means[labels]<EOL>bc = numpy.bincount(labels.ravel(),<EOL>weights=(centered_input *<EOL>centered_input.conjugate()).ravel())<EOL>return bc<EOL><DEDENT>if (not _safely_castable_to_int(labels.dtype) or<EOL>labels.min() < <NUM_LIT:0> or labels.max() > labels.size):<EOL><INDENT>unique_labels, new_labels = numpy.unique(labels, return_inverse=True)<EOL>counts = numpy.bincount(new_labels)<EOL>sums = numpy.bincount(new_labels, weights=input.ravel())<EOL>if centered:<EOL><INDENT>sums_c = _sum_centered(new_labels.reshape(labels.shape))<EOL><DEDENT>idxs = numpy.searchsorted(unique_labels, index)<EOL>idxs[idxs >= unique_labels.size] = <NUM_LIT:0><EOL>found = (unique_labels[idxs] == index)<EOL><DEDENT>else:<EOL><INDENT>counts = numpy.bincount(labels.ravel())<EOL>sums = numpy.bincount(labels.ravel(), weights=input.ravel())<EOL>if centered:<EOL><INDENT>sums_c = _sum_centered(labels)<EOL><DEDENT>idxs = numpy.asanyarray(index, numpy.int).copy()<EOL>found = (idxs >= <NUM_LIT:0>) & (idxs < counts.size)<EOL>idxs[~found] = <NUM_LIT:0><EOL><DEDENT>counts = counts[idxs]<EOL>counts[~found] = <NUM_LIT:0><EOL>sums = sums[idxs]<EOL>sums[~found] = <NUM_LIT:0><EOL>if not centered:<EOL><INDENT>return (counts, sums)<EOL><DEDENT>else:<EOL><INDENT>sums_c = sums_c[idxs]<EOL>sums_c[~found] = <NUM_LIT:0><EOL>return (counts, sums, sums_c)<EOL><DEDENT>

Count, sum, and optionally compute (sum - centre)^2 of input by label

    Parameters
    ----------
    input : array_like, n-dimensional
        The input data to be analyzed.
    labels : array_like (n-dimensional), optional
        The labels of the data in `input`.  This array must be broadcast
        compatible with `input`; typically it is the same shape as `input`.
        If `labels` is None, all nonzero values in `input` are treated as
        the single labeled group.
    index : label or sequence of labels, optional
        These are the labels of the groups for which the stats are computed.
        If `index` is None, the stats are computed for the single group where
        `labels` is greater than 0.
    centered : bool, optional
        If True, the centered sum of squares for each labeled group is
        also returned.  Default is False.

    Returns
    -------
    counts : int or ndarray of ints
        The number of elements in each labeled group.
    sums : scalar or ndarray of scalars
        The sums of the values in each labeled group.
    sums_c : scalar or ndarray of scalars, optional
        The sums of mean-centered squares of the values in each labeled group.
        This is only returned if `centered` is True.


def sum(input, labels=None, index=None):

count, sum = _stats(input, labels, index)<EOL>return sum<EOL>

Calculate the sum of the values of the array.

Parameters
----------
input : array_like
    Values of `input` inside the regions defined by `labels`
    are summed together.
labels : array_like of ints, optional
    Assign labels to the values of the array. Has to have the same shape as
    `input`.
index : array_like, optional
    A single label number or a sequence of label numbers of
    the objects to be measured.

Returns
-------
sum : ndarray or scalar
    An array of the sums of values of `input` inside the regions defined
    by `labels` with the same shape as `index`. If 'index' is None or scalar,
    a scalar is returned.

See also
--------
mean, median

Examples
--------
>>> input =  [0,1,2,3]
>>> labels = [1,1,2,2]
>>> sum(input, labels, index=[1,2])
[1.0, 5.0]
>>> sum(input, labels, index=1)
1
>>> sum(input, labels)
6


def mean(input, labels=None, index=None):

count, sum = _stats(input, labels, index)<EOL>return sum / numpy.asanyarray(count).astype(numpy.float)<EOL>

Calculate the mean of the values of an array at labels.

Parameters
----------
input : array_like
    Array on which to compute the mean of elements over distinct
    regions.
labels : array_like, optional
    Array of labels of same shape, or broadcastable to the same shape as
    `input`. All elements sharing the same label form one region over
    which the mean of the elements is computed.
index : int or sequence of ints, optional
    Labels of the objects over which the mean is to be computed.
    Default is None, in which case the mean for all values where label is
    greater than 0 is calculated.

Returns
-------
out : list
    Sequence of same length as `index`, with the mean of the different
    regions labeled by the labels in `index`.

See also
--------
ndimage.variance, ndimage.standard_deviation, ndimage.minimum,
ndimage.maximum, ndimage.sum
ndimage.label

Examples
--------
>>> a = np.arange(25).reshape((5,5))
>>> labels = np.zeros_like(a)
>>> labels[3:5,3:5] = 1
>>> index = np.unique(labels)
>>> labels
array([[0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0],
       [0, 0, 0, 1, 1],
       [0, 0, 0, 1, 1]])
>>> index
array([0, 1])
>>> ndimage.mean(a, labels=labels, index=index)
[10.285714285714286, 21.0]


def variance(input, labels=None, index=None):

count, sum, sum_c_sq = _stats(input, labels, index, centered=True)<EOL>return sum_c_sq / np.asanyarray(count).astype(float)<EOL>

Calculate the variance of the values of an n-D image array, optionally at
specified sub-regions.

Parameters
----------
input : array_like
    Nd-image data to process.
labels : array_like, optional
    Labels defining sub-regions in `input`.
    If not None, must be same shape as `input`.
index : int or sequence of ints, optional
    `labels` to include in output.  If None (default), all values where
    `labels` is non-zero are used.

Returns
-------
variance : float or ndarray
    Values of variance, for each sub-region if `labels` and `index` are
    specified.

See Also
--------
label, standard_deviation, maximum, minimum, extrema

Examples
--------
>>> a = np.array([[1, 2, 0, 0],
                  [5, 3, 0, 4],
                  [0, 0, 0, 7],
                  [9, 3, 0, 0]])
>>> from scipy import ndimage
>>> ndimage.variance(a)
7.609375

Features to process can be specified using `labels` and `index`:

>>> lbl, nlbl = ndimage.label(a)
>>> ndimage.variance(a, lbl, index=np.arange(1, nlbl+1))
array([ 2.1875,  2.25  ,  9.    ])

If no index is given, all non-zero `labels` are processed:

>>> ndimage.variance(a, lbl)
6.1875


def standard_deviation(input, labels=None, index=None):

return numpy.sqrt(variance(input, labels, index))<EOL>

Calculate the standard deviation of the values of an n-D image array,
optionally at specified sub-regions.

Parameters
----------
input : array_like
    Nd-image data to process.
labels : array_like, optional
    Labels to identify sub-regions in `input`.
    If not None, must be same shape as `input`.
index : int or sequence of ints, optional
    `labels` to include in output.  If None (default), all values where
    `labels` is non-zero are used.

Returns
-------
standard_deviation : float or ndarray
    Values of standard deviation, for each sub-region if `labels` and
    `index` are specified.

See Also
--------
label, variance, maximum, minimum, extrema

Examples
--------
>>> a = np.array([[1, 2, 0, 0],
                  [5, 3, 0, 4],
                  [0, 0, 0, 7],
                  [9, 3, 0, 0]])
>>> from scipy import ndimage
>>> ndimage.standard_deviation(a)
2.7585095613392387

Features to process can be specified using `labels` and `index`:

>>> lbl, nlbl = ndimage.label(a)
>>> ndimage.standard_deviation(a, lbl, index=np.arange(1, nlbl+1))
array([ 1.479,  1.5  ,  3.   ])

If no index is given, non-zero `labels` are processed:

>>> ndimage.standard_deviation(a, lbl)
2.4874685927665499


def _select(input, labels=None, index=None, find_min=False, find_max=False,<EOL>find_min_positions=False, find_max_positions=False,<EOL>find_median=False):

input = numpy.asanyarray(input)<EOL>find_positions = find_min_positions or find_max_positions<EOL>positions = None<EOL>if find_positions:<EOL><INDENT>positions = numpy.arange(input.size).reshape(input.shape)<EOL><DEDENT>def single_group(vals, positions):<EOL><INDENT>result = []<EOL>if find_min:<EOL><INDENT>result += [vals.min()]<EOL><DEDENT>if find_min_positions:<EOL><INDENT>result += [positions[vals == vals.min()][<NUM_LIT:0>]]<EOL><DEDENT>if find_max:<EOL><INDENT>result += [vals.max()]<EOL><DEDENT>if find_max_positions:<EOL><INDENT>result += [positions[vals == vals.max()][<NUM_LIT:0>]]<EOL><DEDENT>if find_median:<EOL><INDENT>result += [numpy.median(vals)]<EOL><DEDENT>return result<EOL><DEDENT>if labels is None:<EOL><INDENT>return single_group(input, positions)<EOL><DEDENT>input, labels = numpy.broadcast_arrays(input, labels)<EOL>if index is None:<EOL><INDENT>mask = (labels > <NUM_LIT:0>)<EOL>masked_positions = None<EOL>if find_positions:<EOL><INDENT>masked_positions = positions[mask]<EOL><DEDENT>return single_group(input[mask], masked_positions)<EOL><DEDENT>if numpy.isscalar(index):<EOL><INDENT>mask = (labels == index)<EOL>masked_positions = None<EOL>if find_positions:<EOL><INDENT>masked_positions = positions[mask]<EOL><DEDENT>return single_group(input[mask], masked_positions)<EOL><DEDENT>if (not _safely_castable_to_int(labels.dtype) or<EOL>labels.min() < <NUM_LIT:0> or labels.max() > labels.size):<EOL><INDENT>unique_labels, labels = numpy.unique(labels, return_inverse=True)<EOL>idxs = numpy.searchsorted(unique_labels, index)<EOL>idxs[idxs >= unique_labels.size] = <NUM_LIT:0><EOL>found = (unique_labels[idxs] == index)<EOL><DEDENT>else:<EOL><INDENT>idxs = numpy.asanyarray(index, numpy.int).copy()<EOL>found = (idxs >= <NUM_LIT:0>) & (idxs <= labels.max())<EOL><DEDENT>idxs[~ found] = labels.max() + <NUM_LIT:1><EOL>if find_median:<EOL><INDENT>order = numpy.lexsort((input.ravel(), labels.ravel()))<EOL><DEDENT>else:<EOL><INDENT>order = input.ravel().argsort()<EOL><DEDENT>input = input.ravel()[order]<EOL>labels = labels.ravel()[order]<EOL>if find_positions:<EOL><INDENT>positions = positions.ravel()[order]<EOL><DEDENT>result = []<EOL>if find_min:<EOL><INDENT>mins = numpy.zeros(labels.max() + <NUM_LIT:2>, input.dtype)<EOL>mins[labels[::-<NUM_LIT:1>]] = input[::-<NUM_LIT:1>]<EOL>result += [mins[idxs]]<EOL><DEDENT>if find_min_positions:<EOL><INDENT>minpos = numpy.zeros(labels.max() + <NUM_LIT:2>, int)<EOL>minpos[labels[::-<NUM_LIT:1>]] = positions[::-<NUM_LIT:1>]<EOL>result += [minpos[idxs]]<EOL><DEDENT>if find_max:<EOL><INDENT>maxs = numpy.zeros(labels.max() + <NUM_LIT:2>, input.dtype)<EOL>maxs[labels] = input<EOL>result += [maxs[idxs]]<EOL><DEDENT>if find_max_positions:<EOL><INDENT>maxpos = numpy.zeros(labels.max() + <NUM_LIT:2>, int)<EOL>maxpos[labels] = positions<EOL>result += [maxpos[idxs]]<EOL><DEDENT>if find_median:<EOL><INDENT>locs = numpy.arange(len(labels))<EOL>lo = numpy.zeros(labels.max() + <NUM_LIT:2>, numpy.int)<EOL>lo[labels[::-<NUM_LIT:1>]] = locs[::-<NUM_LIT:1>]<EOL>hi = numpy.zeros(labels.max() + <NUM_LIT:2>, numpy.int)<EOL>hi[labels] = locs<EOL>lo = lo[idxs]<EOL>hi = hi[idxs]<EOL>step = (hi - lo) // <NUM_LIT:2><EOL>lo += step<EOL>hi -= step<EOL>result += [(input[lo] + input[hi]) / <NUM_LIT>]<EOL><DEDENT>return result<EOL>

Returns min, max, or both, plus their positions (if requested), and
    median.


def minimum(input, labels=None, index=None):

return _select(input, labels, index, find_min=True)[<NUM_LIT:0>]<EOL>

Calculate the minimum of the values of an array over labeled regions.

Parameters
----------
input : array_like
    Array_like of values. For each region specified by `labels`, the
    minimal values of `input` over the region is computed.
labels : array_like, optional
    An array_like of integers marking different regions over which the
    minimum value of `input` is to be computed. `labels` must have the
    same shape as `input`. If `labels` is not specified, the minimum
    over the whole array is returned.
index : array_like, optional
    A list of region labels that are taken into account for computing the
    minima. If index is None, the minimum over all elements where `labels`
    is non-zero is returned.

Returns
-------
minimum : float or list of floats
    List of minima of `input` over the regions determined by `labels` and
    whose index is in `index`. If `index` or `labels` are not specified, a
    float is returned: the minimal value of `input` if `labels` is None,
    and the minimal value of elements where `labels` is greater than zero
    if `index` is None.

See also
--------
label, maximum, median, minimum_position, extrema, sum, mean, variance,
standard_deviation

Notes
-----
The function returns a Python list and not a Numpy array, use
`np.array` to convert the list to an array.

Examples
--------
>>> a = np.array([[1, 2, 0, 0],
...               [5, 3, 0, 4],
...               [0, 0, 0, 7],
...               [9, 3, 0, 0]])
>>> labels, labels_nb = ndimage.label(a)
>>> labels
array([[1, 1, 0, 0],
       [1, 1, 0, 2],
       [0, 0, 0, 2],
       [3, 3, 0, 0]])
>>> ndimage.minimum(a, labels=labels, index=np.arange(1, labels_nb + 1))
[1.0, 4.0, 3.0]
>>> ndimage.minimum(a)
0.0
>>> ndimage.minimum(a, labels=labels)
1.0