§
    µ³ÙgZa  ã                   óà   — d dl Zd dlmZ ddlmZmZ ddlm	Z	 d dl
mZ d dlmZ d dlmZ dZdd
„Zdd„Zdd„Z G d„ d¦  «        Z G d„ d¦  «        Z G d„ d¦  «        Z G d„ de¦  «        ZdS )é    Né   )Úapprox_derivativeÚgroup_columns)ÚHessianUpdateStrategy)ÚLinearOperator)Úarray_namespace)Úarray_api_extra)z2-pointz3-pointÚcs© c                 ó$   ‡ ‡‡— dgŠˆˆ ˆfd„}|‰fS )Nr   c                 ó(  •— ‰dxx         dz  cc<    ‰t          j        | ¦  «        g‰¢R Ž }t          j        |¦  «        sQ	 t          j        |¦  «                             ¦   «         }n)# t
          t          f$ r}t          d¦  «        |‚d }~ww xY w|S )Nr   r   z@The user-provided objective function must return a scalar value.)ÚnpÚcopyÚisscalarÚasarrayÚitemÚ	TypeErrorÚ
ValueError)ÚxÚfxÚeÚargsÚfunÚncallss      €€€ú^/granary/edev/quantum/lib/python3.11/site-packages/scipy/optimize/_differentiable_functions.pyÚwrappedz_wrapper_fun.<locals>.wrapped   s°   ø€ Øˆqˆ	ˆ	Œ	�Q‰ˆ	ˆ	‰	ð ˆS•”˜‘”Ð#˜dÐ#Ð#Ð#ˆåŒ{˜2‰Œð 	ðÝ”Z ‘^”^×(Ò(Ñ*Ô*��øÝ�zÐ*ð ð ð Ý ð2ñô ð ðøøøøðøøøð
 ˆ	s   Á&A) Á)BÁ:B
Â
Br   )r   r   r   r   s   `` @r   Ú_wrapper_funr      s;   øøø€ ØˆS€Fðð ð ð ð ð ð ð  �Fˆ?Ðó    c                 ót   ‡ ‡‡‡‡— dgŠt          ‰ ¦  «        rˆˆ ˆfd„}|‰fS ‰ t          v rdˆˆˆfd„	}|‰fS d S )Nr   c                 ó~   •— ‰dxx         dz  cc<   t          j         ‰t          j        | ¦  «        g‰¢R Ž ¦  «        S ©Nr   r   )r   Ú
atleast_1dr   )r   Úkwdsr   Úgradr   s     €€€r   r   z_wrapper_grad.<locals>.wrapped'   sB   ø€ à�1ˆIˆIŒI˜‰NˆIˆI‰IÝ”=  ¥b¤g¨a¡j¤jÐ!8°4Ð!8Ð!8Ð!8Ñ9Ô9Ð9r   c                 óD   •— ‰dxx         dz  cc<   t          ‰| fd|i‰¤ŽS )Nr   r   Úf0©r   )r   r&   Úfinite_diff_optionsr   r   s     €€€r   Úwrapped1z_wrapper_grad.<locals>.wrapped1.   sD   ø€ Ø�1ˆIˆIŒI˜‰NˆIˆI‰IÝ$Ø�Qðð ØðØ!4ðð ð r   ©N)ÚcallableÚ
FD_METHODS)r$   r   r   r(   r   r)   r   s   ````  @r   Ú_wrapper_gradr-   #   s“   øøøøø€ ØˆS€Få��~„~ð  ð	:ð 	:ð 	:ð 	:ð 	:ð 	:ð 	:ð ˜ˆÐà	•Ð	Ð	ð	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð ˜ÐÐð 
Ð	r   c                 ó˜  ‡ ‡‡‡‡— t          ‰ ¦  «        r� ‰ t          j        |¦  «        g‰¢R Ž }dgŠt          j        |¦  «        rˆˆ ˆfd„}t          j        |¦  «        }nJt          |t          ¦  «        rˆˆ ˆfd„}n-ˆˆ ˆfd„}t          j        t          j	        |¦  «        ¦  «        }|‰|fS ‰ t          v rdgŠdˆˆfd„	}|‰d fS d S )Nr   c                 ó~   •— ‰dxx         dz  cc<   t          j         ‰t          j        | ¦  «        g‰¢R Ž ¦  «        S r!   )ÚspsÚ
csr_matrixr   r   ©r   r#   r   Úhessr   s     €€€r   r   z_wrapper_hess.<locals>.wrapped=   sB   ø€ Ø�q�	�	”	˜Q‘�	�	‘	Ý”~ d d­2¬7°1©:¬:Ð&=¸Ð&=Ð&=Ð&=Ñ>Ô>Ð>r   c                 óZ   •— ‰dxx         dz  cc<    ‰t          j        | ¦  «        g‰¢R Ž S r!   )r   r   r2   s     €€€r   r   z_wrapper_hess.<locals>.wrappedD   s8   ø€ Ø�q�	�	”	˜Q‘�	�	‘	Ø�t�BœG A™JœJÐ.¨Ð.Ð.Ð.Ð.r   c           	      ó¢   •— ‰dxx         dz  cc<   t          j        t          j         ‰t          j        | ¦  «        g‰¢R Ž ¦  «        ¦  «        S r!   )r   Ú
atleast_2dr   r   r2   s     €€€r   r   z_wrapper_hess.<locals>.wrappedI   sL   ø€ Ø�q�	�	”	˜Q‘�	�	‘	Ý”}¥R¤Z°°µR´W¸Q±Z´ZÐ0GÀ$Ð0GÐ0GÐ0GÑ%HÔ%HÑIÔIÐIr   r   c                 ó$   •— t          ‰| fd|i‰¤ŽS ©Nr&   r'   )r   r&   r(   r$   s     €€r   r)   z_wrapper_hess.<locals>.wrapped1S   s/   ø€ Ý$Ø�aðð ØðØ"5ðð ð r   r*   )r+   r   r   r0   Úissparser1   Ú
isinstancer   r6   r   r,   )	r3   r$   Úx0r   r(   ÚHr   r)   r   s	   `` ``   @r   Ú_wrapper_hessr=   7   sW  øøøøø€ Ý��~„~ð  &ØˆD•”˜‘”Ð$˜tÐ$Ð$Ð$ˆØ�ˆåŒ<˜‰?Œ?ð 	-ð?ð ?ð ?ð ?ð ?ð ?ð ?õ ”˜qÑ!Ô!ˆAˆAå˜�>Ñ*Ô*ð 
	-ð/ð /ð /ð /ð /ð /ð /ð /ð
Jð Jð Jð Jð Jð Jð Jõ ”�bœj¨™mœmÑ,Ô,ˆAà˜ Ð!Ð!Ø	•Ð	Ð	Ø�ˆð	ð 	ð 	ð 	ð 	ð 	ð 	ð
 ˜ Ð%Ð%ð 
Ð	r   c                   óŽ   — e Zd ZdZ	 dd„Zed„ ¦   «         Zed„ ¦   «         Zed„ ¦   «         Zd„ Z	d„ Z
d	„ Zd
„ Zd„ Zd„ Zd„ Zd„ ZdS )ÚScalarFunctiona©  Scalar function and its derivatives.

    This class defines a scalar function F: R^n->R and methods for
    computing or approximating its first and second derivatives.

    Parameters
    ----------
    fun : callable
        evaluates the scalar function. Must be of the form ``fun(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. Should return a scalar.
    x0 : array-like
        Provides an initial set of variables for evaluating fun. Array of real
        elements of size (n,), where 'n' is the number of independent
        variables.
    args : tuple, optional
        Any additional fixed parameters needed to completely specify the scalar
        function.
    grad : {callable, '2-point', '3-point', 'cs'}
        Method for computing the gradient vector.
        If it is a callable, it should be a function that returns the gradient
        vector:

            ``grad(x, *args) -> array_like, shape (n,)``

        where ``x`` is an array with shape (n,) and ``args`` is a tuple with
        the fixed parameters.
        Alternatively, the keywords  {'2-point', '3-point', 'cs'} can be used
        to select a finite difference scheme for numerical estimation of the
        gradient with a relative step size. These finite difference schemes
        obey any specified `bounds`.
    hess : {callable, '2-point', '3-point', 'cs', HessianUpdateStrategy}
        Method for computing the Hessian matrix. If it is callable, it should
        return the  Hessian matrix:

            ``hess(x, *args) -> {LinearOperator, spmatrix, array}, (n, n)``

        where x is a (n,) ndarray and `args` is a tuple with the fixed
        parameters. Alternatively, the keywords {'2-point', '3-point', 'cs'}
        select a finite difference scheme for numerical estimation. Or, objects
        implementing `HessianUpdateStrategy` interface can be used to
        approximate the Hessian.
        Whenever the gradient is estimated via finite-differences, the Hessian
        cannot be estimated with options {'2-point', '3-point', 'cs'} and needs
        to be estimated using one of the quasi-Newton strategies.
    finite_diff_rel_step : None or array_like
        Relative step size to use. The absolute step size is computed as
        ``h = finite_diff_rel_step * sign(x0) * max(1, abs(x0))``, possibly
        adjusted to fit into the bounds. For ``method='3-point'`` the sign
        of `h` is ignored. If None then finite_diff_rel_step is selected
        automatically,
    finite_diff_bounds : tuple of array_like
        Lower and upper bounds on independent variables. Defaults to no bounds,
        (-np.inf, np.inf). Each bound must match the size of `x0` or be a
        scalar, in the latter case the bound will be the same for all
        variables. Use it to limit the range of function evaluation.
    epsilon : None or array_like, optional
        Absolute step size to use, possibly adjusted to fit into the bounds.
        For ``method='3-point'`` the sign of `epsilon` is ignored. By default
        relative steps are used, only if ``epsilon is not None`` are absolute
        steps used.

    Notes
    -----
    This class implements a memoization logic. There are methods `fun`,
    `grad`, hess` and corresponding attributes `f`, `g` and `H`. The following
    things should be considered:

        1. Use only public methods `fun`, `grad` and `hess`.
        2. After one of the methods is called, the corresponding attribute
           will be set. However, a subsequent call with a different argument
           of *any* of the methods may overwrite the attribute.
    Nc	                 óh  — t          |¦  «        s!|t          vrt          dt          › d�¦  «        ‚t          |¦  «        s6|t          v s-t          |t          ¦  «        st          dt          › d�¦  «        ‚|t          v r|t          v rt          d¦  «        ‚t          |¦  «        x| _        }	t          j        |	 	                    |¦  «        d|	¬¦  «        }
|	j
        }|	                     |
j        d¦  «        r|
j        }t          ||¬¦  «        \  | _        | _        || _        || _        || _        || _        |	                     |
|¦  «        | _        || _        | j        j        | _        d	| _        d	| _        d	| _        d | _        t:          j        | _        i }|t          v r||d
<   ||d<   ||d<   ||d<   |t          v r||d
<   ||d<   ||d<   d|d<   |                       ¦   «          tC          || j        ||¬¦  «        \  | _"        | _#        |  $                    ¦   «          t          |¦  «        r.tK          |||¬¦  «        \  | _&        | _'        | _(        d| _        d S |t          v rntK          || j"        ||¬¦  «        \  | _&        | _'        | _(        |  $                    ¦   «          |  &                    | j        | j)        ¬¦  «        | _(        d| _        d S t          |t          ¦  «        rF|| _(        | j(         *                    | j        d¦  «         d| _        d | _+        d | _,        dg| _'        d S d S )Nz)`grad` must be either callable or one of ú.z@`hess` must be either callable, HessianUpdateStrategy or one of z‹Whenever the gradient is estimated via finite-differences, we require the Hessian to be estimated using one of the quasi-Newton strategies.r   ©ÚndimÚxpúreal floating)r   FÚmethodÚrel_stepÚabs_stepÚboundsTÚas_linear_operator)r   r   r(   )r;   r   )r$   r;   r(   ©r&   r3   r   )-r+   r,   r   r:   r   r   rD   ÚxpxÚ
atleast_ndr   Úfloat64ÚisdtypeÚdtyper   Ú_wrapped_funÚ_nfevÚ	_orig_funÚ
_orig_gradÚ
_orig_hessÚ_argsÚastyper   Úx_dtypeÚsizeÚnÚ	f_updatedÚ	g_updatedÚ	H_updatedÚ	_lowest_xr   ÚinfÚ	_lowest_fÚ_update_funr-   Ú_wrapped_gradÚ_ngevÚ_update_gradr=   Ú_wrapped_hessÚ_nhevr<   ÚgÚ
initializeÚx_prevÚg_prev)Úselfr   r;   r   r$   r3   Úfinite_diff_rel_stepÚfinite_diff_boundsÚepsilonrD   Ú_xÚ_dtyper(   s                r   Ú__init__zScalarFunction.__init__¦   s�  € å˜‰~Œ~ð 	 $­jÐ"8Ð"8ÝØI½JÐIÐIÐIñô ð õ ˜‘”ð 	 $­*Ð"4Ð"4Ý˜dÕ$9Ñ:Ô:ð #5åð,Ý(ð,ð ,ð ,ñô ð ð
 •:ÐÐ $­*Ð"4Ð"4Ýð 8ñ 9ô 9ð 9õ
 ' rÑ*Ô*Ð*ˆŒ�"ÝŒ^˜BŸJšJ r™NœN°°rÐ:Ñ:Ô:ˆØ”ˆØ�:Š:�b”h Ñ0Ô0ð 	Ø”XˆFõ )5°S¸tÐ(DÑ(DÔ(DÑ%ˆÔ˜4œ:ØˆŒØˆŒØˆŒØˆŒ
ð —’˜2˜vÑ&Ô&ˆŒØˆŒØ””ˆŒØˆŒØˆŒØˆŒàˆŒÝœˆŒà ÐØ•:ÐÐØ,0Ð Ñ)Ø.BÐ 
Ñ+Ø.5Ð 
Ñ+Ø,>Ð Ñ)Ø•:ÐÐØ,0Ð Ñ)Ø.BÐ 
Ñ+Ø.5Ð 
Ñ+Ø8<ÐÐ 4Ñ5ð 	×ÒÑÔÐõ *7ØØÔ!ØØ 3ð	*
ñ *
ô *
Ñ&ˆÔ˜DœJð 	×ÒÑÔÐõ �D‰>Œ>ð 	Ý5BØ˜ $ð6ñ 6ô 6Ñ2ˆDÔ ¤
¨D¬Fð "ˆDŒNˆNˆNØ•ZÐÐÝ5BØØÔ'ØØ$7ð	6ñ 6ô 6Ñ2ˆDÔ ¤
¨D¬Fð ×ÒÑÔÐØ×'Ò'¨¬°4´6Ð'Ñ:Ô:ˆDŒFØ!ˆDŒNˆNˆNÝ˜Õ3Ñ4Ô4ð 	ØˆDŒFØŒF×Ò˜dœf fÑ-Ô-Ð-Ø!ˆDŒNØˆDŒKØˆDŒKØ˜ˆDŒJˆJˆJð	ð 	r   c                 ó   — | j         d         S ©Nr   )rR   ©rk   s    r   ÚnfevzScalarFunction.nfev  ó   € àŒz˜!Œ}Ðr   c                 ó   — | j         d         S rs   )rc   rt   s    r   ÚngevzScalarFunction.ngev  rv   r   c                 ó   — | j         d         S rs   )rf   rt   s    r   ÚnhevzScalarFunction.nhev
  rv   r   c                 óv  — t          | j        t          ¦  «        r°|                      ¦   «          | j        | _        | j        | _        t          j	        | j
                             |¦  «        d| j
        ¬¦  «        }| j
                             || j        ¦  «        | _        d| _        d| _        d| _        |                      ¦   «          d S t          j	        | j
                             |¦  «        d| j
        ¬¦  «        }| j
                             || j        ¦  «        | _        d| _        d| _        d| _        d S ©Nr   rB   F)r:   rU   r   rd   r   ri   rg   rj   rL   rM   rD   r   rW   rX   r[   r\   r]   Ú_update_hess©rk   r   ro   s      r   Ú	_update_xzScalarFunction._update_x  s  € Ý�d”oÕ'<Ñ=Ô=ð 	#Ø×ÒÑÔÐØœ&ˆDŒKØœ&ˆDŒKõ ” ¤§¢°Ñ 2Ô 2¸¸t¼wÐGÑGÔGˆBØ”W—^’^ B¨¬Ñ5Ô5ˆDŒFØ"ˆDŒNØ"ˆDŒNØ"ˆDŒNØ×ÒÑÔÐÐÐõ ” ¤§¢°Ñ 2Ô 2¸¸t¼wÐGÑGÔGˆBØ”W—^’^ B¨¬Ñ5Ô5ˆDŒFØ"ˆDŒNØ"ˆDŒNØ"ˆDŒNˆNˆNr   c                 ó¤   — | j         sH|                      | j        ¦  «        }|| j        k     r| j        | _        || _        || _        d| _         d S d S ©NT)r[   rQ   r   r`   r^   Úf)rk   r   s     r   ra   zScalarFunction._update_fun%  sZ   € ØŒ~ð 	"Ø×"Ò" 4¤6Ñ*Ô*ˆBØ�D”NÒ"Ð"Ø!%¤�”Ø!#�”àˆDŒFØ!ˆDŒNˆNˆNð	"ð 	"r   c                 ó¶   — | j         sQ| j        t          v r|                      ¦   «          |                      | j        | j        ¬¦  «        | _        d| _         d S d S ©NrK   T)r\   rT   r,   ra   rb   r   r‚   rg   rt   s    r   rd   zScalarFunction._update_grad/  s^   € ØŒ~ð 	"ØŒ¥*Ð,Ð,Ø× Ò Ñ"Ô"Ð"Ø×'Ò'¨¬°4´6Ð'Ñ:Ô:ˆDŒFØ!ˆDŒNˆNˆNð		"ð 	"r   c                 ó¾  — | j         sÕ| j        t          v r;|                      ¦   «          |                      | j        | j        ¬¦  «        | _        nƒt          | j        t          ¦  «        rJ|                      ¦   «          | j         
                    | j        | j        z
  | j        | j        z
  ¦  «         n|                      | j        ¦  «        | _        d| _         d S d S r„   )r]   rU   r,   rd   re   r   rg   r<   r:   r   Úupdateri   rj   rt   s    r   r}   zScalarFunction._update_hess6  sÉ   € ØŒ~ð 
	"ØŒ¥*Ð,Ð,Ø×!Ò!Ñ#Ô#Ð#Ø×+Ò+¨D¬F°t´vÐ+Ñ>Ô>�”�Ý˜DœOÕ-BÑCÔCð 4Ø×!Ò!Ñ#Ô#Ð#Ø”—’˜dœf t¤{Ñ2°D´F¸T¼[Ñ4HÑIÔIÐIÐIà×+Ò+¨D¬FÑ3Ô3�”à!ˆDŒNˆNˆNð
	"ð 
	"r   c                 ó–   — t          j        || j        ¦  «        s|                      |¦  «         |                      ¦   «          | j        S r*   )r   Úarray_equalr   r   ra   r‚   ©rk   r   s     r   r   zScalarFunction.funC  sC   € ÝŒ~˜a ¤Ñ(Ô(ð 	Ø�NŠN˜1ÑÔÐØ×ÒÑÔÐØŒvˆr   c                 ó–   — t          j        || j        ¦  «        s|                      |¦  «         |                      ¦   «          | j        S r*   )r   rˆ   r   r   rd   rg   r‰   s     r   r$   zScalarFunction.gradI  óC   € ÝŒ~˜a ¤Ñ(Ô(ð 	Ø�NŠN˜1ÑÔÐØ×ÒÑÔÐØŒvˆr   c                 ó–   — t          j        || j        ¦  «        s|                      |¦  «         |                      ¦   «          | j        S r*   )r   rˆ   r   r   r}   r<   r‰   s     r   r3   zScalarFunction.hessO  r‹   r   c                 óÌ   — t          j        || j        ¦  «        s|                      |¦  «         |                      ¦   «          |                      ¦   «          | j        | j        fS r*   )r   rˆ   r   r   ra   rd   r‚   rg   r‰   s     r   Úfun_and_gradzScalarFunction.fun_and_gradU  s\   € ÝŒ~˜a ¤Ñ(Ô(ð 	Ø�NŠN˜1ÑÔÐØ×ÒÑÔÐØ×ÒÑÔÐØŒv�t”vˆ~Ðr   r*   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__rq   Úpropertyru   rx   rz   r   ra   rd   r}   r   r$   r3   rŽ   r   r   r   r?   r?   [   s  € € € € € ðIð IðV .2ðZð Zð Zð Zðx ðð ñ „Xðð ðð ñ „Xðð ðð ñ „Xðð#ð #ð #ð."ð "ð "ð"ð "ð "ð"ð "ð "ðð ð ðð ð ðð ð ðð ð ð ð r   r?   c                   óH   — e Zd ZdZd„ Zd„ Zd„ Zd„ Zd„ Zd„ Z	d„ Z
d	„ Zd
„ ZdS )ÚVectorFunctiona‘  Vector function and its derivatives.

    This class defines a vector function F: R^n->R^m and methods for
    computing or approximating its first and second derivatives.

    Notes
    -----
    This class implements a memoization logic. There are methods `fun`,
    `jac`, hess` and corresponding attributes `f`, `J` and `H`. The following
    things should be considered:

        1. Use only public methods `fun`, `jac` and `hess`.
        2. After one of the methods is called, the corresponding attribute
           will be set. However, a subsequent call with a different argument
           of *any* of the methods may overwrite the attribute.
    c	                 ó   ‡ ‡‡‡‡‡‡‡‡— t          ‰¦  «        s!‰t          vrt          dt          › d�¦  «        ‚t          ‰¦  «        s6‰t          v s-t          ‰t          ¦  «        st          dt          › d�¦  «        ‚‰t          v r‰t          v rt          d¦  «        ‚t          |¦  «        x‰ _        }	t          j        |	 	                    |¦  «        d|	¬¦  «        }
|	j
        }|	                     |
j        d¦  «        r|
j        }|	                     |
|¦  «        ‰ _        |‰ _        ‰ j        j        ‰ _        d‰ _        d‰ _        d‰ _        d	‰ _        d	‰ _        d	‰ _        i Š‰t          v rE‰‰d
<   |‰d<   |�t1          |¦  «        }||f‰d<   |‰d<   t3          j        ‰ j        ¦  «        ‰ _        ‰t          v r-‰‰d
<   |‰d<   d‰d<   t3          j        ‰ j        ¦  «        ‰ _        ‰t          v r‰t          v rt          d¦  «        ‚ˆˆ fd„Šˆˆ fd„}|‰ _         |¦   «          t3          j        ‰ j        ¦  «        ‰ _        ‰ j        j        ‰ _         t          ‰¦  «        rì ‰‰ j        ¦  «        ‰ _!        d‰ _        ‰ xj        dz  c_        |s|€EtE          j#        ‰ j!        ¦  «        r,ˆˆ fd„ŠtE          j$        ‰ j!        ¦  «        ‰ _!        d‰ _%        nptE          j#        ‰ j!        ¦  «        r,ˆˆ fd„Š‰ j!         &                    ¦   «         ‰ _!        d	‰ _%        n+ˆˆ fd„Št3          j'        ‰ j!        ¦  «        ‰ _!        d	‰ _%        ˆˆ fd„}në‰t          v râtQ          ‰‰ j        fd‰ j        i‰¤Ž‰ _!        d‰ _        |s|€FtE          j#        ‰ j!        ¦  «        r-ˆˆˆ fd„}tE          j$        ‰ j!        ¦  «        ‰ _!        d‰ _%        nrtE          j#        ‰ j!        ¦  «        r-ˆˆˆ fd„}‰ j!         &                    ¦   «         ‰ _!        d	‰ _%        n,ˆˆˆ fd„}t3          j'        ‰ j!        ¦  «        ‰ _!        d	‰ _%        |‰ _)        t          ‰¦  «        rÎ ‰‰ j        ‰ j        ¦  «        ‰ _*        d‰ _        ‰ xj        dz  c_        tE          j#        ‰ j*        ¦  «        r%ˆˆ fd„ŠtE          j$        ‰ j*        ¦  «        ‰ _*        nWt          ‰ j*        tV          ¦  «        rˆˆ fd„Šn6ˆˆ fd„Št3          j'        t3          j	        ‰ j*        ¦  «        ¦  «        ‰ _*        ˆˆ fd„}n}‰t          v rˆfd„Šˆˆˆ fd„} |¦   «          d‰ _        nVt          ‰t          ¦  «        rA‰‰ _*        ‰ j*         ,                    ‰ j        d ¦  «         d‰ _        d ‰ _-        d ‰ _.        ˆ fd!„}|‰ _/        t          ‰t          ¦  «        rˆ fd"„}nˆ fd#„}|‰ _0        d S )$Nz(`jac` must be either callable or one of rA   z?`hess` must be either callable,HessianUpdateStrategy or one of z‹Whenever the Jacobian is estimated via finite-differences, we require the Hessian to be estimated using one of the quasi-Newton strategies.r   rB   rE   r   FrF   rG   ÚsparsityrI   TrJ   c                 ó^   •— ‰xj         dz  c_         t          j         ‰| ¦  «        ¦  «        S ©Nr   )ru   r   r"   )r   r   rk   s    €€r   Úfun_wrappedz,VectorFunction.__init__.<locals>.fun_wrapped§  s*   ø€ ØˆIŒI˜‰NˆIŒIÝ”=   Q¡¤Ñ(Ô(Ð(r   c                  ó2   •—  ‰ ‰j         ¦  «        ‰_        d S r*   )r   r‚   )rš   rk   s   €€r   Ú
update_funz+VectorFunction.__init__.<locals>.update_fun«  s   ø€ Ø �[ ¤Ñ(Ô(ˆDŒFˆFˆFr   c                 ó^   •— ‰xj         dz  c_         t          j         ‰| ¦  «        ¦  «        S r™   )Únjevr0   r1   ©r   Újacrk   s    €€r   Újac_wrappedz,VectorFunction.__init__.<locals>.jac_wrapped¼  s*   ø€ Ø�I”I ‘N�I”IÝœ>¨#¨#¨a©&¬&Ñ1Ô1Ð1r   c                 ó^   •— ‰xj         dz  c_          ‰| ¦  «                             ¦   «         S r™   )rž   ÚtoarrayrŸ   s    €€r   r¡   z,VectorFunction.__init__.<locals>.jac_wrappedÃ  s*   ø€ Ø�I”I ‘N�I”IØ˜3˜q™6œ6Ÿ>š>Ñ+Ô+Ð+r   c                 ó^   •— ‰xj         dz  c_         t          j         ‰| ¦  «        ¦  «        S r™   )rž   r   r6   rŸ   s    €€r   r¡   z,VectorFunction.__init__.<locals>.jac_wrappedÊ  s*   ø€ Ø�I”I ‘N�I”IÝœ=¨¨¨Q©¬Ñ0Ô0Ð0r   c                  ó2   •—  ‰ ‰j         ¦  «        ‰_        d S r*   )r   ÚJ)r¡   rk   s   €€r   Ú
update_jacz+VectorFunction.__init__.<locals>.update_jacÐ  s   ø€ Ø$˜ T¤VÑ,Ô,�”��r   r&   c                  ó’   •— ‰                      ¦   «          t          j        t          ‰‰j        fd‰j        i‰ ¤Ž¦  «        ‰_        d S r8   )ra   r0   r1   r   r   r‚   r¦   ©r(   rš   rk   s   €€€r   r§   z+VectorFunction.__init__.<locals>.update_jacÚ  s\   ø€ Ø×$Ò$Ñ&Ô&Ð&Ý œ^Ý)¨+°t´vð Að AÀ$Ä&ð AØ,?ðAð AñBô B�D”F�F�Fr   c                  ó’   •— ‰                      ¦   «          t          ‰‰j        fd‰j        i‰ ¤Ž                     ¦   «         ‰_        d S r8   )ra   r   r   r‚   r£   r¦   r©   s   €€€r   r§   z+VectorFunction.__init__.<locals>.update_jacã  sW   ø€ Ø×$Ò$Ñ&Ô&Ð&Ý.¨{¸D¼Fð Fð FÀtÄvð FØ1DðFð FßFMÂgÁiÄið ”F�F�Fr   c                  ó’   •— ‰                      ¦   «          t          j        t          ‰‰j        fd‰j        i‰ ¤Ž¦  «        ‰_        d S r8   )ra   r   r6   r   r   r‚   r¦   r©   s   €€€r   r§   z+VectorFunction.__init__.<locals>.update_jacë  s\   ø€ Ø×$Ò$Ñ&Ô&Ð&Ýœ]Ý)¨+°t´vð Að AÀ$Ä&ð AØ,?ðAð AñBô B�D”F�F�Fr   c                 ó`   •— ‰xj         dz  c_         t          j         ‰| |¦  «        ¦  «        S r™   )rz   r0   r1   ©r   Úvr3   rk   s     €€r   Úhess_wrappedz-VectorFunction.__init__.<locals>.hess_wrappedü  s,   ø€ Ø�I”I ‘N�I”IÝœ>¨$¨$¨q°!©*¬*Ñ5Ô5Ð5r   c                 ó<   •— ‰xj         dz  c_          ‰| |¦  «        S r™   )rz   r­   s     €€r   r¯   z-VectorFunction.__init__.<locals>.hess_wrapped  s"   ø€ Ø�I”I ‘N�I”IØ˜4  1™:œ:Ð%r   c                 ó„   •— ‰xj         dz  c_         t          j        t          j         ‰| |¦  «        ¦  «        ¦  «        S r™   )rz   r   r6   r   r­   s     €€r   r¯   z-VectorFunction.__init__.<locals>.hess_wrapped  s6   ø€ Ø�I”I ‘N�I”IÝœ=­¬°D°D¸¸A±J´JÑ)?Ô)?Ñ@Ô@Ð@r   c                  ó>   •—  ‰ ‰j         ‰j        ¦  «        ‰_        d S r*   )r   r®   r<   )r¯   rk   s   €€r   Úupdate_hessz,VectorFunction.__init__.<locals>.update_hess  s   ø€ Ø%˜ d¤f¨d¬fÑ5Ô5�”��r   c                 óJ   •—  ‰| ¦  «        j                              |¦  «        S r*   )ÚTÚdot)r   r®   r¡   s     €r   Ú	jac_dot_vz*VectorFunction.__init__.<locals>.jac_dot_v  s"   ø€ Ø"�{ 1‘~”~Ô'×+Ò+¨AÑ.Ô.Ð.r   c                  ó¶   •— ‰                      ¦   «          t          ‰‰j        f‰j        j                             ‰j        ¦  «        ‰j        fdœ‰ ¤Ž‰_        d S )N)r&   r   )Ú_update_jacr   r   r¦   rµ   r¶   r®   r<   )r(   r·   rk   s   €€€r   r³   z,VectorFunction.__init__.<locals>.update_hess  sf   ø€ Ø× Ò Ñ"Ô"Ð"Ý*¨9°d´fð BØ.2¬f¬h¯lªl¸4¼6Ñ.BÔ.BØ15´°	ðBð Bð .AðBð B�”��r   r3   c                  ó:  •— ‰                      ¦   «          ‰j        �|‰j        �w‰j        ‰j        z
  } ‰j        j                             ‰j        ¦  «        ‰j        j                             ‰j        ¦  «        z
  }‰j         	                    | |¦  «         d S d S d S r*   )
r¹   ri   ÚJ_prevr   r¦   rµ   r¶   r®   r<   r†   )Údelta_xÚdelta_grk   s     €r   r³   z,VectorFunction.__init__.<locals>.update_hess!  sŽ   ø€ Ø× Ò Ñ"Ô"Ð"ð ”;Ð*¨t¬{Ð/FØ"œf t¤{Ñ2�GØ"œfœhŸlšl¨4¬6Ñ2Ô2°T´[´]×5FÒ5FÀtÄvÑ5NÔ5NÑN�GØ”F—M’M '¨7Ñ3Ô3Ð3Ð3Ð3ð +Ð*Ð/FÐ/Fr   c                 ód  •— ‰                      ¦   «          ‰j        ‰_        ‰j        ‰_        t          j        ‰j                             | ¦  «        d‰j        ¬¦  «        }‰j         	                    |‰j
        ¦  «        ‰_        d‰_        d‰_        d‰_        ‰                     ¦   «          d S r|   )r¹   r   ri   r¦   r»   rL   rM   rD   r   rW   rX   r[   Ú	J_updatedr]   r}   ©r   ro   rk   s     €r   Úupdate_xz)VectorFunction.__init__.<locals>.update_x-  s“   ø€ Ø× Ò Ñ"Ô"Ð"Ø"œf�”Ø"œf�”Ý”^ D¤G§O¢O°AÑ$6Ô$6¸QÀ4Ä7ÐKÑKÔK�ØœŸš¨¨D¬LÑ9Ô9�”Ø!&�”Ø!&�”Ø!&�”Ø×!Ò!Ñ#Ô#Ð#Ð#Ð#r   c                 óä   •— t          j        ‰j                             | ¦  «        d‰j        ¬¦  «        }‰j                             |‰j        ¦  «        ‰_        d‰_        d‰_        d‰_	        d S r|   )
rL   rM   rD   r   rW   rX   r   r[   r¿   r]   rÀ   s     €r   rÁ   z)VectorFunction.__init__.<locals>.update_x8  s[   ø€ Ý”^ D¤G§O¢O°AÑ$6Ô$6¸QÀ4Ä7ÐKÑKÔK�ØœŸš¨¨D¬LÑ9Ô9�”Ø!&�”Ø!&�”Ø!&�”��r   )1r+   r,   r   r:   r   r   rD   rL   rM   r   rN   rO   rP   rW   r   rX   rY   rZ   ru   rž   rz   r[   r¿   r]   r   r   r   Úx_diffÚ_update_fun_implÚ
zeros_liker‚   r®   Úmr¦   r0   r9   r1   Úsparse_jacobianr£   r6   r   Ú_update_jac_implr<   r   rh   ri   r»   Ú_update_hess_implÚ_update_x_impl)rk   r   r;   r    r3   rl   Úfinite_diff_jac_sparsityrm   rÇ   rD   ro   rp   Úsparsity_groupsrœ   r§   r³   rÁ   r(   rš   r¯   r·   r¡   s   `` ``            @@@@@r   rq   zVectorFunction.__init__n  s¥  øøøøøøøøø€ õ ˜‰}Œ}ð 	W ­JÐ!6Ð!6ÝÐUÍ
ÐUÐUÐUÑVÔVÐVå˜‘”ð 	O $­*Ð"4Ð"4Ý˜dÕ$9Ñ:Ô:ð #5åð NÝ@JðNð Nð Nñ Oô Oð Oð •*ÐÐ ­Ð!3Ð!3Ýð +ñ ,ô ,ð ,õ
 ' rÑ*Ô*Ð*ˆŒ�"ÝŒ^˜BŸJšJ r™NœN°°rÐ:Ñ:Ô:ˆØ”ˆØ�:Š:�b”h Ñ0Ô0ð 	Ø”XˆFð —’˜2˜vÑ&Ô&ˆŒØˆŒà””ˆŒØˆŒ	ØˆŒ	ØˆŒ	ØˆŒØˆŒØˆŒà ÐØ•*ÐÐØ,/Ð Ñ)Ø.BÐ 
Ñ+Ø'Ð3Ý"/Ð0HÑ"IÔ"I�Ø3KØ3Bð3DÐ# JÑ/à,>Ð Ñ)Ýœ' $¤&™/œ/ˆDŒKØ•:ÐÐØ,0Ð Ñ)Ø.BÐ 
Ñ+Ø8<ÐÐ 4Ñ5Ýœ' $¤&™/œ/ˆDŒKØ•*ÐÐ ­Ð!3Ð!3Ýð +ñ ,ô ,ð ,ð	)ð 	)ð 	)ð 	)ð 	)ð 	)ð	)ð 	)ð 	)ð 	)ð 	)ð 	)ð !+ˆÔØˆ
‰Œˆå”˜tœvÑ&Ô&ˆŒØ””ˆŒõ �C‰=Œ=ð <	-Ø�S˜œ‘[”[ˆDŒFØ!ˆDŒNØˆIŒI˜‰NˆIŒIàð -Ø#Ð+µ´¸T¼VÑ0DÔ0DÐ+ð2ð 2ð 2ð 2ð 2ð 2õ œ¨¬Ñ/Ô/�”Ø'+�Ô$Ð$å”˜dœfÑ%Ô%ð -ð,ð ,ð ,ð ,ð ,ð ,ð œŸšÑ)Ô)�”Ø',�Ô$Ð$ð1ð 1ð 1ð 1ð 1ð 1õ œ t¤vÑ.Ô.�”Ø',�Ô$ð-ð -ð -ð -ð -ð -ð -ð •JÐÐÝ& {°D´Fð >ð >¸t¼vð >Ø)<ð>ð >ˆDŒFà!ˆDŒNàð -Ø#Ð+µ´¸T¼VÑ0DÔ0DÐ+ðBð Bð Bð Bð Bð Bð Bõ
 œ¨¬Ñ/Ô/�”Ø'+�Ô$Ð$å”˜dœfÑ%Ô%ð -ðPð Pð Pð Pð Pð Pð Pð œŸšÑ)Ô)�”Ø',�Ô$Ð$ðBð Bð Bð Bð Bð Bð Bõ
 œ t¤vÑ.Ô.�”Ø',�Ô$à *ˆÔõ �D‰>Œ>ð 2	4Ø�T˜$œ& $¤&Ñ)Ô)ˆDŒFØ!ˆDŒNØˆIŒI˜‰NˆIŒIåŒ|˜DœFÑ#Ô#ð ;ð6ð 6ð 6ð 6ð 6ð 6õ œ¨¬Ñ/Ô/�”�å˜DœF¥NÑ3Ô3ð 	;ð&ð &ð &ð &ð &ð &ð &ð
Að Að Að Að Að Aõ œ¥r¤z°$´&Ñ'9Ô'9Ñ:Ô:�”ð6ð 6ð 6ð 6ð 6ð 6ð 6à•ZÐÐð/ð /ð /ð /ð /ðBð Bð Bð Bð Bð Bð Bð ˆK‰MŒMˆMØ!ˆDŒNˆNÝ˜Õ3Ñ4Ô4ð 	4ØˆDŒFØŒF×Ò˜dœf fÑ-Ô-Ð-Ø!ˆDŒNØˆDŒKØˆDŒKð4ð 4ð 4ð 4ð 4ð "-ˆÔå�dÕ1Ñ2Ô2ð 	'ð	$ð 	$ð 	$ð 	$ð 	$ð 	$ð'ð 'ð 'ð 'ð 'ð 'ˆÔÐÐr   c                 óZ   — t          j        || j        ¦  «        s|| _        d| _        d S d S )NF)r   rˆ   r®   r]   )rk   r®   s     r   Ú	_update_vzVectorFunction._update_vA  s4   € ÝŒ~˜a ¤Ñ(Ô(ð 	#ØˆDŒFØ"ˆDŒNˆNˆNð	#ð 	#r   c                 óh   — t          j        || j        ¦  «        s|                      |¦  «         d S d S r*   )r   rˆ   r   rÊ   r‰   s     r   r   zVectorFunction._update_xF  s<   € ÝŒ~˜a ¤Ñ(Ô(ð 	#Ø×Ò Ñ"Ô"Ð"Ð"Ð"ð	#ð 	#r   c                 óN   — | j         s|                      ¦   «          d| _         d S d S r�   )r[   rÄ   rt   s    r   ra   zVectorFunction._update_funJ  ó3   € ØŒ~ð 	"Ø×!Ò!Ñ#Ô#Ð#Ø!ˆDŒNˆNˆNð	"ð 	"r   c                 óN   — | j         s|                      ¦   «          d| _         d S d S r�   )r¿   rÈ   rt   s    r   r¹   zVectorFunction._update_jacO  rÑ   r   c                 óN   — | j         s|                      ¦   «          d| _         d S d S r�   )r]   rÉ   rt   s    r   r}   zVectorFunction._update_hessT  s3   € ØŒ~ð 	"Ø×"Ò"Ñ$Ô$Ð$Ø!ˆDŒNˆNˆNð	"ð 	"r   c                 ób   — |                       |¦  «         |                      ¦   «          | j        S r*   )r   ra   r‚   r‰   s     r   r   zVectorFunction.funY  ó-   € Ø�Š�qÑÔÐØ×ÒÑÔÐØŒvˆr   c                 ób   — |                       |¦  «         |                      ¦   «          | j        S r*   )r   r¹   r¦   r‰   s     r   r    zVectorFunction.jac^  rÕ   r   c                 óŒ   — |                       |¦  «         |                      |¦  «         |                      ¦   «          | j        S r*   )rÎ   r   r}   r<   ©rk   r   r®   s      r   r3   zVectorFunction.hessc  s?   € à�Š�qÑÔÐØ�Š�qÑÔÐØ×ÒÑÔÐØŒvˆr   N)r�   r�   r‘   r’   rq   rÎ   r   ra   r¹   r}   r   r    r3   r   r   r   r•   r•   ]  s©   € € € € € ðð ð Q'ð Q'ð Q'ðf#ð #ð #ð
#ð #ð #ð"ð "ð "ð
"ð "ð "ð
"ð "ð "ð
ð ð ð
ð ð ð
ð ð ð ð r   r•   c                   ó0   — e Zd ZdZd„ Zd„ Zd„ Zd„ Zd„ ZdS )ÚLinearVectorFunctionzüLinear vector function and its derivatives.

    Defines a linear function F = A x, where x is N-D vector and
    A is m-by-n matrix. The Jacobian is constant and equals to A. The Hessian
    is identically zero and it is returned as a csr matrix.
    c                 óx  — |s|€5t          j        |¦  «        r!t          j        |¦  «        | _        d| _        ngt          j        |¦  «        r!|                     ¦   «         | _        d| _        n2t          j        t          j        |¦  «        ¦  «        | _        d| _        | j        j	        \  | _
        | _        t          |¦  «        x| _        }t          j        |                     |¦  «        d|¬¦  «        }|j        }|                     |j        d¦  «        r|j        }|                     ||¦  «        | _        || _        | j                             | j        ¦  «        | _        d| _        t          j        | j
        t4          ¬¦  «        | _        t          j        | j        | j        f¦  «        | _        d S )NTFr   rB   rE   )rP   )r0   r9   r1   r¦   rÇ   r£   r   r6   r   ÚshaperÆ   rZ   r   rD   rL   rM   rN   rO   rP   rW   r   rX   r¶   r‚   r[   ÚzerosÚfloatr®   r<   )rk   ÚAr;   rÇ   rD   ro   rp   s          r   rq   zLinearVectorFunction.__init__r  sb  € Øð 		)˜oÐ5½#¼,Àq¹/¼/Ð5Ý”^ AÑ&Ô&ˆDŒFØ#'ˆDÔ Ð ÝŒ\˜!‰_Œ_ð 	)Ø—Y’Y‘[”[ˆDŒFØ#(ˆDÔ Ð õ ”]¥2¤:¨a¡=¤=Ñ1Ô1ˆDŒFØ#(ˆDÔ àœœ‰ˆŒ�”å& rÑ*Ô*Ð*ˆŒ�"ÝŒ^˜BŸJšJ r™NœN°°rÐ:Ñ:Ô:ˆØ”ˆØ�:Š:�b”h Ñ0Ô0ð 	Ø”XˆFð —’˜2˜vÑ&Ô&ˆŒØˆŒà”—’˜DœFÑ#Ô#ˆŒØˆŒå”˜$œ&­Ð.Ñ.Ô.ˆŒÝ” ¤¨¬Ð 0Ñ1Ô1ˆŒˆˆr   c                 óþ   — t          j        || j        ¦  «        sbt          j        | j                             |¦  «        d| j        ¬¦  «        }| j                             || j        ¦  «        | _        d| _	        d S d S r|   )
r   rˆ   r   rL   rM   rD   r   rW   rX   r[   r~   s      r   r   zLinearVectorFunction._update_x�  sl   € ÝŒ~˜a ¤Ñ(Ô(ð 	#Ý” ¤§¢°Ñ 2Ô 2¸¸t¼wÐGÑGÔGˆBØ”W—^’^ B¨¬Ñ5Ô5ˆDŒFØ"ˆDŒNˆNˆNð	#ð 	#r   c                 ó”   — |                       |¦  «         | j        s&| j                             |¦  «        | _        d| _        | j        S r�   )r   r[   r¦   r¶   r‚   r‰   s     r   r   zLinearVectorFunction.fun–  s?   € Ø�Š�qÑÔÐØŒ~ð 	"Ø”V—Z’Z ‘]”]ˆDŒFØ!ˆDŒNØŒvˆr   c                 ó:   — |                       |¦  «         | j        S r*   )r   r¦   r‰   s     r   r    zLinearVectorFunction.jac�  s   € Ø�Š�qÑÔÐØŒvˆr   c                 óH   — |                       |¦  «         || _        | j        S r*   )r   r®   r<   rØ   s      r   r3   zLinearVectorFunction.hess¡  s"   € Ø�Š�qÑÔÐØˆŒØŒvˆr   N)	r�   r�   r‘   r’   rq   r   r   r    r3   r   r   r   rÚ   rÚ   k  si   € € € € € ðð ð2ð 2ð 2ð<#ð #ð #ðð ð ðð ð ðð ð ð ð r   rÚ   c                   ó"   ‡ — e Zd ZdZˆ fd„Zˆ xZS )ÚIdentityVectorFunctionzþIdentity vector function and its derivatives.

    The Jacobian is the identity matrix, returned as a dense array when
    `sparse_jacobian=False` and as a csr matrix otherwise. The Hessian is
    identically zero and it is returned as a csr matrix.
    c                 óÒ   •— t          |¦  «        }|s|€t          j        |d¬¦  «        }d}nt          j        |¦  «        }d}t	          ¦   «                              |||¦  «         d S )NÚcsr)ÚformatTF)Úlenr0   Úeyer   Úsuperrq   )rk   r;   rÇ   rZ   rß   Ú	__class__s        €r   rq   zIdentityVectorFunction.__init__®  sj   ø€ Ý�‰GŒGˆØð 	$˜oÐ5Ý”˜ %Ð(Ñ(Ô(ˆAØ"ˆOˆOå”�q‘	”	ˆAØ#ˆOÝ‰Œ×Ò˜˜B Ñ0Ô0Ð0Ð0Ð0r   )r�   r�   r‘   r’   rq   Ú__classcell__)rì   s   @r   rå   rå   §  sB   ø€ € € € € ðð ð1ð 1ð 1ð 1ð 1ð 1ð 1ð 1ð 1r   rå   )r   )Nr   N)NNr   N)Únumpyr   Úscipy.sparseÚsparser0   Ú_numdiffr   r   Ú_hessian_update_strategyr   Úscipy.sparse.linalgr   Úscipy._lib._array_apir   Ú
scipy._libr	   rL   r,   r   r-   r=   r?   r•   rÚ   rå   r   r   r   ú<module>rö      s‡  ðØ Ð Ð Ð Ø Ð Ð Ð Ð Ð Ø 6Ð 6Ð 6Ð 6Ð 6Ð 6Ð 6Ð 6Ø ;Ð ;Ð ;Ð ;Ð ;Ð ;Ø .Ð .Ð .Ð .Ð .Ð .Ø 1Ð 1Ð 1Ð 1Ð 1Ð 1Ø -Ð -Ð -Ð -Ð -Ð -ð *€
ðð ð ð ð, ð  ð  ð  ð(!&ð !&ð !&ð !&ðHð ð ð ð ñ ô ð ðDKð Kð Kð Kð Kñ Kô Kð Kð\9ð 9ð 9ð 9ð 9ñ 9ô 9ð 9ðx1ð 1ð 1ð 1ð 1Ð1ñ 1ô 1ð 1ð 1ð 1r   