
    biu                     .   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y)    )
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y)_ScalarGradWrapperz0
    Wrapper class for gradient calculation
    Nc                 `    || _         || _        |g n|| _        || _        d| _        d| _        y Nr   )fungradargsfinite_diff_optionsngevnfev)selfr   r   r   r   s        c/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/scipy/optimize/_differentiable_functions.py__init__z_ScalarGradWrapper.__init__   s5     	,BD	#6 		    c                    t        | j                        rDt        j                   | j                  t        j                  |      g| j
                         }nP| j                  t        v r>t        | 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     DIIidii
?TYY?@AYY*$&  **	FAs IIV$I		Q	r   NNNN__name__
__module____qualname____doc__r   r'    r   r   r   r      s      $r   r   c                   B    e Zd ZdZ	 	 	 	 d	dZd
dZd
dZd Zd Zd Z	y)_ScalarHessWrapperzC
    Wrapper class for hess calculation via finite differences
    Nc                 v   || _         || _        |g n|| _        || _        d| _        d| _        d | _        d | _        t        |      r |t        j                  |      g| | _        | xj
                  dz  c_        t        j                  | j                        r,d| _        t        j                  | j                        | _        y t        | j                  t              rd| _        y d| _        t        j                   t        j"                  | j                              | _        y |t$        v rd| _        y y )Nr   r   sparse_callablelinearoperator_callabledense_callablefd_hess)hessr   r   r   r   nhevH
_hess_funcr   r   r!   spsissparse	csr_array
isinstancer   
atleast_2dasarrayr"   )r   r7   x0r   r   r   s         r   r   z_ScalarHessWrapper.__init__9   s     		,BD	#6 		D>"''"+--DFIINI||DFF#"3tvv.DFFN3"; #3rzz$&&'9:Z"+  r   c                     | j                   xdk(  r | j                  }n6xdk(  r | j                  }n#xdk(  r | j                  }ndk(  r| j                  } t        j                  |      |      S )Nr3   r4   r5   r6   r   )r:   _sparse_callable_linearoperator_callable_dense_callable_fd_hessr   r!   )r   r#   r   r$   _hs        r   r'   z_ScalarHessWrapper.__call__[   sT    oo"***22!))]]"''!*$$r   c                     t        | j                  |fd|i| j                  \  | _        }| xj                  |d   z  c_        | j                  S )Nr   r   )r   r   r   r9   r   )r   r#   r   r$   r&   s        r   rG   z_ScalarHessWrapper._fd_hessh   sN    'IIq

#'#;#;
 			S[ 	vvr   c                     | xj                   dz  c_         t        j                   | j                  |g| j                         | _        | j
                  S Nr   )r8   r;   r=   r7   r   r9   r   r#   r$   s      r   rD   z#_ScalarHessWrapper._sparse_callableo   s<    		Q	ytyy7TYY78vvr   c                     | xj                   dz  c_         t        j                  t        j                   | j                  |g| j
                               | _        | j                  S rK   )r8   r   r?   r@   r7   r   r9   rL   s      r   rF   z"_ScalarHessWrapper._dense_callablet   sJ    		Q	JJytyy/TYY/0
 vvr   c                     | xj                   dz  c_          | j                  |g| j                   | _        | j                  S rK   )r8   r7   r   r9   rL   s      r   rE   z+_ScalarHessWrapper._linearoperator_callable{   s3    		Q	1)tyy)vvr   )NNNNr)   )
r+   r,   r-   r.   r   r'   rG   rD   rF   rE   r/   r   r   r1   r1   5   s4      $ ,D%
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y)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
                 n   t        |      s|t        vrt        dt         d      t        |      s+|t        v s#t        |t              st        dt         d      |t        v r|t        v rt        d      t        |      x| _        }
t        j                  |
j                  |      d|
      }|
j                  }|
j                  |j                  d      r|j                  }t        ||      | _        || _        || _        || _        || _        |
j'                  ||      | _        || _        | j(                  j,                  | _        d| _        d| _        d| _        d | _        t8        j:                  | _        |	xs t>        }	i }|t        v r||d	<   ||d
<   ||d<   ||d<   |	|d<   d|d<   |t        v r||d	<   ||d
<   ||d<   d|d<   |	|d<   d|d<   d| _         | jC                          tE        || j                  ||      | _#        | jI                          t        |t              r`|| _%        | jJ                  jM                  | j.                  d       d| _        d | _'        d | _(        tS        dddg      } |dd      | _*        y t        |      r7tW        ||||      | _*        | jT                  jJ                  | _%        d| _        y |t        v rctW        |||| jF                  |      | _*        | jI                          | jU                  | j(                  | jX                        | _%        d| _        y y )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   r7   _FakeCounterr   r8   )r   r8   )rA   r   r   )rA   r   r   r   rC   )-r   r"   
ValueErrorr>   r   r	   rU   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_gradr9   
initializex_prevg_prevr   _wrapped_hessr1   r%   )r   r   rA   r   r   r7   finite_diff_rel_stepfinite_diff_boundsepsilonr[   rU   _x_dtyper   r^   s                  r   r   zScalarFunction.__init__   s@    ~$j"8;J<qI  $*"4d$9:(\, 
 :$*"4 8 9 9 'r**"^^BJJrNr:::bhh0XXF 33=
 2v& .S :,0).B
+.5
+,>)-4	*15.:,0).B
+.5
+8< 45-4	*15. 
 0!! 3	
 	 d12DFFFdfff-!DNDKDK%nvv6FGL!-11!=D~%7(;	&" ++--!%#%7++(;&" !!#++DFFtvv+>!% $r   c                 H    | j                   | j                  j                  z   S r)   )ru   rw   r   r   s    r   r   zScalarFunction.nfevE  s    zzD..3333r   c                 .    | j                   j                  S r)   )rw   r   r   s    r   r   zScalarFunction.ngevI      !!&&&r   c                 .    | j                   j                  S r)   )r|   r8   r   s    r   r8   zScalarFunction.nhevM  r   r   c                    t        | j                  t              r| j                          | j                  | _        | j                  | _        t        j                  | j                  j                  |      d| j                        }| j                  j                  || j                        | _        d| _        d| _        d| _        | j#                          y t        j                  | j                  j                  |      d| j                        }| j                  j                  || j                        | _        d| _        d| _        d| _        y Nr   rS   F)r>   rh   r   rx   r#   rz   r%   r{   r`   ra   rU   r@   rj   rk   rn   ro   rp   _update_hessr   r#   r   s      r   	_update_xzScalarFunction._update_xQ  s    doo'<=&&DK&&DK  2twwGBWW^^B5DF"DN"DN"DN  2twwGBWW^^B5DF"DN"DN"DNr   c                     | j                   sf| j                  | j                        }| xj                  dz  c_        || j                  k  r| j                  | _        || _        || _        d| _         y y Nr   T)rn   re   r#   ru   rs   rq   f)r   fxs     r   rv   zScalarFunction._update_funh  s[    ~~""466*BJJ!OJDNN"!%!#DF!DN r   c                     | j                   sV| j                  t        v r| j                          | j	                  | j
                  | j                        | _        d| _         y y NrC   T)ro   rg   r"   rv   rw   r#   r   r%   r   s    r   rx   zScalarFunction._update_grads  sL    ~~*,  "''466':DF!DN	 r   c                    | j                   s| j                  t        v r=| j                          | j	                  | j
                  | j                        | _        nt        | j                  t              r[| j                          | j                  j                  | j
                  | j                  z
  | j                  | j                  z
         n | j	                  | j
                        | _        d| _         y y r   )rp   rh   r"   rx   r|   r#   r%   r9   r>   r   updaterz   r{   r   s    r   r   zScalarFunction._update_hessz  s    ~~*,!!#++DFFtvv+>DOO-BC!!#dfft{{2DFFT[[4HI++DFF3!DN r   c                     t        j                  || j                        s| j                  |       | j	                          | j
                  S r)   )r   array_equalr#   r   rv   r   r   r#   s     r   r   zScalarFunction.fun  s5    ~~a(NN1vvr   c                     t        j                  || j                        s| j                  |       | j	                          | j
                  S r)   )r   r   r#   r   rx   r%   r   s     r   r   zScalarFunction.grad  5    ~~a(NN1vvr   c                     t        j                  || j                        s| j                  |       | j	                          | j
                  S r)   )r   r   r#   r   r   r9   r   s     r   r7   zScalarFunction.hess  r   r   c                     t        j                  || j                        s| j                  |       | j	                          | j                          | j                  | j                  fS r)   )r   r   r#   r   rv   rx   r   r%   r   s     r   fun_and_gradzScalarFunction.fun_and_grad  sJ    ~~a(NN1vvtvv~r   )r+   r,   r-   r.   r   rr   r   propertyr   r   r8   r   rv   rx   r   r   r   r7   r   r/   r   r   rP   rP      s    Xr HL&(ffWbff$5tTi&V 4 4 ' ' ' '#.	"""r   rP   c                       e Zd Zd Zd Zy)_VectorFunWrapperc                      || _         d| _        y r   )r   r   )r   r   s     r   r   z_VectorFunWrapper.__init__  s    	r   c                 t    | xj                   dz  c_         t        j                  | j                  |            S rK   )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     s    *r   r   c                   &    e Zd ZdZ	 	 	 ddZddZy)_VectorJacWrapper0
    Wrapper class for Jacobian calculation
    Nc                 X    || _         || _        || _        || _        d| _        d| _        y r   )r   jacr   sparse_jacobiannjevr   )r   r   r   r   r   s        r   r   z_VectorJacWrapper.__init__  s0     #6 .		r   c                    t        | j                        r'| j                  |      }| xj                  dz  c_        nP| j                  t        v r>t	        | j
                  |fd|i| j                  \  }}| xj                  |d   z  c_        | j                  rt        j                        S t        j                        r|j                         S t        |t              r|S t        j                   |      S )Nr   r   r   )r   r   r   r"   r   r   r   r   r   r;   r=   r<   toarrayr>   r   r   r?   )r   r#   r   r$   Jr&   s         r   r'   z_VectorJacWrapper.__call__  s     DHHAIINIXX#&  **	FAs IIV$I==##\\!_99;>*H==##r   r(   r)   r*   r/   r   r   r   r     s      $  $r   r   c                   8    e Zd ZdZ	 	 ddZd	dZd	dZd Zd Zy)
_VectorHessWrapperr   Nc                 J    || _         || _        || _        d| _        d| _        y r   )r   r7   r   r8   r   )r   r7   r   r   s       r   r   z_VectorHessWrapper.__init__  s(     	#6 		r   c                     t        | j                        r'| xj                  dz  c_        | j                  ||      S | j                  t        v r| j                  |||      S y )Nr   J0)r   r7   r8   _callable_hessr"   rG   )r   r#   vr   r$   s        r   r'   z_VectorHessWrapper.__call__  sV     DIIIINI&&q!,,YY*$==A"=-- %r   c                     |&| j                  |      }| xj                  dz  c_        t        | j                  |f|j                  j                  |      |fd| j                  }|S )Nr   )r   r   )r   r   r   	jac_dot_vTdotr   )r   r#   r   r   r9   s        r   rG   z_VectorHessWrapper._fd_hess  se    :!BIINI dnna :!#!$%4: !% 8 8: r   c                     | xj                   dz  c_         | j                  |      j                  j                  |      S rK   )r   r   r   r   r   r#   r   s      r   r   z_VectorHessWrapper.jac_dot_v  s,    		Q	xx{}}  ##r   c                     | j                  ||      }t        j                  |      rt        j                  |      S t	        |t
              r|S t        j                  t        j                  |            S r)   )	r7   r;   r<   r=   r>   r   r   r?   r@   )r   r#   r   r9   s       r   r   z!_VectorHessWrapper._callable_hess  sQ    IIaO<<?==##>*H==A//r   )NNr)   )	r+   r,   r-   r.   r   r'   rG   r   r   r/   r   r   r   r     s(      $	.
$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y)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        |      s+|t        v s#t        |t              st        dt         d      |t        v r|t        v rt        d      t        |      x| _        }
t        j                  |
j                  |      d|
      }|
j                  }|
j                  |j                  d      r|j                  }|| _        || _        || _        |
j!                  ||      | _        || _        | j"                  j&                  | _        d| _        d| _        d| _        d	| _        d	| _        d	| _        |	xs t6        }	i }|t        v rQ||d
<   ||d<   |t9        |      }||f|d<   ||d<   |	|d<   d|d<   t;        j<                  | j"                        | _        |t        v r3||d
<   ||d<   d|d<   t;        j<                  | j"                        | _        |t        v r|t        v rt        d      tA        |      | _!        | jE                          t;        jF                  | jH                        | _%        | jJ                  j&                  | _&        t        |      r= |tO        | j"                              | _(        d| _        | xj,                  dz  c_        n\|t        v rTtS        | jB                  | j"                  fd| jH                  i|\  | _(        }d| _        | xj*                  |d   z  c_        d	| _*        |s!|KtW        jX                  | jP                        r,tW        jZ                  | jP                        | _(        d| _*        n~tW        jX                  | jP                        r | jP                  j]                         | _(        n?t        | jP                  t^              rn$t;        j`                  | jP                        | _(        tc        || jB                  || jT                        | _2        tg        || jd                  |      | _4        t        |      s|t        v ri| ji                  tO        | j"                        | jJ                  | jP                        | _5        d| _        t        |      r| xj.                  dz  c_        y y t        |t              rC|| _5        | jj                  jm                  | j(                  d       d| _        d | _7        d | _8        y y )Nz(`jac` must be either callable or one of rR   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   rS   rV   r   FrW   rX   sparsityrZ   r[   Tr\   r]   r   r   )r   r   r   )r   r   r   r7   )9r   r"   r_   r>   r   r	   rU   r`   ra   r@   rb   rc   rd   rf   	_orig_jacrh   rj   r#   rk   rl   rm   ru   _njev_nhevrn   	J_updatedrp   rt   r   r   r!   x_diffr   fun_wrappedrv   
zeros_liker   r   mr
   r   r   r   r;   r<   r=   r   r   r?   r   jac_wrappedr   hess_wrappedr9   ry   rz   J_prev)r   r   rA   r   r7   r}   finite_diff_jac_sparsityr~   r   r[   rU   r   r   r   sparsity_groupsr&   s                   r   r   zVectorFunction.__init__  sS    }J!6G
|STUVV$*"4d$9: @@J|1N O O *!3 + , ,
 'r**"^^BJJrNr:::bhh0XXF  2v&


 .S *,/).B
+'3"/0H"I3K3B3D#J/,>)-4	*15.''$&&/DK:,0).B
+8< 45
 ''$&&/DK*!3 + , ,
 -S1tvv& C=)DF!DNJJ!OJJ+  $&&-1VV7JKDFC "DNJJ#f+%J$'CLL,@ ]]466*DF#'D \\$&&!VV^^%DF/]]466*DF,   3 00	
 /d&&<O

 D>TZ/&&wtvv466&JDF!DN~

a
 34DFFFdfff-!DNDKDK 5r   c                 H    | j                   | j                  j                  z   S r)   )ru   r   r   r   s    r   r   zVectorFunction.nfev  s    zzD,,1111r   c                 H    | j                   | j                  j                  z   S r)   )r   r   r   r   s    r   r   zVectorFunction.njev  s    zzD--2222r   c                     | j                   S r)   )r   r   s    r   r8   zVectorFunction.nhev  s    zzr   c                 b    t        j                  || j                        s|| _        d| _        y y )NF)r   r   r   rp   )r   r   s     r   	_update_vzVectorFunction._update_v  s'    ~~a(DF"DN )r   c                    t        j                  || j                        sTt        | j                  t
              r| j                          | j                  | _        | j                  | _	        t        j                  | j                  j                  |      d| j                        }| j                  j                  || j                        | _        d| _        d| _        d| _        | j'                          y t        j                  | j                  j                  |      d| j                        }| j                  j                  || j                        | _        d| _        d| _        d| _        y y r   )r   r   r#   r>   rh   r   _update_jacrz   r   r   r`   ra   rU   r@   rj   rk   rn   r   rp   r   r   s      r   r   zVectorFunction._update_x  s    ~~a($//+@A  ""ff"ff^^DGGOOA$6Q477KDLL9!&!&!&!!#^^DGGOOA$6Q477KDLL9!&!&!&! )r   c                     | j                   sF| j                  t        | j                              | _        | xj
                  dz  c_        d| _         y y r   )rn   r   r
   r#   r   ru   r   s    r   rv   zVectorFunction._update_fun  s<    ~~%%gdffo6DFJJ!OJ!DN r   c                    | j                   su| j                  t        v r| j                          n| xj                  dz  c_        | j                  t        | j                        | j                        | _	        d| _         y y )Nr   rC   T)
r   r   r"   rv   r   r   r
   r#   r   r   r   s    r   r   zVectorFunction._update_jac  s]    ~~~~+  "

a
%%gdffo$&&%ADF!DN r   c                 `   | j                   st        | j                        rK| j                  t	        | j
                        | j                        | _        | xj                  dz  c_        n9| j                  t        v rQ| j                          | j                  t	        | j
                        | j                  | j                        | _        nt        | j                  t              r| j                          | j                  | j                  | j
                  | j                  z
  }| j                  j                   j#                  | j                        | j                  j                   j#                  | j                        z
  }| j                  j%                  ||       d| _         y y )Nr   r   T)rp   r   rh   r   r
   r#   r   r9   r   r"   r   r   r>   r   rz   r   r   r   r   )r   delta_xdelta_gs      r   r   zVectorFunction._update_hess  s   ~~(**7466?DFFC

a
J.  "**7466?DFFtvv*NDOO-BC  " ;;*t{{/F"fft{{2G"ffhhll4662T[[]]5F5Ftvv5NNGFFMM'73!DN! r   c                 n    | j                  |       | j                          t        | j                        S r)   )r   rv   r
   r   r   s     r   r   zVectorFunction.fun  s*    q tvvr   c                     | j                  |       | j                          t        | j                  d      r/| j                  j	                  | j                  j
                        S | j                  S Nrj   )r   r   hasattrr   rj   rd   r   s     r   r   zVectorFunction.jac  sN    q4668$ 66==..vvr   c                    | j                  |       | j                  |       | j                          t        | j                  d      r/| j                  j                  | j                  j                        S | j                  S r   )r   r   r   r   r9   rj   rd   r   s      r   r7   zVectorFunction.hess  sZ    qq4668$ 66==..vvr   )r+   r,   r-   r.   r   rr   r   r   r   r   r8   r   r   rv   r   r   r   r   r7   r/   r   r   r   r     s    " '+T&(ffWbff$5t}~ 2 2 3 3  #
'&"	""&	r   r   c                   .    e Zd ZdZd Zd Zd Zd Zd Zy)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                    |s|7t        j                  |      r"t        j                  |      | _        d| _        nft        j                  |      r|j                         | _        d| _        n4t        j                  t        j                  |            | _        d| _        | j                  j                  \  | _
        | _        t        |      x| _        }t        j                  |j                  |      d|      }|j                   }|j#                  |j$                  d      r|j$                  }|j'                  ||      | _        || _        | j                  j-                  | j(                        | _        d| _        t        j2                  | j                  t4              | _        t        j                  | j                  | j                  f      | _        y )NTFr   rS   rV   )rd   )r;   r<   r=   r   r   r   r   r?   r@   shaper   rm   r	   rU   r`   ra   rb   rc   rd   rj   r#   rk   r   r   rn   zerosfloatr   r9   )r   ArA   r   rU   r   r   s          r   r   zLinearVectorFunction.__init__  s?   o5#,,q/]]1%DF#'D \\!_YY[DF#(D  ]]2::a=1DF#(D &r**"^^BJJrNr:::bhh0XXF 2v&DFF#$&&./0r   c                     t        j                  || j                        snt        j                  | j
                  j                  |      d| j
                        }| j
                  j                  || j                        | _        d| _	        y y r   )
r   r   r#   r`   ra   rU   r@   rj   rk   rn   r   s      r   r   zLinearVectorFunction._update_x&  s]    ~~a( 2twwGBWW^^B5DF"DN )r   c                     | j                  |       | j                  s'| j                  j                  |      | _        d| _        | j                  S )NT)r   rn   r   r   r   r   s     r   r   zLinearVectorFunction.fun,  s8    q~~VVZZ]DF!DNvvr   c                 <    | j                  |       | j                  S r)   )r   r   r   s     r   r   zLinearVectorFunction.jac3  s    qvvr   c                 J    | j                  |       || _        | j                  S r)   )r   r   r9   r   s      r   r7   zLinearVectorFunction.hess7  s    qvvr   N)	r+   r,   r-   r.   r   r   r   r   r7   r/   r   r   r   r     s     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
        |   |||       y )Ncsr)formatTF)lenr;   	eye_arrayr   eyesuperr   )r   rA   r   rm   r   	__class__s        r   r   zIdentityVectorFunction.__init__D  sL    Go5a.A"Oq	A#OB0r   )r+   r,   r-   r.   r   __classcell__)r   s   @r   r   r   =  s    1 1r   r   ) collectionsr   numpyr   scipy.sparsesparser;   _numdiffr   r   _hessian_update_strategyr   scipy.sparse.linalgr   scipy._lib._array_apir	   r
   
scipy._libr   r`   scipy._lib._utilr   r"   r   r1   rP   r   r   r   r   r   r   r/   r   r   <module>r      s    "   6 ; . : - 3 *
" "JI IV^ ^B	* **$ *$Z20 20jq qh9 9x11 1r   