L i]DddlZddlmZmZddlmZmZmZddl m Z ddl m Z ddl mZmZmZmZmZmZmZmZddlmZmZd Zd Zd Zd Zd ZdZdZGddeZ GddeZ!y)N) lu_factorlu_solve)issparse csc_matrixeye)splu) group_columns)validate_max_step validate_tolselect_initial_stepnormEPSnum_jacvalidate_first_stepwarn_extraneous) OdeSolver DenseOutputg? ctjd|dzdddf}tjd|dz}tj|dz|dzf}|dz ||zz |z |ddddf<d|d<tj|dS)z6Compute the matrix for changing the differences array.r Nraxis)nparangezeroscumprod)orderfactorIJMs ^/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/scipy/integrate/_ivp/bdf.py compute_Rr%s !UQY4(A !UQYA %!)UQY'(AQ!#q(Aab!"fI AaD ::aa  ct||}t|d}|j|}tj|j|d|dz|d|dzy)z " qMvvbkk!n%  b!a%#+/ *rEz"  D[(D  $!)!+,D9GCcI  R R qL TQX%6%@3%FI  36 a!eQ !!r&cXeZdZdZej ddddddffd ZdZdZd Z xZ S) BDFaImplicit 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 0t| t ||||| dt|_t ||j \__jjj} | Vtjjj||| jdjj _nt| ||_d_d_t%dt&z|z t)d|dz_d_j/|| \__t5j2r5fd}d}t7j d jj8 }n=fd }d }t;j<j jj8 }|_|_ |_!t;jDgd}t;jFdt;jHdt;jJdtLdzz f_'d|z jNz_(|jNzdt;jJdtLdzz z_)t;jTtLdzj fjj8 }j|d<| jzjz|d<|_+d_,d_-d_.y)NT)support_complexr rgQ??cDxjdz c_t|SNr )nlurAselfs r$luzBDF.__init__..lusA Awr&c$|j|S)N)solver:bs r$r;zBDF.__init__..solve_lusxx{"r&csc)formatdtypecHxjdz c_t|dS)Nr T) overwrite_a)rOrrPs r$rSzBDF.__init__..lusA  55r&ct||dS)NT) overwrite_b)rrVs r$r;zBDF.__init__..solve_lusA488r&rZ)rgGzǿgqqgugsh|?rr)/rsuper__init__r max_stepr nrtolatolr5tr?r directionh_absr h_abs_olderror_norm_oldmaxrmin newton_tol jac_factor _validate_jacjacr"rrrZridentityrSr;r!arrayhstackcumsumr MAX_ORDERgammaalpha error_constemptyr*r n_equal_stepsr:)rRr5t0y0t_boundrdrfrgrr jac_sparsity vectorized first_step extraneousrCrSr;r!kappar* __class__s` r$rcz BDF.__init__sh  # b"gz)-  /)(3 +D$? 49 HHTVVTVV $  ,TXXtvvtvv-4h-1^^Q-1YY CDJ -ZWEDJ"b3hos4/EF--c<@$& DFF   #DFF5 =A 6 9 DFF$&&,,7A  @AYY299Q1i!m1L-L#MNO %i4::-  4::-BIIaQ4O0OO HHi!mTVV,DFFLL Avv!4::~.! r&cj}j:%tr tt }|ffd}||}||fSt r|}xj dz c_t|rt|j}fd}n(tj|j}fd}|jjjfk7r2tdjjfd|jd||fStrtj}n!tjj}|jjjfk7r2tdjjfd|jdd}||fS) Nc xjdz c_j||}tj|||jj \}_|SrN)njev fun_singlerfun_vectorizedrgrp)rhr?rCr"rRsparsitys r$ jac_wrappedz&BDF._validate_jac..jac_wrapped sV Q OOAq)%,T-@-@!Q-1YY-5&7"4?r&r r_cjxjdz c_t||jSNr r_)rrrZrhr?rrrRr~s r$rz&BDF._validate_jac..jac_wrappeds'IINI%c!Qirxx@@r&c~xjdz c_tj||jSr)rrasarrayrZrs r$rz&BDF._validate_jac..jac_wrapped s+IINI::c!Qirxx@@r&z `jac` is expected to have shape z, but actually has .) rhr?rrr callablerrZrrshapere ValueError)rRrrrr}groupsrr"r~s``` @r$rqzBDF._validate_jacs VV VV ;#H%)(3H&x0$f- B#A>A~=c]B A IINI{q1AJJq1Aww466466** #CTVVTVVDTCUV667ggYa"ABBA~}s"((3JJs"((3ww466466** #CTVVTVVDTCUV667ggYa"ABBKA~r&c  |j}|j}|j}dtjtj ||j tjz|z z}|j|kDr.|}t||j||jz d|_ nI|j|kr.|}t||j||jz d|_ n |j}|j}|j}|j}|j} |j} |j } |j"} |j$} |j&du}d}|s7||krd|j(fS||j z}||z}|j ||j*z zdkDr;|j*}t||tj||z |z d|_ d} ||z }tj|}tj,|d|dzd}||tj|zz}tj.|d|dzj0| d|dz| |z }d}|| |z }|sw| !|j3|j4|| zz } t7|j8||||| |j:||j< \}}}}|s|rn|j'||} d} d}|sw|sd}||z}t|||d|_ d} dd t>zdzzd t>zzz }||tjzz}| |z}tA||z }|dkDr6tCtD||d |dzz zz}||z}t|||d|_ nd}|s7|xjdz c_ |_|_#||_| |_| |_||dzz ||d z<|||dz<tItK|dzD]}||xx||dzz cc<|j|dzkry |dkDr| |dz ||z}tA|z } ntj} |tLkr | |dz||d zz}!tA|!z }"ntj}"tjN| |"g}#tjPd 5|#d tjR||dzz z}$dddtjT$dz }%||%z }||_ tWtXtjB|$z}|xj|zc_t|||d|_ d|_y #1swYxYw)NrrFr rTrLg?r`)TNignore)dividera)-rhr*rdrabs nextafterriinfrjr.rr|rgrfryrxrzr"r:rrTOO_SMALL_STEPrsumr(r)rSr!rGr5r;ror2rrm MIN_FACTORr?reversedr1rwrterrstaterargmaxrn MAX_FACTOR)&rRrhr*rdmin_steprjrgrfrryrxrzr"r: current_jac step_acceptedhr6r7r<r9rAr8n_itery_newr>r safetyerror error_normierror_m error_m_normerror_p error_p_norm error_normsfactors delta_orders& r$ _step_implzBDF._step_impl4se FF FF==r||At~~/FG!KLL :: E Q Htzz$9 :!"D  ZZ( "E Q Htzz$9 :!"D JJEyyyy   && FF WWhh$&  xd1111&AEE~~!56: E266%!)#4u#<=%&" AFF1IEq%!)}15I4"&&"333E&&1eai**E!UQY,?@5<OCIE%L A:!a%0B.>HHeY3DMM4??/,+ 65!!" 2AB"&K E6*%&"A.23q>7I9?8@AF4"&&-//E&*Eeem,JA~Z#jR5195E&FFHE6*%&"!% } @ a  1UQY<'%!) %!) % *+ A aDAa!eH D     ) 19!%!),qx7G%0L66L 9 !%!),q|;G%0L66Lhh j,GH [[ ) H!b299UEAI+F&FGG Hii(1,   Z"&&/!9: f E6" H Hs U..U7c t|j|j|j|jz|j |j d|j dzjSrN)BdfDenseOutputt_oldrhrjrirr*r0)rRs r$_dense_output_implzBDF._dense_output_implsQdjj$&&$**t~~2M"jj$&&$**q.*A*F*F*HJ Jr&) __name__ __module__ __qualname____doc__rrrcrqrr __classcell__rs@r$rIrIHs:{x79ff4d!d:x1fM^Jr&rIc$eZdZfdZdZxZS)rct|||||_|j|t j |jzz |_|dt j |jzz|_||_yrN) rbrcrrhrrt_shiftdenomr*)rRrrhrrr*rs r$rczBdfDenseOutput.__init__s` " vvBIIdjj$9 99 !bii 334 r&c|jdk(r2||jz |jz }tj|}nA||jdddfz |jdddfz }tj|d}tj |j ddj|}|jdk(r||j dz }|S||j ddddfz }|S)Nrrr )ndimrrrrr(r*r))rRrhxpr?s r$ _call_implzBdfDenseOutput._call_impls 66Q;T\\!TZZ/A 1 AT\\!T'**djjD.AAA 11%A FF466!":<< # 66Q; NA 1d # #Ar&)rrrrcrrrs@r$rrs r&r)"numpyr scipy.linalgrr scipy.sparserrrscipy.sparse.linalgrscipy.optimize._numdiffr commonr r r rrrrrbaserrrwr2rrr%r.rGrIrr&r$rsn,22$1&&&)     !0!"H}J)}J@ [r&