
    (ph]D                         S SK rS SKJrJr  S SKJrJrJr  S SK	J
r
  S SKJr  SSKJrJrJrJrJrJrJrJr  SSKJrJr  S	rS
rSrSrS rS rS r " S S\5      r  " S S\5      r!g)    N)	lu_factorlu_solve)issparse
csc_matrixeye)splu)group_columns   )validate_max_stepvalidate_tolselect_initial_stepnormEPSnum_jacvalidate_first_stepwarn_extraneous)	OdeSolverDenseOutput      g?
   c                    [         R                  " SU S-   5      SS2S4   n[         R                  " SU S-   5      n[         R                  " U S-   U S-   45      nUS-
  X-  -
  U-  USS2SS24'   SUS'   [         R                  " USS9$ )z6Compute the matrix for changing the differences array.r
   Nr   axis)nparangezeroscumprod)orderfactorIJMs        K/var/www/html/venv/lib/python3.13/site-packages/scipy/integrate/_ivp/bdf.py	compute_Rr%      s    
		!UQY4(A
		!UQYA
%!)UQY'(AQ#q(Aab!"fIAaD::aa      c                     [        X5      n[        US5      nUR                  U5      n[        R                  " UR                  U SUS-    5      U SUS-   & g)z<Change differences array in-place when step size is changed.r
   N)r%   dotr   T)Dr   r    RURUs         r$   change_Dr.      sM    % A%A	
qBFF244:EAI/AjuqyMr&   c	                    Sn	UR                  5       n
SnSn[        [        5       H  nU " X5      n[        R                  " [        R
                  " U5      5      (       d    OwU" XSU-  U-
  U	-
  5      n[        X-  5      nUc  SnOUU-  nUb!  US:  d  U[        U-
  -  SU-
  -  U-  U:  a    O*X-  n
X-  n	US:X  d  Ub  USU-
  -  U-  U:  a  Sn  OUnM     UWS-   X4$ )z5Solve the algebraic system resulting from BDF method.r   NFr
   T)copyrangeNEWTON_MAXITERr   allisfiniter   )funt_new	y_predictcpsiLUsolve_luscaletoldydy_norm_old	convergedkfdydy_normrates                     r$   solve_bdf_systemrG   $   s   	AAKI>"Mvvbkk!n%%ba%#+/*rz"D[(D$!)!+,D9GCcI		qL TQX%6%@3%FI3 #6 a!eQ!!r&   c                   d   ^  \ rS rSrSr\R                  SSSSSS4U 4S jjrS rS	 r	S
 r
SrU =r$ )BDFH   a9  Implicit method based on backward-differentiation formulas.

This is a variable order method with the order varying automatically from
1 to 5. The general framework of the BDF algorithm is described in [1]_.
This class implements a quasi-constant step size as explained in [2]_.
The error estimation strategy for the constant-step BDF is derived in [3]_.
An accuracy enhancement using modified formulas (NDF) [2]_ is also implemented.

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`.
jac : {None, array_like, sparse_matrix, callable}, optional
    Jacobian matrix of the right-hand side of the system with respect to y,
    required by this method. The Jacobian matrix has shape (n, n) and its
    element (i, j) is equal to ``d f_i / d y_j``.
    There are three ways to define the Jacobian:

        * If array_like or sparse_matrix, the Jacobian is assumed to
          be constant.
        * If callable, the Jacobian is assumed to depend on both
          t and y; it will be called as ``jac(t, y)`` as necessary.
          For the 'Radau' and 'BDF' methods, the return value might be a
          sparse matrix.
        * If None (default), the Jacobian will be approximated by
          finite differences.

    It is generally recommended to provide the Jacobian rather than
    relying on a finite-difference approximation.
jac_sparsity : {None, array_like, sparse matrix}, optional
    Defines a sparsity structure of the Jacobian matrix for a
    finite-difference approximation. Its shape must be (n, n). This argument
    is ignored if `jac` is not `None`. If the Jacobian has only few non-zero
    elements in *each* row, providing the sparsity structure will greatly
    speed up the computations [4]_. A zero entry means that a corresponding
    element in the Jacobian is always zero. If None (default), the Jacobian
    is assumed to be dense.
vectorized : bool, optional
    Whether `fun` can be called in a vectorized fashion. Default is False.

    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 this method, but may result in slower
    execution overall in some circumstances (e.g. small ``len(y0)``).

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 of evaluations of the right-hand side.
njev : int
    Number of evaluations of the Jacobian.
nlu : int
    Number of LU decompositions.

References
----------
.. [1] G. D. Byrne, A. C. Hindmarsh, "A Polyalgorithm for the Numerical
       Solution of Ordinary Differential Equations", ACM Transactions on
       Mathematical Software, Vol. 1, No. 1, pp. 71-96, March 1975.
.. [2] L. F. Shampine, M. W. Reichelt, "THE MATLAB ODE SUITE", SIAM J. SCI.
       COMPUTE., Vol. 18, No. 1, pp. 1-22, January 1997.
.. [3] E. Hairer, G. Wanner, "Solving Ordinary Differential Equations I:
       Nonstiff Problems", Sec. III.2.
.. [4] 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, pp. 117-120, 1974.
gMbP?gư>NFc                 *  >^  [        U5        [        TT ]	  XX4U
SS9  [        U5      T l        [        XgT R                  5      u  T l        T l        T R                  T R                  T R                  5      nUcU  [        T R                  T R                  T R                  XEUT R                  ST R                  T R                  5
      T l        O[        XU5      T l        S T l        S T l        [%        S[&        -  U-  [)        SUS-  5      5      T l        S T l        T R/                  X5      u  T l        T l        [5        T R2                  5      (       a3  U 4S jnS n[7        T R                  S	T R                  R8                  S
9nO<U 4S jnS n[:        R<                  " T R                  T R                  R8                  S9nUT l        UT l         UT l!        [:        RD                  " / SQ5      n[:        RF                  " S[:        RH                  " S[:        RJ                  " S[L        S-   5      -  5      45      T l'        SU-
  T RN                  -  T l(        UT RN                  -  S[:        RJ                  " S[L        S-   5      -  -   T l)        [:        RT                  " [L        S-   T R                  4T R                  R8                  S9nT R                  US'   UT R                  -  T R                  -  US'   UT l+        ST l,        ST l-        S T l.        g )NT)support_complexr
   r   gQ?      ?c                 D   > T=R                   S-  sl         [        U 5      $ Nr
   )nlur   Aselfs    r$   luBDF.__init__.<locals>.lu   s    AAwr&   c                 $    U R                  U5      $ )N)solver:   bs     r$   r;   BDF.__init__.<locals>.solve_lu   s    xx{"r&   csc)formatdtypec                 B   > T=R                   S-  sl         [        U SS9$ )Nr
   T)overwrite_a)rP   r   rQ   s    r$   rT   rU      s    A 55r&   c                     [        XSS9$ )NT)overwrite_b)r   rX   s     r$   r;   rZ      s    488r&   r]   )r   gGzǿgqqgugsh|?r   r         )/r   super__init__r   max_stepr   nrtolatolr5   tr?   r   	directionh_absr   	h_abs_olderror_norm_oldmaxr   min
newton_tol
jac_factor_validate_jacjacr"   r   r   r]   r   identityrT   r;   r!   arrayhstackcumsumr   	MAX_ORDERgammaalphaerror_constemptyr*   r   n_equal_stepsr:   )rS   r5   t0y0t_boundrg   ri   rj   ru   jac_sparsity
vectorized
first_step
extraneousrC   rT   r;   r!   kappar*   	__class__s   `                  r$   rf   BDF.__init__   s_    	
#"z)- 	 	/)(3+D?	49HHTVVTVV$,TXXtvvtvv-4-1^^Q-1YY		CDJ
 -ZWEDJ"b3hos4/EF--c@$&DFF# DFF5=A69 DFF$&&,,7A @AYY299Q1i!m1L-L#MNO
%i4::-
 4::-BIIaQ4O0OOHHi!mTVV,DFFLLAvv!4::~.!
r&   c                   ^ ^^^ T R                   nT R                  mTc@  Tb*  [        T5      (       a  [        T5      m[	        T5      nTU4mU U4S jnU" UT5      nXV4$ [        T5      (       a  T" UT5      nT =R                  S-  sl        [        U5      (       a  [        UTR                  S9nUU U4S jnO'[        R                  " UTR                  S9nUU U4S jnUR                  T R                  T R                  4:w  a2  [        ST R                  T R                  4 SUR                   S35      e XV4$ [        T5      (       a  [        TTR                  S9nO[        R                  " TTR                  S9nUR                  T R                  T R                  4:w  a2  [        ST R                  T R                  4 SUR                   S35      eS nXV4$ )	Nc           	         > T=R                   S-  sl         TR                  X5      n[        TR                  XUTR                  TR
                  T5      u  nTl        U$ rO   )njev
fun_singler   fun_vectorizedrj   rs   )rk   r?   rC   r"   rS   sparsitys       r$   jac_wrapped&BDF._validate_jac.<locals>.jac_wrapped  sR    		Q	OOA)%,T-@-@!-1YY-5&7"4? r&   r
   rb   c                 b   > T=R                   S-  sl         [        T" X5      TR                  S9$ Nr
   rb   )r   r   r]   rk   r?   ru   rS   r   s     r$   r   r     s%    IINI%c!irxx@@r&   c                 x   > T=R                   S-  sl         [        R                  " T" X5      TR                  S9$ r   )r   r   asarrayr]   r   s     r$   r   r      s)    IINI::c!irxx@@r&   z `jac` is expected to have shape z, but actually has .)rk   r?   r   r   r	   callabler   r]   r   r   shaperh   
ValueError)rS   ru   r   r   groupsr   r"   r   s   ```    @r$   rt   BDF._validate_jac  s   VVVV;#H%%)(3H&x0$f- B#A> ~= c]]BAIINI{{q1A A JJq1A ww466466** #CTVVTVVDTCU V667ggYa"A B B + ~ }}s"((3JJs"((3ww466466** #CTVVTVVDTCU V667ggYa"A B BK~r&   c                 4   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[        X R                  X0R                  -  5        SU l        OHU R                  U:  a,  Un[        X R                  X@R                  -  5        SU l        OU R                  nU R                  nU R                  nU R                  nU R                  n	U R                  n
U R                   nU R"                  nU R$                  nU R&                  S L nSnU(       GdT  XT:  a  SU R(                  4$ XPR                  -  nUU-   nU R                  UU R*                  -
  -  S:  a;  U R*                  n[        X([        R                  " UU-
  5      U-  5        SU l        S nUU-
  n[        R                  " U5      n[        R,                  " US US-    SS9nXg[        R                  " U5      -  -   n[        R.                  " USUS-    R0                  U
SUS-    5      X   -  nSnUX   -  nU(       d  Uc!  U R3                  U R4                  UU-  -
  5      n[7        U R8                  UUUUXR:                  UU R<                  5	      u  nnnnU(       d  U(       a  OU R'                  UU5      nS nSnU(       d  M  U(       d  SnUU-  n[        X(U5        SU l        S nGM  SS	[>        -  S-   -  S	[>        -  W-   -  nXg[        R                  " W5      -  -   nX   W-  n[A        UU-  5      nUS:  a5  [C        [D        UUS
US-   -  -  -  5      nUU-  n[        X(U5        SU l        OSnU(       d  GMT  U =R                  S-  sl        WU l         WU l#        XPl        Xl        Xl        WX(S-      -
  X(S	-   '   UX(S-   '   [I        [K        US-   5      5       H  nUU==   UUS-      -  ss'   M     U R                  US-   :  a  gUS:  a  XS-
     X(   -  n[A        UW-  5      n O[        R                  n U[L        :  a  XS-      X(S	-      -  n![A        U!W-  5      n"O[        R                  n"[        RN                  " U WU"/5      n#[        RP                  " SS9   U#S
[        RR                  " XS-   5      -  -  n$S S S 5        [        RT                  " W$5      S-
  n%UU%-  nXl
        [W        [X        W[        RB                  " U$5      -  5      nU =R                  U-  sl        [        X(U5        SU l        S U l        g! , (       d  f       N= f)Nr   r   Fr
   r   TrM   g?rc   )TNignore)dividerd   )-rk   r*   rg   r   abs	nextafterrl   infrm   r.   r   r   rj   ri   r|   r{   r}   r"   r:   ru   TOO_SMALL_STEPr   sumr(   r)   rT   r!   rG   r5   r;   rr   r2   r   rp   
MIN_FACTORr?   reversedr1   rz   rw   errstater   argmaxrq   
MAX_FACTOR)&rS   rk   r*   rg   min_steprm   rj   ri   r   r|   r{   r}   r"   r:   current_jacstep_acceptedhr6   r7   r<   r9   rA   r8   n_itery_newr>   r    safetyerror
error_normierror_merror_m_normerror_perror_p_normerror_normsfactorsdelta_orders&                                         r$   
_step_implBDF._step_impl4  s   FFFF==r||A~~/FG!KLL:: EQ

Hzz$9:!"DZZ("EQ

Hzz$9:!"DJJEyyyy





&&FFWWhh$&d1111&AEE~~!56:266%!)#4u#<=%&"	AFF1IEq%!)}15I"&&"333E&&1eai**E!UQY,?@5<OCIEL A:!a%0B.>HHeY3MM4??/,+	65! !"	2AB"&K  i 6*%&"A.23q>7I9?8@ AF "&&-//E&*Eeem,JA~Z#jR5195E&FFH6*%&" !%}  -@ 	a
 1QY<'!)!)%	*+AaDAa!eHD , 	)19!!),qx7G%0L66L9!!),q|;G%0L66Lhhj,GH[[)!b299UAI+F&FGG * ii(1,
Z"&&/!9:

f
6" *)s   & V		
Vc           
          [        U R                  U R                  U R                  U R                  -  U R
                  U R                  S U R
                  S-    R                  5       5      $ rO   )BdfDenseOutputt_oldrk   rm   rl   r   r*   r0   )rS   s    r$   _dense_output_implBDF._dense_output_impl  sQ    djj$&&$**t~~2M"jj$&&$**q.*A*F*F*HJ 	Jr&   )r*   r!   r"   r:   r|   rj   r}   ro   r{   rm   rn   ru   rs   rT   rg   r   rr   r   ri   r;   rk   r?   )__name__
__module____qualname____firstlineno____doc__r   r   rf   rt   r   r   __static_attributes____classcell__r   s   @r$   rI   rI   H   s@    {x 79ff4d!d:x1fM^J Jr&   rI   c                   .   ^  \ rS rSrU 4S jrS rSrU =r$ )r   i  c                    > [         TU ]  X5        X@l        U R                  U[        R
                  " U R                  5      -  -
  U l        US[        R
                  " U R                  5      -   -  U l        XPl        g rO   )	re   rf   r   rk   r   r   t_shiftdenomr*   )rS   r   rk   r   r   r*   r   s         r$   rf   BdfDenseOutput.__init__  sZ    "
vvBIIdjj$9 99!bii

334
r&   c                    UR                   S:X  a2  XR                  -
  U R                  -  n[        R                  " U5      nO>XR                  S S 2S 4   -
  U R                  S S 2S 4   -  n[        R                  " USS9n[        R
                  " U R                  SS  R                  U5      nUR                   S:X  a  X@R                  S   -  nU$ X@R                  SS S 2S 4   -  nU$ )Nr   r   r
   )ndimr   r   r   r   r(   r*   r)   )rS   rk   xpr?   s        r$   
_call_implBdfDenseOutput._call_impl  s    66Q;\\!TZZ/A

1A\\!T'**djjD.AAA

11%AFF466!":<<#66Q;NA  1d
##Ar&   )r*   r   r   r   )r   r   r   r   rf   r   r   r   r   s   @r$   r   r     s     r&   r   )"numpyr   scipy.linalgr   r   scipy.sparser   r   r   scipy.sparse.linalgr   scipy.optimize._numdiffr	   commonr   r   r   r   r   r   r   r   baser   r   rz   r2   r   r   r%   r.   rG   rI   r    r&   r$   <module>r      sn     , 2 2 $ 1& & & ) 	

!0!"H}J) }J@[ r&   