
&]\c           @` s   d  d l  m Z m Z m Z 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 Z d Z d Z d	   Z d
 e f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d S(   i    (   t   divisiont   print_functiont   absolute_importNi   (   t	   OdeSolvert   DenseOutput(   t   validate_max_stept   validate_tolt   select_initial_stept   normt   warn_extraneoust   validate_first_stepg?g?i
   c
         C` s   | |	 d <xm t  t | |   D]V \ }
 \ } } t j |	 |
 d  j |  | } |  | | | | |  |	 |
 d <q  W| | t j |	 d  j |  } |  | | |  } | |	 d <t j |	 j |  | } | | | f S(   s  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 : list of ndarray, length n_stages - 1
        Coefficients for combining previous RK stages to compute the next
        stage. For explicit methods the coefficients above the main diagonal
        are zeros, so `A` is stored as a list of arrays of increasing lengths.
        The first stage is always just `f`, thus no coefficients for it
        are required.
    B : ndarray, shape (n_stages,)
        Coefficients for combining RK stages for computing the final
        prediction.
    C : ndarray, shape (n_stages - 1,)
        Coefficients for incrementing time for consecutive RK stages.
        The value for the first stage is always zero, thus it is not stored.
    E : ndarray, shape (n_stages + 1,)
        Coefficients for estimating the error of a less accurate method. They
        are computed as the difference between b's in an extended tableau.
    K : ndarray, shape (n_stages + 1, n)
        Storage array for putting RK stages here. Stages are stored in rows.

    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)``.
    error : ndarray, shape (n,)
        Error estimate of a less accurate method.

    References
    ----------
    .. [1] E. Hairer, S. P. Norsett G. Wanner, "Solving Ordinary Differential
           Equations I: Nonstiff Problems", Sec. II.4.
    i    i   i(   t	   enumeratet   zipt   npt   dott   T(   t   funt   tt   yt   ft   ht   At   Bt   Ct   Et   Kt   st   at   ct   dyt   y_newt   f_newt   error(    (    s6   lib/python2.7/site-packages/scipy/integrate/_ivp/rk.pyt   rk_step   s    4
(!'!
t
   RungeKuttac           B` se   e  Z d  Z e Z e Z e Z e Z e Z e Z	 e Z
 e j d d e d d  Z d   Z d   Z RS(   s,   Base class for explicit Runge-Kutta methods.gMbP?gư>c
      	   K` s  t  |
  t t |   j | | | | | d t d  |  _ t |  |  _ t	 | | |  j
  \ |  _ |  _ |  j |  j |  j  |  _ |	 d  k r t |  j |  j |  j |  j |  j |  j |  j |  j  |  _ n t |	 | |  |  _ t j |  j d |  j
 f d |  j j |  _ d  S(   Nt   support_complexi   t   dtype(   R	   t   superR"   t   __init__t   Truet   Nonet   y_oldR   t   max_stepR   t   nt   rtolt   atolR   R   R   R   R   t	   directiont   ordert   h_absR
   R   t   emptyt   n_stagesR$   R   (   t   selfR   t   t0t   y0t   t_boundR*   R,   R-   t
   vectorizedt
   first_stept
   extraneous(    (    s6   lib/python2.7/site-packages/scipy/integrate/_ivp/rk.pyR&   [   s    
!	!c         C` sY  |  j  } |  j } |  j } |  j } |  j } d t j t j | |  j t j	  |  } |  j
 | k rr | } n! |  j
 | k  r | } n	 |  j
 } |  j } t }	 x}|	 s!| | k  r t |  j f S| |  j }
 | |
 } |  j | |  j d k r|  j } n  | | }
 t j |
  } t |  j | | |  j |
 |  j |  j |  j |  j |  j 
 \ } } } | t j t j |  t j |   | } t | |  } | d k r| t 9} t }	 q | d k  r| t t t d t | d | d   9} t }	 q | t t t | d | d  9} q W| |  _ | |  _  | |  _ | |  _
 | |  _ t d  f S(   Ni
   i    g        i   i(!   R   R   R*   R,   R-   R   t   abst	   nextafterR.   t   infR0   R/   t   Falset   TOO_SMALL_STEPR6   R!   R   R   R   R   R   R   R   t   maximumR   t
   MAX_FACTORR'   t   mint   maxt   SAFETYt
   MIN_FACTORR)   R(   (   R3   R   R   R*   R,   R-   t   min_stepR0   R/   t   step_acceptedR   t   t_newR   R   R    t   scalet
   error_norm(    (    s6   lib/python2.7/site-packages/scipy/integrate/_ivp/rk.pyt
   _step_implm   sR    					-					

',
		#							c         C` s4   |  j  j j |  j  } t |  j |  j |  j |  S(   N(   R   R   R   t   Pt   RkDenseOutputt   t_oldR   R)   (   R3   t   Q(    (    s6   lib/python2.7/site-packages/scipy/integrate/_ivp/rk.pyt   _dense_output_impl   s    N(   t   __name__t
   __module__t   __doc__t   NotImplementedR   R   R   R   RK   R/   R2   R   R<   R=   R(   R&   RJ   RO   (    (    (    s6   lib/python2.7/site-packages/scipy/integrate/_ivp/rk.pyR"   Q   s   		;t   RK23c           B` s   e  Z d  Z d Z d Z e j d d g  Z e j d g  e j d d g  g Z e j d d d g  Z	 e j d d d d g  Z
 e j d d d g d d d g d d d g d d	 d g g  Z RS(   s  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). But if a component of `y`
        is approximately below `atol`, the error only needs to fall within
        the same `atol` threshold, and the number of correct digits is not
        guaranteed. 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.
    i   i   i   i   i    i	   i   iH   ii   i   iiig      ?g      ?g      ?g      ?gqq?gUUUUUU?gqq?grqǱ?gUUUUUUgqqg      ?gUUUUUUgrq?gUUUUUUgUUUUUU?gqq(   RP   RQ   RR   R/   R2   R   t   arrayR   R   R   R   RK   (    (    (    s6   lib/python2.7/site-packages/scipy/integrate/_ivp/rk.pyRT      s   Kt   RK45c           B` sd  e  Z d  Z d Z d Z e j dS dT dU dV d g  Z e j dW g  e j dX dY g  e j dZ d[ d\ g  e j d] d^ d_ d` g  e j da db dc dd de g  g Z e j df d" dg dh di dj g  Z	 e j dk d" dl dm dn do dp g  Z
 e j d dq dr ds g d" d" d" d" g d" dt du dv g d" dw dx dy g d" dz d{ d| g d" d} d~ d g d" d d d g g  Z RS(   s  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). But if a component of `y`
        is approximately below `atol`, the error only needs to fall within
        the same `atol` threshold, and the number of correct digits is not
        guaranteed. 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.
    i   i   i   i   i   i
   i   i	   i(   i,   i-   ii   i    iK  i  ii  i  i,i  i9#  i`  ii!   i  i  i1   i   iiH  i#   i  i    i  iY  i}   i   iui  i   iT   ii   iG   i7A  i  ieC  i - ii  IdD I    Ih   I
Iv   Iy   I   I#I㲉   I^   i9kipIU`N   iPTI9ǂipI	4   I`6   IaTNµI˚Hq   I4h.   i&i*BikrZxiӀ$i_Wiī1i2kiGOiU$hiy,g?g333333?g?gqq?g?g333333?g?gII?ggqq@gq@g 1'gR<6R#@gE3ҿg+@g>%gr!@gE]t?g/pѿgUUUUUU?gVI?gUUUUU?gϡԿg10?g2TgĿ
UZkq?ggX
?g{tg?g#
!gJ<@gFCgF@gFj'NgDg@gdDgaP#$@g2g<p@g@갘g,@gRq#g_40g.
@gFg'?g'gK@(   RP   RQ   RR   R/   R2   R   RU   R   R   R   R   RK   (    (    (    s6   lib/python2.7/site-packages/scipy/integrate/_ivp/rk.pyRV     s0   N!!				RL   c           B` s   e  Z d    Z d   Z RS(   c         C` sP   t  t |   j | |  | | |  _ | |  _ | j d d |  _ | |  _ d  S(   Ni   (   R%   RL   R&   R   RN   t   shapeR/   R)   (   R3   RM   R   R)   RN   (    (    s6   lib/python2.7/site-packages/scipy/integrate/_ivp/rk.pyR&   p  s
    	c         C` s   | |  j  |  j } | j d k rN t j | |  j d  } t j |  } n4 t j | |  j d d f  } t j | d d } |  j t j |  j |  } | j d k r | |  j	 d  d   d  f 7} n | |  j	 7} | S(   Ni    i   t   axisi   (   RM   R   t   ndimR   t   tileR/   t   cumprodR   RN   R)   R(   (   R3   R   t   xt   pR   (    (    s6   lib/python2.7/site-packages/scipy/integrate/_ivp/rk.pyt
   _call_implw  s     (   RP   RQ   R&   R^   (    (    (    s6   lib/python2.7/site-packages/scipy/integrate/_ivp/rk.pyRL   o  s   	(   t
   __future__R    R   R   t   numpyR   t   baseR   R   t   commonR   R   R   R   R	   R
   RC   RD   R@   R!   R"   RT   RV   RL   (    (    (    s6   lib/python2.7/site-packages/scipy/integrate/_ivp/rk.pyt   <module>   s   .	B\Yi