
    Yhs                     f   d dl mZ 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mZ d dlmZ d d	lmZ d
Z G d d          Z G d d          Z G d d          Z G d d          Z G d d          Z G d d          Z G d d          Z G d d          Z G d de          ZdS )    )
namedtupleN   )approx_derivativegroup_columns)HessianUpdateStrategy)LinearOperator)array_namespacexp_copy)array_api_extra)_ScalarFunctionWrapper)z2-pointz3-pointcsc                   (    e Zd ZdZ	 	 	 ddZddZdS )_ScalarGradWrapperz0
    Wrapper class for gradient calculation
    Nc                 b    || _         || _        |g n|| _        || _        d| _        d| _        d S Nr   )fungradargsfinite_diff_optionsngevnfev)selfr   r   r   r   s        z/var/www/tools.fuzzalab.pt/emblema-extractor/venv/lib/python3.11/site-packages/scipy/optimize/_differentiable_functions.py__init__z_ScalarGradWrapper.__init__   s;     	,BBD	#6 				    c                 @   t          | j                  r8t          j         | j        t          j        |          g| j        R            }nA| j        t          v r3t          | j        |fd|i| j	        \  }}| xj
        |d         z  c_
        | xj        dz  c_        |S )Nf0r   r   )callabler   np
atleast_1dcopyr   
FD_METHODSr   r   r   r   r   )r   xr   kwdsgdcts         r   __call__z_ScalarGradWrapper.__call__#   s     DI 		%idi

?TY???@@AAY*$$&   *	 FAs IIV$II		Q		r   NNNN__name__
__module____qualname____doc__r   r'    r   r   r   r      sQ           $        r   r   c                   D    e Zd ZdZ	 	 	 	 d	dZd
dZd
dZd Zd Zd Z	dS )_ScalarHessWrapperzC
    Wrapper class for hess calculation via finite differences
    Nc                    || _         || _        |g n|| _        || _        d| _        d| _        d | _        d | _        t          |          r |t          j
        |          g|R  | _        | xj        dz  c_        t          j        | j                  r,| j        | _        t          j        | j                  | _        d S t          | j        t                     r| j        | _        d S | j        | _        t          j        t          j        | j                            | _        d S |t*          v r| j        | _        d S d S )Nr   r   )hessr   r   r   r   nhevH
_hess_funcr   r   r!   spsissparse_sparse_callable	csr_array
isinstancer   _linearoperator_callable_dense_callable
atleast_2dasarrayr"   _fd_hess)r   r3   x0r   r   r   s         r   r   z_ScalarHessWrapper.__init__9   s'    		,BBD	#6 		D>> 	0T"'"++----DFIINII|DF## ;"&"7tv..DFN33 ;"&"? #'"6rz$&'9'9::Z"&-  r   c                 T    |                      t          j        |          |          S )Nr   )r6   r   r!   )r   r#   r   r$   s       r   r'   z_ScalarHessWrapper.__call__[   s     rwqzzb111r   c                     t          | j        |fd|i| j        \  | _        }| xj        |d         z  c_        | j        S )Nr   r   )r   r   r   r5   r   )r   r#   r   r$   r&   s        r   r@   z_ScalarHessWrapper._fd_hess^   sS    'Iq
 

#'#;
 
 			S[ 		vr   c                     | xj         dz  c_         t          j         | j        |g| j        R            | _        | j        S Nr   )r4   r7   r:   r3   r   r5   r   r#   r$   s      r   r9   z#_ScalarHessWrapper._sparse_callablee   s?    		Q		yty7TY77788vr   c                     | xj         dz  c_         t          j        t          j         | j        |g| j        R                      | _        | j        S rF   )r4   r   r>   r?   r3   r   r5   rG   s      r   r=   z"_ScalarHessWrapper._dense_callablej   sP    		Q		Jyty/TY///00
 
 vr   c                 `    | xj         dz  c_          | j        |g| j        R  | _        | j        S rF   )r4   r3   r   r5   rG   s      r   r<   z+_ScalarHessWrapper._linearoperator_callableq   s5    		Q		1)ty)))vr   )NNNNr)   )
r+   r,   r-   r.   r   r'   r@   r9   r=   r<   r/   r   r   r1   r1   5   s           $ 0  0  0  0D2 2 2 2     
      r   r1   c                       e Zd ZdZdej         ej        fddf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.
    workers : map-like callable, optional
        A map-like callable, such as `multiprocessing.Pool.map` for evaluating
        any numerical differentiation in parallel.
        This evaluation is carried out as ``workers(fun, iterable)``, or
        ``workers(grad, iterable)``, depending on what is being numerically
        differentiated.
        Alternatively, if `workers` is an int the task is subdivided into `workers`
        sections and the function evaluated in parallel
        (uses `multiprocessing.Pool <multiprocessing>`).
        Supply -1 to use all available CPU cores.
        It is recommended that a map-like be used instead of int, as repeated
        calls to `approx_derivative` will incur large overhead from setting up
        new processes.

        .. versionadded:: 1.16.0

    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
                    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 | _        t8          j        | _        |	pt>          }	i }|t          v r||d	<   ||d
<   ||d<   ||d<   |	|d<   d|d<   |t          v r||d	<   ||d
<   ||d<   d|d<   |	|d<   d|d<   d| _         | !                                 tE          || j        ||          | _#        | $                                 t          |t                    rb|| _%        | j%        &                    | j        d           d| _        d | _'        d | _(        tS          dddg          } |dd          | _*        d S t          |          r2tW          ||||          | _*        | j*        j%        | _%        d| _        d S |t          v ratW          |||| j#        |          | _*        | $                                 | *                    | j        | j,                  | _%        d| _        d S d S )Nz)`grad` must be either callable or one of .z@`hess` must be either callable, HessianUpdateStrategy or one of zWhenever 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 floatingFmethodrel_stepabs_stepboundsworkersTfull_outputas_linear_operatorr   )r   r   r   r3   _FakeCounterr   r4   )r   r4   )rA   r   r   )rA   r   r   r   rC   )-r   r"   
ValueErrorr;   r   r	   rP   xpx
atleast_ndr?   float64isdtypedtyper   _wrapped_fun	_orig_fun
_orig_grad
_orig_hess_argsastyper#   x_dtypesizen	f_updated	g_updated	H_updated	_lowest_xr   inf	_lowest_fmap_nfev_update_funr   _wrapped_grad_update_gradr5   
initializex_prevg_prevr   _wrapped_hessr1   r%   )r   r   rA   r   r   r3   finite_diff_rel_stepfinite_diff_boundsepsilonrV   rP   _x_dtyper   rY   s                  r   r   zScalarFunction.__init__   s    ~~ 	$j"8"8IJIII    	$*"4"4d$9:: #5,(, , ,  
 :$*"4"4 8 9 9 9 'r***"^BJJrNNr:::::bh00 	XF 33==
 2v&& .S :,0).B
+.5
+,>)-4	*15.:,0).B
+.5
+8< 45-4	*15. 
 0! 3	
 
 
 	 d122 	&DFFdff---!DNDKDK%nvv6FGGL!-11!=!=!=D~~ &%7(;	& & &" +-!%##%7+(;& & &" !!###++DFtv+>>!% $#r   c                 *    | j         | j        j        z   S r)   )rp   rr   r   r   s    r   r   zScalarFunction.nfev<  s    zD.333r   c                     | j         j        S r)   )rr   r   r~   s    r   r   zScalarFunction.ngev@      !&&r   c                     | j         j        S r)   )rw   r4   r~   s    r   r4   zScalarFunction.nhevD  r   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   rN   F)r;   rc   r   rs   r#   ru   r%   rv   r[   r\   rP   r?   re   rf   ri   rj   rk   _update_hessr   r#   r{   s      r   	_update_xzScalarFunction._update_xH  s   do'<== 	#&DK&DK  2 2twGGGBW^^B55DF"DN"DN"DN  2 2twGGGBW^^B55DF"DN"DN"DNNNr   c                     | j         sX|                     | j                  }| xj        dz  c_        || j        k     r| j        | _        || _        || _        d| _         d S d S Nr   T)ri   r`   r#   rp   rn   rl   f)r   fxs     r   rq   zScalarFunction._update_fun_  si    ~ 	"""46**BJJ!OJJDN""!%!#DF!DNNN	" 	"r   c                     | j         sQ| j        t          v r|                                  |                     | j        | j                  | _        d| _         d S d S NrC   T)rj   rb   r"   rq   rr   r#   r   r%   r~   s    r   rs   zScalarFunction._update_gradj  s^    ~ 	"*,,  """''46'::DF!DNNN		" 	"r   c                    | j         s| j        t          v r;|                                  |                     | j        | j                  | _        nt          | j        t                    rJ|                                  | j        
                    | j        | j        z
  | j        | j        z
             n|                     | j                  | _        d| _         d S d S r   )rk   rc   r"   rs   rw   r#   r%   r5   r;   r   updateru   rv   r~   s    r   r   zScalarFunction._update_hessq  s    ~ 
	"*,,!!###++DFtv+>>DO-BCC 4!!###dft{2DFT[4HIIII++DF33!DNNN
	" 
	"r   c                     t          j        || j                  s|                     |           |                                  | j        S r)   )r   array_equalr#   r   rq   r   r   r#   s     r   r   zScalarFunction.fun~  sC    ~a(( 	NN1vr   c                     t          j        || j                  s|                     |           |                                  | j        S r)   )r   r   r#   r   rs   r%   r   s     r   r   zScalarFunction.grad  C    ~a(( 	NN1vr   c                     t          j        || j                  s|                     |           |                                  | j        S r)   )r   r   r#   r   r   r5   r   s     r   r3   zScalarFunction.hess  r   r   c                     t          j        || j                  s|                     |           |                                  |                                  | j        | j        fS r)   )r   r   r#   r   rq   rs   r   r%   r   s     r   fun_and_gradzScalarFunction.fun_and_grad  s\    ~a(( 	NN1vtv~r   )r+   r,   r-   r.   r   rm   r   propertyr   r   r4   r   rq   rs   r   r   r   r3   r   r/   r   r   rK   rK   w   s       X Xr HL&(fWbf$5tTi& i& i& i&V 4 4 X4 ' ' X' ' ' X'# # #.	" 	" 	"" " "" " "          r   rK   c                       e Zd Zd Zd ZdS )_VectorFunWrapperc                 "    || _         d| _        d S r   )r   r   )r   r   s     r   r   z_VectorFunWrapper.__init__  s    			r   c                 p    | xj         dz  c_         t          j        |                     |                    S rF   )r   r   r    r   r   s     r   r'   z_VectorFunWrapper.__call__  s+    		Q		}TXXa[[)))r   N)r+   r,   r-   r   r'   r/   r   r   r   r     s2          * * * * *r   r   c                   (    e Zd ZdZ	 	 	 ddZddZdS )_VectorJacWrapper0
    Wrapper class for Jacobian calculation
    Nc                 Z    || _         || _        || _        || _        d| _        d| _        d S r   )r   jacr   sparse_jacobiannjevr   )r   r   r   r   r   s        r   r   z_VectorJacWrapper.__init__  s4     #6 .				r   c                    t          | j                  r&|                     |          }| xj        dz  c_        nA| j        t          v r3t	          | j        |fd|i| j        \  }}| xj        |d         z  c_        | j        rt          j
        |          S t          j        |          r|                                S t          |t                    r|S t          j        |          S )Nr   r   r   )r   r   r   r"   r   r   r   r   r   r7   r:   r8   toarrayr;   r   r   r>   )r   r#   r   r$   Jr&   s         r   r'   z_VectorJacWrapper.__call__  s     DH 
	%AIINIIIX##&   *	 FAs IIV$II 	$=###\!__ 	$99;;>** 	$H=###r   r(   r)   r*   r/   r   r   r   r     sQ           $     $ $ $ $ $ $r   r   c                   :    e Zd ZdZ	 	 ddZd	dZd	dZd Zd ZdS )
_VectorHessWrapperr   Nc                 L    || _         || _        || _        d| _        d| _        d S r   )r   r3   r   r4   r   )r   r3   r   r   s       r   r   z_VectorHessWrapper.__init__  s,     	#6 				r   c                     t          | j                  r&| xj        dz  c_        |                     ||          S | j        t          v r|                     |||          S d S )Nr   J0)r   r3   r4   _callable_hessr"   r@   )r   r#   vr   r$   s        r   r'   z_VectorHessWrapper.__call__  sh     DI 	.IINII&&q!,,,Y*$$==A"=--- %$r   c                     |%|                      |          }| xj        dz  c_        t          | j        |f|j                            |          |fd| j        }|S )Nr   )r   r   )r   r   r   	jac_dot_vTdotr   )r   r#   r   r   r5   s        r   r@   z_VectorHessWrapper._fd_hess  sn    :!BIINII dna :!#!$%4: : !% 8: : r   c                 |    | xj         dz  c_         |                     |          j                            |          S rF   )r   r   r   r   r   r#   r   s      r   r   z_VectorHessWrapper.jac_dot_v  s1    		Q		xx{{}  ###r   c                     |                      ||          }t          j        |          rt          j        |          S t	          |t
                    r|S t          j        t          j        |                    S r)   )	r3   r7   r8   r:   r;   r   r   r>   r?   )r   r#   r   r5   s       r   r   z!_VectorHessWrapper._callable_hess  sb    IIaOO<?? 	0=###>** 	0H=A///r   )NNr)   )	r+   r,   r-   r.   r   r'   r@   r   r   r/   r   r   r   r     s           $	   . . . .
 
 
 
$ $ $0 0 0 0 0r   r   c                       e Zd ZdZddej         ej        fddf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 )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.
    Nc
                 	   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	| _        |	pt6          }	i }|t          v rO||d
<   ||d<   |t9          |          }||f|d<   ||d<   |	|d<   d|d<   t;          j        | j                  | _        |t          v r-||d
<   ||d<   d|d<   t;          j        | j                  | _        |t          v r|t          v rt          d          tA          |          | _!        | "                                 t;          j#        | j$                  | _%        | j%        j        | _&        t          |          r: |tO          | j                            | _(        d| _        | xj        dz  c_        nM|t          v rDtS          | j!        | j        fd| j$        i|\  | _(        }d| _        | xj        |d         z  c_        d	| _*        |s|?tW          j,        | j(                  r&tW          j-        | j(                  | _(        d| _*        nqtW          j,        | j(                  r| j(        .                                | _(        n9t          | j(        t^                    rnt;          j0        | j(                  | _(        tc          || j!        || j*                  | _2        tg          || j2        |          | _4        t          |          s	|t          v rc| 4                    tO          | j                  | j%        | j(                  | _5        d| _        t          |          r| xj        dz  c_        d S d S t          |t                    r>|| _5        | j5        6                    | j        d           d| _        d | _7        d | _8        d S d S )Nz(`jac` must be either callable or one of rM   z?`hess` must be either callable,HessianUpdateStrategy or one of zWhenever the Jacobian is estimated via finite-differences, we require the Hessian to be estimated using one of the quasi-Newton strategies.r   rN   rQ   r   FrR   rS   sparsityrU   rV   TrW   rX   r   r   )r   r   r   )r   r   r   r3   )9r   r"   rZ   r;   r   r	   rP   r[   r\   r?   r]   r^   r_   ra   	_orig_jacrc   re   r#   rf   rg   rh   rp   _njev_nhevri   	J_updatedrk   ro   r   r   r!   x_diffr   fun_wrappedrq   
zeros_liker   r   mr
   r   r   r   r7   r8   r:   r   r   r>   r   jac_wrappedr   hess_wrappedr5   rt   ru   J_prev)r   r   rA   r   r3   rx   finite_diff_jac_sparsityry   r   rV   rP   r{   r|   r   sparsity_groupsr&   s                   r   r   zVectorFunction.__init__  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***"^BJJrNNr:::::bh00 	XF  2v&&


 .S *,/).B
+'3"/0H"I"I3K3B3D#J/,>)-4	*15.'$&//DK:,0).B
+8< 45
 '$&//DK*!3!3 + , , ,
 -S11tv&& C== 		&S))DF!DNJJ!OJJJJ+ $& -1V7J KDFC "DNJJ#f+%JJ$ 	+'CL,@,@' ]46**DF#'D  \$&!! 	+V^^%%DFF// 	+]46**DF,  3 0	
 
 
 /d&<O
 
 

 D>> 
	TZ//&&wtv46&JJDF!DN~~  

a



   344 	DFFdff---!DNDKDKKK	 	r   c                 *    | j         | j        j        z   S r)   )rp   r   r   r~   s    r   r   zVectorFunction.nfev  s    zD,111r   c                 *    | j         | j        j        z   S r)   )r   r   r   r~   s    r   r   zVectorFunction.njev  s    zD-222r   c                     | j         S r)   )r   r~   s    r   r4   zVectorFunction.nhev  s
    zr   c                 Z    t          j        || j                  s|| _        d| _        d S d S )NF)r   r   r   rk   )r   r   s     r   	_update_vzVectorFunction._update_v  s4    ~a(( 	#DF"DNNN	# 	#r   c                    t          j        || j                  s: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 d S r   )r   r   r#   r;   rc   r   _update_jacru   r   r   r[   r\   rP   r?   re   rf   ri   r   rk   r   r   s      r   r   zVectorFunction._update_x  s   ~a(( 	'$/+@AA '  """"f"f^DGOOA$6$6Q47KKKDL99!&!&!&!!#####^DGOOA$6$6Q47KKKDL99!&!&!&!	' 	'r   c                     | j         sE|                     t          | j                            | _        | xj        dz  c_        d| _         d S d S r   )ri   r   r
   r#   r   rp   r~   s    r   rq   zVectorFunction._update_fun  sM    ~ 	"%%gdfoo66DFJJ!OJJ!DNNN	" 	"r   c                     | j         so| j        t          v r|                                  n| xj        dz  c_        |                     t          | j                  | j                  | _	        d| _         d S d S )Nr   rC   T)
r   r   r"   rq   r   r   r
   r#   r   r   r~   s    r   r   zVectorFunction._update_jac  sv    ~ 	"~++  """"

a

%%gdfoo$&%AADF!DNNN	" 	"r   c                    | j         slt          | j                  rD|                     t	          | j                  | j                  | _        | xj        dz  c_        n| j        t          v rN| 
                                 |                     t	          | j                  | j        | j                  | _        nt          | j        t                    r| 
                                 | j        z| j        s| j        | j        z
  }| j        j                            | j                  | j        j                            | j                  z
  }| j                            ||           d| _         d S d S )Nr   r   T)rk   r   rc   r   r
   r#   r   r5   r   r"   r   r   r;   r   ru   r   r   r   r   )r   delta_xdelta_gs      r   r   zVectorFunction._update_hess  s:   ~ 	"(( 4**746??DFCC

a


J..  """**746??DFtv*NNDO-BCC 4  """ ;*t{/F"ft{2G"fhll4622T[]5F5Ftv5N5NNGFMM'7333!DNNN!	" 	"r   c                 |    |                      |           |                                  t          | j                  S r)   )r   rq   r
   r   r   s     r   r   zVectorFunction.fun  s6    q tvr   c                     |                      |           |                                  t          | j        d          r$| j                            | j        j                  S | j        S Nre   )r   r   hasattrr   re   r_   r   s     r   r   zVectorFunction.jac  s[    q468$$ 	/ 6==...vr   c                     |                      |           |                     |           |                                  t          | j        d          r$| j                            | j        j                  S | j        S r   )r   r   r   r   r5   re   r_   r   s      r   r3   zVectorFunction.hess  sm    qq468$$ 	/ 6==...vr   )r+   r,   r-   r.   r   rm   r   r   r   r   r4   r   r   rq   r   r   r   r   r3   r/   r   r   r   r     s        " '+T&(fWbf$5t} } } }~ 2 2 X2 3 3 X3   X# # #
' ' '&" " "	" 	" 	"" " "&    	 	 	 	 	r   r   c                   0    e Zd ZdZd Zd Zd Zd Zd ZdS )LinearVectorFunctionzLinear 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   rN   rQ   )r_   )r7   r8   r:   r   r   r   r   r>   r?   shaper   rh   r	   rP   r[   r\   r]   r^   r_   re   r#   rf   r   r   ri   zerosfloatr   r5   )r   ArA   r   rP   r{   r|   s          r   r   zLinearVectorFunction.__init__  sb    		)o5#,q//5]1%%DF#'D  \!__ 	)YY[[DF#(D   ]2:a==11DF#(D &r***"^BJJrNNr:::::bh00 	XF 2v&&DF##$&.../00r   c                     t          j        || j                  sbt          j        | j                            |          d| j                  }| j                            || j                  | _        d| _	        d S d S r   )
r   r   r#   r[   r\   rP   r?   re   rf   ri   r   s      r   r   zLinearVectorFunction._update_x  sl    ~a(( 	# 2 2twGGGBW^^B55DF"DNNN	# 	#r   c                     |                      |           | j        s&| j                            |          | _        d| _        | j        S )NT)r   ri   r   r   r   r   s     r   r   zLinearVectorFunction.fun#  s?    q~ 	"VZZ]]DF!DN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   r5   r   s      r   r3   zLinearVectorFunction.hess.  s"    qvr   N)	r+   r,   r-   r.   r   r   r   r   r3   r/   r   r   r   r     si         1 1 1<# # #        r   r   c                   "     e Zd ZdZ fdZ xZS )IdentityVectorFunctionzIdentity 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)lenr7   	eye_arrayr   eyesuperr   )r   rA   r   rh   r   	__class__s        r   r   zIdentityVectorFunction.__init__;  sj    GG 	$o5a...A"OOq		A#OB00000r   )r+   r,   r-   r.   r   __classcell__)r   s   @r   r   r   4  sB         1 1 1 1 1 1 1 1 1r   r   ) collectionsr   numpyr   scipy.sparsesparser7   _numdiffr   r   _hessian_update_strategyr   scipy.sparse.linalgr   scipy._lib._array_apir	   r
   
scipy._libr   r[   scipy._lib._utilr   r"   r   r1   rK   r   r   r   r   r   r   r/   r   r   <module>r      s@   " " " " " "           6 6 6 6 6 6 6 6 ; ; ; ; ; ; . . . . . . : : : : : : : : - - - - - - 3 3 3 3 3 3 *
" " " " " " " "J? ? ? ? ? ? ? ?D^ ^ ^ ^ ^ ^ ^ ^B	* * * * * * * **$ *$ *$ *$ *$ *$ *$ *$Z20 20 20 20 20 20 20 20jq q q q q q q qh9 9 9 9 9 9 9 9x1 1 1 1 11 1 1 1 1 1r   