
    5[g2M                        d dl Zd dlmZmZ d dlmZmZmZ d dl	m
Z
 d dlmZ ddlmZmZmZmZmZmZmZmZ ddlmZmZ d	Z ej4                  d
ez
  dz  d
ez   dz  dg      Z ej4                  ddez  z
  ddez  z   dg      dz  ZdZdZ ej4                  g dg dg dg      Z ej4                  g dg dg dg      Z e d    Z!e d   de d   z  z   Z" ej4                  ddez  dz  z   ddez  dz  z
  ddez  z   gddez  dz  z
  ddez  dz  z   ddez  z
  gg dg      Z#d Z$d!Z%dZ&d" Z'd# Z( G d$ d%e      Z) G d& d'e      Z*y)(    N)	lu_factorlu_solve)
csc_matrixissparseeye)splu)group_columns   )validate_max_stepvalidate_tolselect_initial_stepnormnum_jacEPSwarn_extraneousvalidate_first_step)	OdeSolverDenseOutputg.!	@   
   i      gs>H@yrr@Gg)g{g]#-?g;@L¿ghm?)g
}?gQ  ?gmؿ)r
   r
   r   )gF@gN]?gV?)gFgN]Կg!R ?)g$Z?goNg{?              ?   gUUUUUU@g   竪
@   )gUUUUUU?gUUUUUUr      g?c
                    |j                   d   }
t        |z  }t        |z  }t        j	                  |      }|}t        j                  d|
f      }|t        z  }d}t        j                  |      }d}d}t        t              D ]N  }t        d      D ]  } | |||   z   |||   z         ||<    t        j                  t        j                  |            s n|j                  j	                  t              ||d   z  z
  }|j                  j	                  t              ||d   d|d   z  z   z  z
  } |	||      } |	||      }||d<   |j                   |d<   |j"                  |d<   t%        ||z        }|||z  }||dk\  s|t        |z
  z  d|z
  z  |z  |kD  r n8||z  }t        j	                  |      }|dk(  s||d|z
  z  |z  |k  rd} n|}Q |dz   ||fS )	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.
    r   r   NFr
   r   r   T)shapeMU_REAL
MU_COMPLEXTIdotnpemptyC
empty_likerangeNEWTON_MAXITERallisfiniteTTI_REAL
TI_COMPLEXrealimagr   )funtyhZ0scaletolLU_real
LU_complexsolve_lunM_real	M_complexWZFchdW_norm_olddW	convergedratekif_real	f_complexdW_real
dW_complexdW_norms                               U/var/www/html/bid-api/venv/lib/python3.12/site-packages/scipy/integrate/_ivp/radau.pysolve_collocation_systemrP   0   s   N 	

Aq[FQI
r
A
A
!QA	
QBK	q	BID>"qAq2a5y!ad(+AaD  vvbkk!n%!FQqTM1CCGGJ')qtb1Q4i7G*HH	7F+j)4
111rEz""[(D$!)!+,D9GCcI	REE!HqL TQX%6%@3%FIC #F a!eQ$$    c                     |||dk(  rd}n| |z  ||z  dz  z  }t        j                  d      5  t        d|      |dz  z  }ddd       |S # 1 sw Y   S xY w)a9  Predict 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.
    Nr   r
   g      ?ignore)divideg      п)r&   errstatemin)h_abs	h_abs_old
error_normerror_norm_old
multiplierfactors         rO   predict_factorr]      sn    : !2jAo
Y&.:*E$)NN
	H	%Q
#jE&99 
& M 
& Ms   AAc                   ^     e Zd ZdZej
                  ddddddf fd	Zd Zd Zd	 Z	d
 Z
 xZS )Radaua  Implicit 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                        \   _         _	         j                   j                   j                         _        |`t         j                   j                   j                  || j                   j                  d j                   j                  
       _        nt#        |||       _        d  _        d  _        t)        dt*        z  |z  t-        d|dz               _        d  _        d  _         j5                  ||	      \   _         _        t;         j8                        r  fd}d }t=         j                  d      }n' fd	}d
 }t?        j@                   j                        }| _!        | _"        | _#        d _$        d  _%        d  _&        d  _'        y )Nr   r   gQ?      ?c                 D    xj                   dz  c_         t        |       S Nr
   )nlur   Aselfs    rO   luzRadau.__init__.<locals>.luA  s    AAwrQ   c                 $    | j                  |      S N)solveLUbs     rO   r<   z Radau.__init__.<locals>.solve_luE  s    xx{"rQ   csc)formatc                 H    xj                   dz  c_         t        | d      S )Nr
   T)overwrite_a)re   r   rf   s    rO   ri   zRadau.__init__.<locals>.luJ  s    A 55rQ   c                     t        | |d      S )NT)overwrite_b)r   rm   s     rO   r<   z Radau.__init__.<locals>.solve_luN  s    A488rQ   T)(r   super__init__y_oldr   max_stepr   r=   rtolatolr3   r4   r5   fr   	directionrW   r   rX   rZ   maxr   rV   
newton_tolsol
jac_factor_validate_jacjacJr   r   r&   identityri   r<   Icurrent_jacr:   r;   rA   )rh   r3   t0y0t_boundry   rz   r{   r   jac_sparsity
vectorized
first_step
extraneousri   r<   r   	__class__s   `               rO   rw   zRadau.__init__'  s    	
#b"gz:
)(3+D$?	49$&&$&&) ,$&&$&&'8TVVT^^499dii)DJ -ZWEDJ"b3hos4/EF--c<@$&DFF# DFF5)A69 DFF#A rQ   c                 t     j                   } j                  }E%t              rt              t	              }|f fd} ||| j
                        }||fS t              r ||      }d _        t        |      rt        |      }d fd	}n"t        j                  |t              }d fd	}|j                   j                   j                  fk7  r;t        dj                   j                   j                  f|j                              ||fS t              rt              }nt        j                  t              }|j                   j                   j                  fk7  r;t        dj                   j                   j                  f|j                              d }||fS )Nc           	          xj                   dz  c_         t        j                  | ||j                  j                        \  }_        |S rd   )njevr   fun_vectorizedr{   r   )r4   r5   r|   r   rh   sparsitys       rO   jac_wrappedz(Radau._validate_jac.<locals>.jac_wrappedg  sF    		Q	%,T-@-@!Q-1YY-5&7"4? rQ   r
   c                 ^    xj                   dz  c_         t         | |      t              S Nr
   dtype)r   r   floatr4   r5   _r   rh   s      rO   r   z(Radau._validate_jac.<locals>.jac_wrappedt  s#    IINI%c!Qiu==rQ   r   c                 r    xj                   dz  c_         t        j                   | |      t              S r   )r   r&   asarrayr   r   s      rO   r   z(Radau._validate_jac.<locals>.jac_wrapped{  s'    IINI::c!Qiu==rQ   z8`jac` is expected to have shape {}, but actually has {}.rk   )r4   r5   r   r   r	   r|   callabler   r&   r   r   r!   r=   
ValueErrorrq   )rh   r   r   r   r   groupsr   r   s   ```     rO   r   zRadau._validate_jac\  s   VVVV;#H%)(3H&x0$f- BDFF+AD A~C c]BADI{qM>
 JJq.> ww466466**  "4"(&$&&$&&)9177"CE E A~ }sOJJs%0ww466466**  "4"(&$&&$&&)9177"CE E KA~rQ   c                 	   | j                   }| j                  }| j                  }| j                  }| j                  }| j
                  }dt        j                  t        j                  || j                  t        j                  z        |z
        z  }| j                  |kD  r|}d }	d }
n:| j                  |k  r|}d }	d }
n$| j                  }| j                  }	| j                  }
| j                  }| j                  }| j                   }| j"                  }| j$                  }d}d}d }|s||k  rd| j&                  fS || j                  z  }||z   }| j                  || j(                  z
  z  dkD  r| j(                  }||z
  }t        j                  |      }| j*                  %t        j,                  d|j.                  d   f      }n(| j+                  ||t0        z  z         j2                  |z
  }|t        j                  |      |z  z   }d}|s||P| j5                  t6        |z  | j8                  z  |z
        }| j5                  t:        |z  | j8                  z  |z
        }t=        | j>                  |||||| j@                  ||| jB                  
      \  }}}}|s|rn| j%                  |||      }d}d }d }|s|s|dz  }d }d }|d   z   }|j2                  jE                  tF              |z  }| jC                  |||z         }|t        jH                  t        j                  |      t        j                  |            |z  z   }tK        ||z        }dd	tL        z  d
z   z  d	tL        z  z   z  }|r;|d
kD  r6| jC                  || j?                  |||z         |z         }tK        ||z        }|d
kD  r+tO        ||	||
      } |tQ        tR        || z        z  }d }d }d}nd}|s|d uxr d	kD  xr dkD  }!tO        ||	|
      } tU        tV        | z        } |!s| dk  rd
} nd }d }| j?                        }"|!r ||||"      }d}n|d}| j                  | _        || _        || z  | _        || _,        || _         || _        |"| _        | _-        || _        || _        || _        || _        || _.        | j_                         | _        ||fS )Nr   Fr   r   Trb   r   g?r   r
   r`   g333333?)0r4   r5   r|   ry   r{   rz   r&   abs	nextafterr}   infrW   rX   rZ   r   r:   r;   r   r   TOO_SMALL_STEPr   r   zerosr!   r(   r.   ri   r"   r   r#   rP   r3   r   r<   r%   Emaximumr   r+   r]   r~   
MIN_FACTORrV   
MAX_FACTORrx   rA   t_old_compute_dense_output)#rh   r4   r5   r|   ry   r{   rz   min_steprW   rX   rZ   r   r:   r;   r   r   rejectedstep_acceptedmessager6   t_newr7   r8   rF   n_iterrA   rG   y_newZEerrorrY   safetyr\   recompute_jacf_news#                                      rO   
_step_implzRadau._step_impl  s   FFFFFF==yyyyr||At~~/FG!KLL:: EI!NZZ("EI!NJJEI!00NFF,,__
&&hhxd1111&AEE~~!56:	AFF1IExxXXq!''!*o.XXa!a%i(**Q.266!9t++EI?j&8"gggkDFF&:Q&>?G!%a$&&)@1)D!EJ-EHHaAr5$//Z.8*	61d !"Aq)A"&K"G!%J!  $ !
"IEaBMM'1r62E2::bffQi?$FFEeem,JA.23q>7I9?8@ AF JNgtxx1u9/E/JK!%%-0
A~'y(2NDZ&99!
 $E  H 4FFQJF4$;y*nMZ&1#FGJ&E5%(AK_K(V^

$&
--/g%%rQ   c                     t        j                  | j                  j                  t              }t        | j                  | j                  | j                  |      S rk   )	r&   r%   rA   r.   PRadauDenseOutputr   r4   rx   )rh   Qs     rO   r   zRadau._compute_dense_output  s7    FF46688Q

DFFDJJBBrQ   c                     | j                   S rk   )r   )rh   s    rO   _dense_output_implzRadau._dense_output_impl#  s    xxrQ   )__name__
__module____qualname____doc__r&   r   rw   r   r   r   r   __classcell__r   s   @rO   r_   r_      s?    rf 79ff4d!d3j3jL&\CrQ   r_   c                   $     e Zd Z fdZd Z xZS )r   c                     t         |   ||       ||z
  | _        || _        |j                  d   dz
  | _        || _        y rd   )rv   rw   r6   r   r!   orderrx   )rh   r   r4   rx   r   r   s        rO   rw   zRadauDenseOutput.__init__(  s>    "UWWQZ!^

rQ   c                    || j                   z
  | j                  z  }|j                  dk(  r9t        j                  || j
                  dz         }t        j                  |      }n<t        j                  || j
                  dz   df      }t        j                  |d      }t        j                  | j                  |      }|j                  dk(  r|| j                  d d d f   z  }|S || j                  z  }|S )Nr   r
   )axisr   )
r   r6   ndimr&   tiler   cumprodr%   r   rx   )rh   r4   xpr5   s        rO   
_call_implzRadauDenseOutput._call_impl/  s    ^tvv%66Q;4::>*A

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

11%AFF466166Q;AtG$$A  OArQ   )r   r   r   rw   r   r   r   s   @rO   r   r   '  s    rQ   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   S6arrayr(   r   r"   r#   r.   r$   r/   r0   r   r+   r   r   rP   r]   r_   r    rQ   rO   <module>r      s    , 2 2 $ 1* * * ) BHHq2vma"f]A./BHHcAFlC!b&L"-.2 *5
 BHHDD  RXXCEDF G
 Q%UR"Q%Z
 BHH	AbDF]EBrE!GOTAF]3	AbDF]EBrE!GOTAF]3  

X%v%PqI qh{ rQ   