U
    GvfN%                     @   s@   d dl Zdd ZG dd dZG dd dZG dd	 d	eZdS )
    Nc                    sf   t |}t |jt jr,|s&tdt nt |j dd}|j	dkrPtd fdd}||fS )z=Helper function for checking arguments common to all solvers.zX`y0` is complex, but the chosen solver does not support integration in a complex domain.F)copy   z`y0` must be 1-dimensional.c                    s   t j| | dS )N)dtype)npasarraytyr   fun =/tmp/pip-unpacked-wheel-96ln3f52/scipy/integrate/_ivp/base.pyfun_wrapped   s    z$check_arguments.<locals>.fun_wrapped)
r   r   Z
issubdtyper   Zcomplexfloating
ValueErrorcomplexfloatZastypendim)r   y0support_complexr   r   r
   r   check_arguments   s    

r   c                   @   sJ   e Zd ZdZdZdddZedd Zdd	 Zd
d Z	dd Z
dd ZdS )	OdeSolvera  Base class for ODE solvers.

    In order to implement a new solver you need to follow the guidelines:

        1. A constructor must accept parameters presented in the base class
           (listed below) along with any other parameters specific to a solver.
        2. A constructor must accept arbitrary extraneous arguments
           ``**extraneous``, but warn that these arguments are irrelevant
           using `common.warn_extraneous` function. Do not pass these
           arguments to the base class.
        3. A solver must implement a private method `_step_impl(self)` which
           propagates a solver one step further. It must return tuple
           ``(success, message)``, where ``success`` is a boolean indicating
           whether a step was successful, and ``message`` is a string
           containing description of a failure if a step failed or None
           otherwise.
        4. A solver must implement a private method `_dense_output_impl(self)`,
           which returns a `DenseOutput` object covering the last successful
           step.
        5. A solver must have attributes listed below in Attributes section.
           Note that ``t_old`` and ``step_size`` are updated automatically.
        6. Use `fun(self, t, y)` method for the system rhs evaluation, this
           way the number of function evaluations (`nfev`) will be tracked
           automatically.
        7. For convenience, a base class provides `fun_single(self, t, y)` and
           `fun_vectorized(self, t, y)` for evaluating the rhs in
           non-vectorized and vectorized fashions respectively (regardless of
           how `fun` from the constructor is implemented). These calls don't
           increment `nfev`.
        8. If a solver uses a Jacobian matrix and LU decompositions, it should
           track the number of Jacobian evaluations (`njev`) and the number of
           LU decompositions (`nlu`).
        9. By convention, the function evaluations used to compute a finite
           difference approximation of the Jacobian should not be counted in
           `nfev`, thus use `fun_single(self, t, y)` or
           `fun_vectorized(self, t, y)` when computing a finite difference
           approximation of the Jacobian.

    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, n_points), then
        ``fun`` must return array_like with shape (n, n_points) (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.
    vectorized : bool
        Whether `fun` is implemented in a vectorized fashion.
    support_complex : bool, optional
        Whether integration in a complex domain should be supported.
        Generally determined by a derived solver class capabilities.
        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 of the system's rhs evaluations.
    njev : int
        Number of the Jacobian evaluations.
    nlu : int
        Number of LU decompositions.
    z8Required step size is less than spacing between numbers.Fc           	         s   d  _ | _t|||\ _ _| _| _|rD fdd} j}n j} fdd} fdd}| _| _| _	||krt
|| nd _ jj _d _d	 _d	 _d	 _d S )
Nc                    s     | |d d d f  S N)_funZravelr   selfr   r   
fun_single|   s    z&OdeSolver.__init__.<locals>.fun_singlec                    s:   t |}t|jD ] \}} | ||d d |f< q|S r   )r   Z
empty_like	enumerateTr   )r   r	   fiyir   r   r   fun_vectorized   s    
z*OdeSolver.__init__.<locals>.fun_vectorizedc                    s     j d7  _  | |S )Nr   )nfevr   r   r   r   r   r      s    zOdeSolver.__init__.<locals>.funr   runningr   )t_oldr   r   r   r	   t_bound
vectorizedr   r   r!   r   sign	directionsizenstatusr"   ZnjevZnlu)	r   r   t0r   r%   r&   r   r   r!   r   r   r   __init__s   s(    
zOdeSolver.__init__c                 C   s$   | j d krd S t| j| j  S d S r   )r$   r   absr   r   r   r   r   	step_size   s    
zOdeSolver.step_sizec                 C   s   | j dkrtd| jdks(| j| jkrD| j| _| j| _d}d| _ n@| j}|  \}}|sbd| _ n"|| _| j| j| j  dkrd| _ |S )a  Perform one integration step.

        Returns
        -------
        message : string or None
            Report from the solver. Typically a reason for a failure if
            `self.status` is 'failed' after the step was taken or None
            otherwise.
        r#   z/Attempt to step on a failed or finished solver.r   Nfinishedfailed)r+   RuntimeErrorr*   r   r%   r$   
_step_implr(   )r   messager   successr   r   r   step   s    

zOdeSolver.stepc                 C   sF   | j dkrtd| jdks(| j| j kr:t| j | j| jS |  S dS )zCompute a local interpolant over the last successful step.

        Returns
        -------
        sol : `DenseOutput`
            Local interpolant over the last successful step.
        Nz;Dense output is available after a successful step was made.r   )r$   r2   r*   r   ConstantDenseOutputr	   _dense_output_implr   r   r   r   dense_output   s
    
zOdeSolver.dense_outputc                 C   s   t d S r   NotImplementedErrorr   r   r   r   r3      s    zOdeSolver._step_implc                 C   s   t d S r   r:   r   r   r   r   r8      s    zOdeSolver._dense_output_implN)F)__name__
__module____qualname____doc__ZTOO_SMALL_STEPr-   propertyr/   r6   r9   r3   r8   r   r   r   r   r      s   W 
%
!r   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	DenseOutputaO  Base class for local interpolant over step made by an ODE solver.

    It interpolates between `t_min` and `t_max` (see Attributes below).
    Evaluation outside this interval is not forbidden, but the accuracy is not
    guaranteed.

    Attributes
    ----------
    t_min, t_max : float
        Time range of the interpolation.
    c                 C   s(   || _ || _t||| _t||| _d S r   )r$   r   minZt_minmaxZt_max)r   r$   r   r   r   r   r-      s    zDenseOutput.__init__c                 C   s&   t |}|jdkrtd| |S )ae  Evaluate the interpolant.

        Parameters
        ----------
        t : float or array_like with shape (n_points,)
            Points to evaluate the solution at.

        Returns
        -------
        y : ndarray, shape (n,) or (n, n_points)
            Computed values. Shape depends on whether `t` was a scalar or a
            1-D array.
        r   z#`t` must be a float or a 1-D array.)r   r   r   r   
_call_implr   r   r   r   r   __call__   s    

zDenseOutput.__call__c                 C   s   t d S r   r:   rE   r   r   r   rD      s    zDenseOutput._call_implN)r<   r=   r>   r?   r-   rF   rD   r   r   r   r   rA      s   rA   c                       s(   e Zd ZdZ fddZdd Z  ZS )r7   zConstant value interpolator.

    This class used for degenerate integration cases: equal integration limits
    or a system with 0 equations.
    c                    s   t  || || _d S r   )superr-   value)r   r$   r   rH   	__class__r   r   r-     s    zConstantDenseOutput.__init__c                 C   sN   |j dkr| jS t| jjd |jd f}| jd d d f |d d < |S d S )Nr   )r   rH   r   emptyshape)r   r   retr   r   r   rD     s
    
zConstantDenseOutput._call_impl)r<   r=   r>   r?   r-   rD   __classcell__r   r   rI   r   r7     s   r7   )Znumpyr   r   r   rA   r7   r   r   r   r   <module>   s
    A)