
    Џkhs                     .   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        d/var/www/teggl/fontify/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                    || _         || _        |g n|| _        || _        d| _        d| _        d | _        d | _        t        |      r  |t        j                  |      g| | _        | xj
                  dz  c_        t        j                  | j                        r6| j                  | _        t        j                  | j                        | _        y t        | j                  t               r| j"                  | _        y | j$                  | _        t        j&                  t        j(                  | j                              | _        y |t*        v r| j,                  | _        y y )Nr   r   )hessr   r   r   r   nhevH
_hess_funcr   r   r!   spsissparse_sparse_callable	csr_array
isinstancer   _linearoperator_callable_dense_callable
atleast_2dasarrayr"   _fd_hess)r   r3   x0r   r   r   s         r   r   z_ScalarHessWrapper.__init__9   s     		,BD	#6 		D>"''"+--DFIINI||DFF#"&"7"7tvv.DFFN3"&"?"? #'"6"6rzz$&&'9:Z"&--  r   c                 N    | j                  t        j                  |      |      S )Nr   )r6   r   r!   )r   r#   r   r$   s       r   r'   z_ScalarHessWrapper.__call__[   s    rwwqzb11r   c                     t        | j                  |fd|i| j                  \  | _        }| xj                  |d   z  c_        | j                  S )Nr   r   )r   r   r   r5   r   )r   r#   r   r$   r&   s        r   r@   z_ScalarHessWrapper._fd_hess^   sN    'IIq

#'#;#;
 			S[ 	vvr   c                     | xj                   dz  c_         t        j                   | j                  |g| j                         | _        | j
                  S Nr   )r4   r7   r:   r3   r   r5   r   r#   r$   s      r   r9   z#_ScalarHessWrapper._sparse_callablee   s<    		Q	ytyy7TYY78vvr   c                     | xj                   dz  c_         t        j                  t        j                   | j                  |g| j
                               | _        | j                  S rF   )r4   r   r>   r?   r3   r   r5   rG   s      r   r=   z"_ScalarHessWrapper._dense_callablej   sJ    		Q	JJytyy/TYY/0
 vvr   c                     | xj                   dz  c_          | j                  |g| j                   | _        | j                  S rF   )r4   r3   r   r5   rG   s      r   r<   z+_ScalarHessWrapper._linearoperator_callableq   s3    		Q	1)tyy)vvr   )NNNNr)   )
r+   r,   r-   r.   r   r'   r@   r9   r=   r<   r/   r   r   r1   r1   5   s4      $ 0D2
r   r1   c                       e Zd ZdZdej
                   ej
                  fddfdZed        Zed        Z	ed        Z
d Zd Zd	 Zd
 Zd Zd Zd Zd Z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   r3   _FakeCounterr   r4   )r   r4   )rA   r   r   )rA   r   r   r   rC   )-r   r"   
ValueErrorr;   r   r	   rP   xpx
atleast_ndr?   float64isdtypedtyper   _wrapped_fun	_orig_fun
_orig_grad
_orig_hess_argsastyper#   x_dtypesizen	f_updated	g_updated	H_updated	_lowest_xr   inf	_lowest_fmap_nfev_update_funr   _wrapped_grad_update_gradr5   
initializex_prevg_prevr   _wrapped_hessr1   r%   )r   r   rA   r   r   r3   finite_diff_rel_stepfinite_diff_boundsepsilonrV   rP   _x_dtyper   rY   s                  r   r   zScalarFunction.__init__   s@    ~$j"8;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)   )rp   rr   r   r   s    r   r   zScalarFunction.nfev<  s    zzD..3333r   c                 .    | j                   j                  S r)   )rr   r   r~   s    r   r   zScalarFunction.ngev@      !!&&&r   c                 .    | j                   j                  S r)   )rw   r4   r~   s    r   r4   zScalarFunction.nhevD  r   r   c                    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   rN   F)r;   rc   r   rs   r#   ru   r%   rv   r[   r\   rP   r?   re   rf   ri   rj   rk   _update_hessr   r#   r{   s      r   	_update_xzScalarFunction._update_xH  s    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)ri   r`   r#   rp   rn   rl   f)r   fxs     r   rq   zScalarFunction._update_fun_  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)rj   rb   r"   rq   rr   r#   r   r%   r~   s    r   rs   zScalarFunction._update_gradj  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   )rk   rc   r"   rs   rw   r#   r%   r5   r;   r   updateru   rv   r~   s    r   r   zScalarFunction._update_hessq  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   rq   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   rs   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   r5   r   s     r   r3   zScalarFunction.hess  r   r   c                     t        j                  || j                        s| j                  |       | j	                          | j                          | j                  | j                  fS r)   )r   r   r#   r   rq   rs   r   r%   r   s     r   fun_and_gradzScalarFunction.fun_and_grad  sJ    ~~a(NN1vvtvv~r   )r+   r,   r-   r.   r   rm   r   propertyr   r   r4   r   rq   rs   r   r   r   r3   r   r/   r   r   rK   rK   w   s    Xr HL&(ffWbff$5tTi&V 4 4 ' ' ' '#.	"""r   rK   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 rF   )r   r   r    r   r   s     r   r'   z_VectorFunWrapper.__call__  s&    		Q	}}TXXa[))r   N)r+   r,   r-   r   r'   r/   r   r   r   r     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   r7   r:   r8   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   r3   r   r4   r   )r   r3   r   r   s       r   r   z_VectorHessWrapper.__init__  s(     	#6 		r   c                     t        | j                        r'| xj                  dz  c_        | j                  ||      S | j                  t        v r| j                  |||      S y )Nr   J0)r   r3   r4   _callable_hessr"   r@   )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   r5   s        r   r@   z_VectorHessWrapper._fd_hess  se    :!BIINI dnna :!#!$%4: !% 8 8: r   c                     | xj                   dz  c_         | j                  |      j                  j                  |      S rF   )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)   )	r3   r7   r8   r:   r;   r   r   r>   r?   )r   r#   r   r5   s       r   r   z!_VectorHessWrapper._callable_hess  sQ    IIaO<<?==##>*H==A//r   )NNr)   )	r+   r,   r-   r.   r   r'   r@   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 rM   z?`hess` must be either callable,HessianUpdateStrategy or one of zWhenever the Jacobian is estimated via finite-differences, we require the Hessian to be estimated using one of the quasi-Newton strategies.r   rN   rQ   r   FrR   rS   sparsityrU   rV   TrW   rX   r   r   )r   r   r   )r   r   r   r3   )9r   r"   rZ   r;   r   r	   rP   r[   r\   r?   r]   r^   r_   ra   	_orig_jacrc   re   r#   rf   rg   rh   rp   _njev_nhevri   	J_updatedrk   ro   r   r   r!   x_diffr   fun_wrappedrq   
zeros_liker   r   mr
   r   r   r   r7   r8   r:   r   r   r>   r   jac_wrappedr   hess_wrappedr5   rt   ru   J_prev)r   r   rA   r   r3   rx   finite_diff_jac_sparsityry   r   rV   rP   r{   r|   r   sparsity_groupsr&   s                   r   r   zVectorFunction.__init__  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)   )rp   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   r4   zVectorFunction.nhev  s    zzr   c                 b    t        j                  || j                        s|| _        d| _        y y )NF)r   r   r   rk   )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;   rc   r   _update_jacru   r   r   r[   r\   rP   r?   re   rf   ri   r   rk   r   r   s      r   r   zVectorFunction._update_x  s    ~~a($//+@A  ""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   )ri   r   r
   r#   r   rp   r~   s    r   rq   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"   rq   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)rk   r   rc   r   r
   r#   r   r5   r   r"   r   r   r;   r   ru   r   r   r   r   )r   delta_xdelta_gs      r   r   zVectorFunction._update_hess  s   ~~(**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   rq   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 Nre   )r   r   hasattrr   re   r_   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   r5   re   r_   r   s      r   r3   zVectorFunction.hess  sZ    qq4668$ 66==..vvr   )r+   r,   r-   r.   r   rm   r   r   r   r   r4   r   r   rq   r   r   r   r   r3   r/   r   r   r   r     s    " '+T&(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   rN   rQ   )r_   )r7   r8   r:   r   r   r   r   r>   r?   shaper   rh   r	   rP   r[   r\   r]   r^   r_   re   r#   rf   r   r   ri   zerosfloatr   r5   )r   ArA   r   rP   r{   r|   s          r   r   zLinearVectorFunction.__init__  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[   r\   rP   r?   re   rf   ri   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   ri   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.jac*  s    qvvr   c                 J    | j                  |       || _        | j                  S r)   )r   r   r5   r   s      r   r3   zLinearVectorFunction.hess.  s    qvvr   N)	r+   r,   r-   r.   r   r   r   r   r3   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)lenr7   	eye_arrayr   eyesuperr   )r   rA   r   rh   r   	__class__s        r   r   zIdentityVectorFunction.__init__;  sL    Go5a.A"Oq	A#OB0r   )r+   r,   r-   r.   r   __classcell__)r   s   @r   r   r   4  s    1 1r   r   ) collectionsr   numpyr   scipy.sparsesparser7   _numdiffr   r   _hessian_update_strategyr   scipy.sparse.linalgr   scipy._lib._array_apir	   r
   
scipy._libr   r[   scipy._lib._utilr   r"   r   r1   rK   r   r   r   r   r   r   r/   r   r   <module>r      s    "   6 ; . : - 3 *
" "J? ?D^ ^B	* **$ *$Z20 20jq qh9 9x11 1r   