
    (phY                         S SK rSSKJrJr  SSKJrJrJrJ	r	J
r
Jr  SSKJr  SrSrSrS	 r " S
 S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      rg)    N   )	OdeSolverDenseOutput)validate_max_stepvalidate_tolselect_initial_stepnormwarn_extraneousvalidate_first_step)dop853_coefficientsg?皙?
   c	                 D   X8S'   [        [        USS USS 5      SS9 HD  u  n	u  p[        R                  " USU	 R                  U
SU	 5      U-  nU " XU-  -   X,-   5      X'   MF     X$[        R                  " USS R                  U5      -  -   nU " X-   U5      nXS'   X4$ )a  Perform a single Runge-Kutta step.

This function computes a prediction of an explicit Runge-Kutta method and
also estimates the error of a less accurate method.

Notation for Butcher tableau is as in [1]_.

Parameters
----------
fun : callable
    Right-hand side of the system.
t : float
    Current time.
y : ndarray, shape (n,)
    Current state.
f : ndarray, shape (n,)
    Current value of the derivative, i.e., ``fun(x, y)``.
h : float
    Step to use.
A : ndarray, shape (n_stages, n_stages)
    Coefficients for combining previous RK stages to compute the next
    stage. For explicit methods the coefficients at and above the main
    diagonal are zeros.
B : ndarray, shape (n_stages,)
    Coefficients for combining RK stages for computing the final
    prediction.
C : ndarray, shape (n_stages,)
    Coefficients for incrementing time for consecutive RK stages.
    The value for the first stage is always zero.
K : ndarray, shape (n_stages + 1, n)
    Storage array for putting RK stages here. Stages are stored in rows.
    The last row is a linear combination of the previous rows with
    coefficients

Returns
-------
y_new : ndarray, shape (n,)
    Solution at t + h computed with a higher accuracy.
f_new : ndarray, shape (n,)
    Derivative ``fun(t + h, y_new)``.

References
----------
.. [1] E. Hairer, S. P. Norsett G. Wanner, "Solving Ordinary Differential
       Equations I: Nonstiff Problems", Sec. II.4.
r   r   Nstart)	enumeratezipnpdotT)funtyfhABCKsacdyy_newf_news                  J/var/www/html/venv/lib/python3.13/site-packages/scipy/integrate/_ivp/rk.pyrk_stepr(      s    ^ aDs1QR5!AB%0:	6AVVAbqEGGQrU#a'11u9af% ; BFF1Sb688Q'''EuEbE<    c                   <  ^  \ rS rSr% Sr\r\R                  \	S'   \r
\R                  \	S'   \r\R                  \	S'   \r\R                  \	S'   \r\R                  \	S'   \r\\	S'   \r\\	S	'   \r\\	S
'   \R$                  SSSS4U 4S jjrS rS rS rS rSrU =r$ )
RungeKuttaJ   z,Base class for explicit Runge-Kutta methods.r   r   r   EPordererror_estimator_ordern_stagesMbP?ư>FNc
                   > [        U
5        [        TU ]	  XX4USS9  S U l        [	        U5      U l        [        XgU R                  5      u  U l        U l	        U R                  U R                  U R                  5      U l        U	ci  [        U R                  U R                  U R                  XEU R                  U R                  U R                   U R                  U R                  5
      U l        O[%        XU5      U l        [&        R(                  " U R*                  S-   U R                  4U R                  R,                  S9U l        SU R                   S-   -  U l        S U l        g )NT)support_complexr   dtyper   )r
   super__init__y_oldr   max_stepr   nrtolatolr   r   r   r   r   	directionr0   h_absr   r   emptyr1   r7   r    error_exponent
h_previousselfr   t0y0t_boundr;   r=   r>   
vectorized
first_step
extraneous	__class__s              r'   r9   RungeKutta.__init__U   s    	
#"z)- 	 	/
)(3+D?	49$&&$&&),$&&$&&'TVVT^^**DIItyyBDJ -ZWEDJ4==1,dff5TVV\\J D$>$>$BCr)   c                 ^    [         R                  " UR                  U R                  5      U-  $ N)r   r   r   r-   )rE   r    r   s      r'   _estimate_errorRungeKutta._estimate_errori   s     vvacc466"Q&&r)   c                 <    [        U R                  X5      U-  5      $ rO   )r	   rP   )rE   r    r   scales       r'   _estimate_error_normRungeKutta._estimate_error_norml   s    D((.677r)   c                    U R                   nU R                  nU R                  nU R                  nU R                  nS[
        R                  " [
        R                  " XR                  [
        R                  -  5      U-
  5      -  nU R                  U:  a  UnOU R                  U:  a  UnOU R                  nSnSn	U(       Gd  Xv:  a  SU R                  4$ XpR                  -  n
X-   nU R                  XR                  -
  -  S:  a  U R                  nX-
  n
[
        R                  " U
5      n[        U R                  XU R                  XR                   U R"                  U R$                  U R&                  5	      u  pU[
        R(                  " [
        R                  " U5      [
        R                  " U5      5      U-  -   nU R+                  U R&                  X5      nUS:  aK  US:X  a  [,        nO#[/        [,        [0        XR2                  -  -  5      nU	(       a  [/        SU5      nUU-  nSnO(U[5        [6        [0        XR2                  -  -  5      -  nSn	U(       d  GM  W
U l        X l        WU l         WU l        Xpl
        WU l        g)Nr   Fr   r   T)TN)r   r   r;   r=   r>   r   abs	nextafterr?   infr@   TOO_SMALL_STEPrH   r(   r   r   r   r   r   r    maximumrT   
MAX_FACTORminSAFETYrB   max
MIN_FACTORrC   r:   )rE   r   r   r;   r=   r>   min_stepr@   step_acceptedstep_rejectedr   t_newr%   r&   rS   
error_normfactors                    r'   
_step_implRungeKutta._step_implo   s   FFFF==yyyyr||A~~/FG!KLL:: EZZ("EJJEd1111&AEE~~!56:	AFF1IE"488Q4661ff#'66466466;LE2::bffQi?$FFE224661DJA~?'F !'*8K8K*K!KMF ! F^F $Z#j4G4G&GGI I $E  -H 

r)   c                     U R                   R                  R                  U R                  5      n[	        U R
                  U R                  U R                  U5      $ rO   )r    r   r   r.   RkDenseOutputt_oldr   r:   )rE   Qs     r'   _dense_output_implRungeKutta._dense_output_impl   s9    FFHHLL TZZQ??r)   )r    r>   rB   r   r@   rC   r;   r=   r   r   r:   )__name__
__module____qualname____firstlineno____doc__NotImplementedr   r   ndarray__annotations__r   r   r-   r.   r/   intr0   r1   rY   r9   rP   rT   rg   rm   __static_attributes____classcell__rL   s   @r'   r+   r+   J   s    6"Arzz""Arzz""Arzz""Arzz""Arzz"E3!/3/"Hc"68ff% ('8AF@ @r)   r+   c                      \ rS rSrSrSrSrSr\R                  " / SQ5      r
\R                  " / SQ/ SQ/ SQ/5      r\R                  " / S	Q5      r\R                  " / S
Q5      r\R                  " / SQ/ SQ/ SQ/ SQ/5      rSrg)RK23   a  Explicit Runge-Kutta method of order 3(2).

This uses the Bogacki-Shampine pair of formulas [1]_. The error is controlled
assuming accuracy of the second-order method, but steps are taken using the
third-order accurate formula (local extrapolation is done). A cubic Hermite
polynomial is used for the dense output.

Can be applied in the complex domain.

Parameters
----------
fun : callable
    Right-hand side of the system: the time derivative of the state ``y``
    at time ``t``. The calling signature is ``fun(t, y)``, where ``t`` is a
    scalar and ``y`` is an ndarray with ``len(y) = len(y0)``. ``fun`` must
    return an array of the same shape as ``y``. See `vectorized` for more
    information.
t0 : float
    Initial time.
y0 : array_like, shape (n,)
    Initial state.
t_bound : float
    Boundary time - the integration won't continue beyond it. It also
    determines the direction of the integration.
first_step : float or None, optional
    Initial step size. Default is ``None`` which means that the algorithm
    should choose.
max_step : float, optional
    Maximum allowed step size. Default is np.inf, i.e., the step size is not
    bounded and determined solely by the solver.
rtol, atol : float and array_like, optional
    Relative and absolute tolerances. The solver keeps the local error
    estimates less than ``atol + rtol * abs(y)``. Here `rtol` controls a
    relative accuracy (number of correct digits), while `atol` controls
    absolute accuracy (number of correct decimal places). To achieve the
    desired `rtol`, set `atol` to be smaller than the smallest value that
    can be expected from ``rtol * abs(y)`` so that `rtol` dominates the
    allowable error. If `atol` is larger than ``rtol * abs(y)`` the
    number of correct digits is not guaranteed. Conversely, to achieve the
    desired `atol` set `rtol` such that ``rtol * abs(y)`` is always smaller
    than `atol`. If components of y have different scales, it might be
    beneficial to set different `atol` values for different components by
    passing array_like with shape (n,) for `atol`. Default values are
    1e-3 for `rtol` and 1e-6 for `atol`.
vectorized : bool, optional
    Whether `fun` may be called in a vectorized fashion. False (default)
    is recommended for this solver.

    If ``vectorized`` is False, `fun` will always be called with ``y`` of
    shape ``(n,)``, where ``n = len(y0)``.

    If ``vectorized`` is True, `fun` may be called with ``y`` of shape
    ``(n, k)``, where ``k`` is an integer. In this case, `fun` must behave
    such that ``fun(t, y)[:, i] == fun(t, y[:, i])`` (i.e. each column of
    the returned array is the time derivative of the state corresponding
    with a column of ``y``).

    Setting ``vectorized=True`` allows for faster finite difference
    approximation of the Jacobian by methods 'Radau' and 'BDF', but
    will result in slower execution for this solver.

Attributes
----------
n : int
    Number of equations.
status : string
    Current status of the solver: 'running', 'finished' or 'failed'.
t_bound : float
    Boundary time.
direction : float
    Integration direction: +1 or -1.
t : float
    Current time.
y : ndarray
    Current state.
t_old : float
    Previous time. None if no steps were made yet.
step_size : float
    Size of the last successful step. None if no steps were made yet.
nfev : int
    Number evaluations of the system's right-hand side.
njev : int
    Number of evaluations of the Jacobian.
    Is always 0 for this solver as it does not use the Jacobian.
nlu : int
    Number of LU decompositions. Is always 0 for this solver.

References
----------
.. [1] P. Bogacki, L.F. Shampine, "A 3(2) Pair of Runge-Kutta Formulas",
       Appl. Math. Lett. Vol. 2, No. 4. pp. 321-325, 1989.
      )r         ?      ?)r   r   r   )r   r   r   )r   r   r   )gqq?gUUUUUU?gqq?)grqǱ?gUUUUUUgqqg      ?)r   gUUUUUUgrq?)r   r   gUUUUUU)r   gUUUUUU?gqq)r   r   r    Nro   rp   rq   rr   rs   r/   r0   r1   r   arrayr   r   r   r-   r.   rx   r   r)   r'   r|   r|      s    [x EH
A
 	A
 	!A
)*A
$  	Ar)   r|   c            
       2   \ rS rSrSrSrSrSr\R                  " / SQ5      r
\R                  " / SQ/ SQ/ S	Q/ S
Q/ SQ/ SQ/5      r\R                  " / SQ5      r\R                  " / SQ5      r\R                  " / SQ/ SQ/ SQ/ SQ/ SQ/ SQ/ SQ/5      rSrg)RK45i%  a  Explicit Runge-Kutta method of order 5(4).

This uses the Dormand-Prince pair of formulas [1]_. The error is controlled
assuming accuracy of the fourth-order method accuracy, but steps are taken
using the fifth-order accurate formula (local extrapolation is done).
A quartic interpolation polynomial is used for the dense output [2]_.

Can be applied in the complex domain.

Parameters
----------
fun : callable
    Right-hand side of the system. The calling signature is ``fun(t, y)``.
    Here ``t`` is a scalar, and there are two options for the ndarray ``y``:
    It can either have shape (n,); then ``fun`` must return array_like with
    shape (n,). Alternatively it can have shape (n, k); then ``fun``
    must return an array_like with shape (n, k), i.e., each column
    corresponds to a single column in ``y``. The choice between the two
    options is determined by `vectorized` argument (see below).
t0 : float
    Initial time.
y0 : array_like, shape (n,)
    Initial state.
t_bound : float
    Boundary time - the integration won't continue beyond it. It also
    determines the direction of the integration.
first_step : float or None, optional
    Initial step size. Default is ``None`` which means that the algorithm
    should choose.
max_step : float, optional
    Maximum allowed step size. Default is np.inf, i.e., the step size is not
    bounded and determined solely by the solver.
rtol, atol : float and array_like, optional
    Relative and absolute tolerances. The solver keeps the local error
    estimates less than ``atol + rtol * abs(y)``. Here `rtol` controls a
    relative accuracy (number of correct digits), while `atol` controls
    absolute accuracy (number of correct decimal places). To achieve the
    desired `rtol`, set `atol` to be smaller than the smallest value that
    can be expected from ``rtol * abs(y)`` so that `rtol` dominates the
    allowable error. If `atol` is larger than ``rtol * abs(y)`` the
    number of correct digits is not guaranteed. Conversely, to achieve the
    desired `atol` set `rtol` such that ``rtol * abs(y)`` is always smaller
    than `atol`. If components of y have different scales, it might be
    beneficial to set different `atol` values for different components by
    passing array_like with shape (n,) for `atol`. Default values are
    1e-3 for `rtol` and 1e-6 for `atol`.
vectorized : bool, optional
    Whether `fun` is implemented in a vectorized fashion. Default is False.

Attributes
----------
n : int
    Number of equations.
status : string
    Current status of the solver: 'running', 'finished' or 'failed'.
t_bound : float
    Boundary time.
direction : float
    Integration direction: +1 or -1.
t : float
    Current time.
y : ndarray
    Current state.
t_old : float
    Previous time. None if no steps were made yet.
step_size : float
    Size of the last successful step. None if no steps were made yet.
nfev : int
    Number evaluations of the system's right-hand side.
njev : int
    Number of evaluations of the Jacobian.
    Is always 0 for this solver as it does not use the Jacobian.
nlu : int
    Number of LU decompositions. Is always 0 for this solver.

References
----------
.. [1] J. R. Dormand, P. J. Prince, "A family of embedded Runge-Kutta
       formulae", Journal of Computational and Applied Mathematics, Vol. 6,
       No. 1, pp. 19-26, 1980.
.. [2] L. W. Shampine, "Some Practical Runge-Kutta Formulas", Mathematics
       of Computation,, Vol. 46, No. 173, pp. 135-150, 1986.
         )r   r   g333333?g?gqq?r   )r   r   r   r   r   )r   r   r   r   r   )g333333?g?r   r   r   )gII?ggqq@r   r   )gq@g 1'gR<6R#@gE3ҿr   )g+@g>%gr!@gE]t?g/pѿ)gUUUUUU?r   gVI?gUUUUU?gϡԿg10?)g2Tr   gĿ
UZkq?ggX
?g{tg?)r   g#
!gJ<@gFC)r   r   r   r   )r   gF@gFj'NgDg@)r   gdDgaP#$@g2)r   g<p@g@갘g,@)r   gRq#g_40g.
@gF)r   g'?g'gK@r   Nr   r   r)   r'   r   r   %  s    Rf EH
,-A
#:= 	A 	BCA
  	A 		#	"	"	&NFH 	IAr)   r   c                   p  ^  \ rS rSrSr\R                  rSrSr	\R                  S\2S\24   r
\R                  r\R                  S\ r\R                  r\R                  r\R                  r\R                  \S-   S r\R                  \S-   S r\R&                  SSS	S4U 4S
 jjrS rS rS rSrU =r$ )DOP853i  a  Explicit Runge-Kutta method of order 8.

This is a Python implementation of "DOP853" algorithm originally written
in Fortran [1]_, [2]_. Note that this is not a literal translation, but
the algorithmic core and coefficients are the same.

Can be applied in the complex domain.

Parameters
----------
fun : callable
    Right-hand side of the system. The calling signature is ``fun(t, y)``.
    Here, ``t`` is a scalar, and there are two options for the ndarray ``y``:
    It can either have shape (n,); then ``fun`` must return array_like with
    shape (n,). Alternatively it can have shape (n, k); then ``fun``
    must return an array_like with shape (n, k), i.e. each column
    corresponds to a single column in ``y``. The choice between the two
    options is determined by `vectorized` argument (see below).
t0 : float
    Initial time.
y0 : array_like, shape (n,)
    Initial state.
t_bound : float
    Boundary time - the integration won't continue beyond it. It also
    determines the direction of the integration.
first_step : float or None, optional
    Initial step size. Default is ``None`` which means that the algorithm
    should choose.
max_step : float, optional
    Maximum allowed step size. Default is np.inf, i.e. the step size is not
    bounded and determined solely by the solver.
rtol, atol : float and array_like, optional
    Relative and absolute tolerances. The solver keeps the local error
    estimates less than ``atol + rtol * abs(y)``. Here `rtol` controls a
    relative accuracy (number of correct digits), while `atol` controls
    absolute accuracy (number of correct decimal places). To achieve the
    desired `rtol`, set `atol` to be smaller than the smallest value that
    can be expected from ``rtol * abs(y)`` so that `rtol` dominates the
    allowable error. If `atol` is larger than ``rtol * abs(y)`` the
    number of correct digits is not guaranteed. Conversely, to achieve the
    desired `atol` set `rtol` such that ``rtol * abs(y)`` is always smaller
    than `atol`. If components of y have different scales, it might be
    beneficial to set different `atol` values for different components by
    passing array_like with shape (n,) for `atol`. Default values are
    1e-3 for `rtol` and 1e-6 for `atol`.
vectorized : bool, optional
    Whether `fun` is implemented in a vectorized fashion. Default is False.

Attributes
----------
n : int
    Number of equations.
status : string
    Current status of the solver: 'running', 'finished' or 'failed'.
t_bound : float
    Boundary time.
direction : float
    Integration direction: +1 or -1.
t : float
    Current time.
y : ndarray
    Current state.
t_old : float
    Previous time. None if no steps were made yet.
step_size : float
    Size of the last successful step. None if no steps were made yet.
nfev : int
    Number evaluations of the system's right-hand side.
njev : int
    Number of evaluations of the Jacobian. Is always 0 for this solver
    as it does not use the Jacobian.
nlu : int
    Number of LU decompositions. Is always 0 for this solver.

References
----------
.. [1] E. Hairer, S. P. Norsett G. Wanner, "Solving Ordinary Differential
       Equations I: Nonstiff Problems", Sec. II.
.. [2] `Page with original Fortran code of DOP853
        <http://www.unige.ch/~hairer/software.html>`_.
      Nr   r2   r3   Fc
                   > [         TU ]  " XX4XVUX4	0 U
D6  [        R                  " [        R
                  U R                  4U R                  R                  S9U l	        U R                  S U R                  S-    U l        g )Nr6   r   )r8   r9   r   rA   r   N_STAGES_EXTENDEDr<   r   r7   
K_extendedr1   r    rD   s              r'   r9   DOP853.__init__  sr     	"xt#	?3=	?(($7$I$I$(FF$,3766<<A!3$--!"34r)   c                    [         R                  " UR                  U R                  5      n[         R                  " UR                  U R                  5      n[         R
                  " [         R                  " U5      S[         R                  " U5      -  5      n[         R                  " U5      nUS:  n[         R                  " X7   5      XW   -  Xg'   X#-  U-  $ )Ng?r   )r   r   r   E5E3hypotrW   	ones_like)rE   r    r   err5err3denomcorrection_factormasks           r'   rP   DOP853._estimate_error  s    vvacc477#vvacc477#sRVVD\'9:LL.qy"$&&"4u{"Bx+++r)   c                    [         R                  " UR                  U R                  5      U-  n[         R                  " UR                  U R                  5      U-  n[         R
                  R                  U5      S-  n[         R
                  R                  U5      S-  nUS:X  a  US:X  a  gUSU-  -   n[         R                  " U5      U-  [         R                  " U[        U5      -  5      -  $ )Nr   r   g        g{Gz?)
r   r   r   r   r   linalgr	   rW   sqrtlen)	rE   r    r   rS   r   r   err5_norm_2err3_norm_2r   s	            r'   rT   DOP853._estimate_error_norm  s    vvacc477#e+vvacc477#e+iinnT*A-iinnT*A-!q 0d[00vvay;&U1C)DDDr)   c                     U R                   nU R                  n[        [        U R                  U R
                  5      U R                  S-   S9 Hb  u  nu  pE[        R                  " US U R                  US U 5      U-  nU R                  U R                  XR-  -   U R                  U-   5      X'   Md     [        R                  " [        R                  U R                   4U R                  R"                  S9nUS   nU R$                  U R                  -
  n	XS'   X(-  U	-
  US'   SU	-  X R&                  U-   -  -
  US'   U[        R                  " U R(                  U5      -  USS & [+        U R                  U R,                  U R                  U5      $ )Nr   r   r6   r   r   r~   )r   rC   r   r   A_EXTRAC_EXTRAr1   r   r   r   r   rk   r:   rA   r   INTERPOLATOR_POWERr<   r7   r   r   DDop853DenseOutputr   )
rE   r    r   r!   r"   r#   r$   Ff_olddelta_ys
             r'   rm   DOP853._dense_output_impl  sL   OOOO"3t||T\\#B)-):<IAv"12A'!+B88DJJ.

R@AD<
 HH)<<dffE::++- !&&4::%!y7"!7{Q&&5.11!BFF4661%%!" TVVTZZCCr)   )r    r   )ro   rp   rq   rr   rs   r   N_STAGESr1   r/   r0   r   r   r   r   r   r   r   r   r   rY   r9   rP   rT   rm   rx   ry   rz   s   @r'   r   r     s    Pb #++HEixi(23AAix(A			B			BA!##HqLM2G!##HqLM2G68ff% 5,ED Dr)   r   c                   .   ^  \ rS rSrU 4S jrS rSrU =r$ )rj   i(  c                 |   > [         TU ]  X5        X!-
  U l        X@l        UR                  S   S-
  U l        X0l        g )Nr   )r8   r9   r   rl   shaper/   r:   )rE   rk   r   r:   rl   rL   s        r'   r9   RkDenseOutput.__init__)  s6    "WWQZ!^

r)   c                 
   XR                   -
  U R                  -  nUR                  S:X  a:  [        R                  " X R
                  S-   5      n[        R                  " U5      nO:[        R                  " X R
                  S-   S45      n[        R                  " USS9nU R                  [        R                  " U R                  U5      -  nUR                  S:X  a  X@R                  S S 2S 4   -  nU$ X@R                  -  nU$ )Nr   r   )axisr   )
rk   r   ndimr   tiler/   cumprodr   rl   r:   )rE   r   xpr   s        r'   
_call_implRkDenseOutput._call_impl0  s    ^tvv%66Q;::>*A

1AJJNA./A

11%AFFRVVDFFA&&66Q;AtG$$A  OAr)   )rl   r   r/   r:   ro   rp   rq   rr   r9   r   rx   ry   rz   s   @r'   rj   rj   (  s     r)   rj   c                   .   ^  \ rS rSrU 4S jrS rSrU =r$ )r   iA  c                 N   > [         TU ]  X5        X!-
  U l        X@l        X0l        g rO   )r8   r9   r   r   r:   )rE   rk   r   r:   r   rL   s        r'   r9   Dop853DenseOutput.__init__B  s#    "
r)   c                    XR                   -
  U R                  -  nUR                  S:X  a!  [        R                  " U R
                  5      nOPUS S 2S 4   n[        R                  " [        U5      [        U R
                  5      4U R
                  R                  S9n[        [        U R                  5      5       H   u  pEX5-  nUS-  S:X  a  X2-  nM  USU-
  -  nM"     X0R
                  -  nUR                  $ )Nr   r6   r   r   )rk   r   r   r   
zeros_liker:   zerosr   r7   r   reversedr   r   )rE   r   r   r   ir   s         r'   r   Dop853DenseOutput._call_implH  s    ^tvv%66Q;djj)A!T'
A#a&#djj/2$**:J:JKAhtvv./DAFA1uzQU
 0 	
ZZss
r)   )r   r   r:   r   rz   s   @r'   r   r   A  s     r)   r   )numpyr   baser   r   commonr   r   r   r	   r
   r    r   r^   r`   r\   r(   r+   r|   r   r   rj   r   r   r)   r'   <module>r      s     (A A ! 


9xj@ j@Zk: k\oI: oIdNDZ NDbK 2 r)   