L iLddlZddlmZmZddlmZmZmZddl m Z ddl m Z ddl mZmZmZmZmZmZmZmZddlmZmZd Zej4d ez d z d ezd z dgZej4d d ezz d d ezzdgdz ZdZdZej4gdgdgdgZej4gdgdgdgZ e dZ!e dde dzzZ"ej4dd ezdz zddezdz z ddezzgdd ezdz z ddezdz zddezz ggdgZ#d Z$d!Z%d Z&d"Z'd#Z(Gd$d%eZ)Gd&d'eZ*y)(N) lu_factorlu_solve) csc_matrixissparseeye)splu) group_columns)validate_max_step validate_tolselect_initial_stepnormnum_jacEPSwarn_extraneousvalidate_first_step) OdeSolver DenseOutputg.! @ igs>H @yrr@Gg)g{g]#-?g;@L¿ghm?)g }?gQ  ?gmؿ)r r r)gF@gN]?gV?)gFgN]Կg!R?)g$Z?goNg{??gUUUUUU@g竪 @)gUUUUUU?gUUUUUUrg?c |jd} t|z } t|z } tj |} |}t j d| f}|tz}d}t j| }d}d}ttD]N}tdD]}||||z|||z||<t jt j|sn|jj t| | dzz }|jj t| | dd| dzzzz }| ||}| ||}||d<|j |d<|j"|d<t%||z }|||z }||dk\s|t|z zd|z z |z|kDrn8| |z } tj | }|dk(s||d|z z |z|krd}n|}Q|dz||fS) a^Solve the collocation system. Parameters ---------- fun : callable Right-hand side of the system. t : float Current time. y : ndarray, shape (n,) Current state. h : float Step to try. Z0 : ndarray, shape (3, n) Initial guess for the solution. It determines new values of `y` at ``t + h * C`` as ``y + Z0``, where ``C`` is the Radau method constants. scale : ndarray, shape (n) Problem tolerance scale, i.e. ``rtol * abs(y) + atol``. tol : float Tolerance to which solve the system. This value is compared with the normalized by `scale` error. LU_real, LU_complex LU decompositions of the system Jacobians. solve_lu : callable Callable which solves a linear system given a LU decomposition. The signature is ``solve_lu(LU, b)``. Returns ------- converged : bool Whether iterations converged. n_iter : int Number of completed iterations. Z : ndarray, shape (3, n) Found solution. rate : float The rate of convergence. rrNFr rrT)shapeMU_REAL MU_COMPLEXTIdotnpemptyC empty_likerangeNEWTON_MAXITERallisfiniteTTI_REAL TI_COMPLEXrealimagr)funtyhZ0scaletolLU_real LU_complexsolve_lunM_real M_complexWZFch dW_norm_olddW convergedratekif_real f_complexdW_real dW_complexdW_norms `/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/scipy/integrate/_ivp/radau.pysolve_collocation_systemrP0sN  A q[FQI r A A !QA QBK q BI D > "!q ,Aq2a5y!ad(+AaD ,vvbkk!n% !FQqTM1CCGGJ')qtb1Q4i7G*HH 7F+j)4 111rEz"  "[(D  $!)!+,D9GCcI  R EE!H qL TQX%6%@3%FI  C!F a!eQ $$c|||dk(rd}n||z ||z dzz}tjd5td||dzz}ddd|S#1swYSxYw)a9Predict by which factor to increase/decrease the step size. The algorithm is described in [1]_. Parameters ---------- h_abs, h_abs_old : float Current and previous values of the step size, `h_abs_old` can be None (see Notes). error_norm, error_norm_old : float Current and previous values of the error norm, `error_norm_old` can be None (see Notes). Returns ------- factor : float Predicted factor. Notes ----- If `h_abs_old` and `error_norm_old` are both not None then a two-step algorithm is used, otherwise a one-step algorithm is used. References ---------- .. [1] E. Hairer, S. P. Norsett G. Wanner, "Solving Ordinary Differential Equations II: Stiff and Differential-Algebraic Problems", Sec. IV.8. Nrr g?ignore)dividegп)r&errstatemin)h_abs h_abs_old error_normerror_norm_old multiplierfactors rOpredict_factorr]ss:!2jAo Y&.:*E$)NN H %:Q #jE&99: M: Ms AAc^eZdZdZej ddddddffd ZdZdZd Z d Z xZ S) RadauaImplicit Runge-Kutta method of Radau IIA family of order 5. The implementation follows [1]_. The error is controlled with a third-order accurate embedded formula. A cubic polynomial which satisfies the collocation conditions is used for the dense output. 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)``. HHere `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 [2]_. 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] E. Hairer, G. Wanner, "Solving Ordinary Differential Equations II: Stiff and Differential-Algebraic Problems", Sec. IV.8. .. [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, pp. 117-120, 1974. MbP?gư>NFc t| t ||||| d_t |_t ||j\__ jjj_ | `tjjj||jjdjj _nt#| ||_d_d_t)dt*z|z t-d|dz_d_d_j5|| \__t;j8r fd} d}t=jd}n'fd } d }t?j@j}| _!|_"|_#d _$d_%d_&d_'y) NrrgQ??cDxjdz c_t|SNr )nlurAselfs rOluzRadau.__init__..luAsA AwrQc$|j|SN)solveLUbs rOr<z Radau.__init__..solve_luEsxx{"rQcsc)formatcHxjdz c_t|dS)Nr T) overwrite_a)rerrfs rOrizRadau.__init__..luJsA  55rQct||dS)NT) overwrite_b)rrms rOr<z Radau.__init__..solve_luNsA488rQT)(rsuper__init__y_oldr max_stepr r=rtolatolr3r4r5fr directionrWrrXrZmaxrrV newton_tolsol jac_factor _validate_jacjacJrrr&identityrir<I current_jacr:r;rA)rhr3t0y0t_boundryrzr{r jac_sparsity vectorized first_step extraneousrir<r __class__s` rOrwzRadau.__init__'s  # b"gz: )(3 +D$? 49$&&$&&)  ,$&&$&&'8TVVT^^499dii)DJ-ZWEDJ"b3hos4/EF--c<@$& DFF   #DFF5)A 6 9 DFF#A   rQcPj}j}E%tr tt }|ffd}|||j }||fSt r||}d_t|rt|}d fd }n"tj|t}d fd }|jjjfk7r2tdjjfd|jd||fStr t}ntjt}|jjjfk7r2tdjjfd|jdd}||fS) Nc xjdz c_tj|||jj\}_|Srd)njevrfun_vectorizedr{r)r4r5r|rrhsparsitys rO jac_wrappedz(Radau._validate_jac..jac_wrappedgsF Q %,T-@-@!Q-1YY-5&7"4?rQr c^xjdz c_t||tSNr dtype)rrfloatr4r5_rrhs rOrz(Radau._validate_jac..jac_wrappedts#IINI%c!Qiu==rQrcrxjdz c_tj||tSr)rr&asarrayrrs rOrz(Radau._validate_jac..jac_wrapped{s'IINI::c!Qiu==rQz `jac` is expected to have shape z, but actually has .rk)r4r5rrr r|callablerr&rrr!r= ValueError)rhrrrrgroupsrrs``` rOrzRadau._validate_jac\s VV VV ;#H%)(3H&x0$f-  BDFF+A@A~?c]B ADI{qM> JJq.>ww466466** #CTVVTVVDTCUV667ggYa"ABBA~}sOJJs%0ww466466** #CTVVTVVDTCUV667ggYa"ABBKA~rQc  |j}|j}|j}|j}|j}|j }dt jt j||jt jz|z z}|j|kDr|}d} d} n:|j|kr|}d} d} n$|j}|j} |j} |j} |j} |j } |j"}|j$}d}d}d}|s||krd|j&fS||jz}||z}|j||j(z zdkDr |j(}||z }t j|}|j*%t j,d|j.df}n(|j+||t0zzj2|z }|t j||zz}d}|s| | P|j5t6|z |j8z| z } |j5t:|z |j8z| z } t=|j>||||||j@| | |jB \}}}}|s|rn|j%|||} d}d} d} |s|s |dz}d} d} |dz}|j2jEtF|z }|jC| ||z}|t jHt j|t j||zz}tK||z }dd tLzd zzd tLzzz }|r;|d kDr6|jC| |j?|||z|z}tK||z }|d kDr+tO|| || } |tQtR|| zz}d} d} d}nd}|s|duxr d kDxrd kD}!tO|| | } tUtV| z} |!s| d krd } nd} d} |j?}"|!r ||||"} d}n|d}|j|_ ||_ || z|_ ||_,||_||_|"|_|_-| |_| |_||_| |_||_.|j_|_||fS) NrFrrTrbrg?rr r`g333333?)0r4r5r|ryr{rzr&abs nextafterr}infrWrXrZrr:r;rrTOO_SMALL_STEPrrzerosr!r(r.rir"rr#rPr3rr<r%Emaximumrr+r]r~ MIN_FACTORrV MAX_FACTORrxrAt_old_compute_dense_output)#rhr4r5r|ryr{rzmin_steprWrXrZrr:r;rrrejected step_acceptedmessager6t_newr7r8rFn_iterrArGy_newZEerrorrYsafetyr\ recompute_jacf_news# rO _step_implzRadau._step_impls FF FF FF==yyyyr||At~~/FG!KLL :: EI!N ZZ( "EI!NJJEI!00N FF,,__ && hh xd1111&AEE~~!56:  AFF1IExxXXq!''!*o.XXa!a%i(**Q.266!9t++EI?j&8"gggkDFF&:Q&>?G!%a$&&)@1)D!EJ-EHHaAr5$//Z.8* 61d!"Aq)A"&K"G!%J! $ ! "IEaBMM'1r62E2::bffQi?$FFEeem,JA.23q>7I9?8@AFJN gtxx1u9/E/JK!%%-0 A~'y(2NDZ&99!  $ E H4FFQJF4$; y*nMZ&1#FGJ& E5%(AK _K(V^   $& --/g%%rQctj|jjt}t |j |j|j|Srk) r&r%rAr.PRadauDenseOutputrr4rx)rhQs rOrzRadau._compute_dense_outputs7 FF46688Q  DFFDJJBBrQc|jSrk)r)rhs rO_dense_output_implzRadau._dense_output_impl!s xxrQ) __name__ __module__ __qualname____doc__r&rrwrrrr __classcell__rs@rOr_r_s?rf79ff4d!d3j1fL&\CrQr_c$eZdZfdZdZxZS)rct|||||z |_||_|jddz |_||_yrd)rvrwr6rr!orderrx)rhrr4rxrrs rOrwzRadauDenseOutput.__init__&s> "UWWQZ!^  rQc||jz |jz }|jdk(r9tj||j dz}tj |}n*A 1 ADJJNA./A 11%A FF4661  66Q; AtG$ $A OArQ)rrrrwrrrs@rOrr%s rQr)+numpyr& scipy.linalgrr scipy.sparserrrscipy.sparse.linalgrscipy.optimize._numdiffr commonr r r rrrrrbaserrS6arrayr(rr"r#r.r$r/r0rr+rrrPr]r_rrQrOrs,22$1***)  BHHq2vma"f]A ./ BHHcAFlC!b&L" -.2 *5  BHHDD RXXCEDFG Q% UR"Q%Z   BHH AbDF]EBrE!GOTAF]3 AbDF]EBrE!GOTAF]3    X%v%PoIod {rQ