a
    5gh                  
   @   s.   d Z ddlmZ ddlZddlZd	ddZdS )
zScipy interface for Minuit.   )Minuit    N migradc              
   C   sF  ddl m}m} t|}|dur*td|s2|r<td d%dd}|| ||
}d|_t	|r|d	u rptd
t
|s|J |||}nd}t|||d}|durt||rdd t|j|jD |_n||_|	r|	|_d}|rh|ddrdnd|_d|v rtd d|v r"tdt |d|d|dd}|dd}|durZ||_|dd|_|dkr|j|d n(|dkr|j|d ntd| d|jrd}|jr|d7 }n|d 7 }n*d!}|j}|jr|d"7 }|jr|d#7 }t|}|t|j |j|j!|j"dur(|j"nt#||f||j$|j%|d$S )&a  
    Interface to MIGRAD using the ``scipy.optimize.minimize`` API.

    This function provides the same interface as ``scipy.optimize.minimize``. If you are
    familiar with the latter, this allows you to use Minuit with a quick start.
    Eventually, you still may want to learn the interface of the :class:`Minuit` class,
    as it provides more functionality if you are interested in parameter uncertainties.

    For a general description of the arguments of this function, see
    ``scipy.optimize.minimize``. We list only a few in the following.

    Parameters
    ----------
    method: str
        Allowed values are "migrad" or "simplex". Default: "migrad".
    options: dict
        Can be used to pass special settings to Minuit. All are optional.
        The following options are supported.
        *disp* (bool): Set to true to print convergence messages. Default: False.
        *stra* (int): Minuit strategy (0: fast/inaccurate, 1: balanced,
        2: slow/accurate). Default: 1.
        *maxfun* (int): Maximum allowed number of iterations. Default: None.
        *maxfev* (int): Deprecated alias for *maxfun*.
        *eps* (sequence): Initial step size to numerical compute derivative.
        Minuit automatically refines this in subsequent iterations and is very
        insensitive to the initial choice. Default: 1.

    Returns
    -------
    OptimizeResult
        Dict with attribute access that holds the result. It contains the following
        lists of fields. *x* (ndarray): Solution of optimization.
        *fun* (float): Value of objective function at minimum.
        *message* (str): Description of cause of termination.
        *hess_inv* (ndarray): Inverse of Hesse matrix at minimum (may not be exact).
        *nfev* (int): Number of function evaluations.
        *njev* (int): Number of jacobian evaluations.
        *minuit* (Minuit): Minuit object internally used to do the minimization.
        Use this to extract more information about the parameter errors.
    r   )OptimizeResultBoundsNz4Constraints are not supported by Minuit, only boundsz:hess and hessp arguments cannot be handled and are ignoredc                    s*   d u r fddS  fdd}|S )Nc                    s   | g R  S Nr   x)argsfuncr   `/mounts/lovelace/software/anaconda3/envs/metaDMG/lib/python3.9/site-packages/iminuit/minimize.py<lambda>J       z+minimize.<locals>.wrapped.<locals>.<lambda>c                    s   |  | g R  S r   r   r	   r   callbackr   r   r   fL   s    z$minimize.<locals>.wrapped.<locals>.fr   )r   r   r   r   r   r   r   wrappedH   s    zminimize.<locals>.wrappedg      ?Tz,jac=True is not supported, only jac=callable)Zgradc                 S   s   g | ]\}}||fqS r   r   ).0abr   r   r   
<listcomp>`   r   zminimize.<locals>.<listcomp>ZdispF   maxiterz/maxiter not supported, acts like maxfun insteadZmaxfevz(maxfev is deprecated, use maxfun insteadZmaxfunepsZstrar   r   )ncallsimplexzkeyword method=z not understoodz$Optimization terminated successfully.z$, but uncertainties are unrealiable.zOptimization failed.z Call limit was reached.z) Estimated distance to minimum too large.)r
   successfunZhess_invmessageZnfevZnjevminuit)N)&Zscipy.optimizer   r   npZ
atleast_1d
ValueErrorwarningswarnZerrordefboolcallabler   
isinstancezipZlbZubZlimitstolgetZprint_levelDeprecationWarningerrorsZstrategyr   r   ZvalidZaccuratefminZhas_reached_call_limitZis_above_max_edmlenarrayvaluesZfvalZ
covarianceZonesZnfcnZngrad)r   Zx0r   methodZjacZhessZhesspZboundsconstraintsr*   r   optionsr   r   r   Zwrapped_funZwrapped_gradmr   r-   r    r.   nr   r   r   minimize   s~    6













r7   )
r   r   NNNNNNNN)__doc__r!   r   r$   numpyr"   r7   r   r   r   r   <module>   s             