
    (phZa                         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  S SKJr  SrSS	 jrSS
 jrSS jr " S S5      r " S S5      r " S S5      r " S S\5      rg)    N   )approx_derivativegroup_columns)HessianUpdateStrategy)LinearOperator)array_namespace)array_api_extra)z2-pointz3-pointcsc                 &   ^ ^^ S/mUU U4S jnUT4$ )Nr   c                 (  > TS==   S-  ss'   T" [         R                  " U 5      /TQ76 n[         R                  " U5      (       d'   [         R                  " U5      R	                  5       nU$ U$ ! [
        [        4 a  n[        S5      UeS nAff = f)Nr   r   z@The user-provided objective function must return a scalar value.)npcopyisscalarasarrayitem	TypeError
ValueError)xfxeargsfunncallss      [/var/www/html/venv/lib/python3.13/site-packages/scipy/optimize/_differentiable_functions.pywrapped_wrapper_fun.<locals>.wrapped   s    q	Q	 #d#{{2ZZ^((* 	r	 z*  2 s   $A0 0B BB )r   r   r   r   s   `` @r   _wrapper_funr      s    SF  F?    c                 |   ^ ^^^^ S/m[        T 5      (       a  UU U4S jnUT4$ T [        ;   a  SUUU4S jjnUT4$ g )Nr   c                    > TS==   S-  ss'   [         R                  " T" [         R                  " U 5      /TQ76 5      $ Nr   r   )r   
atleast_1dr   )r   kwdsr   gradr   s     r   r   _wrapper_grad.<locals>.wrapped'   s1    1INI==bggaj!84!899r   c                 <   > TS==   S-  ss'   [        TU 4SU0TD6$ )Nr   r   f0r   )r   r(   finite_diff_optionsr   r   s     r   wrapped1_wrapper_grad.<locals>.wrapped1.   s2    1INI$Q!4 r   N)callable
FD_METHODS)r%   r   r   r*   r   r+   r   s   ````  @r   _wrapper_gradr0   #   sH    SF~~	: 			 	  
r   c                   ^ ^^^^ [        T 5      (       a  T " [        R                  " U5      /TQ76 nS/m[        R                  " U5      (       a  UU U4S jn[        R
                  " U5      nOP[        U[        5      (       a	  UU U4S jnO2UU U4S jn[        R                  " [        R                  " U5      5      nUTU4$ T [        ;   a  S/mSUU4S jjnUTS 4$ g )Nr   c                    > TS==   S-  ss'   [         R                  " T" [        R                  " U 5      /TQ76 5      $ r"   )sps
csr_matrixr   r   r   r$   r   hessr   s     r   r   _wrapper_hess.<locals>.wrapped=   s1    q	Q	~~d2771:&=&=>>r   c                 X   > TS==   S-  ss'   T" [         R                  " U 5      /TQ76 $ r"   )r   r   r5   s     r   r   r7   D   s(    q	Q	BGGAJ...r   c           	         > TS==   S-  ss'   [         R                  " [         R                  " T" [         R                  " U 5      /TQ76 5      5      $ r"   )r   
atleast_2dr   r   r5   s     r   r   r7   I   s:    q	Q	}}RZZRWWQZ0G$0G%HIIr   r   c                 "   > [        TU 4SU0TD6$ Nr(   r)   )r   r(   r*   r%   s     r   r+   _wrapper_hess.<locals>.wrapped1S   s%    $a"5 r   r-   )r.   r   r   r3   issparser4   
isinstancer   r:   r   r/   )	r6   r%   x0r   r*   Hr   r+   r   s	   `` ``   @r   _wrapper_hessrB   7   s    ~~$t$<<??? q!A>**/ /
J bjjm,A!!			 	
 %% 
r   c                       \ rS rSrSr S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)ScalarFunction[   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.

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	                 0   [        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S9u  U l        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        [:        R<                  U l        0 nU[        ;   a  XLS
'   XlS'   XS'   X|S'   U[        ;   a  X\S
'   XlS'   XS'   SUS'   U RA                  5         [C        UU R                  UUS9u  U l"        U l#        U RI                  5         [        U5      (       a%  [K        XRUS9u  U l&        U l'        U l(        SU l        g U[        ;   aj  [K        UU RD                  UUS9u  U l&        U l'        U l(        U RI                  5         U RM                  U R*                  U RR                  S9U l(        SU l        g [        U[        5      (       aJ  XPl(        U RP                  RU                  U R0                  S5        SU l        S U l+        S 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 floating)r   Fmethodrel_stepabs_stepboundsTas_linear_operator)r   r   r*   )r@   r   )r%   r@   r*   r(   r6   r   )-r.   r/   r   r?   r   r   rJ   xpx
atleast_ndr   float64isdtypedtyper   _wrapped_fun_nfev	_orig_fun
_orig_grad
_orig_hess_argsastyper   x_dtypesizen	f_updated	g_updated	H_updated	_lowest_xr   inf	_lowest_f_update_funr0   _wrapped_grad_ngev_update_gradrB   _wrapped_hess_nhevrA   g
initializex_prevg_prev)selfr   r@   r   r%   r6   finite_diff_rel_stepfinite_diff_boundsepsilonrJ   _x_dtyper*   s                r   __init__ScalarFunction.__init__   s   ~~$j"8;J<qI  $*"4d$9::(\, 
 :$*"4 8 9 9
 'r**"^^BJJrNr:::bhh00XXF )5S(D%4:
 2& :,0).B
+.5
+,>):,0).B
+.5
+8< 45 	 *7!! 3	*
&DJ 	 D>>5B$62D
DF "DNZ5B''$7	62D
DF ''466':DF!DN344FFFdfff-!DNDKDKDJ 5r   c                      U R                   S   $ Nr   )rX   rq   s    r   nfevScalarFunction.nfev      zz!}r   c                      U R                   S   $ rz   )ri   r{   s    r   ngevScalarFunction.ngev  r~   r   c                      U R                   S   $ rz   )rl   r{   s    r   nhevScalarFunction.nhev
  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   rH   F)r?   r[   r   rj   r   ro   rm   rp   rR   rS   rJ   r   r]   r^   ra   rb   rc   _update_hessrq   r   ru   s      r   	_update_xScalarFunction._update_x  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                   (       dO  U R                  U R                  5      nXR                  :  a  U R                  U l        Xl        Xl        SU l         g g NT)ra   rW   r   rf   rd   f)rq   r   s     r   rg   ScalarFunction._update_fun%  sH    ~~""466*B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 NrQ   T)rb   rZ   r/   rg   rh   r   r   rm   r{   s    r   rj   ScalarFunction._update_grad/  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   )rc   r[   r/   rj   rk   r   rm   rA   r?   r   updatero   rp   r{   s    r   r   ScalarFunction._update_hess6  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   rg   r   rq   r   s     r   r   ScalarFunction.funC  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   rj   rm   r   s     r   r%   ScalarFunction.gradI  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   rA   r   s     r   r6   ScalarFunction.hessO  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   rg   rj   r   rm   r   s     r   fun_and_gradScalarFunction.fun_and_gradU  sK    ~~a((NN1vvtvv~r   )rA   rc   r\   rf   rd   rX   ri   rl   rY   rZ   r[   rW   rh   rk   r   ra   rm   rp   rb   r`   r   r^   ro   rJ   r-   )__name__
__module____qualname____firstlineno____doc__rw   propertyr|   r   r   r   rg   rj   r   r   r%   r6   r   __static_attributes__r   r   r   rD   rD   [   sy    IV .2Zx      #."""r   rD   c                   N    \ rS rSrSrS 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.
c	                 >  ^ ^^^^^^^^ [        T5      (       d  T[        ;  a  [        S[         S35      e[        T5      (       d2  T[        ;   d(  [        T[        5      (       d  [        S[         S35      eT[        ;   a  T[        ;   a  [        S5      e[        U5      =T 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                  X5      T l        UT l        T R                  R                   T l        ST l        ST l        ST l        S	T l        S	T l        S	T l        0 mT[        ;   aI  TTS
'   UTS'   Ub  [1        U5      nUU4TS'   UTS'   [2        R4                  " T R                  5      T l        T[        ;   a4  TTS
'   UTS'   STS'   [2        R4                  " T R                  5      T l        T[        ;   a  T[        ;   a  [        S5      eUU 4S jmUU 4S jnUT l        U" 5         [2        R:                  " T R<                  5      T l        T R>                  R                   T l         [        T5      (       Ga%  T" T R                  5      T l!        ST l        T =R&                  S-  sl        U(       d(  UcY  [D        RF                  " T RB                  5      (       a4  UU 4S jm[D        RH                  " T RB                  5      T l!        ST l%        O[D        RF                  " T RB                  5      (       a.  UU 4S jmT RB                  RM                  5       T l!        S	T l%        O3UU 4S jm[2        RN                  " T RB                  5      T l!        S	T l%        UU 4S jnGO&T[        ;   Ga  [Q        TT R                  4ST R<                  0TD6T l!        ST l        U(       d(  UcZ  [D        RF                  " T RB                  5      (       a5  UUU 4S jn[D        RH                  " T RB                  5      T l!        ST l%        O[D        RF                  " T RB                  5      (       a/  UUU 4S jnT RB                  RM                  5       T l!        S	T l%        O4UUU 4S jn[2        RN                  " T RB                  5      T l!        S	T l%        WT l)        [        T5      (       a  T" T R                  T R>                  5      T l*        ST l        T =R(                  S-  sl        [D        RF                  " T RT                  5      (       a-  UU 4S jm[D        RH                  " T RT                  5      T l*        Og[        T RT                  [V        5      (       a  UU 4S jmO@UU 4S jm[2        RN                  " [2        R                  " T RT                  5      5      T l*        UU 4S jnOT[        ;   a  U4S jmUUU 4S jnU" 5         ST l        O][        T[        5      (       aH  TT l*        T RT                  RY                  T R"                  S 5        ST l        S T l-        S T l.        U 4S! jnWT l/        [        T[        5      (       a  U 4S" jnOU 4S# jnUT l0        g )$Nz(`jac` must be either callable or one of rG   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   rH   rK   r   FrL   rM   sparsityrO   TrP   c                 f   > T=R                   S-  sl         [        R                  " T" U 5      5      $ Nr   )r|   r   r#   )r   r   rq   s    r   fun_wrapped,VectorFunction.__init__.<locals>.fun_wrapped  s#    IINI==Q((r   c                  4   > T " TR                   5      Tl        g r-   )r   r   )r   rq   s   r   
update_fun+VectorFunction.__init__.<locals>.update_fun  s     (DFr   c                 f   > T=R                   S-  sl         [        R                  " T" U 5      5      $ r   )njevr3   r4   r   jacrq   s    r   jac_wrapped,VectorFunction.__init__.<locals>.jac_wrapped  s#    IINI>>#a&11r   c                 Z   > T=R                   S-  sl         T" U 5      R                  5       $ r   )r   toarrayr   s    r   r   r     s!    IINIq6>>++r   c                 f   > T=R                   S-  sl         [        R                  " T" U 5      5      $ r   )r   r   r:   r   s    r   r   r     s#    IINI==Q00r   c                  4   > T " TR                   5      Tl        g r-   )r   J)r   rq   s   r   
update_jac+VectorFunction.__init__.<locals>.update_jac  s    $TVV,r   r(   c                     > TR                  5         [        R                  " [        TTR                  4STR
                  0T D65      Tl        g r<   )rg   r3   r4   r   r   r   r   r*   r   rq   s   r   r   r     sF    $$& ^^)+tvv A$&& A,?ABDFr   c                     > TR                  5         [        TTR                  4STR                  0T D6R	                  5       Tl        g r<   )rg   r   r   r   r   r   r   s   r   r   r     sC    $$&.{DFF Ftvv F1DFFMgi Fr   c                     > TR                  5         [        R                  " [        TTR                  4STR
                  0T D65      Tl        g r<   )rg   r   r:   r   r   r   r   r   s   r   r   r     sF    $$&]])+tvv A$&& A,?ABDFr   c                 f   > T=R                   S-  sl         [        R                  " T" X5      5      $ r   )r   r3   r4   r   vr6   rq   s     r   hess_wrapped-VectorFunction.__init__.<locals>.hess_wrapped  s#    IINI>>$q*55r   c                 >   > T=R                   S-  sl         T" X5      $ r   )r   r   s     r   r   r     s    IINI:%r   c                    > T=R                   S-  sl         [        R                  " [        R                  " T" X5      5      5      $ r   )r   r   r:   r   r   s     r   r   r     s,    IINI==DJ)?@@r   c                  J   > T " TR                   TR                  5      Tl        g r-   )r   r   rA   )r   rq   s   r   update_hess,VectorFunction.__init__.<locals>.update_hess  s    %dffdff5r   c                 F   > T" U 5      R                   R                  U5      $ r-   )Tdot)r   r   r   s     r   	jac_dot_v*VectorFunction.__init__.<locals>.jac_dot_v  s    "1~''++A..r   c                     > TR                  5         [        TTR                  4TR                  R                  R                  TR                  5      TR                  4S.T D6Tl        g )N)r(   r   )_update_jacr   r   r   r   r   r   rA   )r*   r   rq   s   r   r   r     sW      "*9dff B.2ffhhll466.B15	B .ABr   r6   c                    > TR                  5         TR                  b  TR                  b  TR                  TR                  -
  n TR                  R
                  R                  TR                  5      TR                  R
                  R                  TR                  5      -
  nTR                  R                  X5        g g g r-   )
r   ro   J_prevr   r   r   r   r   rA   r   )delta_xdelta_grq   s     r   r   r   !  s      " ;;*t{{/F"fft{{2G"ffhhll4662T[[]]5F5Ftvv5NNGFFMM'3 0G*r   c                 |  > TR                  5         TR                  Tl        TR                  Tl        [
        R                  " TR                  R                  U 5      STR                  S9nTR                  R                  UTR                  5      Tl        STl        STl        STl        TR                  5         g r   )r   r   ro   r   r   rR   rS   rJ   r   r]   r^   ra   	J_updatedrc   r   r   ru   rq   s     r   update_x)VectorFunction.__init__.<locals>.update_x-  s      ""ff"ff^^DGGOOA$6Q477KDLL9!&!&!&!!#r   c                    > [         R                  " TR                  R                  U 5      STR                  S9nTR                  R	                  UTR
                  5      Tl        STl        STl        STl	        g r   )
rR   rS   rJ   r   r]   r^   r   ra   r   rc   r   s     r   r   r   8  sU    ^^DGGOOA$6Q477KDLL9!&!&!&r   )1r.   r/   r   r?   r   r   rJ   rR   rS   r   rT   rU   rV   r]   r   r^   r_   r`   r|   r   r   ra   r   rc   r   r   r   x_diff_update_fun_impl
zeros_liker   r   mr   r3   r>   r4   sparse_jacobianr   r:   r   _update_jac_implrA   r   rn   ro   r   _update_hess_impl_update_x_impl)rq   r   r@   r   r6   rr   finite_diff_jac_sparsityrs   r   rJ   ru   rv   sparsity_groupsr   r   r   r   r*   r   r   r   r   s   `` ``            @@@@@r   rw   VectorFunction.__init__n  s    }}J!6G
|STUVV$*"4d$9:: @@J|1N O O *!3 + , ,
 'r**"^^BJJrNr:::bhh00XXF 2&			 *,/).B
+'3"/0H"I3K3B3D#J/,>)''$&&/DK:,0).B
+8< 45''$&&/DK*!3 + , ,	)	) !+tvv& C==[DF!DNIINI#+TVV0D0D2 /'+$dff%%, )',$1 tvv.',$- - J&{DFF >tvv >)<>DF!DN#+TVV0D0DB
 /'+$dff%%P )',$B
 tvv.',$ * D>>$&&$&&)DF!DNIINI||DFF##6 /DFFN33&
A rzz$&&'9:6Z/B M!DN344DFFFdfff-!DNDKDK4 "-d122	$' 'r   c                 j    [         R                  " XR                  5      (       d  Xl        SU l        g g )NF)r   r   r   rc   )rq   r   s     r   	_update_vVectorFunction._update_vA  s&    ~~a((F"DN )r   c                 r    [         R                  " XR                  5      (       d  U R                  U5        g g r-   )r   r   r   r   r   s     r   r   VectorFunction._update_xF  s(    ~~a((" )r   c                 V    U R                   (       d  U R                  5         SU l         g g r   )ra   r   r{   s    r   rg   VectorFunction._update_funJ  !    ~~!!#!DN r   c                 V    U R                   (       d  U R                  5         SU l         g g r   )r   r   r{   s    r   r   VectorFunction._update_jacO  r   r   c                 V    U R                   (       d  U R                  5         SU l         g g r   )rc   r   r{   s    r   r   VectorFunction._update_hessT  s!    ~~""$!DN r   c                 \    U R                  U5        U R                  5         U R                  $ r-   )r   rg   r   r   s     r   r   VectorFunction.funY  #    qvvr   c                 \    U R                  U5        U R                  5         U R                  $ r-   )r   r   r   r   s     r   r   VectorFunction.jac^  r   r   c                 ~    U R                  U5        U R                  U5        U R                  5         U R                  $ r-   )r   r   r   rA   rq   r   r   s      r   r6   VectorFunction.hessc  s/    qqvvr   )rA   rc   r   r   r   r   r   r   r   r   ra   r   r`   r|   r   r   r   r   r   r   r^   ro   rJ   N)r   r   r   r   r   rw   r   r   rg   r   r   r   r   r6   r   r   r   r   r   r   ]  s6     Q'f#
#"
"
"


r   r   c                   6    \ rS rSrSrS rS rS rS rS r	Sr
g	)
LinearVectorFunctionik  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   rH   rK   )rV   )r3   r>   r4   r   r   r   r   r:   r   shaper   r`   r   rJ   rR   rS   rT   rU   rV   r]   r   r^   r   r   ra   zerosfloatr   rA   )rq   Ar@   r   rJ   ru   rv   s          r   rw   LinearVectorFunction.__init__r  sB   o5#,,q//^^A&DF#'D \\!__YY[DF#(D  ]]2::a=1DF#(D &r**"^^BJJrNr:::bhh00XXF 2&DFF#$&&. 01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   rR   rS   rJ   r   r]   r^   ra   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                  $ r   )r   ra   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   rA   r   s      r   r6   LinearVectorFunction.hess  s    qvvr   )rA   r   r   ra   r   r`   r   r   r   r^   rJ   N)r   r   r   r   r   rw   r   r   r   r6   r   r   r   r   r   r   k  s     2<#r   r   c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )IdentityVectorFunctioni  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)lenr3   eyer   superrw   )rq   r@   r   r`   r  	__class__s        r   rw   IdentityVectorFunction.__init__  sJ    Go5%(A"Oq	A#O0r   r   )r   r   r   r   r   rw   r   __classcell__)r  s   @r   r  r    s    1 1r   r  )r   )Nr   N)NNr   N)numpyr   scipy.sparsesparser3   _numdiffr   r   _hessian_update_strategyr   scipy.sparse.linalgr   scipy._lib._array_apir   
scipy._libr	   rR   r/   r   r0   rB   rD   r   r   r  r   r   r   <module>r!     sc      6 ; . 1 - *
, (!&H DK K\9 9x11 1r   