+
    Y°³jo‘  ã                   ó|  € R t ^ RIt^ RIt^ RIHt ^ RIHt ^RIH	t	H
t
HtHtHtHt ^RIHtHt ^ RIHtHt ^ RIHt ^ RIHt R	 t]P6                  R
 4       tR tR tRR ltRRRR]P@                  ) ]P@                  3RRRRRR3R lt!R t"R t#R t$ ! R R4      t%]P@                  ) ]P@                  3RR3R lt&R# )z'Routines for numerical differentiation.N)Únorm)ÚLinearOperator)ÚissparseÚspmatrixÚfindÚ	csc_arrayÚ	csr_arrayÚ
csr_matrix)Úgroup_denseÚgroup_sparse)Úarray_namespaceÚxp_result_type)Ú
MapWrapper)Úarray_api_extrac                óÔ  € VR8X  d   \         P                  ! V\        R7      pMEVR8X  d4   \         P                  ! V4      p\         P                  ! V\        R7      pM\        R4      h\         P                  ! V\         P                  ) 8H  V\         P                  8H  ,          4      '       d   W3# W,          pVP                  4       pW,
          p	WP,
          p
VR8X  dœ   W,           pW´8  Wµ8„  ,          p\         P                  ! V4      \         P                  ! Wš4      8*  pWŒV,          ;;,          R,          uu&   W©8¬  V( ,          pW®,          V,          WŽ&   W©8  V( ,          pWŸ,          ) V,          W�&   W†3# VR8X  dä   W—8¬  W§8¬  ,          pW©8¬  V( ,          p\         P                  ! W,          RW®,          ,          V,          4      WŽ&   RWn&   W©8  V( ,          p\         P                  ! W,          RWŸ,          ,          V,          4      ) W�&   RWo&   \         P                  ! W©4      V,          pV( \         P                  ! V4      V8*  ,          pVV,          VV&   RVV&   W†3# )	a<  Adjust final difference scheme to the presence of bounds.

Parameters
----------
x0 : ndarray, shape (n,)
    Point at which we wish to estimate derivative.
h : ndarray, shape (n,)
    Desired absolute finite difference steps.
num_steps : int
    Number of `h` steps in one direction required to implement finite
    difference scheme. For example, 2 means that we need to evaluate
    f(x0 + 2 * h) or f(x0 - 2 * h)
scheme : {'1-sided', '2-sided'}
    Whether steps in one or both directions are required. In other
    words '1-sided' applies to forward and backward schemes, '2-sided'
    applies to center schemes.
lb : ndarray, shape (n,)
    Lower bounds on independent variables.
ub : ndarray, shape (n,)
    Upper bounds on independent variables.

Returns
-------
h_adjusted : ndarray, shape (n,)
    Adjusted absolute step sizes. Step size decreases only if a sign flip
    or switching to one-sided scheme doesn't allow to take a full step.
use_one_sided : ndarray of bool, shape (n,)
    Whether to switch to one-sided scheme. Informative only for
    ``scheme='2-sided'``.
ú1-sided©Údtypeú2-sidedz(`scheme` must be '1-sided' or '2-sided'.ç      à?TFéÿÿÿÿ)ÚnpÚ	ones_likeÚboolÚabsÚ
zeros_likeÚ
ValueErrorÚallÚinfÚcopyÚmaximumÚminimum)Úx0ÚhÚ	num_stepsÚschemeÚlbÚubÚuse_one_sidedÚh_totalÚ
h_adjustedÚ
lower_distÚ
upper_distÚxÚviolatedÚfittingÚforwardÚbackwardÚcentralÚmin_distÚadjusted_centrals   &&&&&&             ÚX/home/luca/screen-checking/.venv/lib/python3.14/site-packages/scipy/optimize/_numdiff.pyÚ_adjust_scheme_to_boundsr6      s  € ð> �ÔÜŸš Q¬dÔ3‰Ø	�9Ô	Ü�FŠF�1‹IˆÜŸš a¬tÔ4‰äÐCÓDÐDä	‡v‚vˆr”b—f‘f�W‰} ¤r§v¡v¡Õ.×/Ò/ØÐÐà�m€GØ—‘“€Jà•€JØ•€Jà�ÔØ�LˆØ‘F˜q™vÕ&ˆÜ—&’&˜“/¤R§Z¢Z°
Ó%GÑGˆØ˜gÕ%×&¨"Õ,Ó&àÑ+°¨xÕ7ˆØ(Õ1°IÕ=ˆ
ÑØÑ+°¨xÕ7ˆØ *Õ 4Ð4°yÕ@ˆ
Ñð& Ð$Ð$ð% 
�9Ô	ØÑ(¨ZÑ-BÕCˆàÑ+°¨xÕ7ˆÜ ŸjšjØ�J˜˜jÕ1Õ1°IÕ=ó?ˆ
Ñà!%ˆÑàÑ+°¨xÕ7ˆÜ "§
¢
Ø�K˜˜zÕ3Õ3°iÕ?ó!Að  Aˆ
Ñà"&ˆÑä—:’:˜jÓ5¸	ÕAˆØ$˜H¬¯ª¨zÓ(:¸hÑ(FÕGÐØ'/Ð0@Õ'Aˆ
Ð#Ñ$Ø*/ˆÐ&Ñ'àÐ$Ð$ó    c                ó†  € \         P                  ! \         P                  4      P                  pRp\         P                  ! V \         P
                  4      '       dC   \         P                  ! V 4      P                  p\         P                  ! V 4      P                  pRp\         P                  ! V\         P
                  4      '       dP   \         P                  ! V4      P                  pV'       d(   VX8  d!   \         P                  ! V4      P                  pVR9   d
   VR,          # VR9   d
   VR,          # \        R4      h)aX  
Calculates relative EPS step to use for a given data type
and numdiff step method.

Progressively smaller steps are used for larger floating point types.

Parameters
----------
f0_dtype: np.dtype
    dtype of function evaluation

x0_dtype: np.dtype
    dtype of parameter vector

method: {'2-point', '3-point', 'cs'}

Returns
-------
EPS: float
    relative step size. May be np.float16, np.float32, np.float64

Notes
-----
The default relative step will be np.float64. However, if x0 or f0 are
smaller floating point types (np.float16, np.float32), then the smallest
floating point type is chosen.
FTr   zBUnknown step method, should be one of {'2-point', '3-point', 'cs'})ú2-pointÚcs)ú3-pointgUUUUUUÕ?)	r   ÚfinfoÚfloat64ÚepsÚ
issubdtypeÚinexactr   ÚitemsizeÚRuntimeError)Úx0_dtypeÚf0_dtypeÚmethodÚEPSÚx0_is_fpÚx0_itemsizeÚf0_itemsizes   &&&    r5   Ú_eps_for_methodrJ   ]   sà   € ô< �(Š(”2—:‘:Ó
×
"Ñ
"€Cà€HÜ	‡}‚}�XœrŸz™z×*Ò*ä�hŠh�xÓ ×$Ñ$ˆÜ—h’h˜xÓ(×1Ñ1ˆØˆä	‡}‚}�XœrŸz™z×*Ò*Ü—h’h˜xÓ(×1Ñ1ˆç˜ kÔ1Ü—(’(˜8Ó$×(Ñ(ˆCàÐ"Ô"Ø�C�xˆØ	�;Ô	Ø�S�zÐäð :ó ;ð 	;r7   c                ó  € V^ 8¬  P                  VP                  4      ^,          ^,
          p\        VP                  VP                  V4      pWT,          \        P                  ! R\        P
                  ! V4      4      ,          P                  VP                  4      pV f   TpV# W,          \        P
                  ! V4      ,          P                  VP                  4      pW,           V,
          p\        P                  ! V^ 8H  VV4      pV# )aº  
Computes an absolute step from a relative step for finite difference
calculation.

Parameters
----------
rel_step: None or array-like
    Relative step for the finite difference calculation
x0 : np.ndarray
    Parameter vector
f0 : np.ndarray or scalar
method : {'2-point', '3-point', 'cs'}

Returns
-------
h : np.array
    The absolute step size, ``h.dtype==x0.dtype``.

Notes
-----
`h` has the same dtype as `x0` because dx is later calculated
as ``(x0 + h) - x0``, and problems would occur if ``x0.dtype==np.float16``
with ``h.dtype==np.float64``.
If `rel_step is None`, then a default relative step is calculated using the
smallest floating point type of `x0` and `f0`, see _eps_for_method.

ç      ð?)Úastyper   rJ   r   r    r   Úwhere)	Úrel_stepr"   Úf0rE   Úsign_x0ÚrstepÚdefault_abs_stepÚabs_stepÚdxs	   &&&&     r5   Ú_compute_absolute_steprV   “   sØ   € ð< �Q‰w×Ñ˜rŸx™xÓ(¨1Õ,¨qÕ0€Gä˜BŸH™H b§h¡h°Ó7€Eà�œ"Ÿ*š* S¬"¯&ª&°«*Ó5Õ5ß�fˆR�X‰XÓð ð ÒØ#ˆð$ €Oð Õ¤§¢¨£Õ+ß
‰&�—‘Ó
ð 	ð �} Õ"ˆÜ—8’8Ø�!‰GØØó
ˆð €Or7   c                óâ   € R V  4       w  r#VP                   ^ 8X  d!   \        P                  ! W!P                  4      pVP                   ^ 8X  d!   \        P                  ! W1P                  4      pW#3# )aA  
Prepares new-style bounds from a two-tuple specifying the lower and upper
limits for values in x0. If a value is not bound then the lower/upper bound
will be expected to be -np.inf/np.inf.

Examples
--------
>>> _prepare_bounds([(0, 1, 2), (1, 2, np.inf)], [0.5, 1.5, 2.5])
(array([0., 1., 2.]), array([ 1.,  2., inf]))
c              3   óZ   "  € T F!  p\         P                  ! V\        R 7      x € K#  	  R# 5i)r   N)r   ÚasarrayÚfloat)Ú.0Úbs   & r5   Ú	<genexpr>Ú"_prepare_bounds.<locals>.<genexpr>Ù   s   é € Ð9±&¨QŒb�jŠj˜¤%×(Ñ(³&ùs   ‚)+)Úndimr   ÚresizeÚshape)Úboundsr"   r&   r'   s   &&  r5   Ú_prepare_boundsrc   Î   sQ   € ñ :±&Ó9�F€BØ	‡w�w�!„|Ü�YŠY�rŸ8™8Ó$ˆà	‡w�w�!„|Ü�YŠY�rŸ8™8Ó$ˆàˆ6€Mr7   c                óÄ  € \        V 4      '       d   \        V 4      p M8\        P                  ! V 4      p V ^ 8g  P	                  \        P
                  4      p V P                  ^8w  d   \        R4      hV P                  w  r#Ve   \        P                  ! V4      '       d2   \        P                  P                  V4      pVP                  V4      pM3\        P                  ! V4      pVP                  V38w  d   \        R4      hV RV3,          p \        V 4      '       d#   \        W#V P                  V P                   4      pM\#        W#V 4      pVP%                  4       WQ&   V# )ai  Group columns of a 2-D matrix for sparse finite differencing [1]_.

Two columns are in the same group if in each row at least one of them
has zero. A greedy sequential algorithm is used to construct groups.

Parameters
----------
A : array_like or sparse array, shape (m, n)
    Matrix of which to group columns.
order : int, iterable of int with shape (n,) or None
    Permutation array which defines the order of columns enumeration.
    If int or None, a random permutation is used with `order` used as
    a random seed. Default is 0, that is use a random permutation but
    guarantee repeatability.

Returns
-------
groups : ndarray of int, shape (n,)
    Contains values from 0 to n_groups-1, where n_groups is the number
    of found groups. Each value ``groups[i]`` is an index of a group to
    which ith column assigned. The procedure was helpful only if
    n_groups is significantly less than n.

References
----------
.. [1] A. Curtis, M. J. D. Powell, and J. Reid, "On the estimation of
       sparse Jacobian matrices", Journal of the Institute of Mathematics
       and its Applications, 13 (1974), pp. 117-120.
z`A` must be 2-dimensional.z`order` has incorrect shape.ºNNN)r   r   r   Ú
atleast_2drM   Úint32r_   r   ra   ÚisscalarÚrandomÚRandomStateÚpermutationrY   r   ÚindicesÚindptrr
   r   )ÚAÚorderÚmÚnÚrngÚgroupss   &&    r5   Úgroup_columnsrt   ã   s  € ô< �‡{‚{Ü�a‹L‰ä�MŠM˜!ÓˆØ�!‰V�O‰OœBŸH™HÓ%ˆà‡v�v�„{ÜÐ5Ó6Ð6à�7‰7�D€Aà‚}œŸš E×*Ò*Ü�i‰i×#Ñ# EÓ*ˆØ—‘ Ó"‰ä—
’
˜5Ó!ˆØ�;‰;˜1˜$ÔÜÐ;Ó<Ð<à	ˆ!ˆUˆ(�€Aä�‡{‚{Ü˜a A§I¡I¨q¯x©xÓ8‰ä˜Q 1Ó%ˆà—K‘K“M€F�Mà€Mr7   r;   Fc                óÎ  € VR9  d   \        RV R24      hRR/p\        V4      p\        P                  ! VP	                  V4      ^VR7      pVP
                  pVP                  VP                  R	4      '       d   VP                  pVP                  VV4      pVP                  ^8”  d   \        R
4      h\        Wa4      w  ppVP                  VP                  8w  g   VP                  VP                  8w  d   \        R4      hV'       dl   \        P                  ! \        P                  ! V4      4      '       d1   \        P                  ! \        P                  ! V4      4      '       g   \        R4      hV
f   / p
\        WWš4      p^ ;ppVf   V! V4      p^pM2\        P                   ! V4      pVP                  ^8”  d   \        R4      h\        P"                  ! VV8  VV8„  ,          4      '       d   \        R4      hV'       d9   Vf"   \%        VP                  VP                  V4      p\'        VVWSV4      w  ppEMõVf   \)        W1WR4      pMÊV^ 8¬  P                  VP                  4      ^,          ^,
          pTpVV,           V,
          p\        P*                  ! V^ 8H  \%        VP                  VP                  V4      V,          \        P,                  ! R\        P.                  ! V4      4      ,          V4      pVP                  VP                  4      pVR8X  d   \1        VV^RVV4      w  ppM$VR8X  d   \1        VV^RVV4      w  ppM	VR8X  d   RpT;'       g    \2        p\5        V4      ;_uu_ 4       pVf   \7        VWVXVV4      w  ppM—\9        V4      '       g   \;        V4      ^8X  d   Vw  ppMTp\=        V4      p\9        V4      '       d   VP?                  4       pM\        P@                  ! V4      p\        P                   ! V4      p\C        VWVXVVVV4	      w  ppRRR4       V'       d   VV,          pVVR&   XV3# X#   + '       g   i     L,; i)u_)  Compute finite difference approximation of the derivatives of a
vector-valued function.

If a function maps from R^n to R^m, its derivatives form m-by-n matrix
called the Jacobian, where an element (i, j) is a partial derivative of
f[i] with respect to x[j].

Parameters
----------
fun : callable
    Function of which to estimate the derivatives. The argument x
    passed to this function is ndarray of shape (n,) (never a scalar
    even if n=1). It must return 1-D array_like of shape (m,) or a scalar.
x0 : array_like of shape (n,) or float
    Point at which to estimate the derivatives. Float will be converted
    to a 1-D array.
method : {'3-point', '2-point', 'cs'}, optional
    Finite difference method to use:
        - '2-point' - use the first order accuracy forward or backward
                      difference.
        - '3-point' - use central difference in interior points and the
                      second order accuracy forward or backward difference
                      near the boundary.
        - 'cs' - use a complex-step finite difference scheme. This assumes
                 that the user function is real-valued and can be
                 analytically continued to the complex plane. Otherwise,
                 produces bogus results.
rel_step : None or array_like, optional
    Relative step size to use. If None (default) the absolute step size is
    computed as ``h = (rel_step * sign(x0) * max(1, abs(x0))).astype(x0.dtype)``,
    with `rel_step` being selected automatically, see Notes. Otherwise
    ``h = (rel_step * sign(x0) * abs(x0)).astype(x0.dtype)``. For
    ``method='3-point'`` the sign of `h` is ignored. The calculated step size
    is possibly adjusted to fit into the bounds.
abs_step : array_like, optional
    Absolute step size to use, possibly adjusted to fit into the bounds.
    For ``method='3-point'`` the sign of `abs_step` is ignored. By default
    relative steps are used, only if ``abs_step is not None`` are absolute
    steps used. `abs_step` is coerced to the dtype of `x0` during calculation.
f0 : None or array_like, optional
    If not None it is assumed to be equal to ``fun(x0)``, in this case
    the ``fun(x0)`` is not called. Default is None.
bounds : tuple of array_like, optional
    Lower and upper bounds on independent variables. Defaults to no bounds.
    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. Bounds checking is not implemented
    when `as_linear_operator` is True.
sparsity : {None, array_like, sparse array, 2-tuple}, optional
    Defines a sparsity structure of the Jacobian matrix. If the Jacobian
    matrix is known to have only few non-zero elements in each row, then
    it's possible to estimate its several columns by a single function
    evaluation [3]_. To perform such economic computations two ingredients
    are required:

    * structure : array_like or sparse array of shape (m, n). A zero
      element means that a corresponding element of the Jacobian
      identically equals to zero.
    * groups : array_like of shape (n,). A column grouping for a given
      sparsity structure, use `group_columns` to obtain it.

    A single array or a sparse array is interpreted as a sparsity
    structure, and groups are computed inside the function. A tuple is
    interpreted as (structure, groups). If None (default), a standard
    dense differencing will be used.

    Note, that sparse differencing makes sense only for large Jacobian
    matrices where each row contains few non-zero elements.
as_linear_operator : bool, optional
    When True the function returns an `scipy.sparse.linalg.LinearOperator`.
    Otherwise it returns a dense array or a sparse array depending on
    `sparsity`. The linear operator provides an efficient way of computing
    ``J.dot(p)`` for any vector ``p`` of shape (n,), but does not allow
    direct access to individual elements of the matrix. By default
    `as_linear_operator` is False.
args, kwargs : tuple and dict, optional
    Additional arguments passed to `fun`. Both empty by default.
    The calling signature is ``fun(x, *args, **kwargs)``.
full_output : bool, optional
    If True then the function also returns a dictionary with extra information
    about the calculation.
workers : int or map-like callable, optional
    Supply a map-like callable, such as
    `multiprocessing.Pool.map` for evaluating the population in parallel.
    This evaluation is carried out as ``workers(fun, iterable)``.
    Alternatively, if `workers` is an int the task is subdivided into `workers`
    sections and the fun 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.

Returns
-------
J : {ndarray, sparse array, LinearOperator}
    Finite difference approximation of the Jacobian matrix.
    If `as_linear_operator` is True returns a LinearOperator
    with shape (m, n). Otherwise it returns a dense array or sparse
    array depending on how `sparsity` is defined. If `sparsity`
    is None then an ndarray with shape (m, n) is returned. If
    `sparsity` is not None returns a csr_array or csr_matrix with
    shape (m, n) following the array/matrix type of the incoming structure.
    For sparse arrays and linear operators it is always returned as
    a 2-D structure. For ndarrays, if m=1 it is returned
    as a 1-D gradient array with shape (n,).
    `J.dtype` is the promoted type of ``fun(x0)`` and `x0`.

info_dict : dict
    Dictionary containing extra information about the calculation. The
    keys include:

    - `nfev`, number of function evaluations. If `as_linear_operator` is True
       then `fun` is expected to track the number of evaluations itself.
       This is because multiple calls may be made to the linear operator which
       are not trackable here.

See Also
--------
check_derivative : Check correctness of a function computing derivatives.

Notes
-----
If `rel_step` is not provided, it is assigned as ``EPS**(1/s)``, where EPS is
determined from the smallest floating point dtype of `x0` or ``fun(x0)``,
``np.finfo(x0.dtype).eps``, s=2 for '2-point' method and
s=3 for '3-point' method. This relative step approximately minimizes a sum
of truncation and round-off errors, see [1]_. Relative steps are used by
default. However, absolute steps are used when ``abs_step is not None``.
If any of the absolute or relative steps produces an indistinguishable
difference from the original `x0`, ``(x0 + dx) - x0 == 0``, then a
automatic step size is substituted for that particular entry.
The calculated absolute step size, `h`, is coerced to have the same dtype as `x0`.

The floating point precision of `J` is the promoted type of ``fun(x0)``
and `x0`. If `rel_step` and `abs_step` are None, then the overall accuracy of
`J` depends on the smallest floating point dtype of `x0` and ``fun(x0)``,
as that dtype is used to calculate the default step size.

A finite difference scheme for '3-point' method is selected automatically.
The well-known central difference scheme is used for points sufficiently
far from the boundary, and 3-point forward or backward scheme is used for
points near the boundary. Both schemes have the second-order accuracy in
terms of Taylor expansion. Refer to [2]_ for the formulas of 3-point
forward and backward difference schemes.

For dense differencing when m=1 Jacobian is returned with a shape (n,),
on the other hand when n=1 Jacobian is returned with a shape (m, 1).
Our motivation is the following: a) It handles a case of gradient
computation (m=1) in a conventional way. b) It clearly separates these two
different cases. b) In all cases np.atleast_2d can be called to get 2-D
Jacobian with correct dimensions.

References
----------
.. [1] W. H. Press et. al. "Numerical Recipes. The Art of Scientific
       Computing. 3rd edition", sec. 5.7.

.. [2] A. Curtis, M. J. D. Powell, and J. Reid, "On the estimation of
       sparse Jacobian matrices", Journal of the Institute of Mathematics
       and its Applications, 13 (1974), pp. 117-120.

.. [3] B. Fornberg, "Generation of Finite Difference Formulas on
       Arbitrarily Spaced Grids", Mathematics of Computation 51, 1988.

Examples
--------
>>> import numpy as np
>>> from scipy.optimize._numdiff import approx_derivative
>>>
>>> def f(x, c1, c2):
...     return np.array([x[0] * np.sin(c1 * x[1]),
...                      x[0] * np.cos(c2 * x[1])])
...
>>> x0 = np.array([1.0, 0.5 * np.pi])
>>> approx_derivative(f, x0, args=(1, 2))
array([[ 1.,  0.],
       [-1.,  0.]])

Bounds can be used to limit the region of function evaluation.
In the example below we compute left and right derivative at point 1.0.

>>> def g(x):
...     return x**2 if x >= 1 else x
...
>>> x0 = 1.0
>>> approx_derivative(g, x0, bounds=(-np.inf, 1.0))
array([ 1.])
>>> approx_derivative(g, x0, bounds=(1.0, np.inf))
array([ 2.])

We can also parallelize the derivative calculation using the workers
keyword.

>>> from multiprocessing import Pool
>>> import time
>>> def fun2(x):       # import from an external file for use with multiprocessing
...     time.sleep(0.002)
...     return rosen(x)

>>> rng = np.random.default_rng()
>>> x0 = rng.uniform(high=10, size=(2000,))
>>> f0 = rosen(x0)

>>> %timeit approx_derivative(fun2, x0, f0=f0)     # may vary
10.5 s Â± 5.91 ms per loop (mean Â± std. dev. of 7 runs, 1 loop each)

>>> elapsed = []
>>> with Pool() as workers:
...     for i in range(10):
...         t = time.perf_counter()
...         approx_derivative(fun2, x0, workers=workers.map, f0=f0)
...         et = time.perf_counter()
...         elapsed.append(et - t)
>>> np.mean(elapsed)    # may vary
np.float64(1.442545195999901)

Create a map-like vectorized version. `x` is a generator, so first of all
a 2-D array, `xx`, is reconstituted. Here `xx` has shape `(Y, N)` where `Y`
is the number of function evaluations to perform and `N` is the dimensionality
of the objective function. The underlying objective function is `rosen`, which
requires `xx` to have shape `(N, Y)`, so a transpose is required.

>>> def fun(f, x, *args, **kwds):
...     xx = np.r_[[xs for xs in x]]
...     return f(xx.T)
>>> %timeit approx_derivative(fun2, x0, workers=fun, f0=f0)    # may vary
91.8 ms Â± 755 Î¼s per loop (mean Â± std. dev. of 7 runs, 10 loops each)

r9   r;   r:   zUnknown method 'z'. ÚnfevN)r_   Úxpúreal floatingz#`x0` must have at most 1 dimension.z,Inconsistent shapes between bounds and `x0`.z7Bounds not supported when `as_linear_operator` is True.z&`f0` passed has more than 1 dimension.z `x0` violates bound constraints.rL   r   r   F)r9   r;   r:   )"r   r   ÚxpxÚ
atleast_ndrY   r=   Úisdtyper   rM   r_   rc   ra   r   r   ÚisinfÚ_Fun_WrapperÚ
atleast_1dÚanyrJ   Ú_linear_operator_differencerV   rN   r    r   r6   Úmapr   Ú_dense_differencer   Úlenrt   Útocscrf   Ú_sparse_difference)Úfunr"   rE   rO   rT   rP   rb   ÚsparsityÚas_linear_operatorÚargsÚkwargsÚfull_outputÚworkersÚ	info_dictrw   Ú_xÚ_dtyper&   r'   Úfun_wrappedrv   Ú_nfevÚJÚ_r#   rQ   rU   r(   ÚmfÚ	structurers   s   &&&&&&&&&&&&&                  r5   Úapprox_derivativer–      sÎ  € ðT Ð1Ô1ÜÐ+¨F¨8°3Ð7Ó8Ð8à˜�€Iä	˜Ó	€BÜ	�Š˜Ÿ
™
 2›¨Q°2Ô	6€BØ�Z‰Z€FØ	‡z�z�"—(‘(˜O×,Ò,Ø—‘ˆð 
�‰�2�vÓ	€Bà	‡w�w�„{ÜÐ>Ó?Ð?ä˜VÓ(�F€Bˆà	‡x�x�2—8‘8Ô˜rŸx™x¨2¯8©8Ô3ÜÐGÓHÐHç¤2§6¢6¬"¯(ª(°2«,×#7Ò#7Ü')§v¢v¬b¯hªh°r«l×';Ò';Üð 9ó :ð 	:ð ‚~Øˆä˜s¨Ó5€Kð Ð€Dˆ5à	‚zÙ˜‹_ˆØ‰ä�]Š]˜2ÓˆØ�7‰7�QŒ;ÜÐEÓFÐFä	‡v‚vˆr�B‰w˜2 ™7Õ#×$Ò$ÜÐ;Ó<Ð<çØÒÜ& r§x¡x°·±¸6ÓBˆHä*¨;¸Ø+-¸óA‰ˆŠ1ð ÒÜ& x°RÓ@‰Að ˜Q‘w×&Ñ& r§x¡xÓ0°1Õ4°qÕ8ˆGØˆAð ˜•6˜R•-ˆBÜ—’˜˜q™Ü(¨¯©°2·8±8¸VÓDØ õ!Ü#%§:¢:¨c´2·6²6¸"³:Ó#>õ?àóˆAð —‘˜Ÿ™Ó"ˆAà�YÔÜ7Ø�A�q˜) R¨ó -ÑˆA‰}à�yÔ Ü7Ø�A�q˜) R¨ó -ÑˆA‰}à�tŒ^Ø!ˆMð —.�.œSˆÜ˜× Ô  BØÒÜ,¨[¸"À!Ø)6¸Ø)+ó-‘�‘5ô   ×)Ò)¬c°(«m¸qÔ.@Ø(0Ñ%�I™và (�IÜ*¨8Ó4�Fä˜I×&Ò&Ø )§¡Ó 1‘Iä "§¢¨iÓ 8�IÜŸš vÓ.�Ü-¨k¸2À1Ø-:¸IØ-3°V¸RóA‘��5÷# !÷* Ø��ˆØ ˆ	�&ÑØ�)ˆ|Ðàˆ÷5 !× ús   Î B0QÑQ$	c                 ó  a aaaaa€ SP                   oSP                   p\        SSR \        R7      oVR8X  d   VV VVVV3R lpM/VR8X  d   V VVVV3R lpMVR8X  d   V VVVV3R lpM\        R4      h\	        SV3VSR	7      ^ 3# )
T©Úforce_floatingrw   r9   c                 ó  <€ \         P                  ! V \         P                  ! V 4      4      '       d   \         P                  ! SSR 7      # S\	        V 4      ,          pS	W,          ,           pS! V4      S,
          pW1,          # ©r   ©r   Úarray_equalr   Úzerosr   )
ÚprU   r-   ÚdfrP   r†   r#   rp   Úresult_dtyper"   s
   &   €€€€€€r5   ÚmatvecÚ+_linear_operator_difference.<locals>.matvec†  sZ   ø€ Ü�~Š~˜a¤§¢¨qÓ!1×2Ò2Ü—x’x ¨Ô6Ð6Ø”T˜!“W•ˆBØ�R•T•	ˆAÙ�Q“˜"•ˆBØ•7ˆNr7   r;   c                 ód  <€ \         P                  ! V \         P                  ! V 4      4      '       d   \         P                  ! S	S
R 7      # ^S,          \	        V 4      ,          pSV^,          V ,          ,
          pSV^,          V ,          ,           pS! V4      pS! V4      pWT,
          pWa,          # r›   rœ   )rŸ   rU   Úx1Úx2Úf1Úf2r    r†   r#   rp   r¡   r"   s   &      €€€€€r5   r¢   r£   �  s�   ø€ Ü�~Š~˜a¤§¢¨qÓ!1×2Ò2Ü—x’x ¨Ô6Ð6Ø�1•”t˜A“w•ˆBØ�r˜!•t˜Q•h•ˆBØ�r˜!•t˜Q•h•ˆBÙ�R“ˆBÙ�R“ˆBØ•ˆBØ•7ˆNr7   r:   c                 ó  <€ \         P                  ! V \         P                  ! V 4      4      '       d   \         P                  ! SSR 7      # S\	        V 4      ,          pS	W,          R,          ,           pS! V4      pVP
                  pWA,          # )r   ù              ð?)r   r�   r   rž   r   Úimag)
rŸ   rU   r-   r§   r    r†   r#   rp   r¡   r"   s
   &    €€€€€r5   r¢   r£   �  sc   ø€ Ü�~Š~˜a¤§¢¨qÓ!1×2Ò2Ü—x’x ¨Ô6Ð6Ø”T˜!“W•ˆBØ�R•T˜#•X•ˆAÙ�Q“ˆBØ—‘ˆBØ•7ˆNr7   úNever be here.)ra   r¢   r   )Úsizer   r   rB   r   )	r†   r"   rP   r#   rE   rq   r¢   rp   r¡   s	   ffff&  @@r5   r€   r€   ~  s„   ý€ Ø
�‰€AØ
�‰€Aä! " b¸Ä"ÔE€Là�Ô÷	ó 	ð 
�9Ô	÷		ò 		ð 
�4Œ÷	ò 	ô Ð+Ó,Ð,ä  A ¨v¸\ÔJÈAÐMÐMr7   c           
      ó>  a€ VP                   pVP                   o^ p\        WR\        R7      p	\        P                  ! SV3V	R7      p
VR8X  d¡   V3R lpV! W! W4      4      p\	        S4       Uu. uF%  qÑV,          W=,          ,           W,          ,
          NK'  	  ppV Uu. uF  qÿV,
          NK  	  pp\        VV4       UUu. uF  w  ppVV,          NK  	  pppV\        V4      ,          pEMÐVR8X  EdV   R p\        V! V V! WV4      4      4      pV! WV4      p\        4       p\        4       p\        V4       FÌ  w  pp\        V4      p\        V4      p\        V4      p\        V4      pV'       dU   VP                  VV,          W,          ,
          4       VP                  RV,          ^V,          ,           V,
          4       KŽ  VP                  VV,          VV,          ,
          4       VP                  VV,
          4       KÎ  	  \        VV4       UUu. uF  w  ppVV,          NK  	  pppV^\        V4      ,          ,          pMsVR8X  db   V3R	 lp\        V! V V! W4      4      4      p\        WÃ4       UUu. uF  w  ppVP                  V,          NK  	  pppV\        V4      ,          pM\        R
4      h\        V4       F
  w  ppVW­&   K  	  V^8X  d   \        P                  ! V
4      p
V
P                  V3# u upi u upi u uppi u uppi u uppi )é    Tr˜   r   r9   c              3   ó–   <"  € \        S4       F4  p\        P                  ! V 4      pW,          W,          ,           W2&   Vx € K6  	  R # 5i©N)Úranger   r   )r"   r#   Úir¥   rq   s   &&  €r5   Úx_generator2Ú'_dense_difference.<locals>.x_generator2¶  s7   øé € Ü˜1–X�ô —W’W˜R“[�Ø� ¥��‘Ø”ó ùs   ƒAA	r;   c              3   óv  "  € \        V4       F¥  w  r4\        P                  ! V 4      p\        P                  ! V 4      pV'       d7   W,          W,          ,           WS&   W,          ^W,          ,          ,           Wc&   M.W,          W,          ,
          WS&   W,          W,          ,           Wc&   Vx € Vx € K§  	  R# 5i©é   N)Ú	enumerater   r   )r"   r#   r(   r³   Ú	one_sidedr¥   r¦   s   &&&    r5   Úx_generator3Ú'_dense_difference.<locals>.x_generator3Ë  s}   é € Ü )¨-Ö 8‘�Ü—W’W˜R“[�Ü—W’W˜R“[�ßØ�E A¥D�L�B‘EØ�E A a¥d¥F�N�B’Eà�E A¥D�L�B‘EØ�E A¥D�L�B‘EØ’Ø”ó !9ùs   ‚B7B9r:   c              3   ó¬   <"  € \        S4       F?  pV P                  \        R R7      pW2;;,          W,          R,          ,          uu&   Vx € KA  	  R# 5i)T©r   rª   N)r²   rM   Úcomplex)r"   r#   r³   Úxcrq   s   &&  €r5   Úx_generator_csÚ)_dense_difference.<locals>.x_generator_csí  s=   øé € Ü˜1–X�Ø—Y‘Yœw¨T�YÓ2�Ø—˜� �Õ#“Ø”ó ùs   ƒAAr¬   g      À)r­   r   r   Úemptyr²   Úziprƒ   ÚiterÚlistr¹   ÚnextÚappendr«   rB   ÚravelÚT)r†   r"   rP   r#   r(   rE   rŒ   rp   rv   Úresult_typeÚJ_transposedr´   Úf_evalsr³   rU   Úf_evalr    ÚdelfÚdelxÚdf_dxr»   Úgenrº   ÚlÚur§   r¨   rÁ   ÚhiÚvrq   s   &&&&&&&                       @r5   r‚   r‚   «  sœ  ø€ Ø
�‰€AØ
�‰€AØ€Dô ! ¸ÄÔD€Kä—8’8˜Q ˜F¨+Ô6€Là�Ôõ
	ñ ˜#˜|¨BÓ2Ó3ˆÜ.3°A¬hÓ7©h¨�!�u�q•t�|˜r�u×$Ð$©hˆÐ7Ù(/Ó0©˜f�r�kˆk©ˆÐ0Ü/2°2°r¬{Ô;©{¡  t�˜—�©{ˆÑ;Ø”�E“
ÕŠà	�9Õ	ò	ô ‘w˜s¡L°¸Ó$FÓGÓHˆÙ˜2 -Ó0ˆÜ‹VˆÜ‹VˆÜ% mÖ4‰LˆAˆyÜ�S“	ˆAÜ�S“	ˆAä�g“ˆBÜ�g“ˆBßØ—	‘	˜!˜A�$ ¥�,Ô'Ø—	‘	˜$ �) a¨"¥fÕ,¨rÕ1Ö2à—	‘	˜!˜A�$  1¥�+Ô&Ø—	‘	˜"˜r�'Ö"ñ 5ô 03°2°r¬{Ô;©{¡  t�˜—�©{ˆÑ;Ø�”C˜“J•Õ‰Ø	�4Œõ	ô ‘w˜s¡N°2Ó$9Ó:Ó;ˆÜ,/°¬OÔ<©O¡& " b�—‘˜2—�©OˆÑ<Ø”�E“
Õ‰äÐ+Ó,Ð,ä˜%Ö ‰ˆˆ1Øˆ‹ñ !ð 	ˆA„vÜ—x’x Ó-ˆà�>‰>˜4ÐÐùòu 8ùÚ0ùÛ;ùóF <ùó =s   Á0+LÂ!LÃLÈLÉ?Lc	                 ó0  aaaaa$a%€ VP                   p	SP                   p
. p. p. p\        SVR \        R7      p\        P                  ! S4      ^,           o%^ pVV%3R lo$V$VV3R lpV$VVV3R lpV$VV3R lpVR8X  d    \	        V! V V! 4       4      4      pV! 4       pMDVR8X  d    \	        V! V V! 4       4      4      pV! 4       pMVR8X  d   \	        V! V V! 4       4      4      pS$! 4        EF+  p\        P
                  ! V4      w  p\        VR	V3,          4      w  pppVV,          pVR8X  d0   \        X4      S,
          p\        X4      V,
          pV^,          pEMpVR8X  Ed-   \        X4      p\        V4      pSV,          pS( V,          p\        P                  ! V
SP                  R
7      pVV,          SV,          ,
          VV&   VV,          VV,          ,
          VV&   \        X4      p \        V4      p!V^,          pSV,          p"\        P                  ! W’P                  R
7      pVV",          p#RVV#,          ,          ^V V#,          ,          ,           V!V#,          ,
          VV#&   VV"( ,          p#V!V#,          V V#,          ,
          VV#&   M<VR8X  d+   \        X4      p V^,          pV P                  pSV,          pM\        R4      hVP                  V4       VP                  V4       VP                  VV,          VV,          ,          4       EK.  	  \        P                  ! V4      p\        P                  ! V4      p\        P                  ! V4      p\        V\         4      '       d   \#        WÛV33Wš3VR7      V3# \%        WÛV33Wš3VR7      V3# )Tr˜   c               3   óf   <"  € \        S4       F  p \        P                  ! V S4      x € K  	  R # 5ir±   )r²   r   Úequal)Úgrouprs   Ún_groupss    €€r5   Úe_generatorÚ'_sparse_difference.<locals>.e_generator  s%   øé € ä˜8–_ˆEÜ—(’(˜5 &Ó)Ô)ó %ùó   ƒ.1c               3   ó\   <"  € S! 4       p V  F  pSV,          pSV,           pVx € K  	  R # 5ir±   © )Úe_genÚeÚh_vecr-   rÜ   r#   r"   s       €€€r5   r´   Ú(_sparse_difference.<locals>.x_generator2  s.   øé € Ù“ˆÛˆAØ˜•EˆEØ�U•
ˆAØŒGó ùs   ƒ),c               3   ó–  <"  € S! 4       p V  F¶  pSV,          pS
P                  4       pS
P                  4       pS	V,          pW5;;,          W%,          ,          uu&   WE;;,          ^W%,          ,          ,          uu&   S	( V,          pW6;;,          W&,          ,          uu&   WF;;,          W&,          ,          uu&   Vx € Vx € K¸  	  R# 5ir·   r¾   )rá   râ   rã   r¥   r¦   Úmask_1Úmask_2rÜ   r#   r(   r"   s          €€€€r5   r»   Ú(_sparse_difference.<locals>.x_generator3  s“   øé € Ù“ˆÛˆAØ˜•EˆEØ—‘“ˆBØ—‘“ˆBà" QÕ&ˆFØ�J˜%�-Õ'‹JØ�J˜!˜e�mÕ+Õ+‹Jà#�^ aÕ'ˆFØ�J˜%�-Õ'‹JØ�J˜%�-Õ'‹JØŠHØŒHó ùs   ƒCC	c               3   óf   <"  € S! 4       p V  F  pSV,          pSVR ,          ,           x € K   	  R# 5i)rª   Nrà   )rá   râ   rã   rÜ   r#   r"   s      €€€r5   rÁ   Ú*_sparse_difference.<locals>.x_generator_cs+  s/   øé € Ù“ˆÛˆAØ˜•EˆEØ�u˜s•{Õ"Ô"ó ùrÞ   r9   r;   r:   re   r   r¬   )ra   r   éýÿÿÿ)r­   r   r   ÚmaxrÅ   Únonzeror   rÇ   rž   r   rÃ   r«   r   rÈ   ÚhstackÚ
isinstancer   r	   r   )&r†   r"   rP   r#   r(   r•   rs   rE   rŒ   rp   rq   Úrow_indicesÚcol_indicesÚ	fractionsrË   rv   r´   r»   rÁ   rÍ   Úxsrâ   Úcolsr³   Újr“   rU   r    r¥   r¦   ræ   rç   r§   r¨   ÚmaskÚrowsrÜ   rÛ   s&   &f&ff&f&&                           @@r5   r…   r…     s*  ý€ à
�‰€AØ
�‰€AØ€KØ€KØ€IÜ   R¸ÄÔD€Kä�vŠv�f‹~ Õ!€HØ€Dö*÷
÷ð ÷"#ð �ÔÜ‘w˜s¡L£NÓ3Ó4ˆÙ‹^‰Ø	�9Ô	Ü‘w˜s¡L£NÓ3Ó4ˆÙ‹^‰Ø	�4ŒÜ‘w˜s¡NÓ$4Ó5Ó6ˆá�]ˆô —
’
˜1“‰ˆä�y  D Õ)Ó*‰ˆˆ1ˆaà��Gˆà�YÔÜ�b“˜B•ˆBÜ�g“ Õ#ˆBØ�A�IŠDØ�yÕ ô �b“ˆBÜ�b“ˆBà" QÕ&ˆFØ#�^ aÕ'ˆFä—’˜! 2§8¡8Ô,ˆBØ˜F� b¨¥jÕ0ˆBˆv‰JØ˜F� b¨¥jÕ0ˆBˆv‰Jä�g“ˆBÜ�g“ˆBØ�A�IˆDà  Õ#ˆDÜ—’˜!§8¡8Ô,ˆBà�T•7ˆDØ˜B˜t�H•} q¨2¨d­8¥|Õ3°b¸µhÕ>ˆBˆt‰Hà�d�U•8ˆDØ˜$•x " T¥(Õ*ˆBˆtŠHØ�tŒ^Ü�g“ˆBØ�A�IˆDØ—‘ˆBØ�Q•‰BäÐ-Ó.Ð.ð 	×Ñ˜1ÔØ×Ñ˜1ÔØ×Ñ˜˜A�  A¥�×'ñe ôh —)’)˜KÓ(€KÜ—)’)˜KÓ(€KÜ—	’	˜)Ó$€Iä�)œX×&Ò&ÜØ kÐ2Ð3Ø�&Øô
ð ð	ð 	ô
 Ø	 +Ð.Ð/ØˆfØôð ð	ð r7   c                   ó,   a € ] tR tRt o R tR tRtV tR# )r}   i€  c                ó6   € Wn         W n        W0n        W@n        R # r±   )r†   r"   r‰   rŠ   )Úselfr†   r"   r‰   rŠ   s   &&&&&r5   Ú__init__Ú_Fun_Wrapper.__init__‚  s   € ØŒØŒØŒ	ØŽr7   c                ór  € \        V P                  4      pVP                  VP                  R 4      '       d&   VP	                  WP                  P                  4      p\
        P                  ! V P                  ! V.V P                  O5/ V P                  B 4      pVP                  ^8”  d   \        R4      hV# )rx   z-`fun` return value has more than 1 dimension.)r   r"   r{   r   rM   r   r~   r†   r‰   rŠ   r_   rB   )rú   r-   rw   Úfs   &&  r5   Ú__call__Ú_Fun_Wrapper.__call__ˆ  sˆ   € ô ˜TŸW™WÓ%ˆà�:‰:�a—g‘g˜×/Ò/Ø—	‘	˜!ŸW™WŸ]™]Ó+ˆAä�MŠM˜$Ÿ(š( 1Ð@ t§y¡yÒ@°D·K±KÑ@ÓAˆØ�6‰6�AŒ:Üð  8ó 9ð 9àˆr7   )r‰   r†   rŠ   r"   N)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__rû   rÿ   Ú__static_attributes__Ú__classdictcell__)Ú__classdict__s   @r5   r}   r}   €  s   ø‡ € ò÷ð r7   r}   c           	     ó’  € Vf   / pV! V.VO5/ VB p\        V4      '       d¶   \        WW6WER7      p\        V4      pWg,
          p\        V4      w  ršp\        P
                  ! WyV
3,          4      P                  4       p\        P                  ! \        P                  ! V4      \        P                  ! ^\        P                  ! V4      4      ,          4      # \        WVWER7      p\        P                  ! Wg,
          4      p\        P                  ! V\        P                  ! ^\        P                  ! V4      4      ,          4      # )a{  Check correctness of a function computing derivatives (Jacobian or
gradient) by comparison with a finite difference approximation.

Parameters
----------
fun : callable
    Function of which to estimate the derivatives. The argument x
    passed to this function is ndarray of shape (n,) (never a scalar
    even if n=1). It must return 1-D array_like of shape (m,) or a scalar.
jac : callable
    Function which computes Jacobian matrix of `fun`. It must work with
    argument x the same way as `fun`. The return value must be array_like
    or sparse array with an appropriate shape.
x0 : array_like of shape (n,) or float
    Point at which to estimate the derivatives. Float will be converted
    to 1-D array.
bounds : 2-tuple of array_like, optional
    Lower and upper bounds on independent variables. Defaults to no bounds.
    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.
args, kwargs : tuple and dict, optional
    Additional arguments passed to `fun` and `jac`. Both empty by default.
    The calling signature is ``fun(x, *args, **kwargs)`` and the same
    for `jac`.

Returns
-------
accuracy : float
    The maximum among all relative errors for elements with absolute values
    higher than 1 and absolute errors for elements with absolute values
    less or equal than 1. If `accuracy` is on the order of 1e-6 or lower,
    then it is likely that your `jac` implementation is correct.

See Also
--------
approx_derivative : Compute finite difference approximation of derivative.

Examples
--------
>>> import numpy as np
>>> from scipy.optimize._numdiff import check_derivative
>>>
>>>
>>> def f(x, c1, c2):
...     return np.array([x[0] * np.sin(c1 * x[1]),
...                      x[0] * np.cos(c2 * x[1])])
...
>>> def jac(x, c1, c2):
...     return np.array([
...         [np.sin(c1 * x[1]),  c1 * x[0] * np.cos(c1 * x[1])],
...         [np.cos(c2 * x[1]), -c2 * x[0] * np.sin(c2 * x[1])]
...     ])
...
>>>
>>> x0 = np.array([1.0, 0.5 * np.pi])
>>> check_derivative(f, jac, x0, args=(1, 2))
2.4492935982947064e-16
)rb   r‡   r‰   rŠ   )rb   r‰   rŠ   )
r   r–   r   r   r   rY   rÉ   rì   r   r    )r†   Újacr"   rb   r‰   rŠ   Ú	J_to_testÚJ_diffÚabs_errr³   rõ   Úabs_err_dataÚJ_diff_datas   &&&&&&       r5   Úcheck_derivativer  —  sÿ   € ðz ‚~ØˆÙ�BÐ(˜Ò( Ñ(€IÜ�	×ÒÜ" 3°6Ø(,ô=ˆä˜iÓ(ˆ	ØÕ$ˆÜ! '›]ÑˆˆlÜ—j’j ¨1¨¥Ó.×4Ñ4Ó6ˆÜ�vŠv”b—f’f˜\Ó*Ü—j’j ¤B§F¢F¨;Ó$7Ó8õ9ó :ð 	:ô # 3°6Ø(,ô=ˆä—&’&˜Õ+Ó,ˆÜ�vŠv�g¤§
¢
¨1¬b¯fªf°V«nÓ =Õ=Ó>Ð>r7   )r¯   rà   )'Ú__doc__Ú	functoolsÚnumpyr   Únumpy.linalgr   Úscipy.sparse.linalgr   Úsparser   r   r   r   r   r	   Ú_group_columnsr
   r   Úscipy._lib._array_apir   r   Úscipy._lib._utilr   Úscipy._externalr   ry   r6   Ú	lru_cacherJ   rV   rc   rt   r   r–   r€   r‚   r…   r}   r  rà   r7   r5   Ú<module>r     sÉ   ðÙ -Û Û Ý å .ß O× Oß 5ß AÝ 'Ý 2òL%ð^ ×Ññ2;ó ð2;òj8òvô*:ðz '0¸$ÈØ¨¯© w°·±Ð&7À$Ø).°RÀØ"'°ô[ò|
*NòZT òn{÷|ñ ð. -/¯F©F¨7°B·F±FÐ*;À"Ø öM?r7   