ó
î&]\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 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
 d Z e j d e d d e d d g ƒ Z e j d d e d d e d g ƒ d Z  d d d d d d d Z! d d d d d d d d d d d d
 d d d
 Z" e j d d d g d d d g d d d  g g ƒ Z# e j d d d g d d d  g d! d" d# g g ƒ Z$ e$ d  Z% e$ d d$ e$ d Z& e j d% d d e d d& d d' e d d d d e g d% d d e d d& d d' e d d d d e g d d d( d d d g g ƒ Z' d
 Z( d) Z) d Z* d* „  Z+ d+ „  Z, d, e f d- „  ƒ  YZ- d. e f d/ „  ƒ  YZ. d S(0   i    (   t   divisiont   print_functiont   absolute_importN(   t	   lu_factort   lu_solve(   t
   csc_matrixt   issparset   eye(   t   splu(   t   group_columnsi   (   t   validate_max_stept   validate_tolt   select_initial_stept   normt   num_jact   EPSt   warn_extraneoust   validate_first_step(   t	   OdeSolvert   DenseOutputi   g      à?i   i
   ióÿÿÿi   iÿÿÿÿi   i   y              à?i   g{g]„#-¸?g÷;@L§Â¿gŽhmù¿ž?gí¡
ç}Ð?gQµ é Ê?gím£¢‚Ø¿gFœ§·@g†N¨]ÁøÔ?gïVõ¿à?gFœ§·Àg†N¨]ÁøÔ¿g!RÅ Þ?gò§$Zˆà?g˜¥ÊoN“ÀgÑß{ÏÀã?y              ð?i   iéÿÿÿi   iøÿÿÿgš™™™™™É?c
         C` s?  | j  d }
 t | } t | } t j | ƒ } | } t j d |
 f ƒ } | t } d } t j	 | ƒ } t
 } x¸t t ƒ D]ª} x7 t d ƒ D]) } |  | | | | | | ƒ | | <q Wt j t j | ƒ ƒ sÙ Pn  | j j t ƒ | | d } | j j t ƒ | | d d | d } |	 | | ƒ } |	 | | ƒ } | | d <| j | d <| j | d <t | | ƒ } | d k	 rŒ| | } n d } | d k	 rÎ| d k sÊ| t | d | | | k rÎPn  | | 7} t j | ƒ } | d k s| d k	 r!| d | | | k  r!t } Pn  | } q} W| | d | | f S(   sQ  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 : float
        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.
    i    i   i   y              ð?i   N(   t   shapet   MU_REALt
   MU_COMPLEXt   TIt   dott   npt   emptyt   Ct   Nonet
   empty_liket   Falset   ranget   NEWTON_MAXITERt   allt   isfinitet   Tt   TI_REALt
   TI_COMPLEXt   realt   imagR   t   True(   t   funt   tt   yt   ht   Z0t   scalet   tolt   LU_realt
   LU_complext   solve_lut   nt   M_realt	   M_complext   Wt   Zt   Ft   cht   dW_norm_oldt   dWt	   convergedt   kt   it   f_realt	   f_complext   dW_realt
   dW_complext   dW_normt   rate(    (    s9   lib/python2.7/site-packages/scipy/integrate/_ivp/radau.pyt   solve_collocation_system1   sJ    '


'*
 
$
c         C` sw   | d k s$ | d k s$ | d k r- d } n |  | | | d } t j d d ƒ  t d | ƒ | d } Wd QX| S(   s9  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.
    i    i   g      Ð?t   dividet   ignoreg      Ð¿N(   R   R   t   errstatet   min(   t   h_abst	   h_abs_oldt
   error_normt   error_norm_oldt
   multipliert   factor(    (    s9   lib/python2.7/site-packages/scipy/integrate/_ivp/radau.pyt   predict_factor   s    $	t   Radauc           B` sS   e  Z d  Z e j d d d d e d d „ Z d „  Z d „  Z	 d „  Z
 d „  Z RS(	   s"  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 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). The
        vectorized implementation allows a faster approximation of the Jacobian
        by finite differences (required for this solver).
    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`.
    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` 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 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.
    gü©ñÒMbP?gíµ ÷Æ°>c      	   ` sò  t  | ƒ t t ˆ  ƒ j | | | | |
 ƒ d  ˆ  _ t | ƒ ˆ  _ t | | ˆ  j	 ƒ \ ˆ  _
 ˆ  _ ˆ  j ˆ  j ˆ  j ƒ ˆ  _ | d  k rÈ t ˆ  j ˆ  j ˆ  j ˆ  j ˆ  j d ˆ  j
 ˆ  j ƒ ˆ  _ n t | | | ƒ ˆ  _ d  ˆ  _ d  ˆ  _ t d t | t d | d ƒ ƒ ˆ  _ d  ˆ  _ d  ˆ  _ ˆ  j | |	 ƒ \ ˆ  _ ˆ  _ t ˆ  j ƒ r…‡  f d †  } d „  } t  ˆ  j	 d d ƒ} n* ‡  f d	 †  } d
 „  } t! j" ˆ  j	 ƒ } | ˆ  _# | ˆ  _$ | ˆ  _% t& ˆ  _' d  ˆ  _( d  ˆ  _) d  ˆ  _* d  S(   Ni   i
   g¸…ëQ¸ž?g      à?c         ` s   ˆ  j  d 7_  t |  ƒ S(   Ni   (   t   nluR   (   t   A(   t   self(    s9   lib/python2.7/site-packages/scipy/integrate/_ivp/radau.pyt   lu6  s    c         S` s   |  j  | ƒ S(   N(   t   solve(   t   LUt   b(    (    s9   lib/python2.7/site-packages/scipy/integrate/_ivp/radau.pyR2   :  s    t   formatt   cscc         ` s   ˆ  j  d 7_  t |  d t ƒS(   Ni   t   overwrite_a(   RR   R   R(   (   RS   (   RT   (    s9   lib/python2.7/site-packages/scipy/integrate/_ivp/radau.pyRU   ?  s    c         S` s   t  |  | d t ƒS(   Nt   overwrite_b(   R   R(   (   RW   RX   (    (    s9   lib/python2.7/site-packages/scipy/integrate/_ivp/radau.pyR2   C  s    (+   R   t   superRQ   t   __init__R   t   y_oldR
   t   max_stepR   R3   t   rtolt   atolR)   R*   R+   t   fR   t	   directionRJ   R   RK   RM   t   maxR   RI   t
   newton_tolt   solt
   jac_factort   _validate_jact   jact   JR   R   R   t   identityRU   R2   t   IR(   t   current_jacR0   R1   R7   (   RT   R)   t   t0t   y0t   t_boundR`   Ra   Rb   Rj   t   jac_sparsityt
   vectorizedt
   first_stept
   extraneousRU   R2   Rm   (    (   RT   s9   lib/python2.7/site-packages/scipy/integrate/_ivp/radau.pyR^     s>    
"	!		'										c         ` sÕ  ˆ j  } ˆ j } ˆ  d  k rŠ ˆ d  k	 r` t ˆ ƒ rE t ˆ ƒ ‰ n  t ˆ ƒ } ˆ | f ‰ n  ‡ ‡ f d †  } | | | ˆ j ƒ } nAt ˆ  ƒ rPˆ  | | ƒ } d ˆ _ t | ƒ rÞ t | ƒ } d  ‡  ‡ f d † } n* t	 j
 | d t ƒ} d  ‡  ‡ f d † } | j ˆ j ˆ j f k rËt d j ˆ j ˆ j f | j ƒ ƒ ‚ qËn{ t ˆ  ƒ rkt ˆ  ƒ } n t	 j
 ˆ  d t ƒ} | j ˆ j ˆ j f k rÅt d j ˆ j ˆ j f | j ƒ ƒ ‚ n  d  } | | f S(   Nc         ` sC   ˆ  j  d 7_  t ˆ  j |  | | ˆ  j ˆ  j ˆ ƒ \ } ˆ  _ | S(   Ni   (   t   njevR   t   fun_vectorizedRb   Rh   (   R*   R+   Rc   Rk   (   RT   t   sparsity(    s9   lib/python2.7/site-packages/scipy/integrate/_ivp/radau.pyt   jac_wrapped\  s
    i   c         ` s(   ˆ j  d 7_  t ˆ  |  | ƒ d t ƒS(   Ni   t   dtype(   Rv   R   t   float(   R*   R+   t   _(   Rj   RT   (    s9   lib/python2.7/site-packages/scipy/integrate/_ivp/radau.pyRy   i  s    Rz   c         ` s+   ˆ j  d 7_  t j ˆ  |  | ƒ d t ƒS(   Ni   Rz   (   Rv   R   t   asarrayR{   (   R*   R+   R|   (   Rj   RT   (    s9   lib/python2.7/site-packages/scipy/integrate/_ivp/radau.pyRy   p  s    s8   `jac` is expected to have shape {}, but actually has {}.(   R*   R+   R   R   R   R	   Rc   t   callableRv   R   R}   R{   R   R3   t
   ValueErrorRY   (   RT   Rj   Rx   Ro   Rp   t   groupsRy   Rk   (    (   Rj   RT   Rx   s9   lib/python2.7/site-packages/scipy/integrate/_ivp/radau.pyRi   Q  s:    				$	!c   #      C` si  |  j  } |  j } |  j } |  j } |  j } |  j } d t j t j | |  j	 t j
 ƒ | ƒ } |  j | k r‡ | } d  }	 d  }
 n? |  j | k  r« | } d  }	 d  }
 n |  j } |  j }	 |  j }
 |  j } |  j } |  j } |  j } |  j } t } t } d  } x| s| | k  r't |  j f S| |  j	 } | | } |  j	 | |  j d k rd|  j } n  | | } t j | ƒ } |  j d  k r«t j d | j d f ƒ } n |  j | | t ƒ j | } | t j | ƒ | } t } x× | s¿| d  k s| d  k rF|  j t | |  j | ƒ } |  j t | |  j | ƒ } n  t  |  j! | | | | | |  j" | | |  j# ƒ
 \ } } } } | sé| r’Pn  |  j | | | ƒ } t$ } d  } d  } qéqéW| sâ| d 9} d  } d  } qn  | | d } | j j% t& ƒ | } |  j# | | | ƒ } | t j' t j | ƒ t j | ƒ ƒ | } t( | | ƒ } d d t) d d t) | } | rÁ| d k rÁ|  j# | |  j! | | | ƒ | ƒ } t( | | ƒ } n  | d k rt* | |	 | |
 ƒ }  | t+ t, | |  ƒ 9} d  } d  } t$ } qt$ } qW| d  k	 o9| d k o9| d	 k }! t* | |	 | |
 ƒ }  t- t. | |  ƒ }  |! r€|  d
 k  r€d }  n d  } d  } |  j! | | ƒ }" |! r¿| | | |" ƒ } t$ } n | d  k	 rÔt } n  |  j |  _ | |  _ | |  |  _ | |  _/ | |  _  | |  _ |" |  _ | |  _0 | |  _ | |  _ | |  _ | |  _ | |  _1 |  j2 ƒ  |  _ | | f S(   Ni
   i    i   g      à?iÿÿÿÿgÍÌÌÌÌÌì?i   i   gü©ñÒMbP?g333333ó?(3   R*   R+   Rc   R`   Rb   Ra   R   t   abst	   nextafterRd   t   infRJ   R   RK   RM   Rk   R0   R1   Rn   Rj   R   t   TOO_SMALL_STEPRq   Rg   t   zerosR   R   R#   RU   R   Rm   R   RE   R)   Rf   R2   R(   R   t   Et   maximumR   R    RP   Re   t
   MIN_FACTORRI   t
   MAX_FACTORR_   R7   t   t_oldt   _compute_dense_output(#   RT   R*   R+   Rc   R`   Rb   Ra   t   min_stepRJ   RK   RM   Rk   R0   R1   Rn   Rj   t   rejectedt   step_acceptedt   messageR,   t   t_newR-   R.   R<   t   n_iterR7   RD   t   y_newt   ZEt   errorRL   t   safetyRO   t   recompute_jact   f_new(    (    s9   lib/python2.7/site-packages/scipy/integrate/_ivp/radau.pyt
   _step_impl†  sÒ    						-											

	!
,&		
$														c         C` s4   t  j |  j j t ƒ } t |  j |  j |  j | ƒ S(   N(	   R   R   R7   R#   t   Pt   RadauDenseOutputRŠ   R*   R_   (   RT   t   Q(    (    s9   lib/python2.7/site-packages/scipy/integrate/_ivp/radau.pyR‹     s    c         C` s   |  j  S(   N(   Rg   (   RT   (    (    s9   lib/python2.7/site-packages/scipy/integrate/_ivp/radau.pyt   _dense_output_impl  s    N(   t   __name__t
   __module__t   __doc__R   Rƒ   R   R   R^   Ri   R˜   R‹   Rœ   (    (    (    s9   lib/python2.7/site-packages/scipy/integrate/_ivp/radau.pyRQ   µ   s   f3	5	Ž	Rš   c           B` s   e  Z d  „  Z d „  Z RS(   c         C` sP   t  t |  ƒ j | | ƒ | | |  _ | |  _ | j d d |  _ | |  _ d  S(   Ni   (   R]   Rš   R^   R,   R›   R   t   orderR_   (   RT   RŠ   R*   R_   R›   (    (    s9   lib/python2.7/site-packages/scipy/integrate/_ivp/radau.pyR^     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 ƒ} t j |  j | ƒ } | j d k rÆ | |  j	 d  d  … d  f 7} n | |  j	 7} | S(   Ni    i   t   axisi   (   RŠ   R,   t   ndimR   t   tileR    t   cumprodR   R›   R_   R   (   RT   R*   t   xt   pR+   (    (    s9   lib/python2.7/site-packages/scipy/integrate/_ivp/radau.pyt
   _call_impl$  s     (   R   Rž   R^   R§   (    (    (    s9   lib/python2.7/site-packages/scipy/integrate/_ivp/radau.pyRš     s   	(/   t
   __future__R    R   R   t   numpyR   t   scipy.linalgR   R   t   scipy.sparseR   R   R   t   scipy.sparse.linalgR   t   scipy.optimize._numdiffR	   t   commonR
   R   R   R   R   R   R   R   t   baseR   R   t   S6t   arrayR   R†   R   R   R#   R   R$   R%   R™   R    Rˆ   R‰   RE   RP   RQ   Rš   (    (    (    s9   lib/python2.7/site-packages/scipy/integrate/_ivp/radau.pyt   <module>   sD   :
(,>
88!	\	(ÿ h