+
    nDjo                     |   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W8  W8  ,          p\         P                  ! V4      \         P                  ! W4      8*  pWV,          ;;,          R,          uu&   W8  V( ,          pW,          V,          W&   W8  V( ,          pW,          ) V,          W&   W3# VR8X  d   W8  W8  ,          pW8  V( ,          p\         P                  ! W,          RW,          ,          V,          4      W&   RWn&   W8  V( ,          p\         P                  ! W,          RW,          ,          V,          4      ) W&   RWo&   \         P                  ! W4      V,          pV( \         P                  ! V4      V8*  ,          pVV,          VV&   RVV&   W3# )	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   &&&&&&             Q/data/cameron/venvs/s3viz/lib/python3.14/site-packages/scipy/optimize/_numdiff.py_adjust_scheme_to_boundsr6      s   > Qd3	9	FF1Iat4CDD	vvrbffW}rvv.//mGJJJLFqv&&&/RZZ
%GGg%&",&+x7(1I=
+x7 * 44y@
& $$% 
9	(Z-BC+x7 jjJj11I=?
!%+x7 "

Kz33i?!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	}}Xrzz**hhx $$hhx(11	}}Xrzz**hhx(11k1((8$((C""Cx	;	Sz : ; 	;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    < Qwrxx(1,q0GBHHbhh7E"**S"&&*55fRXX  #$ O +
&
 	 }"88!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&Qbjj%((&s   )+)ndimr   resizeshape)boundsr"   r&   r'   s   &&  r5   _prepare_boundsrc      sQ     :&9FB	ww!|YYr88$	ww!|YYr88$6Mr7   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   < {{aLMM!!VOOBHH%vv{56677DA}E**ii##E*"

5!;;1$;<<	!U(A{{aAIIqxx8Q1%KKMFM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W4      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MVf   \)        W1WR4      pMV^ 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+F83788I		B	

2Q2	6BZZF	zz"((O,, 
2v	B	ww{>??V(FB	xx288rxx2883GHH266"((2,#7#7')vvbhhrl';'; 9 : 	: ~s5K D5	z_]]277Q;EFF	vvrBw27#$$;<<&rxx6BH*;+-A1 &xR@A Qw&&rxx014q8GA 6R-Bq(288VD !#%::c266":#>?A "AY7Aq)R -A}y 7Aq)R -A}t^!M ..S  B,["!)6)+-5  ))c(mq.@(0%Iv (I*84FI&& ) 1I "i 8Iv.-k21-:I-3VR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xx66T!WBRT	AQ"B7Nr7   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xx661tAwBr!tQhBr!tQhBRBRBB7Nr7   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xx66T!WBRT#XAQBB7Nr7   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"EL	 	 
9			 		 
4	 	 +,,Av\JA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%  qV,          W=,          ,           W,          ,
          NK'  	  ppV Uu. uF  qV,
          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MV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\        W4       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     1X WWR[ 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WWR[WWR[EADLBEEAadFNBEEADLBEEADLBE !9s   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=     1XYYwTY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 !DK88QF+6L
	 #|B23.3Ah7h!uqt|ru$$h7(/0frkk0/22r{;{t{;E
	9		 wsL$FGH2-0VV%m4LAyS	AS	AgBgB		!A$,'		$)a"f,r12		!A$1+&		"r'" 5 032r{;{t{;CJ	4	 wsN2$9:;,/O<O&"b2O<E
+,,% 1 ! 	Avxx->>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                  ! WP                  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   \#        WV33W3VR7      V3# \%        WV33W3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EE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EEBB"Q&FJ%-'JJ!em++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EEus{"" 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DKvvf~!HD*
 "# wsLN34^	9	wsLN34^	4wsN$456] 

1yD)*1aGYbBBg#BAIDy  bBbB"Q&F#^a'F!288,BFbj0BvJFbj0BvJgBgBAID #D!88,BT7DBtH}q2d8|3bh>BtHdU8D$x"T(*BtHt^gBAID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     TWW%::agg//		!WW]]+AMM$((1@tyy@DKK@A66A:  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  rp\        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	"36(,=i(	$!']ljj1.446vvbff\*jjBFF;$789 : 	: #36(,=&&+,vvg

1bffVn ==>>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{| . -/FF7BFF*;" M?r7   