
    ^MhL                        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j        d
ez
  dz  d
ez   dz  dg          Z ej        ddez  z
  ddez  z   dg          dz  ZdZdZ ej        g dg dg dg          Z ej        g dg dg dg          Z e d          Z!e d         de d         z  z   Z" ej        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*dS )(    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                              |          }|}t          j        d|
f          }|t          z  }d}t          j        |          }d}d}t          t                    D ]k}t          d          D ]#} | |||         z   |||         z             ||<   $t          j        t          j        |                    s n|j                            t                    ||d         z  z
  }|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  |k    r n>||z  }t                              |          }|dk    s||d|z
  z  |z  |k     rd} n|}m||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                               Z/var/www/html/test/jupyter/venv/lib/python3.11/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3q2a5y!ad(++AaDDvbk!nn%% 	E!!FQqTM1CGGJ'')qtb1Q4i7G*HH	(7F++Xj)44
111rEz"""[(D$!))!+,D9GCcIIE	REE!HHqLL TQX%6%@3%F%FIE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           n# 1 swxY w Y   |S )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]      s    : !2jAoo

Y&.:*E$)NN
	H	%	%	% : :Q
##jE&99: : : : : : : : : : : : : : : Ms   AAAc                   T     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                   _        |At           j
         j         j        || j         j        d j         j	        
  
         _        nt#          |||           _        d  _        d  _        t)          dt*          z  |z  t-          d|dz                       _        d  _        d  _                             ||	          \   _         _        t;           j                  r fd}d }t=           j        d          }n! fd	}d
 }t?          j          j                  }| _!        | _"        | _#        d _$        d  _%        d  _&        d  _'        d S )Nr   r   gQ?      ?c                 B    xj         dz  c_         t          |           S Nr
   )nlur   Aselfs    rO   luzRadau.__init__.<locals>.luA  s    AAwwrQ   c                 ,    |                      |          S N)solveLUbs     rO   r<   z Radau.__init__.<locals>.solve_luE  s    xx{{"rQ   csc)formatc                 F    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555rQ   c                 &    t          | |d          S )NT)overwrite_b)r   rm   s     rO   r<   z Radau.__init__.<locals>.solve_luN  s    A488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3+D$??	49$&$&)) ,$&$&'8TVT^49di) )DJJ -ZWEEDJ"b3hos4/E/EFF--c<@@$&DF 	$    # # # DF5)))AA6 6 6 6 69 9 9 DF##A rQ   c                      j         } j        }M1t                    rt                    t	                    }|f fd} ||| j                  }n#t                    r ||          }d _        t          |          rt          |          }d	 fd	}n"t          j	        |t                    }d	 fd	}|j         j         j        fk    r't          d j         j        f d|j         d          nzt                    rt                    }nt          j	        t                    }|j         j         j        fk    r't          d j         j        f d|j         d          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-1Y-5&7 &7"4? rQ   r
   c                 d    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NII%cc!Qiiu====rQ   r   c                 n    xj         dz  c_         t          j         | |          t                    S r   )r   r&   asarrayr   r   s      rO   r   z(Radau._validate_jac.<locals>.jac_wrapped{  s1    IINII:cc!Qiiu====rQ   z `jac` is expected to have shape z, but actually has .rk   )r4   r5   r   r   r	   r|   callabler   r&   r   r   r!   r=   
ValueError)rh   r   r   r   r   groupsr   r   s   ```     rO   r   zRadau._validate_jac\  s,   VV;#H%% 4)(33H&x00$f-      BDF++AAc]] 	BADI{{ >qMM> > > > > > > >
 Jq...> > > > > > > w4646***  "ATVTVDT "A "A67g"A "A "A B B B + }} 1sOOJs%000w4646***  "ATVTVDT "A "A67g"A "A "A B B BKA~rQ   c                    | j         }| j        }| j        }| j        }| j        }| j        }dt          j        t          j        || j	        t          j
        z            |z
            z  }| j        |k    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k    r| j        }||z
  }t          j        |          }| j        "t          j        d|j        d         f          }n(|                     ||t0          z  z             j        |z
  }|t          j        |          |z  z   }d}|s||P|                     t6          |z  | j        z  |z
            }|                     t:          |z  | j        z  |z
            }t=          | j        |||||| j         ||| j!        
  
        \  }}}}|s |rn|                     |||          }d}d }d }||s|dz  }d }d }w||d         z   }|j        "                    tF                    |z  }| !                    |||z             }|t          j$        t          j        |          t          j        |                    |z  z   }tK          ||z            }dd	tL          z  d
z   z  d	tL          z  |z   z  }|rH|d
k    rB| !                    ||                     |||z             |z             }tK          ||z            }|d
k    r4tO          ||	||
          } |tQ          tR          || z            z  }d }d }d}nd}||d uo|d	k    o|dk    }!tO          ||	||
          } tU          tV          || z            } |!s	| dk     rd
} nd }d }|                     ||          }"|!r ||||"          }d}n|d}| j        | _        || _        || z  | _        || _,        || _         || _        |"| _        || _-        || _        || _        || _        || _        || _.        | /                                | _        ||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   FFF=yyr|At~/FGG!KLLL:  EI!NNZ(""EI!NNJEI!0NF,_
&h B	%xd111&AEE~!56::	AF1IIExXq!'!*o..XXa!a%i((*Q.26!99t++EI &?j&8"gggkDF&:Q&>??G!%a$&)@1)D!E!EJ-EHaAr5$/Z.8 .8*	61d ! &" Aq))A"&K"G!%J!   &$  !
"IEaBMM'1r622E2:bfQii??$FFEeem,,JA.23q>7I9?8@ AF  1JNNgtxx1u9/E/E/JKK!%%-00
A~~'y(2ND DZ&999!
 $E   B	%H 4FFQJF4$;y*nMMZ&11 	#FFGJ&& 	 E5%((AKK_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  s1    F468Q
DFDJBBBrQ   c                     | j         S rk   )r   )rh   s    rO   _dense_output_implzRadau._dense_output_impl!  s	    xrQ   )__name__
__module____qualname____doc__r&   r   rw   r   r   r   r   __classcell__r   s   @rO   r_   r_      s        r rf 79f4d!d3 3 3 3 3 3j1 1 1fL& L& L&\C C C      rQ   r_   c                   $     e Zd Z fdZd Z xZS )r   c                     t                                          ||           ||z
  | _        || _        |j        d         dz
  | _        || _        d S rd   )rv   rw   r6   r   r!   orderrx   )rh   r   r4   rx   r   r   s        rO   rw   zRadauDenseOutput.__init__&  sK    """UWQZ!^



rQ   c                    || j         z
  | j        z  }|j        dk    r2t          j        || j        dz             }t          j        |          }n5t          j        || j        dz   df          }t          j        |d          }t          j        | j        |          }|j        dk    r|| j	        d d d f         z  }n
|| 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    ^tv%6Q;;4:>**A
1AADJNA.//A
11%%%AF4616Q;;AAAtG$$AAOArQ   )r   r   r   rw   r   r   r   s   @rO   r   r   %  sG                  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 2 2 2 2 2 2 2 2 $ $ $ $ $ $ 1 1 1 1 1 1* * * * * * * * * * * * * * * * * * * * ) ( ( ( ( ( ( ( BHq2vma"f]A.//BHcAFlC!b&L"-..2 *5
 BHDDDDDDII   RXCCCEEEDDDF G G
 Q%UR"Q%Z
 BH	AbDF]EBrE!GOTAF]3	AbDF]EBrE!GOTAF]3   

X% X% X%v% % %Po o o o oI o o od    {     rQ   