a
    ˜¥»anV  ã                   @   s~   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ZG dd„ dƒZG d	d
„ d
ƒZG dd„ dƒZG dd„ deƒZdS )é    Né   )Úapprox_derivativeÚgroup_columns)ÚHessianUpdateStrategy)ÚLinearOperator)z2-pointz3-pointÚcsc                   @   sR   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
d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	                    sÊ  t ˆƒs ˆtvr tdt› d�ƒ‚t ˆƒsJˆtv sJtˆtƒsJtdt› d�ƒ‚ˆtv rbˆtv rbtdƒ‚t |¡ t¡ˆ_	ˆj	j
ˆ_dˆ_dˆ_dˆ_dˆ_dˆ_dˆ_i ‰ˆtv rÎˆˆd< |ˆd< |ˆd	< |ˆd
< ˆtv röˆˆd< |ˆd< |ˆd	< dˆd< ‡ ‡‡fdd„‰‡‡fdd„}	|	ˆ_ˆ ¡  t ˆƒ�rL‡ ‡‡fdd„‰‡‡fdd„}
nˆtv �rf‡‡‡fdd„}
|
ˆ_ˆ ¡  t ˆƒ�r,ˆt |¡gˆ ¢R Ž ˆ_dˆ_ˆ jd7  _t ˆj¡�rØ‡ ‡‡fdd„‰t ˆj¡ˆ_nDtˆjtƒ�rø‡ ‡‡fdd„‰n$‡ ‡‡fdd„‰t t ˆj¡¡ˆ_‡‡fdd„}nhˆtv �rT‡‡‡fdd„}|ƒ  dˆ_n@tˆtƒ�r”ˆˆ_ˆj ˆjd¡ dˆ_d ˆ_d ˆ_ ‡fdd„}|ˆ_!tˆtƒ�r´‡fd d!„}n‡fd"d!„}|ˆ_"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   FÚmethodÚrel_stepZabs_stepÚboundsTÚas_linear_operatorc                    s$   ˆ j d7  _ ˆt | ¡gˆ ¢R Ž S ©Nr   )ÚnfevÚnpÚcopy©Úx)ÚargsÚfunÚself© úb/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/scipy/optimize/_differentiable_functions.pyÚfun_wrapped�   s    z,ScalarFunction.__init__.<locals>.fun_wrappedc                      s   ˆ ˆj ƒˆ_d S ©N©r   Úfr   ©r   r   r   r   Ú
update_funˆ   s    z+ScalarFunction.__init__.<locals>.update_func                    s*   ˆ j d7  _ t ˆt | ¡gˆ ¢R Ž ¡S r   )Úngevr   Ú
atleast_1dr   r   )r   Úgradr   r   r   Úgrad_wrapped�   s    z-ScalarFunction.__init__.<locals>.grad_wrappedc                      s   ˆ ˆj ƒˆ_d S r   )r   Úgr   )r"   r   r   r   Úupdate_grad”   s    z,ScalarFunction.__init__.<locals>.update_gradc                      s6   ˆ  ¡  ˆ jd7  _tˆˆjfdˆjiˆ ¤Žˆ_d S )Nr   Úf0)Ú_update_funr   r   r   r   r#   r   ©Úfinite_diff_optionsr   r   r   r   r$   ˜   s
    ÿr   c                    s*   ˆ j d7  _ t ˆt | ¡gˆ ¢R Ž ¡S r   )ÚnhevÚspsÚ
csr_matrixr   r   r   ©r   Úhessr   r   r   Úhess_wrapped¨   s    z-ScalarFunction.__init__.<locals>.hess_wrappedc                    s$   ˆ j d7  _ ˆt | ¡gˆ ¢R Ž S r   )r)   r   r   r   r,   r   r   r.   ®   s    c                    s0   ˆ j d7  _ t t ˆt | ¡gˆ ¢R Ž ¡¡S r   )r)   r   Ú
atleast_2dÚasarrayr   r   r,   r   r   r.   ³   s    c                      s   ˆ ˆj ƒˆ_d S r   )r   ÚHr   ©r.   r   r   r   Úupdate_hess¸   s    z,ScalarFunction.__init__.<locals>.update_hessc                      s*   ˆ  ¡  tˆˆjfdˆjiˆ ¤Žˆ_ˆjS ©Nr%   )Ú_update_gradr   r   r#   r1   r   )r(   r"   r   r   r   r3   ¼   s
    ÿr-   c                      s*   ˆ   ¡  ˆ j ˆ jˆ j ˆ jˆ j ¡ d S r   )r5   r1   Úupdater   Úx_prevr#   Úg_prevr   ©r   r   r   r3   Ë   s    c                    sH   ˆ   ¡  ˆ jˆ _ˆ jˆ _t | ¡ t¡ˆ _dˆ _	dˆ _
dˆ _ˆ  ¡  d S ©NF)r5   r   r7   r#   r8   r   r    ÚastypeÚfloatÚ	f_updatedÚ	g_updatedÚ	H_updatedÚ_update_hessr   r9   r   r   Úupdate_xÒ   s    z)ScalarFunction.__init__.<locals>.update_xc                    s(   t  | ¡ t¡ˆ _dˆ _dˆ _dˆ _d S r:   )r   r    r;   r<   r   r=   r>   r?   r   r9   r   r   rA   Þ   s    )#ÚcallableÚ
FD_METHODSÚ
ValueErrorÚ
isinstancer   r   r    r;   r<   r   ÚsizeÚnr   r   r)   r=   r>   r?   Ú_update_fun_implr&   Ú_update_grad_implr5   r   r1   r*   Úissparser+   r   r/   r0   Ú
initializer7   r8   Ú_update_hess_implÚ_update_x_impl)r   r   Úx0r   r!   r-   Úfinite_diff_rel_stepÚfinite_diff_boundsÚepsilonr   r$   r3   rA   r   )	r   r(   r   r   r!   r"   r-   r.   r   r   Ú__init__V   s�    
ÿÿÿÿ




zScalarFunction.__init__c                 C   s   | j s|  ¡  d| _ d S ©NT©r=   rH   r9   r   r   r   r&   ç   s    zScalarFunction._update_func                 C   s   | j s|  ¡  d| _ d S rS   )r>   rI   r9   r   r   r   r5   ì   s    zScalarFunction._update_gradc                 C   s   | j s|  ¡  d| _ d S rS   ©r?   rL   r9   r   r   r   r@   ñ   s    zScalarFunction._update_hessc                 C   s&   t  || j¡s|  |¡ |  ¡  | jS r   )r   Úarray_equalr   rM   r&   r   ©r   r   r   r   r   r   ö   s    
zScalarFunction.func                 C   s&   t  || j¡s|  |¡ |  ¡  | jS r   )r   rV   r   rM   r5   r#   rW   r   r   r   r!   ü   s    
zScalarFunction.gradc                 C   s&   t  || j¡s|  |¡ |  ¡  | jS r   )r   rV   r   rM   r@   r1   rW   r   r   r   r-     s    
zScalarFunction.hessc                 C   s4   t  || j¡s|  |¡ |  ¡  |  ¡  | j| jfS r   )r   rV   r   rM   r&   r5   r   r#   rW   r   r   r   Úfun_and_grad  s
    
zScalarFunction.fun_and_grad)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__rR   r&   r5   r@   r   r!   r-   rX   r   r   r   r   r      s   K ÿ
 r   c                   @   sX   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zd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	                    sf  t ˆƒsˆtvrtd t¡ƒ‚t ˆƒsFˆtv sFtˆtƒsFtd t¡ƒ‚ˆtv r^ˆtv r^tdƒ‚t |¡ t	¡ˆ_
ˆj
jˆ_dˆ_dˆ_dˆ_dˆ_dˆ_dˆ_i ‰ ˆtv rìˆˆ d< |ˆ d< |d urÖt|ƒ}	||	fˆ d< |ˆ d	< t ˆj
¡ˆ_ˆtv �rˆˆ d< |ˆ d< d
ˆ d< t ˆj
¡ˆ_ˆtv �r8ˆtv �r8tdƒ‚‡‡fdd„‰‡‡fdd„}
|
ˆ_|
ƒ  t ˆj¡ˆ_ˆjjˆ_t ˆƒ�rFˆˆj
ƒˆ_d
ˆ_ˆ jd7  _|�sÀ|d u �rät ˆj¡�rä‡‡fdd„‰t ˆj¡ˆ_d
ˆ_nRt ˆj¡�r‡‡fdd„‰ˆj  ¡ ˆ_dˆ_n"‡‡fdd„‰t !ˆj¡ˆ_dˆ_‡‡fdd„}nÆˆtv �rt"ˆˆj
fdˆjiˆ ¤Žˆ_d
ˆ_|�s�|d u �r¶t ˆj¡�r¶‡ ‡‡fdd„}t ˆj¡ˆ_d
ˆ_nVt ˆj¡�rè‡ ‡‡fdd„}ˆj  ¡ ˆ_dˆ_n$‡ ‡‡fdd„}t !ˆj¡ˆ_dˆ_|ˆ_#t ˆƒ�r¼ˆˆj
ˆjƒˆ_$d
ˆ_ˆ jd7  _t ˆj$¡�rl‡‡fdd„‰t ˆj$¡ˆ_$n@tˆj$t%ƒ�rŠ‡‡fdd„‰n"‡‡fdd„‰t !t &ˆj$¡¡ˆ_$‡‡fdd „}ntˆtv �rð‡fd!d"„‰‡ ‡‡fd#d „}|ƒ  d
ˆ_n@tˆtƒ�r0ˆˆ_$ˆj$ 'ˆjd$¡ d
ˆ_d ˆ_(d ˆ_)‡fd%d „}|ˆ_*tˆtƒ�rP‡fd&d'„}n‡fd(d'„}|ˆ_+d S ))Nz+`jac` must be either callable or one of {}.zB`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   Fr
   r   Zsparsityr   Tr   c                    s   ˆ j d7  _ t ˆ | ƒ¡S r   )r   r   r    r   )r   r   r   r   r   S  s    z,VectorFunction.__init__.<locals>.fun_wrappedc                      s   ˆ ˆj ƒˆ_d S r   r   r   r   r   r   r   W  s    z+VectorFunction.__init__.<locals>.update_funr   c                    s   ˆ j d7  _ t ˆ | ƒ¡S r   )Únjevr*   r+   r   ©Újacr   r   r   Újac_wrappedh  s    z,VectorFunction.__init__.<locals>.jac_wrappedc                    s   ˆ j d7  _ ˆ | ƒ ¡ S r   )r^   Útoarrayr   r_   r   r   ra   o  s    c                    s   ˆ j d7  _ t ˆ | ƒ¡S r   )r^   r   r/   r   r_   r   r   ra   v  s    c                      s   ˆ ˆj ƒˆ_d S r   )r   ÚJr   )ra   r   r   r   Ú
update_jac|  s    z+VectorFunction.__init__.<locals>.update_jacr%   c                      s.   ˆ  ¡  t tˆˆjfdˆjiˆ ¤Ž¡ˆ_d S r4   )r&   r*   r+   r   r   r   rc   r   r'   r   r   rd   †  s    ÿÿc                      s,   ˆ  ¡  tˆˆjfdˆjiˆ ¤Ž ¡ ˆ_d S r4   )r&   r   r   r   rb   rc   r   r'   r   r   rd   �  s    ÿc                      s.   ˆ  ¡  t tˆˆjfdˆjiˆ ¤Ž¡ˆ_d S r4   )r&   r   r/   r   r   r   rc   r   r'   r   r   rd   —  s    ÿÿc                    s   ˆ j d7  _ t ˆ | |ƒ¡S r   )r)   r*   r+   ©r   Úv©r-   r   r   r   r.   ¨  s    z-VectorFunction.__init__.<locals>.hess_wrappedc                    s   ˆ j d7  _ ˆ | |ƒS r   )r)   re   rg   r   r   r.   ®  s    c                    s$   ˆ j d7  _ t t ˆ | |ƒ¡¡S r   )r)   r   r/   r0   re   rg   r   r   r.   ³  s    c                      s   ˆ ˆj ˆjƒˆ_d S r   )r   rf   r1   r   r2   r   r   r3   ¸  s    z,VectorFunction.__init__.<locals>.update_hessc                    s   ˆ | ƒj  |¡S r   )ÚTÚdotre   )ra   r   r   Ú	jac_dot_v»  s    z*VectorFunction.__init__.<locals>.jac_dot_vc                      s8   ˆ  ¡  tˆˆjfˆjj ˆj¡ˆjfdœˆ ¤Žˆ_d S )N)r%   r   )Ú_update_jacr   r   rc   rh   ri   rf   r1   r   )r(   rj   r   r   r   r3   ¾  s    
þýr-   c                     sZ   ˆ   ¡  ˆ jd urVˆ jd urVˆ jˆ j } ˆ jj ˆ j¡ˆ jj ˆ j¡ }ˆ j 	| |¡ d S r   )
rk   r7   ÚJ_prevr   rc   rh   ri   rf   r1   r6   )Zdelta_xZdelta_gr9   r   r   r3   Í  s
     c                    sH   ˆ   ¡  ˆ jˆ _ˆ jˆ _t | ¡ t¡ˆ _dˆ _	dˆ _
dˆ _ˆ  ¡  d S r:   )rk   r   r7   rc   rl   r   r    r;   r<   r=   Ú	J_updatedr?   r@   r   r9   r   r   rA   Ù  s    z)VectorFunction.__init__.<locals>.update_xc                    s(   t  | ¡ t¡ˆ _dˆ _dˆ _dˆ _d S r:   )r   r    r;   r<   r   r=   rm   r?   r   r9   r   r   rA   ã  s    ),rB   rC   rD   ÚformatrE   r   r   r    r;   r<   r   rF   rG   r   r^   r)   r=   rm   r?   r   r   Zx_diffrH   Z
zeros_liker   rf   Úmrc   r*   rJ   r+   Úsparse_jacobianrb   r/   r   Ú_update_jac_implr1   r   r0   rK   r7   rl   rL   rM   )r   r   rN   r`   r-   rO   Zfinite_diff_jac_sparsityrP   rp   Zsparsity_groupsr   rd   r3   rA   r   )	r(   r   r   r-   r.   r`   rj   ra   r   r   rR   !  sä    ÿÿþ
ÿ


ÿ
ÿ
ÿÿ
ÿ

	
zVectorFunction.__init__c                 C   s   t  || j¡s|| _d| _d S r:   )r   rV   rf   r?   )r   rf   r   r   r   Ú	_update_vë  s    zVectorFunction._update_vc                 C   s   t  || j¡s|  |¡ d S r   )r   rV   r   rM   rW   r   r   r   Ú	_update_xð  s    zVectorFunction._update_xc                 C   s   | j s|  ¡  d| _ d S rS   rT   r9   r   r   r   r&   ô  s    zVectorFunction._update_func                 C   s   | j s|  ¡  d| _ d S rS   )rm   rq   r9   r   r   r   rk   ù  s    zVectorFunction._update_jacc                 C   s   | j s|  ¡  d| _ d S rS   rU   r9   r   r   r   r@   þ  s    zVectorFunction._update_hessc                 C   s   |   |¡ |  ¡  | jS r   )rs   r&   r   rW   r   r   r   r     s    
zVectorFunction.func                 C   s   |   |¡ |  ¡  | jS r   )rs   rk   rc   rW   r   r   r   r`     s    
zVectorFunction.jacc                 C   s"   |   |¡ |  |¡ |  ¡  | jS r   )rr   rs   r@   r1   ©r   r   rf   r   r   r   r-     s    

zVectorFunction.hessN)rY   rZ   r[   r\   rR   rr   rs   r&   rk   r@   r   r`   r-   r   r   r   r   r]     s    Kr]   c                   @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
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                 C   sÀ   |s|d u r*t  |¡r*t  |¡| _d| _n4t  |¡rF| ¡ | _d| _nt t |¡¡| _d| _| jj	\| _
| _t |¡ t¡| _| j | j¡| _d| _tj| j
td�| _t  | j| jf¡| _d S )NTF)Zdtype)r*   rJ   r+   rc   rp   rb   r   r/   r0   Úshapero   rG   r    r;   r<   r   ri   r   r=   Úzerosrf   r1   )r   ÚArN   rp   r   r   r   rR     s    

zLinearVectorFunction.__init__c                 C   s*   t  || j¡s&t  |¡ t¡| _d| _d S r:   )r   rV   r   r    r;   r<   r=   rW   r   r   r   rs   1  s    zLinearVectorFunction._update_xc                 C   s*   |   |¡ | js$| j |¡| _d| _| jS rS   )rs   r=   rc   ri   r   rW   r   r   r   r   6  s
    
zLinearVectorFunction.func                 C   s   |   |¡ | jS r   )rs   rc   rW   r   r   r   r`   =  s    
zLinearVectorFunction.jacc                 C   s   |   |¡ || _| jS r   )rs   rf   r1   rt   r   r   r   r-   A  s    
zLinearVectorFunction.hessN)	rY   rZ   r[   r\   rR   rs   r   r`   r-   r   r   r   r   ru     s   ru   c                       s    e Zd ZdZ‡ fdd„Z‡  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                    sJ   t |ƒ}|s|d u r(tj|dd�}d}nt |¡}d}tƒ  |||¡ d S )NZcsr)rn   TF)Úlenr*   Zeyer   ÚsuperrR   )r   rN   rp   rG   rx   ©Ú	__class__r   r   rR   N  s    
zIdentityVectorFunction.__init__)rY   rZ   r[   r\   rR   Ú__classcell__r   r   r|   r   ry   G  s   ry   )Únumpyr   Zscipy.sparseÚsparser*   Z_numdiffr   r   Z_hessian_update_strategyr   Zscipy.sparse.linalgr   rC   r   r]   ru   ry   r   r   r   r   Ú<module>   s       2