
    -is                     .   S SK Jr  S SKrS SKJr  SSKJrJ	r	  SSK
Jr  S SKJr  S SKJrJr  S SKJr  S S	KJr  S
r " S S5      r " S S5      r " S S5      r " S S5      r " S S5      r " S S5      r " S S5      r " S S5      r " S S\5      rg)    )
namedtupleN   )approx_derivativegroup_columns)HessianUpdateStrategy)LinearOperator)array_namespacexp_copy)array_api_extra)_ScalarFunctionWrapper)z2-pointz3-pointcsc                   2    \ rS rSrSr   SS jrSS jrSrg)	_ScalarGradWrapper   z(
Wrapper class for gradient calculation
Nc                 \    X l         Xl        Uc  / OUU l        X@l        SU l        SU l        g Nr   )fungradargsfinite_diff_optionsngevnfev)selfr   r   r   r   s        [/var/www/html/venv/lib/python3.13/site-packages/scipy/optimize/_differentiable_functions.py__init___ScalarGradWrapper.__init__   s/     	,BD	#6 		    c                    [        U R                  5      (       aF  [        R                  " U R                  " [        R                  " U5      /U R
                  Q76 5      nOQU R                  [        ;   a=  [        U R                  U4SU0U R                  D6u  pEU =R                  US   -  sl
        U =R                  S-  sl        W$ )Nf0r   r   )callabler   np
atleast_1dcopyr   
FD_METHODSr   r   r   r   r   )r   xr   kwdsgdcts         r   __call___ScalarGradWrapper.__call__#   s     DIIdii
?TYY?@AYY*$&  **	FA IIV$I		Q	r   )r   r   r   r   r   r   NNNN__name__
__module____qualname____firstlineno____doc__r   r)   __static_attributes__ r   r   r   r      s      $r   r   c                   P    \ rS rSrSr    SS jrSS jrSS jrS rS r	S	 r
S
rg)_ScalarHessWrapper5   z;
Wrapper class for hess calculation via finite differences
Nc                    Xl         X0l        Uc  / OUU l        XPl        SU l        SU l        S U l        S U l        [        U5      (       Ga  U" [        R                  " U5      /UQ76 U l        U =R
                  S-  sl        [        R                  " U R                  5      (       a7  U R                  U l        [        R                  " U R                  5      U l        g [        U R                  [         5      (       a  U R"                  U l        g U R$                  U l        [        R&                  " [        R(                  " U R                  5      5      U l        g U[*        ;   a  U R,                  U l        g g )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   r9   x0r   r   r   s         r   r   _ScalarHessWrapper.__init__9   s    		,BD	#6 		D>>"''"+--DFIINI||DFF##"&"7"7tvv.DFFN33"&"?"? #'"6"6rzz$&&'9:Z"&--  r   c                 J    U R                  [        R                  " U5      US9$ )Nr   )r<   r!   r#   )r   r%   r   r&   s       r   r)   _ScalarHessWrapper.__call__[   s    rwwqzb11r   c                     [        U R                  U4SU0U R                  D6u  U l        nU =R                  US   -  sl        U R                  $ )Nr   r   )r   r   r   r;   r   )r   r%   r   r&   r(   s        r   rF   _ScalarHessWrapper._fd_hess^   sN    'IIq

#'#;#;
 			S[ 	vvr   c                     U =R                   S-  sl         [        R                  " U R                  " U/U R                  Q76 5      U l        U R
                  $ Nr   )r:   r=   r@   r9   r   r;   r   r%   r&   s      r   r?   #_ScalarHessWrapper._sparse_callablee   s:    		Q	tyy7TYY78vvr   c                     U =R                   S-  sl         [        R                  " [        R                  " U R                  " U/U R
                  Q76 5      5      U l        U R                  $ rO   )r:   r!   rD   rE   r9   r   r;   rP   s      r   rC   "_ScalarHessWrapper._dense_callablej   sH    		Q	JJtyy/TYY/0
 vvr   c                     U =R                   S-  sl         U R                  " U/U R                  Q76 U l        U R                  $ rO   )r:   r9   r   r;   rP   s      r   rB   +_ScalarHessWrapper._linearoperator_callableq   s1    		Q	1)tyy)vvr   )r;   r<   r   r   r   r9   r   r:   )NNNNr,   )r.   r/   r0   r1   r2   r   r)   rF   r?   rC   rB   r3   r4   r   r   r6   r6   5   s4      $ 0D2
r   r6   c                       \ rS rSrSrS\R                  * \R                  4SS4S jr\S 5       r	\S 5       r
\S 5       rS rS	 rS
 rS rS rS rS rS rSrg)ScalarFunctionw   a  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
                    [        U5      (       d  U[        ;  a  [        S[         S35      e[        U5      (       d2  U[        ;   d(  [        U[        5      (       d  [        S[         S35      eU[        ;   a  U[        ;   a  [        S5      e[        U5      =U l        n
[        R                  " U
R                  U5      SU
S9nU
R                  nU
R                  UR                  S5      (       a  UR                  n[        X5      U l        Xl        X@l        XPl        X0l        U
R'                  X5      U l        Xl        U R(                  R,                  U l        SU l        SU l        SU l        S U l        [8        R:                  U l        U	=(       d    [>        n	0 nU[        ;   a  XMS	'   XmS
'   XS'   X}S'   XS'   SUS'   U[        ;   a  X]S	'   XmS
'   XS'   SUS'   XS'   SUS'   SU l         U RC                  5         [E        UU R                  UUS9U l#        U RI                  5         [        U[        5      (       a\  XPl%        U RJ                  RM                  U R.                  S5        SU l        S U l'        S U l(        [S        SSS/5      nU" SSS9U l*        g [        U5      (       a4  [W        UUUUS9U l*        U RT                  RJ                  U l%        SU l        g U[        ;   a]  [W        UUUU RF                  US9U l*        U RI                  5         U RU                  U R(                  U RX                  S9U l%        SU l        g g )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   r9   _FakeCounterr   r:   )r   r:   )rG   r   r   )rG   r   r   r   rJ   )-r    r$   
ValueErrorrA   r   r	   r]   xpx
atleast_ndrE   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_gradr;   
initializex_prevg_prevr   _wrapped_hessr6   r'   )r   r   rG   r   r   r9   finite_diff_rel_stepfinite_diff_boundsepsilonrc   r]   _x_dtyper   rf   s                  r   r   ScalarFunction.__init__   s$    ~~$j"8;J<qI  $*"4d$9::(\, 
 :$*"4 8 9 9 'r**"^^BJJrNr:::bhh00XXF 33=
 2& .S :,0).B
+.5
+,>)-4	*15.:,0).B
+.5
+8< 45-4	*15. 
 0!! 3	
 	 d122FFFdfff-!DNDKDK%nvv6FGL!-11!=D~~%7(;	&" ++--!%#%7++(;&" !!#++DFFtvv+>!% $r   c                 H    U R                   U R                  R                  -   $ r,   )r}   r   r   r   s    r   r   ScalarFunction.nfev<  s    zzD..3333r   c                 .    U R                   R                  $ r,   )r   r   r   s    r   r   ScalarFunction.ngev@      !!&&&r   c                 .    U R                   R                  $ r,   )r   r:   r   s    r   r:   ScalarFunction.nhevD  r   r   c                    [        U R                  [        5      (       a  U R                  5         U R                  U l        U R                  U l        [        R                  " U R                  R                  U5      SU R                  S9nU R                  R                  X R                  5      U l        SU l        SU l        SU l        U R#                  5         g [        R                  " U R                  R                  U5      SU R                  S9nU R                  R                  X R                  5      U l        SU l        SU l        SU l        g Nr   r[   F)rA   rp   r   r   r%   r   r'   r   rh   ri   r]   rE   rr   rs   rv   rw   rx   _update_hessr   r%   r   s      r   	_update_xScalarFunction._update_xH  s    doo'<==&&DK&&DK  2twwGBWW^^B5DF"DN"DN"DN  2twwGBWW^^B5DF"DN"DN"DNr   c                     U R                   (       dd  U R                  U R                  5      nU =R                  S-  sl        XR                  :  a  U R                  U l        Xl        Xl        SU l         g g Nr   T)rv   rm   r%   r}   r{   ry   f)r   fxs     r   r~   ScalarFunction._update_fun_  sU    ~~""466*BJJ!OJNN"!%!#F!DN r   c                     U R                   (       dU  U R                  [        ;   a  U R                  5         U R	                  U R
                  U R                  S9U l        SU l         g g NrJ   T)rw   ro   r$   r~   r   r%   r   r'   r   s    r   r   ScalarFunction._update_gradj  sL    ~~*,  "''466':DF!DN	 r   c                    U R                   (       d  U R                  [        ;   a:  U R                  5         U R	                  U R
                  U R                  S9U l        O[        U R                  [        5      (       a[  U R                  5         U R                  R                  U R
                  U R                  -
  U R                  U R                  -
  5        O U R	                  U R
                  5      U l        SU l         g g r   )rx   rp   r$   r   r   r%   r'   r;   rA   r   updater   r   r   s    r   r   ScalarFunction._update_hessq  s    ~~*,!!#++DFFtvv+>DOO-BCC!!#dfft{{2DFFT[[4HI++DFF3!DN r   c                     [         R                  " XR                  5      (       d  U R                  U5        U R	                  5         U R
                  $ r,   )r!   array_equalr%   r   r~   r   r   r%   s     r   r   ScalarFunction.fun~  s6    ~~a((NN1vvr   c                     [         R                  " XR                  5      (       d  U R                  U5        U R	                  5         U R
                  $ r,   )r!   r   r%   r   r   r'   r   s     r   r   ScalarFunction.grad  6    ~~a((NN1vvr   c                     [         R                  " XR                  5      (       d  U R                  U5        U R	                  5         U R
                  $ r,   )r!   r   r%   r   r   r;   r   s     r   r9   ScalarFunction.hess  r   r   c                     [         R                  " XR                  5      (       d  U R                  U5        U R	                  5         U R                  5         U R                  U R                  4$ r,   )r!   r   r%   r   r~   r   r   r'   r   s     r   fun_and_gradScalarFunction.fun_and_grad  sK    ~~a((NN1vvtvv~r   )r;   rx   rq   r{   ry   r}   rn   ro   rp   rm   r   r   r   rv   r'   r   rw   ru   r%   rs   r   r]   )r.   r/   r0   r1   r2   r!   rz   r   propertyr   r   r:   r   r~   r   r   r   r   r9   r   r3   r4   r   r   rW   rW   w   s    Xr HL&(ffWbff$5tTi&V 4 4 ' ' ' '#.	"""r   rW   c                        \ rS rSrS rS rSrg)_VectorFunWrapperi  c                     Xl         SU l        g r   r   r   )r   r   s     r   r   _VectorFunWrapper.__init__  s    	r   c                 v    U =R                   S-  sl         [        R                  " U R                  U5      5      $ rO   )r   r!   r"   r   r   s     r   r)   _VectorFunWrapper.__call__  s&    		Q	}}TXXa[))r   r   N)r.   r/   r0   r1   r   r)   r3   r4   r   r   r   r     s    *r   r   c                   2    \ rS rSrSr   SS jrSS jrSrg)	_VectorJacWrapperi  (
Wrapper class for Jacobian calculation
Nc                 P    X l         Xl        X0l        X@l        SU l        SU l        g r   )r   jacr   sparse_jacobiannjevr   )r   r   r   r   r   s        r   r   _VectorJacWrapper.__init__  s(     #6 .		r   c                 $   [        U R                  5      (       a'  U R                  U5      nU =R                  S-  sl        OQU R                  [        ;   a=  [	        U R
                  U4SU0U R                  D6u  pEU =R                  US   -  sl        U R                  (       a  [        R                  " W5      $ [        R                  " W5      (       a  UR                  5       $ [        U[        5      (       a  U$ [        R                   " U5      $ )Nr   r   r   )r    r   r   r$   r   r   r   r   r   r=   r@   r>   toarrayrA   r   r!   rD   )r   r%   r   r&   Jr(   s         r   r)   _VectorJacWrapper.__call__  s     DHHAIINIXX#&  **	FA IIV$I==##\\!__99;>**H==##r   )r   r   r   r   r   r   r+   r,   r-   r4   r   r   r   r     s      $  $r   r   c                   F    \ rS rSrSr  S
S jrSS jrSS jrS rS r	S	r
g)_VectorHessWrapperi  r   Nc                 D    X l         Xl        X0l        SU l        SU l        g r   )r   r9   r   r:   r   )r   r9   r   r   s       r   r   _VectorHessWrapper.__init__  s"     	#6 		r   c                     [        U R                  5      (       a&  U =R                  S-  sl        U R                  X5      $ U R                  [        ;   a  U R                  XUS9$ g )Nr   J0)r    r9   r:   _callable_hessr$   rF   )r   r%   vr   r&   s        r   r)   _VectorHessWrapper.__call__  sU     DIIIINI&&q,,YY*$=="=-- %r   c                     Uc&  U R                  U5      nU =R                  S-  sl        [        U R                  U4UR                  R                  U5      U4S.U R                  D6nU$ )Nr   )r   r   )r   r   r   	jac_dot_vTdotr   )r   r%   r   r   r;   s        r   rF   _VectorHessWrapper._fd_hess  se    :!BIINI dnna :!#!$%4: !% 8 8: r   c                     U =R                   S-  sl         U R                  U5      R                  R                  U5      $ rO   )r   r   r   r   r   r%   r   s      r   r   _VectorHessWrapper.jac_dot_v  s,    		Q	xx{}}  ##r   c                    U R                  X5      n[        R                  " U5      (       a  [        R                  " U5      $ [	        U[
        5      (       a  U$ [        R                  " [        R                  " U5      5      $ r,   )	r9   r=   r>   r@   rA   r   r!   rD   rE   )r   r%   r   r;   s       r   r   !_VectorHessWrapper._callable_hess  sT    IIaO<<??==##>**H==A//r   )r   r9   r   r:   r   )NNr,   )r.   r/   r0   r1   r2   r   r)   rF   r   r   r3   r4   r   r   r   r     s(      $	.
$0r   r   c                       \ rS rSrSrSS\R                  * \R                  4SS4S jr\S 5       r	\S 5       r
\S 5       rS rS	 rS
 rS rS rS rS rS rSrg)VectorFunctioni  aa  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
                 
   [        U5      (       d  U[        ;  a  [        S[         S35      e[        U5      (       d2  U[        ;   d(  [        U[        5      (       d  [        S[         S35      eU[        ;   a  U[        ;   a  [        S5      e[        U5      =U l        n
[        R                  " U
R                  U5      SU
S9nU
R                  nU
R                  UR                  S5      (       a  UR                  nXl        X0l        X@l        U
R!                  X5      U l        Xl        U R"                  R&                  U l        SU l        SU l        SU l        S	U l        S	U l        S	U l        U	=(       d    [6        n	0 nU[        ;   aO  X=S
'   X]S'   Ub  [9        U5      nUU4US'   X}S'   XS'   SUS'   [:        R<                  " U R"                  5      U l        U[        ;   a2  XMS
'   X]S'   SUS'   [:        R<                  " U R"                  5      U l        U[        ;   a  U[        ;   a  [        S5      e[A        U5      U l!        U RE                  5         [:        RF                  " U RH                  5      U l%        U RJ                  R&                  U l&        [        U5      (       a=  U" [O        U R"                  5      5      U l(        SU l        U =R,                  S-  sl        O^U[        ;   aT  [S        U RB                  U R"                  4SU RH                  0UD6u  U l(        nSU l        U =R*                  US   -  sl        S	U l*        U(       d(  UcR  [V        RX                  " U RP                  5      (       a-  [V        RZ                  " U RP                  5      U l(        SU l*        O[V        RX                  " U RP                  5      (       a   U RP                  R]                  5       U l(        OE[        U RP                  [^        5      (       a  O%[:        R`                  " U RP                  5      U l(        [c        UU RB                  UU RT                  S9U l2        [g        X@Rd                  US9U l4        [        U5      (       d
  U[        ;   ak  U Ri                  [O        U R"                  5      U RJ                  U RP                  S9U l5        SU l        [        U5      (       a  U =R.                  S-  sl        g g [        U[        5      (       aB  X@l5        U Rj                  Rm                  U R(                  S5        SU l        S U l7        S U l8        g g )Nz(`jac` must be either callable or one of rZ   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   r[   r^   r   Fr_   r`   sparsityrb   rc   Trd   re   r   r   )r   r   r   )r   r   r   r9   )9r    r$   rg   rA   r   r	   r]   rh   ri   rE   rj   rk   rl   rn   	_orig_jacrp   rr   r%   rs   rt   ru   r}   _njev_nhevrv   	J_updatedrx   r|   r   r!   r#   x_diffr   fun_wrappedr~   
zeros_liker   r   mr
   r   r   r   r=   r>   r@   r   r   rD   r   jac_wrappedr   hess_wrappedr;   r   r   J_prev)r   r   rG   r   r9   r   finite_diff_jac_sparsityr   r   rc   r]   r   r   r   sparsity_groupsr(   s                   r   r   VectorFunction.__init__  sO    }}J!6G
|STUVV$*"4d$9:: @@J|1N O O *!3 + , ,
 'r**"^^BJJrNr:::bhh00XXF  2&


 .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	
 /&&<O

 D>>TZ/&&wtvv466&JDF!DN~~

a
 344FFFdfff-!DNDKDK 5r   c                 H    U R                   U R                  R                  -   $ r,   )r}   r   r   r   s    r   r   VectorFunction.nfev  s    zzD,,1111r   c                 H    U R                   U R                  R                  -   $ r,   )r   r   r   r   s    r   r   VectorFunction.njev  s    zzD--2222r   c                     U R                   $ r,   )r   r   s    r   r:   VectorFunction.nhev  s    zzr   c                 j    [         R                  " XR                  5      (       d  Xl        SU l        g g )NF)r!   r   r   rx   )r   r   s     r   	_update_vVectorFunction._update_v  s&    ~~a((F"DN )r   c                    [         R                  " XR                  5      (       GdS  [        U R                  [
        5      (       a  U R                  5         U R                  U l        U R                  U l	        [        R                  " U R                  R                  U5      SU R                  S9nU R                  R                  X R                  5      U l        SU l        SU l        SU l        U R'                  5         g [        R                  " U R                  R                  U5      SU R                  S9nU R                  R                  X R                  5      U l        SU l        SU l        SU l        g g r   )r!   r   r%   rA   rp   r   _update_jacr   r   r   rh   ri   r]   rE   rr   rs   rv   r   rx   r   r   s      r   r   VectorFunction._update_x  s    ~~a(($//+@AA  ""ff"ff^^DGGOOA$6Q477KLL9!&!&!&!!#^^DGGOOA$6Q477KLL9!&!&!&! )r   c                     U R                   (       dF  U R                  [        U R                  5      5      U l        U =R
                  S-  sl        SU l         g g r   )rv   r   r
   r%   r   r}   r   s    r   r~   VectorFunction._update_fun  s<    ~~%%gdffo6DFJJ!OJ!DN r   c                    U R                   (       dt  U R                  [        ;   a  U R                  5         OU =R                  S-  sl        U R                  [        U R                  5      U R                  S9U l	        SU l         g g )Nr   rJ   T)
r   r   r$   r~   r   r   r
   r%   r   r   r   s    r   r   VectorFunction._update_jac  s]    ~~~~+  "

a
%%gdffo$&&%ADF!DN r   c                 ~   U R                   (       Gd  [        U R                  5      (       aK  U R                  [	        U R
                  5      U R                  5      U l        U =R                  S-  sl        GO>U R                  [        ;   aN  U R                  5         U R                  [	        U R
                  5      U R                  U R                  S9U l        O[        U R                  [        5      (       a  U R                  5         U R                  b  U R                  b  U R
                  U R                  -
  nU R                  R                   R#                  U R                  5      U R                  R                   R#                  U R                  5      -
  nU R                  R%                  X5        SU l         g g )Nr   r   T)rx   r    rp   r   r
   r%   r   r;   r   r$   r   r   rA   r   r   r   r   r   r   )r   delta_xdelta_gs      r   r   VectorFunction._update_hess  s   ~~~((**7466?DFFC

a
J.  "**7466?DFFtvv*NDOO-BCC  " ;;*t{{/F"fft{{2G"ffhhll4662T[[]]5F5Ftvv5NNGFFMM'3!DN! r   c                 n    U R                  U5        U R                  5         [        U R                  5      $ r,   )r   r~   r
   r   r   s     r   r   VectorFunction.fun  s*    q tvvr   c                     U R                  U5        U R                  5         [        U R                  S5      (       a/  U R                  R	                  U R                  R
                  5      $ U R                  $ Nrr   )r   r   hasattrr   rr   rl   r   s     r   r   VectorFunction.jac  sQ    q4668$$ 66==..vvr   c                    U R                  U5        U R                  U5        U R                  5         [        U R                  S5      (       a/  U R                  R                  U R                  R                  5      $ U R                  $ r  )r   r   r   r  r;   rr   rl   r   s      r   r9   VectorFunction.hess  s]    qq4668$$ 66==..vvr   )r;   rx   r   r   r   r}   r   r   rn   rp   r   r   rv   r   r   r   r   ru   r   r   r%   r   rs   r   r]   )r.   r/   r0   r1   r2   r!   rz   r   r   r   r   r:   r   r   r~   r   r   r   r   r9   r3   r4   r   r   r   r     s    " '+T&(ffWbff$5t}~ 2 2 3 3  #
'&"	""&	r   r   c                   6    \ rS rSrSrS rS rS rS rS r	Sr
g	)
LinearVectorFunctioni  zLinear 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                    U(       d  Uc>  [         R                  " U5      (       a#  [         R                  " U5      U l        SU l        On[         R                  " U5      (       a  UR                  5       U l        SU l        O6[        R                  " [        R                  " U5      5      U l        SU l        U R                  R                  u  U l
        U l        [        U5      =U l        n[        R                  " UR                  U5      SUS9nUR                   nUR#                  UR$                  S5      (       a  UR$                  nUR'                  XV5      U l        X`l        U R                  R-                  U R(                  5      U l        SU l        [        R2                  " U R                  [4        S9U l        [         R                  " U R                  U R                  45      U l        g )NTFr   r[   r^   )rl   )r=   r>   r@   r   r   r   r!   rD   rE   shaper   ru   r	   r]   rh   ri   rj   rk   rl   rr   r%   rs   r   r   rv   zerosfloatr   r;   )r   ArG   r   r]   r   r   s          r   r   LinearVectorFunction.__init__  sB   o5#,,q//]]1%DF#'D \\!__YY[DF#(D  ]]2::a=1DF#(D &r**"^^BJJrNr:::bhh00XXF 2&DFF#$&&./0r   c                 $   [         R                  " XR                  5      (       dk  [        R                  " U R
                  R                  U5      SU R
                  S9nU R
                  R                  X R                  5      U l        SU l	        g g r   )
r!   r   r%   rh   ri   r]   rE   rr   rs   rv   r   s      r   r   LinearVectorFunction._update_x  s\    ~~a(( 2twwGBWW^^B5DF"DN )r   c                     U R                  U5        U R                  (       d'  U R                  R                  U5      U l        SU l        U R                  $ )NT)r   rv   r   r   r   r   s     r   r   LinearVectorFunction.fun#  s8    q~~VVZZ]DF!DNvvr   c                 <    U R                  U5        U R                  $ r,   )r   r   r   s     r   r   LinearVectorFunction.jac*  s    qvvr   c                 H    U R                  U5        X l        U R                  $ r,   )r   r   r;   r   s      r   r9   LinearVectorFunction.hess.  s    qvvr   )r;   r   r   rv   r   ru   r   r   r%   rs   r]   N)r.   r/   r0   r1   r2   r   r   r   r   r9   r3   r4   r   r   r  r    s     1<#r   r  c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )IdentityVectorFunctioni4  zIdentity 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                    > [        U5      nU(       d  Uc  [        R                  " USS9nSnO[        R                  " U5      nSn[
        TU ]  XAU5        g )Ncsr)formatTF)lenr=   	eye_arrayr!   eyesuperr   )r   rG   r   ru   r  	__class__s        r   r   IdentityVectorFunction.__init__;  sJ    Go5a.A"Oq	A#O0r   r4   )r.   r/   r0   r1   r2   r   r3   __classcell__)r  s   @r   r  r  4  s    1 1r   r  ) collectionsr   numpyr!   scipy.sparsesparser=   _numdiffr   r   _hessian_update_strategyr   scipy.sparse.linalgr   scipy._lib._array_apir	   r
   
scipy._libr   rh   scipy._lib._utilr   r$   r   r6   rW   r   r   r   r   r  r  r4   r   r   <module>r,     s    "   6 ; . : - 3 *
" "J? ?D^ ^B	* **$ *$Z20 20jq qh9 9x11 1r   