U
    »mœdäV  ã                   @   s¼   d dl ZddlmZmZ ddlmZmZmZm	Z	m
Z
mZ ddlmZ dZdZdZd	d
„ ZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZdS )é    Né   )Ú	OdeSolverÚDenseOutput)Úvalidate_max_stepÚvalidate_tolÚselect_initial_stepÚnormÚwarn_extraneousÚvalidate_first_step)Údop853_coefficientsgÍÌÌÌÌÌì?çš™™™™™É?é
   c	                 C   s°   ||d< t t|dd… |dd… ƒdd�D ]H\}	\}
}t |d|	… j|
d|	… ¡| }| |||  || ƒ||	< q*||t |dd… j|¡  }| || |ƒ}||d< ||fS )a8  Perform a single Runge-Kutta step.

    This function computes a prediction of an explicit Runge-Kutta method and
    also estimates the error of a less accurate method.

    Notation for Butcher tableau is as in [1]_.

    Parameters
    ----------
    fun : callable
        Right-hand side of the system.
    t : float
        Current time.
    y : ndarray, shape (n,)
        Current state.
    f : ndarray, shape (n,)
        Current value of the derivative, i.e., ``fun(x, y)``.
    h : float
        Step to use.
    A : ndarray, shape (n_stages, n_stages)
        Coefficients for combining previous RK stages to compute the next
        stage. For explicit methods the coefficients at and above the main
        diagonal are zeros.
    B : ndarray, shape (n_stages,)
        Coefficients for combining RK stages for computing the final
        prediction.
    C : ndarray, shape (n_stages,)
        Coefficients for incrementing time for consecutive RK stages.
        The value for the first stage is always zero.
    K : ndarray, shape (n_stages + 1, n)
        Storage array for putting RK stages here. Stages are stored in rows.
        The last row is a linear combination of the previous rows with
        coefficients

    Returns
    -------
    y_new : ndarray, shape (n,)
        Solution at t + h computed with a higher accuracy.
    f_new : ndarray, shape (n,)
        Derivative ``fun(t + h, y_new)``.

    References
    ----------
    .. [1] E. Hairer, S. P. Norsett G. Wanner, "Solving Ordinary Differential
           Equations I: Nonstiff Problems", Sec. II.4.
    r   r   N©Ústartéÿÿÿÿ)Ú	enumerateÚzipÚnpÚdotÚT)ÚfunÚtÚyÚfÚhÚAÚBÚCÚKÚsÚaÚcÚdyÚy_newÚf_new© r%   úP/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/scipy/integrate/_ivp/rk.pyÚrk_step   s    /."r'   c                       sº   e Zd ZU dZeZejed< eZ	ejed< eZ
ejed< eZejed< eZejed< eZeed< eZeed< eZeed	< ejd
dddf‡ fdd„	Zdd„ Zdd„ Zdd„ Zdd„ Z‡  ZS )Ú
RungeKuttaz,Base class for explicit Runge-Kutta methods.r   r   r   ÚEÚPÚorderÚerror_estimator_orderÚn_stagesçü©ñÒMbP?ç�íµ ÷Æ°>FNc
              	      sÒ   t |
ƒ tƒ j|||||dd� d | _t|ƒ| _t||| jƒ\| _| _	|  
| j| j¡| _|	d krŠt| j
| j| j| j| j| j| j| j	ƒ| _nt|	||ƒ| _tj| jd | jf| jjd�| _d| jd  | _d | _d S )NT)Zsupport_complexr   ©Údtyper   )r	   ÚsuperÚ__init__Úy_oldr   Úmax_stepr   ÚnÚrtolÚatolr   r   r   r   r   Ú	directionr,   Úh_absr
   r   Úemptyr-   r1   r   Úerror_exponentÚ
h_previous©Úselfr   Út0Zy0Út_boundr5   r7   r8   Z
vectorizedZ
first_stepZ
extraneous©Ú	__class__r%   r&   r3   U   s.    ÿ
      þ zRungeKutta.__init__c                 C   s   t  |j| j¡| S ©N)r   r   r   r)   )r?   r   r   r%   r%   r&   Ú_estimate_errori   s    zRungeKutta._estimate_errorc                 C   s   t |  ||¡| ƒS rD   )r   rE   )r?   r   r   Úscaler%   r%   r&   Ú_estimate_error_norml   s    zRungeKutta._estimate_error_normc              
   C   s®  | j }| j}| j}| j}| j}dt t || jtj	 ¡| ¡ }| j
|krP|}n| j
|k r`|}n| j
}d}d}	|�s†||k r†d| jfS || j }
||
 }| j|| j  dkr²| j}|| }
t |
¡}t| j||| j|
| j| j| j| jƒ	\}}|t t |¡t |¡¡|  }|  | j|
|¡}|dk �rh|dk�r6t}nttt|| j  ƒ}|	�rZtd|ƒ}||9 }d}qn|ttt|| j  ƒ9 }d}	qn|
| _|| _|| _ || _|| _
|| _dS )Nr   Fr   r   T)TN)r   r   r5   r7   r8   r   ÚabsZ	nextafterr9   Úinfr:   ZTOO_SMALL_STEPrA   r'   r   r   r   r   r   r   ÚmaximumrG   Ú
MAX_FACTORÚminÚSAFETYr<   ÚmaxÚ
MIN_FACTORr=   r4   )r?   r   r   r5   r7   r8   Zmin_stepr:   Zstep_acceptedZstep_rejectedr   Zt_newr#   r$   rF   Z
error_normÚfactorr%   r%   r&   Ú
_step_implo   sd    "




  ÿ 

ÿ
ÿzRungeKutta._step_implc                 C   s$   | j j | j¡}t| j| j| j|ƒS rD   )r   r   r   r*   ÚRkDenseOutputÚt_oldr   r4   )r?   ÚQr%   r%   r&   Ú_dense_output_impl²   s    zRungeKutta._dense_output_impl)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚNotImplementedr   r   ZndarrayÚ__annotations__r   r   r)   r*   r+   Úintr,   r-   rI   r3   rE   rG   rQ   rU   Ú__classcell__r%   r%   rB   r&   r(   J   s&   
  þCr(   c                   @   sš   e Zd ZdZdZdZdZe dddg¡Z	e dddgdddgdddgg¡Z
e ddd	g¡Ze d
dddg¡Ze dddgdddgdddgdddgg¡ZdS )ÚRK23aþ  Explicit Runge-Kutta method of order 3(2).

    This uses the Bogacki-Shampine pair of formulas [1]_. The error is controlled
    assuming accuracy of the second-order method, but steps are taken using the
    third-order accurate formula (local extrapolation is done). A cubic Hermite
    polynomial is used for the dense output.

    Can be applied in the complex domain.

    Parameters
    ----------
    fun : callable
        Right-hand side of the system. The calling signature is ``fun(t, y)``.
        Here ``t`` is a scalar and there are two options for ndarray ``y``.
        It can either have shape (n,), then ``fun`` must return array_like with
        shape (n,). Or alternatively it can have shape (n, k), then ``fun``
        must return array_like with shape (n, k), i.e. each column
        corresponds to a single column in ``y``. The choice between the two
        options is determined by `vectorized` argument (see below).
    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`.
    vectorized : bool, optional
        Whether `fun` is implemented in a vectorized fashion. Default is False.

    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 evaluations of the system's right-hand side.
    njev : int
        Number of evaluations of the Jacobian. Is always 0 for this solver as it does not use the Jacobian.
    nlu : int
        Number of LU decompositions. Is always 0 for this solver.

    References
    ----------
    .. [1] P. Bogacki, L.F. Shampine, "A 3(2) Pair of Runge-Kutta Formulas",
           Appl. Math. Lett. Vol. 2, No. 4. pp. 321-325, 1989.
    é   é   r   g      à?g      è?gÇqÇqÌ?gUUUUUUÕ?gÇqÇqÜ?grÇqÇ±?gUUUUUUµ¿gÇqÇq¼¿g      À?r   gUUUUUUõ¿grÇqÇá?gUUUUUUå¿gUUUUUUõ?gÇqÇqì¿r   N©rV   rW   rX   rY   r+   r,   r-   r   Úarrayr   r   r   r)   r*   r%   r%   r%   r&   r^   ·   s"   Oýýr^   c                   @   s  e Zd ZdZdZdZdZe ddddd	d
g¡Z	e dddddgdddddgdddddgdddddgdddddgdddddgg¡Z
e ddddddg¡Ze dddd d!d"d#g¡Ze d
d$d%d&gddddgdd'd(d)gdd*d+d,gdd-d.d/gdd0d1d2gdd3d4d5gg¡Zd6S )7ÚRK45aØ  Explicit Runge-Kutta method of order 5(4).

    This uses the Dormand-Prince pair of formulas [1]_. The error is controlled
    assuming accuracy of the fourth-order method accuracy, but steps are taken
    using the fifth-order accurate formula (local extrapolation is done).
    A quartic interpolation polynomial is used for the dense output [2]_.

    Can be applied in the complex domain.

    Parameters
    ----------
    fun : callable
        Right-hand side of the system. The calling signature is ``fun(t, y)``.
        Here ``t`` is a scalar, and there are two options for the ndarray ``y``:
        It can either have shape (n,); then ``fun`` must return array_like with
        shape (n,). Alternatively it can have shape (n, k); then ``fun``
        must return an array_like with shape (n, k), i.e., each column
        corresponds to a single column in ``y``. The choice between the two
        options is determined by `vectorized` argument (see below).
    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`.
    vectorized : bool, optional
        Whether `fun` is implemented in a vectorized fashion. Default is False.

    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 evaluations of the system's right-hand side.
    njev : int
        Number of evaluations of the Jacobian. Is always 0 for this solver as it does not use the Jacobian.
    nlu : int
        Number of LU decompositions. Is always 0 for this solver.

    References
    ----------
    .. [1] J. R. Dormand, P. J. Prince, "A family of embedded Runge-Kutta
           formulae", Journal of Computational and Applied Mathematics, Vol. 6,
           No. 1, pp. 19-26, 1980.
    .. [2] L. W. Shampine, "Some Practical Runge-Kutta Formulas", Mathematics
           of Computation,, Vol. 46, No. 173, pp. 135-150, 1986.
    é   é   é   r   r   g333333Ó?gš™™™™™é?gÇqÇqì?r   g333333³?gÍÌÌÌÌÌÌ?gŸôIŸôIï?gÞÝÝÝÝÝÀgÇqÇq@g�qÃìž@gä •Ò1'Àg�R<6R¥#@gE3ºžœÒ¿g°¨õ+Å@g„>øàƒ%Àg‹r£Ð!@gÑE]tÑÑ?g/ÌÙp‰�Ñ¿gUUUUUU·?gûVšIÀÜ?gUUUUUÕä?gŒ·²Ï¡Ô¿g1Ã0ÃÀ?g‡©Ëí2T¿gÄ¿
UZkq?gïîîîîî¢¿gXÊÒÑ
ª?gâðÚ{Št¥¿gš™™™™™™?g#Ð
É!ÔÀgñJÀ<î’@gF ’Cò¿gãõÌF°@gFj'NÿÀg‡©¹óDg@gdD�õÛÀga‡÷P#$@g2¢Çú½Àg¸’ý<p@g›@ê°˜Àg’Œ—àê,@gRqÖ#¤ýõ¿g_40g.
@gå•¶ÈFü¿g'’¾—ö?g'’¾—ÀgÉßK@Nra   r%   r%   r%   r&   rc     sD   Rúÿÿ
ÿÿÿ

õrc   c                       s´   e Zd ZdZejZdZdZej	de…de…f Z	ej
Z
ejde… ZejZejZejZej	ed d… Zejed d… Zejddddf‡ fd	d
„	Zdd„ Zdd„ Zdd„ Z‡  ZS )ÚDOP853a#  Explicit Runge-Kutta method of order 8.

    This is a Python implementation of "DOP853" algorithm originally written
    in Fortran [1]_, [2]_. Note that this is not a literate translation, but
    the algorithmic core and coefficients are the same.

    Can be applied in the complex domain.

    Parameters
    ----------
    fun : callable
        Right-hand side of the system. The calling signature is ``fun(t, y)``.
        Here, ``t`` is a scalar, and there are two options for the ndarray ``y``:
        It can either have shape (n,); then ``fun`` must return array_like with
        shape (n,). Alternatively it can have shape (n, k); then ``fun``
        must return an array_like with shape (n, k), i.e. each column
        corresponds to a single column in ``y``. The choice between the two
        options is determined by `vectorized` argument (see below).
    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`.
    vectorized : bool, optional
        Whether `fun` is implemented in a vectorized fashion. Default is False.

    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 evaluations of the system's right-hand side.
    njev : int
        Number of evaluations of the Jacobian. Is always 0 for this solver
        as it does not use the Jacobian.
    nlu : int
        Number of LU decompositions. Is always 0 for this solver.

    References
    ----------
    .. [1] E. Hairer, S. P. Norsett G. Wanner, "Solving Ordinary Differential
           Equations I: Nonstiff Problems", Sec. II.
    .. [2] `Page with original Fortran code of DOP853
            <http://www.unige.ch/~hairer/software.html>`_.
    é   é   Nr   r.   r/   Fc
              
      sV   t ƒ j|||||||||	f	|
Ž tjtj| jf| jjd�| _	| j	d | j
d … | _d S )Nr0   r   )r2   r3   r   r;   r   ZN_STAGES_EXTENDEDr6   r   r1   Ú
K_extendedr-   r   r>   rB   r%   r&   r3   è  s     ÿÿÿÿzDOP853.__init__c                 C   st   t  |j| j¡}t  |j| j¡}t  t  |¡dt  |¡ ¡}t  |¡}|dk}t  || ¡||  ||< || | S )Ngš™™™™™¹?r   )r   r   r   ÚE5ÚE3ÚhypotrH   Z	ones_like)r?   r   r   Úerr5Úerr3ÚdenomZcorrection_factorÚmaskr%   r%   r&   rE   ñ  s    
zDOP853._estimate_errorc           	      C   sˆ   t  |j| j¡| }t  |j| j¡| }t j |¡d }t j |¡d }|dkr\|dkr\dS |d|  }t  |¡| t  |t	|ƒ ¡ S )Nr`   r   g        g{®Gáz„?)
r   r   r   rk   rl   Zlinalgr   rH   ÚsqrtÚlen)	r?   r   r   rF   rn   ro   Zerr5_norm_2Zerr3_norm_2rp   r%   r%   r&   rG   ú  s    zDOP853._estimate_error_normc           
      C   s  | j }| j}tt| j| jƒ| jd d�D ]N\}\}}t |d |… j	|d |… ¡| }|  
| j||  | j| ¡||< q(tjtj| jf| jjd�}|d }| j| j }	|	|d< || |	 |d< d|	 || j|   |d< |t | j|¡ |dd …< t| j| j| j|ƒS )Nr   r   r0   r   r`   r_   )rj   r=   r   r   ÚA_EXTRAÚC_EXTRAr-   r   r   r   r   rS   r4   r;   r   ZINTERPOLATOR_POWERr6   r1   r   r   ÚDÚDop853DenseOutputr   )
r?   r   r   r   r    r!   r"   ÚFZf_oldZdelta_yr%   r%   r&   rU     s"    ÿ""ÿzDOP853._dense_output_impl)rV   rW   rX   rY   r   ZN_STAGESr-   r+   r,   r   r   r   rl   rk   rv   rt   ru   r   rI   r3   rE   rG   rU   r]   r%   r%   rB   r&   rg   ‰  s*   Q  þ		
rg   c                       s$   e Zd Z‡ fdd„Zdd„ Z‡  ZS )rR   c                    s8   t ƒ  ||¡ || | _|| _|jd d | _|| _d S )Nr   )r2   r3   r   rT   Úshaper+   r4   )r?   rS   r   r4   rT   rB   r%   r&   r3     s
    
zRkDenseOutput.__init__c                 C   s    || j  | j }|jdkr8t || jd ¡}t |¡}n$t || jd df¡}tj|dd�}| jt | j|¡ }|jdkr’|| j	d d …d f 7 }n
|| j	7 }|S )Nr   r   )Zaxisr`   )
rS   r   Úndimr   Ztiler+   Zcumprodr   rT   r4   )r?   r   ÚxÚpr   r%   r%   r&   Ú
_call_impl"  s    


zRkDenseOutput._call_impl©rV   rW   rX   r3   r}   r]   r%   r%   rB   r&   rR     s   rR   c                       s$   e Zd Z‡ fdd„Zdd„ Z‡  ZS )rw   c                    s(   t ƒ  ||¡ || | _|| _|| _d S rD   )r2   r3   r   rx   r4   )r?   rS   r   r4   rx   rB   r%   r&   r3   4  s    
zDop853DenseOutput.__init__c                 C   sª   || j  | j }|jdkr(t | j¡}n0|d d …d f }tjt|ƒt| jƒf| jjd�}t	t
| jƒƒD ]2\}}||7 }|d dkrŒ||9 }qf|d| 9 }qf|| j7 }|jS )Nr   r0   r`   r   )rS   r   rz   r   Z
zeros_liker4   Zzerosrs   r1   r   Úreversedrx   r   )r?   r   r{   r   Úir   r%   r%   r&   r}   :  s    
 

zDop853DenseOutput._call_implr~   r%   r%   rB   r&   rw   3  s   rw   )Únumpyr   Úbaser   r   Úcommonr   r   r   r   r	   r
   Ú r   rM   rO   rK   r'   r(   r^   rc   rg   rR   rw   r%   r%   r%   r&   Ú<module>   s    <maq 