
    (ph=                         S SK Jr  S SKJr  S SKrS SKJrJr  \R                  " \
5      R                  rS rS rS rS rS	 rS
 r " S S5      r\S-  r\S-  r\S-  rS\-  rSrSrSS jrS rS rg)    )groupby)warnN)find
coo_matrixc                 v    U S::  a  [        S5      eU [        R                  " X!-
  5      :  a  [        S5      eU $ )z.Assert that first_step is valid and return it.r   z`first_step` must be positive.z`first_step` exceeds bounds.)
ValueErrornpabs)
first_stept0t_bounds      N/var/www/html/venv/lib/python3.13/site-packages/scipy/integrate/_ivp/common.pyvalidate_first_stepr   
   s:    Q9::BFF7<((788    c                 (    U S::  a  [        S5      eU $ )z,Assert that max_Step is valid and return it.r   z`max_step` must be positive.)r   )max_steps    r   validate_max_stepr      s    1}788Or   c                 \    U (       a%  [        SSR                  S U  5       5       S3SS9  gg)aE  Display a warning for extraneous keyword arguments.

The initializer of each solver class is expected to collect keyword
arguments that it doesn't understand and warn about them. This function
prints a warning for each key in the supplied dictionary.

Parameters
----------
extraneous : dict
    Extraneous keyword arguments
z<The following arguments have no effect for a chosen solver: z, c              3   .   #    U  H  nS U S 3v   M     g7f)`N ).0xs     r   	<genexpr>"warn_extraneous.<locals>.<genexpr>(   s     7JqAaS(Js   .   
stacklevelN)r   join)
extraneouss    r   warn_extraneousr"      s6     K		7J778;	 r   c                    [         R                  " U S[        -  :  5      (       a3  [        SS[        -   S3SS9  [         R                  " U S[        -  5      n [         R
                  " U5      nUR                  S:  a  UR                  U4:w  a  [        S5      e[         R                  " US:  5      (       a  [        S5      eX4$ )	zValidate tolerance values.d   zNAt least one element of `rtol` is too small. Setting `rtol = np.maximum(rtol, z)`.r   r   r   z`atol` has wrong shape.z`atol` must be positive.)	r	   anyEPSr   maximumasarrayndimshaper   )rtolatolns      r   validate_tolr.   ,   s     
vvdS3Y 114s3@	 zz$c	*::dDyy1}t+233	vvdQh344:r   c                 `    [         R                  R                  U 5      U R                  S-  -  $ )zCompute RMS norm.      ?)r	   linalgnormsize)r   s    r   r2   r2   ?   s"    99>>!qvv},,r   c
                    UR                   S:X  a  [        R                  $ [        X1-
  5      n
U
S:X  a  gU	[        R                  " U5      U-  -   n[	        X+-  5      n[	        X[-  5      nUS:  d  US:  a  SnOSU-  U-  n[        X5      nX.U-  U-  -   nU " XU-  -   U5      n[	        UU-
  U-  5      U-  nUS::  a  US::  a  [        SUS-  5      nOS[        UU5      -  SUS-   -  -  n[        S	U-  UX5      $ )
a4  Empirically select a good initial step.

The algorithm is described in [1]_.

Parameters
----------
fun : callable
    Right-hand side of the system.
t0 : float
    Initial value of the independent variable.
y0 : ndarray, shape (n,)
    Initial value of the dependent variable.
t_bound : float
    End-point of integration interval; used to ensure that t0+step<=tbound
    and that fun is only evaluated in the interval [t0,tbound]
max_step : float
    Maximum allowable step size.
f0 : ndarray, shape (n,)
    Initial value of the derivative, i.e., ``fun(t0, y0)``.
direction : float
    Integration direction.
order : float
    Error estimator order. It means that the error controlled by the
    algorithm is proportional to ``step_size ** (order + 1)`.
rtol : float
    Desired relative tolerance.
atol : float
    Desired absolute tolerance.

Returns
-------
h_abs : float
    Absolute value of the suggested initial step.

References
----------
.. [1] E. Hairer, S. P. Norsett G. Wanner, "Solving Ordinary Differential
       Equations I: Nonstiff Problems", Sec. II.4.
r   g        gh㈵>gư>g{Gz?gV瞯<gMbP?   r$   )r3   r	   infr
   r2   minmax)funr   y0r   r   f0	directionorderr+   r,   interval_lengthscaled0d1h0y1f1d2h1s                      r   select_initial_steprG   D   s   R 
ww!|vv','O#266":$$E	bj	B	bj	B	DyBIBY^	R	!B	9nr!	!B	Ry. "	%B	rBw%	 2	%B	U{rU{rDy!SR[ a519o6sRx_77r   c                   .    \ rS rSrSrSS jrS rS rSrg)	OdeSolution   a  Continuous ODE solution.

It is organized as a collection of `DenseOutput` objects which represent
local interpolants. It provides an algorithm to select a right interpolant
for each given point.

The interpolants cover the range between `t_min` and `t_max` (see
Attributes below). Evaluation outside this interval is not forbidden, but
the accuracy is not guaranteed.

When evaluating at a breakpoint (one of the values in `ts`) a segment with
the lower index is selected.

Parameters
----------
ts : array_like, shape (n_segments + 1,)
    Time instants between which local interpolants are defined. Must
    be strictly increasing or decreasing (zero segment with two points is
    also allowed).
interpolants : list of DenseOutput with n_segments elements
    Local interpolants. An i-th interpolant is assumed to be defined
    between ``ts[i]`` and ``ts[i + 1]``.
alt_segment : boolean
    Requests the alternative interpolant segment selection scheme. At each
    solver integration point, two interpolant segments are available. The
    default (False) and alternative (True) behaviours select the segment
    for which the requested time corresponded to ``t`` and ``t_old``,
    respectively. This functionality is only relevant for testing the
    interpolants' accuracy: different integrators use different
    construction strategies.

Attributes
----------
t_min, t_max : float
    Time range of the interpolation.
c                    [         R                  " U5      n[         R                  " U5      nUR                  S:X  a  US   US   :X  dG  [         R                  " US:  5      (       d)  [         R                  " US:  5      (       d  [        S5      e[        U5      U l        UR                  U R                  S-   4:w  a  [        S5      eXl	        X l
        US   US   :  a2  US   U l        US   U l        SU l        U(       a  SOS	U l        Xl        g US   U l        US   U l        S
U l        U(       a  S	OSU l        US S S2   U l        g )N   r   z/`ts` must be strictly increasing or decreasing.r5   z4Numbers of time stamps and interpolants don't match.TrightleftF)r	   r(   diffr3   allr   len
n_segmentsr*   tsinterpolantst_mint_max	ascendingside	ts_sorted)selfrT   rU   alt_segmentds        r   __init__OdeSolution.__init__   s   ZZ^GGBKA"Q%2b6/66!a%==BFF1q5MMNOOl+88!+-- , - - (b6RU?ADJBDJ!DN#.FDINBDJADJ"DN"-7DI"XDNr   c                    [         R                  " U R                  XR                  S9n[	        [        US-
  S5      U R                  S-
  5      nU R                  (       d  U R                  S-
  U-
  nU R                  U   " U5      $ )NrY   r5   r   )	r	   searchsortedrZ   rY   r7   r8   rS   rX   rU   )r[   tindsegments       r   _call_singleOdeSolution._call_single   sk     oodnnaii@c#'1ot':;~~oo)G3G  )!,,r   c                     [         R                  " U5      nUR                  S:X  a  U R                  U5      $ [         R                  " U5      n[         R
                  " U5      n[         R                  " UR                  S   5      X2'   X   n[         R                  " U R                  X@R                  S9nUS-  nSXUS:  '   U R                  S-
  XUU R                  S-
  :  '   U R                  (       d  U R                  S-
  U-
  n/ nSn[        U5       HF  u  pU[        [        U	5      5      -   n
U R                   U   " XGU
 5      nUR#                  U5        U
nMH     [         R$                  " U5      nUSS2U4   nU$ )a  Evaluate the solution.

Parameters
----------
t : float or array_like with shape (n_points,)
    Points to evaluate at.

Returns
-------
y : ndarray, shape (n_states,) or (n_states, n_points)
    Computed values. Shape depends on whether `t` is a scalar or a
    1-D array.
r   ra   r5   N)r	   r(   r)   rf   argsort
empty_likearanger*   rb   rZ   rY   rS   rX   r   rR   listrU   appendhstack)r[   rc   r=   reverset_sortedsegmentsysgroup_startre   group	group_endys               r   __call__OdeSolution.__call__   sH    JJqM66Q;$$Q''

1--&5;;q>28 ??4>>8))LA!"A37??Q3FDOOa//0~~*X5H%h/NG#c$u+&66I!!'*8	+JKAIIaL#K	 0 YYr]7
^	r   )rX   rU   rS   rY   rW   rV   rT   rZ   N)F)	__name__
__module____qualname____firstlineno____doc__r^   rf   rw   __static_attributes__r   r   r   rI   rI      s    #H&8
-+r   rI   g      ?g      ?g      ?g     @@
   g?c                    [         R                  " U5      nUR                  S   nUS:X  a  [         R                  " S5      U4$ Uc  [         R                  " U[
        S-  5      nOUR                  5       nS[         R                  " U5      S:  R                  [        5      -  S-
  nU[         R                  " U[         R                  " U5      5      -  n	X%U	-  -   U-
  n
[         R                  " U
S:H  5      S    H8  nX   S:X  d  M  X[==   S-  ss'   X+   X[   X   -  -   X+   -
  X'   X   S:X  a  M+  M:     Uc  [        XX#XU	5      $ Uu  p[        XX#XU	X5	      $ )a  Finite differences Jacobian approximation tailored for ODE solvers.

This function computes finite difference approximation to the Jacobian
matrix of `fun` with respect to `y` using forward differences.
The Jacobian matrix has shape (n, n) and its element (i, j) is equal to
``d f_i / d y_j``.

A special feature of this function is the ability to correct the step
size from iteration to iteration. The main idea is to keep the finite
difference significantly separated from its round-off error which
approximately equals ``EPS * np.abs(f)``. It reduces a possibility of a
huge error and assures that the estimated derivative are reasonably close
to the true values (i.e., the finite difference approximation is at least
qualitatively reflects the structure of the true Jacobian).

Parameters
----------
fun : callable
    Right-hand side of the system implemented in a vectorized fashion.
t : float
    Current time.
y : ndarray, shape (n,)
    Current state.
f : ndarray, shape (n,)
    Value of the right hand side at (t, y).
threshold : float
    Threshold for `y` value used for computing the step size as
    ``factor * np.maximum(np.abs(y), threshold)``. Typically, the value of
    absolute tolerance (atol) for a solver should be passed as `threshold`.
factor : ndarray with shape (n,) or None
    Factor to use for computing the step size. Pass None for the very
    evaluation, then use the value returned from this function.
sparsity : tuple (structure, groups) or None
    Sparsity structure of the Jacobian, `structure` must be csc_matrix.

Returns
-------
J : ndarray or csc_matrix, shape (n, n)
    Jacobian matrix.
factor : ndarray, shape (n,)
    Suggested `factor` for the next evaluation.
r   )r   r   r0   rL   r5   r   )r	   r(   r*   emptyfullr&   copyrealastypefloatr'   r
   nonzero_dense_num_jac_sparse_num_jac)r9   rc   rv   f	thresholdfactorsparsityr-   f_signy_scalehi	structuregroupss                 r   num_jacr     sI   V 	

1A	
AAvxx''~C3J'
 "''!*/))%0014Frzz)RVVAY77G	
g	"A ZZQ"daiIOID69wz11QT9AD dai #
 caAw??$	sqQ(2 	2r   c                    UR                   S   n[        R                  " U5      nU " XS S 2S 4   U-   5      n	XS S 2S 4   -
  n
[        R                  " [        R                  " U
5      SS9n[        R
                  " U5      n[        R                  " XU4   5      n[        R                  " [        R                  " X;   5      [        R                  " XU4   5      5      nU[        U-  :  n[        R                  " U5      (       Ga  [        R                  " U5      u  n[        UU   -  nUU   UUU   -  -   UU   -
  nUUUU4'   U " XS S 2S 4   US S 2U4   -   5      n	XS S 2S 4   -
  n[        R                  " [        R                  " U5      SS9n[        R
                  " UR                   S   5      n[        R                  " UX4   5      n[        R                  " [        R                  " X;   5      [        R                  " XU4   5      5      nUU   U-  UUU   -  :  n[        R                  " U5      (       aM  [        R                  " U5      u  nUU   nUU   UU'   UU   UU'   US S 2U4   U
S S 2U4'   UU   UU'   UU   UU'   X-  n
X][        U-  :  ==   [        -  ss'   X][        U-  :  ==   [        -  ss'   [        R                  " U[        5      nX4$ )Nr   axis)r*   r	   diagargmaxr
   rk   r'   NUM_JAC_DIFF_REJECTr%   r   NUM_JAC_FACTOR_INCREASENUM_JAC_DIFF_SMALLNUM_JAC_DIFF_BIGNUM_JAC_FACTOR_DECREASENUM_JAC_MIN_FACTOR)r9   rc   rv   r   r   r   r   r-   h_vecsf_newrP   max_indrmax_diffr?   diff_too_smallrd   
new_factorh_newdiff_newmax_diff_new	scale_newupdate
update_inds                           r   r   r   W  s   	
AWWQZFQW:&'EQW:Diit1-G
		!AvvdA:&'HJJrvvaj)266%
2C+DEE 3e ;;N	vvnzz.),vc{:
3*ws|33qv= sCxADzF1c6N23QW:%))BFF8,15IIciil#vvhwz23JJrvvaj1266%
:K3LM	#*\E#J-FF66&>>jj(GFVJ!+F!3F:!&MAjM"*1f9"5DJ )& 1E*#/#7HZ ID
(50015LL1
&../3JJ/ZZ 23F<r   c	           
      B
   UR                   S   n	[        R                  " U5      S-   n
[        R                  " X45      n[	        U
5       H  n[        R
                  " X5      nXM-  X'   M!     UR                  nU " XS S 2S 4   U-   5      nXS S 2S 4   -
  n[        U5      u  nnn[        UUUU   4   UU44X4S9R                  5       n[        R                  " [        U5      R                  SS95      R                  5       n[        R                  " U	5      n[        R                  " [        R                  " UUU4   5      5      R                  5       n[        R                   " [        R                  " UU   5      [        R                  " UUUU   4   5      5      nU["        U-  :  n[        R$                  " U5      (       Ga  [        R&                  " U5      u  n[(        UU   -  nUU   UUU   -  -   UU   -
  n[        R*                  " U	5      nUUU'   [        R,                  " UU   5      n[        R                  " U
[.        S9n[        R                  " UR                   S   U	45      n[1        U5       H)  u  nn[        R
                  " X5      nUU-  UU'   UUU'   M+     UR                  nU " XS S 2S 4   U-   5      nXS S 2S 4   -
  n[        US S 2U4   5      u  nnn[        UUUUUU         4   UU44U	UR                   S   4S9R                  5       n [        R                  " [        U 5      R                  SS95      R                  5       n![        R                  " UR                   S   5      n[        R                  " [        R                  " U U!U4   5      5      R                  5       n"[        R                   " [        R                  " UU!   5      [        R                  " UU!UUU      4   5      5      n#UU   U#-  U"UU   -  :  n$[        R$                  " U$5      (       aM  [        R&                  " U$5      u  n$UU$   n%UU$   UU%'   UU$   UU%'   U S S 2U$4   US S 2U%4'   U#U$   UU%'   U"U$   UU%'   U=R2                  [        R4                  " U[        R6                  " UR8                  5      5      -  sl        UU[:        U-  :  ==   [(        -  ss'   UU[<        U-  :  ==   [>        -  ss'   [        R                   " U[@        5      nUU4$ )Nr   r5   )r*   r   )dtype)!r*   r	   r8   r   rangeequalTr   r   tocscarrayr
   r   ravelrk   r(   r'   r   r%   r   r   zerosuniqueint	enumeratedatarepeatrP   indptrr   r   r   r   )&r9   rc   rv   r   r   r   r   r   r   r-   n_groupsr   rt   er   dfr   j_rP   r   r   r   r?   r   rd   r   r   	h_new_allgroups_unique
groups_mapkr   max_ind_newr   r   r   r   s&                                         r   r   r     s   	
Avvf~!HXXxm$FxHHU# ! XXFQW:&'E	1d7	B9oGAq!r!VAY,'!Q0?EEGDhhs4y''Q'/0668G
		!Azz"&&gqj!12399;HJJrvvaj)vveGVAY$678:E  3e ;;N	vvnzz.),vc{:
3*ws|33qv=HHQK		#		&+.XXhc2
=..q1156!-0HAu'A!AF1I !Ju 1 ADzF*+q$wZyC()1ar!Zs1v%?"?@ !1v'/0#))A,.?AAF 	 hhs8}333;<BBDIIciil#zz"&&+q.)A"BCIIKJJFF1[>"FF5j&==>?A	 #*\E#J-FF66&>>jj(GFVJ!+F!3F:!&MAjM"*1f9"5DJ )& 1E*#/#7HZ II1bggdkk233I
8(50015LL1
8&../3JJ/ZZ 23F<r   )N)	itertoolsr   warningsr   numpyr	   scipy.sparser   r   finfor   epsr&   r   r   r"   r.   r2   rG   rI   r   r   r   r   r   r   r   r   r   r   r   r   <module>r      s       ) 	hhuo$&-
B8Jx xv Ul D[ $; 3Y   H2V'TBr   