Ë
    çÿæiu  ã                   ó.  — d dl mZ d dlZd dlmZ ddlmZm	Z	 ddl
mZ d dlmZ d dlmZmZ d dlmZ d d	lmZ d
Z G d„ d«      Z G d„ d«      Z G d„ d«      Z G d„ d«      Z G d„ d«      Z G d„ d«      Z G d„ d«      Z G d„ d«      Z G d„ de«      Zy)é    )Ú
namedtupleNé   )Úapprox_derivativeÚgroup_columns)ÚHessianUpdateStrategy)ÚLinearOperator)Úarray_namespaceÚxp_copy)Úarray_api_extra)Ú_ScalarFunctionWrapper)z2-pointz3-pointÚcsc                   ó&   — e Zd ZdZ	 	 	 dd„Zdd„Zy)Ú_ScalarGradWrapperz0
    Wrapper class for gradient calculation
    Nc                 ó`   — || _         || _        |€g n|| _        || _        d| _        d| _        y ©Nr   )ÚfunÚgradÚargsÚfinite_diff_optionsÚngevÚnfev)Úselfr   r   r   r   s        ú}/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/scipy/optimize/_differentiable_functions.pyÚ__init__z_ScalarGradWrapper.__init__   s5   € ð ˆŒØˆŒ	Ø˜,‘B¨DˆŒ	Ø#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ž   € ô �D—I‘IÔÜ—‘˜i˜dŸi™i¬¯©°«
Ð?°T·Y±YÒ?Ó@‰AØ�Y‰Yœ*Ñ$Ü&Ø—‘Øñð ðð ×*Ñ*ñ	‰FˆAˆsð �IŠI˜˜V™Ñ$�Ià�	Š	�Q‰�	Øˆr   ©NNN©N©Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r'   © r   r   r   r      s   „ ñð ØØ $óôr   r   c                   óB   — e Zd ZdZ	 	 	 	 d	d„Zd
d„Zd
d„Zd„ Zd„ Zd„ Z	y)Ú_ScalarHessWrapperzC
    Wrapper class for hess calculation via finite differences
    Nc                 óv  — || _         || _        |€g n|| _        || _        d| _        d| _        d | _        d | _        t        |«      râ |t        j                  |«      g|¢­Ž | _        | xj
                  dz  c_        t        j                  | j                  «      r,d| _        t        j                  | j                  «      | _        y t        | j                  t        «      rd| _        y d| _        t        j                   t        j"                  | j                  «      «      | _        y |t$        v rd| _        y y )Nr   r   Úsparse_callableÚlinearoperator_callableÚdense_callableÚfd_hess)Úhessr   r   r   r   ÚnhevÚHÚ
_hess_funcr   r   r!   ÚspsÚissparseÚ	csr_arrayÚ
isinstancer   Ú
atleast_2dÚasarrayr"   )r   r7   Úx0r   r   r   s         r   r   z_ScalarHessWrapper.__init__9   sê   € ð ˆŒ	ØˆŒ	Ø˜,‘B¨DˆŒ	Ø#6ˆÔ àˆŒ	ØˆŒ	ØˆŒØˆŒä�DŒ>Ùœ"Ÿ'™' "›+Ð-¨Ò-ˆDŒFØ�IŠI˜‰N�Iä�|‰|˜DŸF™FÔ#Ø"3�”ÜŸ™ t§v¡vÓ.�•Ü˜DŸF™F¤NÔ3Ø";�•ð #3�”ÜŸ™¤r§z¡z°$·&±&Ó'9Ó:�•Ø”ZÑØ"+�•ð  r   c                 óä   — | j                   xdk(  r | j                  }n6xdk(  r | j                  }n#xdk(  r | j                  }ndk(  r| j                  } t        j                  |«      |¬«      S )Nr3   r4   r5   r6   ©r   )r:   Ú_sparse_callableÚ_linearoperator_callableÚ_dense_callableÚ_fd_hessr   r!   )r   r#   r   r$   Ú_hs        r   r'   z_ScalarHessWrapper.__call__[   sT   € Ø�o‰oÝ"Ø×*Ñ*‘Ý*Ø×2Ñ2‘Ý!Ø×)Ñ)‘ÛØ—]‘]�á”"—'‘'˜!“* Ô$Ð$r   c                 ó    — t        | j                  |fd|i| j                  ¤Ž\  | _        }| xj                  |d   z  c_        | j                  S )Nr   r   )r   r   r   r9   r   )r   r#   r   r$   r&   s        r   rG   z_ScalarHessWrapper._fd_hessh   sN   € Ü'Ø�I‰I�qñ
Øð
Ø#'×#;Ñ#;ñ
‰ˆŒ�ð 	�	Š	�S˜‘[Ñ �	Ø�v‰vˆr   c                 ó®   — | xj                   dz  c_         t        j                   | j                  |g| j                  ¢­Ž «      | _        | j
                  S ©Nr   )r8   r;   r=   r7   r   r9   ©r   r#   r$   s      r   rD   z#_ScalarHessWrapper._sparse_callableo   s<   € Ø�	Š	�Q‰�	Ü—‘˜y˜tŸy™y¨Ð7¨T¯Y©YÒ7Ó8ˆŒØ�v‰vˆr   c                 óÔ   — | xj                   dz  c_         t        j                  t        j                   | j                  |g| j
                  ¢­Ž «      «      | _        | j                  S rK   )r8   r   r?   r@   r7   r   r9   rL   s      r   rF   z"_ScalarHessWrapper._dense_callablet   sJ   € Ø�	Š	�Q‰�	Ü—‘Ü�J‰J�y�t—y‘y Ð/ T§Y¡YÒ/Ó0ó
ˆŒð �v‰vˆr   c                 óˆ   — | xj                   dz  c_          | j                  |g| j                  ¢­Ž | _        | j                  S rK   )r8   r7   r   r9   rL   s      r   rE   z+_ScalarHessWrapper._linearoperator_callable{   s3   € Ø�	Š	�Q‰�	Ø�—‘˜1Ð)˜tŸy™yÒ)ˆŒØ�v‰vˆr   )NNNNr)   )
r+   r,   r-   r.   r   r'   rG   rD   rF   rE   r/   r   r   r1   r1   5   s4   „ ñð ØØØ $ó ,óD%óòò
ór   r1   c                   ó®   — e Zd ZdZdej
                   ej
                  fddfd„Zed„ «       Zed„ «       Z	ed„ «       Z
d„ Zd„ Zd	„ Zd
„ Zd„ Zd„ Zd„ Zd„ Zy)ÚScalarFunctionaÏ  Scalar function and its derivatives.

    This class defines a scalar function F: R^n->R and methods for
    computing or approximating its first and second derivatives.

    Parameters
    ----------
    fun : callable
        evaluates the scalar function. Must be of the form ``fun(x, *args)``,
        where ``x`` is the argument in the form of a 1-D array and ``args`` is
        a tuple of any additional fixed parameters needed to completely specify
        the function. Should return a scalar.
    x0 : array-like
        Provides an initial set of variables for evaluating fun. Array of real
        elements of size (n,), where 'n' is the number of independent
        variables.
    args : tuple, optional
        Any additional fixed parameters needed to completely specify the scalar
        function.
    grad : {callable, '2-point', '3-point', 'cs'}
        Method for computing the gradient vector.
        If it is a callable, it should be a function that returns the gradient
        vector:

            ``grad(x, *args) -> array_like, shape (n,)``

        where ``x`` is an array with shape (n,) and ``args`` is a tuple with
        the fixed parameters.
        Alternatively, the keywords  {'2-point', '3-point', 'cs'} can be used
        to select a finite difference scheme for numerical estimation of the
        gradient with a relative step size. These finite difference schemes
        obey any specified `bounds`.
    hess : {callable, '2-point', '3-point', 'cs', HessianUpdateStrategy}
        Method for computing the Hessian matrix. If it is callable, it should
        return the  Hessian matrix:

            ``hess(x, *args) -> {LinearOperator, spmatrix, array}, (n, n)``

        where x is a (n,) ndarray and `args` is a tuple with the fixed
        parameters. Alternatively, the keywords {'2-point', '3-point', 'cs'}
        select a finite difference scheme for numerical estimation. Or, objects
        implementing `HessianUpdateStrategy` interface can be used to
        approximate the Hessian.
        Whenever the gradient is estimated via finite-differences, the Hessian
        cannot be estimated with options {'2-point', '3-point', 'cs'} and needs
        to be estimated using one of the quasi-Newton strategies.
    finite_diff_rel_step : None or array_like
        Relative step size to use. The absolute step size is computed as
        ``h = finite_diff_rel_step * sign(x0) * max(1, abs(x0))``, possibly
        adjusted to fit into the bounds. For ``method='3-point'`` the sign
        of `h` is ignored. If None then finite_diff_rel_step is selected
        automatically,
    finite_diff_bounds : tuple of array_like
        Lower and upper bounds on independent variables. Defaults to no bounds,
        (-np.inf, np.inf). Each bound must match the size of `x0` or be a
        scalar, in the latter case the bound will be the same for all
        variables. Use it to limit the range of function evaluation.
    epsilon : None or array_like, optional
        Absolute step size to use, possibly adjusted to fit into the bounds.
        For ``method='3-point'`` the sign of `epsilon` is ignored. By default
        relative steps are used, only if ``epsilon is not None`` are absolute
        steps used.
    workers : map-like callable, optional
        A map-like callable, such as `multiprocessing.Pool.map` for evaluating
        any numerical differentiation in parallel.
        This evaluation is carried out as ``workers(fun, iterable)``, or
        ``workers(grad, iterable)``, depending on what is being numerically
        differentiated.
        Alternatively, if `workers` is an int the task is subdivided into `workers`
        sections and the function evaluated in parallel
        (uses `multiprocessing.Pool <multiprocessing>`).
        Supply -1 to use all available CPU cores.
        It is recommended that a map-like be used instead of int, as repeated
        calls to `approx_derivative` will incur large overhead from setting up
        new processes.

        .. versionadded:: 1.16.0

    Notes
    -----
    This class implements a memoization logic. There are methods `fun`,
    `grad`, hess` and corresponding attributes `f`, `g` and `H`. The following
    things should be considered:

        1. Use only public methods `fun`, `grad` and `hess`.
        2. After one of the methods is called, the corresponding attribute
           will be set. However, a subsequent call with a different argument
           of *any* of the methods may overwrite the attribute.
    Nc
                 ón  — t        |«      s|t        vrt        dt        › d�«      ‚t        |«      s+|t        v s#t        |t        «      st        dt        › d�«      ‚|t        v r|t        v rt        d«      ‚t        |«      x| _        }
t        j                  |
j                  |«      d|
¬«      }|
j                  }|
j                  |j                  d«      r|j                  }t        ||«      | _        || _        || _        || _        || _        |
j'                  ||«      | _        || _        | j(                  j,                  | _        d| _        d| _        d| _        d | _        t8        j:                  | _        |	xs t>        }	i }|t        v r||d	<   ||d
<   ||d<   ||d<   |	|d<   d|d<   |t        v r||d	<   ||d
<   ||d<   d|d<   |	|d<   d|d<   d| _         | jC                  «        tE        || j                  ||¬«      | _#        | jI                  «        t        |t        «      r`|| _%        | jJ                  jM                  | j.                  d«       d| _        d | _'        d | _(        tS        dddg«      } |dd¬«      | _*        y t        |«      r7tW        ||||¬«      | _*        | jT                  jJ                  | _%        d| _        y |t        v rctW        |||| jF                  |¬«      | _*        | jI                  «        | jU                  | j(                  | jX                  ¬«      | _%        d| _        y y )Nz)`grad` must be either callable or one of Ú.z@`hess` must be either callable, HessianUpdateStrategy or one of z‹Whenever the gradient is estimated via finite-differences, we require the Hessian to be estimated using one of the quasi-Newton strategies.r   ©ÚndimÚxpúreal floatingFÚmethodÚrel_stepÚabs_stepÚboundsÚworkersTÚfull_outputÚas_linear_operatorr   )r   r   r   r7   Ú_FakeCounterr   r8   )r   r8   )rA   r   r   )rA   r   r   r   rC   )-r   r"   Ú
ValueErrorr>   r   r	   rU   ÚxpxÚ
atleast_ndr@   Úfloat64ÚisdtypeÚdtyper   Ú_wrapped_funÚ	_orig_funÚ
_orig_gradÚ
_orig_hessÚ_argsÚastyper#   Úx_dtypeÚsizeÚnÚ	f_updatedÚ	g_updatedÚ	H_updatedÚ	_lowest_xr   ÚinfÚ	_lowest_fÚmapÚ_nfevÚ_update_funr   Ú_wrapped_gradÚ_update_gradr9   Ú
initializeÚx_prevÚg_prevr   Ú_wrapped_hessr1   r%   )r   r   rA   r   r   r7   Úfinite_diff_rel_stepÚfinite_diff_boundsÚepsilonr[   rU   Ú_xÚ_dtyper   r^   s                  r   r   zScalarFunction.__init__Ú   s@  € ô ˜Œ~ $¬jÑ"8ÜØ;¼J¸<ÀqÐIóð ô ˜” $¬*Ñ"4Ü˜dÔ$9Ô:ÜðÜ(˜\¨ð,óð ð
 ”:Ñ $¬*Ñ"4Üð 8ó 9ð 9ô ' rÓ*Ð*ˆŒ�"Ü�^‰^˜BŸJ™J r›N°°rÔ:ˆØ—‘ˆØ�:‰:�b—h‘h Ô0Ø—X‘XˆFô 3°3¸Ó=ˆÔØˆŒØˆŒØˆŒØˆŒ
ð —‘˜2˜vÓ&ˆŒØˆŒØ—‘—‘ˆŒØˆŒØˆŒØˆŒàˆŒÜŸ™ˆŒð ’.œSˆà ÐØ”:ÑØ,0Ð Ñ)Ø.BÐ 
Ñ+Ø.5Ð 
Ñ+Ø,>Ð Ñ)Ø-4Ð 	Ñ*Ø15Ð Ñ.Ø”:ÑØ,0Ð Ñ)Ø.BÐ 
Ñ+Ø.5Ð 
Ñ+Ø8<ÐÐ 4Ñ5Ø-4Ð 	Ñ*Ø15Ð Ñ.ð ˆŒ
Ø×ÑÔô 0ØØ×!Ñ!ØØ 3ô	
ˆÔð 	×ÑÔô �dÔ1Ô2ØˆDŒFØ�F‰F×Ñ˜dŸf™f fÔ-Ø!ˆDŒNØˆDŒKØˆDŒKÜ% n°v¸vÐ6FÓGˆLÙ!-°1¸1Ô!=ˆDÕä˜Œ~Ü%7ØØØØ(;ô	&�Ô"ð ×+Ñ+×-Ñ-�”Ø!%�•ØœÑ#Ü%7ØØØØ×+Ñ+Ø(;ô&�Ô"ð ×!Ñ!Ô#Ø×+Ñ+¨D¯F©F°t·v±vÐ+Ó>�”Ø!%�•ð $r   c                 óH   — | j                   | j                  j                  z   S r)   )ru   rw   r   ©r   s    r   r   zScalarFunction.nfevE  s   € à�z‰z˜D×.Ñ.×3Ñ3Ñ3Ð3r   c                 ó.   — | j                   j                  S r)   )rw   r   rƒ   s    r   r   zScalarFunction.ngevI  ó   € à×!Ñ!×&Ñ&Ð&r   c                 ó.   — | j                   j                  S r)   )r|   r8   rƒ   s    r   r8   zScalarFunction.nhevM  r…   r   c                 óª  — t        | j                  t        «      r¾| j                  «        | j                  | _        | j                  | _        t        j                  | j                  j                  |«      d| j                  ¬«      }| j                  j                  || j                  «      | _        d| _        d| _        d| _        | j#                  «        y t        j                  | j                  j                  |«      d| j                  ¬«      }| j                  j                  || j                  «      | _        d| _        d| _        d| _        y ©Nr   rS   F)r>   rh   r   rx   r#   rz   r%   r{   r`   ra   rU   r@   rj   rk   rn   ro   rp   Ú_update_hess©r   r#   r€   s      r   Ú	_update_xzScalarFunction._update_xQ  sç   € Ü�d—o‘oÔ'<Ô=Ø×ÑÔØŸ&™&ˆDŒKØŸ&™&ˆDŒKô —‘ §¡§¡°Ó 2¸¸t¿w¹wÔGˆBØ—W‘W—^‘^ B¨¯©Ó5ˆDŒFØ"ˆDŒNØ"ˆDŒNØ"ˆDŒNØ×ÑÕô —‘ §¡§¡°Ó 2¸¸t¿w¹wÔGˆBØ—W‘W—^‘^ B¨¯©Ó5ˆDŒFØ"ˆDŒNØ"ˆDŒNØ"ˆD�Nr   c                 óè   — | j                   sf| j                  | j                  «      }| xj                  dz  c_        || j                  k  r| j                  | _        || _        || _        d| _         y y ©Nr   T)rn   re   r#   ru   rs   rq   Úf)r   Úfxs     r   rv   zScalarFunction._update_funh  s[   € Ø�~Š~Ø×"Ñ" 4§6¡6Ó*ˆBØ�JŠJ˜!‰O�JØ�D—N‘NÒ"Ø!%§¡�”Ø!#�”àˆDŒFØ!ˆD�Nð r   c                 óÈ   — | j                   sV| j                  t        v r| j                  «        | j	                  | j
                  | j                  ¬«      | _        d| _         y y ©NrC   T)ro   rg   r"   rv   rw   r#   rŽ   r%   rƒ   s    r   rx   zScalarFunction._update_grads  sL   € Ø�~Š~Ø�‰¤*Ñ,Ø× Ñ Ô"Ø×'Ñ'¨¯©°4·6±6Ð'Ó:ˆDŒFØ!ˆD�Nð	 r   c                 óô  — | j                   sì| j                  t        v r=| j                  «        | j	                  | j
                  | j                  ¬«      | _        n•t        | j                  t        «      r[| j                  «        | j                  j                  | j
                  | j                  z
  | j                  | j                  z
  «       n | j	                  | j
                  «      | _        d| _         y y r‘   )rp   rh   r"   rx   r|   r#   r%   r9   r>   r   Úupdaterz   r{   rƒ   s    r   r‰   zScalarFunction._update_hessz  s©   € Ø�~Š~Ø�‰¤*Ñ,Ø×!Ñ!Ô#Ø×+Ñ+¨D¯F©F°t·v±vÐ+Ó>�•Ü˜DŸO™OÔ-BÔCØ×!Ñ!Ô#Ø—‘—‘˜dŸf™f t§{¡{Ñ2°D·F±F¸T¿[¹[Ñ4HÕIà×+Ñ+¨D¯F©FÓ3�”à!ˆD�Nð r   c                 óœ   — t        j                  || j                  «      s| j                  |«       | j	                  «        | j
                  S r)   )r   Úarray_equalr#   r‹   rv   rŽ   ©r   r#   s     r   r   zScalarFunction.fun‡  s5   € Ü�~‰~˜a §¡Ô(Ø�N‰N˜1ÔØ×ÑÔØ�v‰vˆr   c                 óœ   — t        j                  || j                  «      s| j                  |«       | j	                  «        | j
                  S r)   )r   r•   r#   r‹   rx   r%   r–   s     r   r   zScalarFunction.grad�  ó5   € Ü�~‰~˜a §¡Ô(Ø�N‰N˜1ÔØ×ÑÔØ�v‰vˆr   c                 óœ   — t        j                  || j                  «      s| j                  |«       | j	                  «        | j
                  S r)   )r   r•   r#   r‹   r‰   r9   r–   s     r   r7   zScalarFunction.hess“  r˜   r   c                 óÔ   — t        j                  || j                  «      s| j                  |«       | j	                  «        | j                  «        | j                  | j                  fS r)   )r   r•   r#   r‹   rv   rx   rŽ   r%   r–   s     r   Úfun_and_gradzScalarFunction.fun_and_grad™  sJ   € Ü�~‰~˜a §¡Ô(Ø�N‰N˜1ÔØ×ÑÔØ×ÑÔØ�v‰v�t—v‘vˆ~Ðr   )r+   r,   r-   r.   r   rr   r   Úpropertyr   r   r8   r‹   rv   rx   r‰   r   r   r7   r›   r/   r   r   rP   rP   €   s‘   „ ñXðr HLØ&(§f¡f W¨b¯f©fÐ$5¸tÈTói&ðV ñ4ó ð4ð ñ'ó ð'ð ñ'ó ð'ò#ò.	"ò"ò"òòòór   rP   c                   ó   — e Zd Zd„ Zd„ Zy)Ú_VectorFunWrapperc                 ó    — || _         d| _        y r   )r   r   )r   r   s     r   r   z_VectorFunWrapper.__init__¢  s   € ØˆŒØˆ�	r   c                 ót   — | xj                   dz  c_         t        j                  | j                  |«      «      S rK   )r   r   r    r   r–   s     r   r'   z_VectorFunWrapper.__call__¦  s&   € Ø�	Š	�Q‰�	Ü�}‰}˜TŸX™X a›[Ó)Ð)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)Ú_VectorJacWrapperú0
    Wrapper class for Jacobian calculation
    Nc                 óX   — || _         || _        || _        || _        d| _        d| _        y r   )r   Újacr   Úsparse_jacobianÚnjevr   )r   r¥   r   r   r¦   s        r   r   z_VectorJacWrapper.__init__¯  s0   € ð ˆŒØˆŒØ#6ˆÔ Ø.ˆÔàˆŒ	àˆ�	r   c                 óô  — t        | j                  «      r'| j                  |«      }| xj                  dz  c_        nP| j                  t        v r>t	        | j
                  |fd|i| j                  ¤Ž\  }}| xj                  |d   z  c_        | j                  rt        j                  «      S t        j                  «      r|j                  «       S t        |t        «      r|S t        j                   |«      S )Nr   r   r   )r   r¥   r§   r"   r   r   r   r   r¦   r;   r=   r<   Útoarrayr>   r   r   r?   )r   r#   r   r$   ÚJr&   s         r   r'   z_VectorJacWrapper.__call__¿  sÉ   € ô �D—H‘HÔØ—‘˜“ˆAØ�IŠI˜‰NŽIØ�X‰XœÑ#Ü&Ø—‘Øñð ðð ×*Ñ*ñ	‰FˆAˆsð �IŠI˜˜V™Ñ$�Ià×ÒÜ—=‘= Ó#Ð#Ü�\‰\˜!Œ_Ø—9‘9“;ÐÜ˜œ>Ô*ØˆHä—=‘= Ó#Ð#r   r(   r)   r*   r/   r   r   r¢   r¢   «  s   „ ñð Ø $Ø óô $r   r¢   c                   ó8   — e Zd ZdZ	 	 dd„Zd	d„Zd	d„Zd„ Zd„ Zy)
Ú_VectorHessWrapperr£   Nc                 óJ   — || _         || _        || _        d| _        d| _        y r   )r¥   r7   r   r8   r§   )r   r7   r¥   r   s       r   r   z_VectorHessWrapper.__init__Ü  s(   € ð ˆŒØˆŒ	Ø#6ˆÔ ØˆŒ	àˆ�	r   c                 óÈ   — t        | j                  «      r'| xj                  dz  c_        | j                  ||«      S | j                  t        v r| j                  |||¬«      S y )Nr   ©ÚJ0)r   r7   r8   Ú_callable_hessr"   rG   )r   r#   Úvr°   r$   s        r   r'   z_VectorHessWrapper.__call__é  sV   € ô �D—I‘IÔØ�IŠI˜‰N�IØ×&Ñ& q¨!Ó,Ð,Ø�Y‰Yœ*Ñ$Ø—=‘=  A¨"�=Ó-Ð-ð %r   c                 óÒ   — |€&| j                  |«      }| xj                  dz  c_        t        | j                  |f|j                  j                  |«      |fdœ| j                  ¤Ž}|S )Nr   )r   r   )r¥   r§   r   Ú	jac_dot_vÚTÚdotr   )r   r#   r²   r°   r9   s        r   rG   z_VectorHessWrapper._fd_hessò  se   € Øˆ:Ø—‘˜!“ˆBØ�IŠI˜‰N�Iô ˜dŸn™n¨að :Ø!#§¡§¡¨!£Ø$% 4ñ:ð !%× 8Ñ 8ñ:ˆð ˆr   c                 ó€   — | xj                   dz  c_         | j                  |«      j                  j                  |«      S rK   )r§   r¥   rµ   r¶   ©r   r#   r²   s      r   r´   z_VectorHessWrapper.jac_dot_vþ  s,   € Ø�	Š	�Q‰�	Ø�x‰x˜‹{�}‰}× Ñ  Ó#Ð#r   c                 óî   — | j                  ||«      }t        j                  |«      rt        j                  |«      S t	        |t
        «      r|S t        j                  t        j                  |«      «      S r)   )	r7   r;   r<   r=   r>   r   r   r?   r@   )r   r#   r²   r9   s       r   r±   z!_VectorHessWrapper._callable_hess  sQ   € Ø�I‰I�a˜‹Oˆä�<‰<˜Œ?Ü—=‘= Ó#Ð#Ü˜œ>Ô*ØˆHä—=‘=¤§¡¨A£Ó/Ð/r   )NNr)   )	r+   r,   r-   r.   r   r'   rG   r´   r±   r/   r   r   r¬   r¬   Ø  s(   „ ñð Ø $ó	ó.ó
ò$ó0r   r¬   c                   ó°   — e Zd ZdZddej
                   ej
                  fddfd„Zed„ «       Zed„ «       Z	ed„ «       Z
d„ Zd„ Zd	„ Zd
„ Zd„ Zd„ Zd„ Zd„ Zy)ÚVectorFunctiona‘  Vector function and its derivatives.

    This class defines a vector function F: R^n->R^m and methods for
    computing or approximating its first and second derivatives.

    Notes
    -----
    This class implements a memoization logic. There are methods `fun`,
    `jac`, hess` and corresponding attributes `f`, `J` and `H`. The following
    things should be considered:

        1. Use only public methods `fun`, `jac` and `hess`.
        2. After one of the methods is called, the corresponding attribute
           will be set. However, a subsequent call with a different argument
           of *any* of the methods may overwrite the attribute.
    Nc
                 ó
  — t        |«      s|t        vrt        dt        › d�«      ‚t        |«      s+|t        v s#t        |t        «      st        dt        › d�«      ‚|t        v r|t        v rt        d«      ‚t        |«      x| _        }
t        j                  |
j                  |«      d|
¬«      }|
j                  }|
j                  |j                  d«      r|j                  }|| _        || _        || _        |
j!                  ||«      | _        || _        | j"                  j&                  | _        d| _        d| _        d| _        d	| _        d	| _        d	| _        |	xs t6        }	i }|t        v rQ||d
<   ||d<   |�t9        |«      }||f|d<   ||d<   |	|d<   d|d<   t;        j<                  | j"                  «      | _        |t        v r3||d
<   ||d<   d|d<   t;        j<                  | j"                  «      | _        |t        v r|t        v rt        d«      ‚tA        |«      | _!        | jE                  «        t;        jF                  | jH                  «      | _%        | jJ                  j&                  | _&        t        |«      r= |tO        | j"                  «      «      | _(        d| _        | xj,                  dz  c_        n\|t        v rTtS        | jB                  | j"                  fd| jH                  i|¤Ž\  | _(        }d| _        | xj*                  |d   z  c_        d	| _*        |s!|€KtW        jX                  | jP                  «      r,tW        jZ                  | jP                  «      | _(        d| _*        n~tW        jX                  | jP                  «      r | jP                  j]                  «       | _(        n?t        | jP                  t^        «      rn$t;        j`                  | jP                  «      | _(        tc        || jB                  || jT                  ¬«      | _2        tg        || jd                  |¬«      | _4        t        |«      s|t        v ri| ji                  tO        | j"                  «      | jJ                  | jP                  ¬«      | _5        d| _        t        |«      r| xj.                  dz  c_        y y t        |t        «      rC|| _5        | jj                  jm                  | j(                  d«       d| _        d | _7        d | _8        y y )Nz(`jac` must be either callable or one of rR   z?`hess` must be either callable,HessianUpdateStrategy or one of z‹Whenever the Jacobian is estimated via finite-differences, we require the Hessian to be estimated using one of the quasi-Newton strategies.r   rS   rV   r   FrW   rX   ÚsparsityrZ   r[   Tr\   r]   r   r   )r   r   r¦   )r¥   r   r¯   r7   )9r   r"   r_   r>   r   r	   rU   r`   ra   r@   rb   rc   rd   rf   Ú	_orig_jacrh   rj   r#   rk   rl   rm   ru   Ú_njevÚ_nhevrn   Ú	J_updatedrp   rt   r   r   r!   Úx_diffrž   Úfun_wrappedrv   Ú
zeros_likerŽ   r²   Úmr
   rª   r   r¦   r;   r<   r=   r©   r   r?   r¢   Újac_wrappedr¬   Úhess_wrappedr9   ry   rz   ÚJ_prev)r   r   rA   r¥   r7   r}   Úfinite_diff_jac_sparsityr~   r¦   r[   rU   r€   r�   r   Úsparsity_groupsr&   s                   r   r   zVectorFunction.__init__  sS  € ô ˜Œ} ¬JÑ!6ÜÐGÌ
À|ÐSTÐUÓVÐVä˜” $¬*Ñ"4Ü˜dÔ$9Ô:Üð @Ü@J¸|È1ðNó Oð Oð ”*Ñ ¬Ñ!3Üð +ó ,ð ,ô
 ' rÓ*Ð*ˆŒ�"Ü�^‰^˜BŸJ™J r›N°°rÔ:ˆØ—‘ˆØ�:‰:�b—h‘h Ô0Ø—X‘XˆFð ˆŒØˆŒØˆŒð —‘˜2˜vÓ&ˆŒØˆŒà—‘—‘ˆŒØˆŒ
ØˆŒ
ØˆŒ
ØˆŒØˆŒØˆŒð ’.œSˆà ÐØ”*ÑØ,/Ð Ñ)Ø.BÐ 
Ñ+Ø'Ð3Ü"/Ð0HÓ"I�Ø3KØ3Bð3DÐ# JÑ/à,>Ð Ñ)Ø-4Ð 	Ñ*Ø15Ð Ñ.ÜŸ'™' $§&¡&›/ˆDŒKØ”:ÑØ,0Ð Ñ)Ø.BÐ 
Ñ+Ø8<ÐÐ 4Ñ5ô
 Ÿ'™' $§&¡&›/ˆDŒKØ”*Ñ ¬Ñ!3Üð +ó ,ð ,ô
 -¨SÓ1ˆÔØ×ÑÔä—‘˜tŸv™vÓ&ˆŒØ—‘—‘ˆŒô �CŒ=Ùœ §¡›Ó)ˆDŒFØ!ˆDŒNØ�JŠJ˜!‰OŽJØ”JÑÜ+Ø× Ñ  $§&¡&ñØ-1¯V©VðØ7Jñ‰KˆDŒF�Cð "ˆDŒNØ�JŠJ˜#˜f™+Ñ%�Jà$ˆÔÙØÐ'¬C¯L©L¸¿¹Ô,@ô —]‘] 4§6¡6Ó*ˆDŒFØ#'ˆDÕ Ü�\‰\˜$Ÿ&™&Ô!Ø—V‘V—^‘^Ó%ˆD�FÜ˜Ÿ™¤Ô/Øä—]‘] 4§6¡6Ó*ˆDŒFä,ØØ× Ñ Ø 3Ø ×0Ñ0ô	
ˆÔô /Ø�d×&Ñ&Ð<Oô
ˆÔô
 �DŒ>˜T¤ZÑ/Ø×&Ñ&¤w¨t¯v©v£¸¿¹À4Ç6Á6Ð&ÓJˆDŒFØ!ˆDŒNÜ˜Œ~Ø—
’
˜a‘–
ð ä˜Ô3Ô4ØˆDŒFØ�F‰F×Ñ˜dŸf™f fÔ-Ø!ˆDŒNØˆDŒKØˆD�Kð 5r   c                 óH   — | j                   | j                  j                  z   S r)   )ru   rÆ   r   rƒ   s    r   r   zVectorFunction.nfev�  s   € à�z‰z˜D×,Ñ,×1Ñ1Ñ1Ð1r   c                 óH   — | j                   | j                  j                  z   S r)   )r¿   rÇ   r§   rƒ   s    r   r§   zVectorFunction.njev¡  s   € à�z‰z˜D×-Ñ-×2Ñ2Ñ2Ð2r   c                 ó   — | j                   S r)   )rÀ   rƒ   s    r   r8   zVectorFunction.nhev¥  s   € à�z‰zÐr   c                 ób   — t        j                  || j                  «      s|| _        d| _        y y )NF)r   r•   r²   rp   )r   r²   s     r   Ú	_update_vzVectorFunction._update_v©  s'   € Ü�~‰~˜a §¡Ô(ØˆDŒFØ"ˆD�Nð )r   c                 óî  — t        j                  || j                  «      �sTt        | j                  t
        «      r¾| j                  «        | j                  | _        | j                  | _	        t        j                  | j                  j                  |«      d| j                  ¬«      }| j                  j                  || j                  «      | _        d| _        d| _        d| _        | j'                  «        y t        j                  | j                  j                  |«      d| j                  ¬«      }| j                  j                  || j                  «      | _        d| _        d| _        d| _        y y rˆ   )r   r•   r#   r>   rh   r   Ú_update_jacrz   rª   rÈ   r`   ra   rU   r@   rj   rk   rn   rÁ   rp   r‰   rŠ   s      r   r‹   zVectorFunction._update_x®  sú   € Ü�~‰~˜a §¡Õ(Ü˜$Ÿ/™/Ô+@ÔAØ× Ñ Ô"Ø"Ÿf™f�”Ø"Ÿf™f�”Ü—^‘^ D§G¡G§O¡O°AÓ$6¸QÀ4Ç7Á7ÔK�ØŸ™Ÿ™¨¨D¯L©LÓ9�”Ø!&�”Ø!&�”Ø!&�”Ø×!Ñ!Õ#ä—^‘^ D§G¡G§O¡O°AÓ$6¸QÀ4Ç7Á7ÔK�ØŸ™Ÿ™¨¨D¯L©LÓ9�”Ø!&�”Ø!&�”Ø!&�•ð! )r   c                 ó¨   — | j                   sF| j                  t        | j                  «      «      | _        | xj
                  dz  c_        d| _         y y r�   )rn   rÃ   r
   r#   rŽ   ru   rƒ   s    r   rv   zVectorFunction._update_funÁ  s<   € Ø�~Š~Ø×%Ñ%¤g¨d¯f©f£oÓ6ˆDŒFØ�JŠJ˜!‰O�JØ!ˆD�Nð r   c                 ó  — | j                   su| j                  t        v r| j                  «        n| xj                  dz  c_        | j                  t        | j                  «      | j                  ¬«      | _	        d| _         y y )Nr   rC   T)
rÁ   r¾   r"   rv   r¿   rÆ   r
   r#   rŽ   rª   rƒ   s    r   rÑ   zVectorFunction._update_jacÇ  s]   € Ø�~Š~Ø�~‰~¤Ñ+à× Ñ Õ"à—
’
˜a‘•
à×%Ñ%¤g¨d¯f©f£o¸$¿&¹&Ð%ÓAˆDŒFØ!ˆD�Nð r   c                 ó`  — | j                   �s¡t        | j                  «      rK| j                  t	        | j
                  «      | j                  «      | _        | xj                  dz  c_        �n9| j                  t        v rQ| j                  «        | j                  t	        | j
                  «      | j                  | j                  ¬«      | _        nÖt        | j                  t        «      r¼| j                  «        | j                  � | j                  �”| j
                  | j                  z
  }| j                  j                   j#                  | j                  «      | j                  j                   j#                  | j                  «      z
  }| j                  j%                  ||«       d| _         y y )Nr   r¯   T)rp   r   rh   rÇ   r
   r#   r²   r9   rÀ   r"   rÑ   rª   r>   r   rz   rÈ   rµ   r¶   r“   )r   Údelta_xÚdelta_gs      r   r‰   zVectorFunction._update_hessÒ  s  € Ø�~‹~Ü˜Ÿ™Ô(Ø×*Ñ*¬7°4·6±6«?¸D¿F¹FÓC�”Ø—
’
˜a‘—
Ø—‘¤JÑ.Ø× Ñ Ô"Ø×*Ñ*¬7°4·6±6«?¸D¿F¹FÀtÇvÁvÐ*ÓN�•Ü˜DŸO™OÔ-BÔCØ× Ñ Ô"ð —;‘;Ð*¨t¯{©{Ð/FØ"Ÿf™f t§{¡{Ñ2�GØ"Ÿf™fŸh™hŸl™l¨4¯6©6Ó2°T·[±[·]±]×5FÑ5FÀtÇvÁvÓ5NÑN�GØ—F‘F—M‘M '¨7Ô3à!ˆD�Nð! r   c                 ón   — | j                  |«       | j                  «        t        | j                  «      S r)   )r‹   rv   r
   rŽ   r–   s     r   r   zVectorFunction.funå  s*   € Ø�‰�qÔØ×ÑÔô �t—v‘v‹Ðr   c                 óæ   — | j                  |«       | j                  «        t        | j                  d«      r/| j                  j	                  | j                  j
                  «      S | j                  S ©Nrj   )r‹   rÑ   Úhasattrrª   rj   rd   r–   s     r   r¥   zVectorFunction.jacì  sN   € Ø�‰�qÔØ×ÑÔÜ�4—6‘6˜8Ô$ð —6‘6—=‘= §¡§¡Ó.Ð.Ø�v‰vˆr   c                 ó  — | j                  |«       | j                  |«       | j                  «        t        | j                  d«      r/| j                  j                  | j                  j                  «      S | j                  S rÙ   )rÏ   r‹   r‰   rÚ   r9   rj   rd   r¸   s      r   r7   zVectorFunction.hessõ  sZ   € à�‰�qÔØ�‰�qÔØ×ÑÔÜ�4—6‘6˜8Ô$ð —6‘6—=‘= §¡§¡Ó.Ð.Ø�v‰vˆr   )r+   r,   r-   r.   r   rr   r   rœ   r   r§   r8   rÏ   r‹   rv   rÑ   r‰   r   r¥   r7   r/   r   r   r»   r»     s�   „ ñð" '+ÀTØ&(§f¡f W¨b¯f©fÐ$5ÀtØó}ð~ ñ2ó ð2ð ñ3ó ð3ð ñó ðò#ò
'ò&"ò	"ò"ò&òó	r   r»   c                   ó.   — e Zd ZdZd„ Zd„ Zd„ Zd„ Zd„ Zy)ÚLinearVectorFunctionzüLinear vector function and its derivatives.

    Defines a linear function F = A x, where x is N-D vector and
    A is m-by-n matrix. The Jacobian is constant and equals to A. The Hessian
    is identically zero and it is returned as a csr matrix.
    c                 ó¸  — |s|€7t        j                  |«      r"t        j                  |«      | _        d| _        nft        j                  |«      r|j                  «       | _        d| _        n4t        j                  t        j                  |«      «      | _        d| _        | j                  j                  \  | _
        | _        t        |«      x| _        }t        j                  |j                  |«      d|¬«      }|j                   }|j#                  |j$                  d«      r|j$                  }|j'                  ||«      | _        || _        | j                  j-                  | j(                  «      | _        d| _        t        j2                  | j                  t4        ¬«      | _        t        j                  | j                  | j                  f«      | _        y )NTFr   rS   rV   )rd   )r;   r<   r=   rª   r¦   r©   r   r?   r@   ÚshaperÅ   rm   r	   rU   r`   ra   rb   rc   rd   rj   r#   rk   r¶   rŽ   rn   ÚzerosÚfloatr²   r9   )r   ÚArA   r¦   rU   r€   r�   s          r   r   zLinearVectorFunction.__init__  s?  € Ù˜oÐ5¼#¿,¹,Àq¼/Ü—]‘] 1Ó%ˆDŒFØ#'ˆDÕ Ü�\‰\˜!Œ_Ø—Y‘Y“[ˆDŒFØ#(ˆDÕ ô —]‘]¤2§:¡:¨a£=Ó1ˆDŒFØ#(ˆDÔ àŸ™Ÿ™‰ˆŒ�”ä& rÓ*Ð*ˆŒ�"Ü�^‰^˜BŸJ™J r›N°°rÔ:ˆØ—‘ˆØ�:‰:�b—h‘h Ô0Ø—X‘XˆFð —‘˜2˜vÓ&ˆŒØˆŒà—‘—‘˜DŸF™FÓ#ˆŒØˆŒä—‘˜$Ÿ&™&¬Ô.ˆŒÜ—‘ §¡¨¯©Ð/Ó0ˆ�r   c                 ó   — t        j                  || j                  «      snt        j                  | j
                  j                  |«      d| j
                  ¬«      }| j
                  j                  || j                  «      | _        d| _	        y y rˆ   )
r   r•   r#   r`   ra   rU   r@   rj   rk   rn   rŠ   s      r   r‹   zLinearVectorFunction._update_x&  s]   € Ü�~‰~˜a §¡Ô(Ü—‘ §¡§¡°Ó 2¸¸t¿w¹wÔGˆBØ—W‘W—^‘^ B¨¯©Ó5ˆDŒFØ"ˆD�Nð )r   c                 ó¢   — | j                  |«       | j                  s'| j                  j                  |«      | _        d| _        | j                  S )NT)r‹   rn   rª   r¶   rŽ   r–   s     r   r   zLinearVectorFunction.fun,  s8   € Ø�‰�qÔØ�~Š~Ø—V‘V—Z‘Z “]ˆDŒFØ!ˆDŒNØ�v‰vˆr   c                 ó<   — | j                  |«       | j                  S r)   )r‹   rª   r–   s     r   r¥   zLinearVectorFunction.jac3  s   € Ø�‰�qÔØ�v‰vˆr   c                 óJ   — | j                  |«       || _        | j                  S r)   )r‹   r²   r9   r¸   s      r   r7   zLinearVectorFunction.hess7  s   € Ø�‰�qÔØˆŒØ�v‰vˆr   N)	r+   r,   r-   r.   r   r‹   r   r¥   r7   r/   r   r   rÝ   rÝ     s    „ ñò1ò<#òòór   rÝ   c                   ó"   ‡ — e Zd ZdZˆ fd„Zˆ xZS )ÚIdentityVectorFunctionzþIdentity vector function and its derivatives.

    The Jacobian is the identity matrix, returned as a dense array when
    `sparse_jacobian=False` and as a csr matrix otherwise. The Hessian is
    identically zero and it is returned as a csr matrix.
    c                 ó¨   •— t        |«      }|s|€t        j                  |d¬«      }d}nt        j                  |«      }d}t
        ‰| �  |||«       y )NÚcsr)ÚformatTF)Úlenr;   Ú	eye_arrayr   ÚeyeÚsuperr   )r   rA   r¦   rm   râ   Ú	__class__s        €r   r   zIdentityVectorFunction.__init__D  sL   ø€ Ü�‹GˆÙ˜oÐ5Ü—‘˜a¨Ô.ˆAØ"‰Oä—‘�q“	ˆAØ#ˆOÜ‰Ñ˜˜B Õ0r   )r+   r,   r-   r.   r   Ú__classcell__)rð   s   @r   rè   rè   =  s   ø„ ñ÷1ð 1r   rè   ) Úcollectionsr   Únumpyr   Úscipy.sparseÚsparser;   Ú_numdiffr   r   Ú_hessian_update_strategyr   Úscipy.sparse.linalgr   Úscipy._lib._array_apir	   r
   Ú
scipy._libr   r`   Úscipy._lib._utilr   r"   r   r1   rP   rž   r¢   r¬   r»   rÝ   rè   r/   r   r   Ú<module>rü      s‘   ðÝ "ã Ý ß 6Ý ;Ý .ß :Ý -Ý 3ð *€
÷"ñ "÷JIñ I÷V^ñ ^÷B	*ñ *÷*$ñ *$÷Z20ñ 20÷jqñ q÷h9ñ 9ôx1Ð1õ 1r   