ó
î&]\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
 Z d Z d Z d Z  d „  Z! d „  Z" d „  Z# d e f d „  ƒ  YZ$ d e f d „  ƒ  YZ% d S(   i    (   t   divisiont   print_functiont   absolute_importN(   t	   lu_factort   lu_solve(   t   issparset
   csc_matrixt   eye(   t   splu(   t   group_columnsi   (   t   validate_max_stept   validate_tolt   select_initial_stept   normt   EPSt   num_jact   validate_first_stept   warn_extraneous(   t	   OdeSolvert   DenseOutputi   i   gš™™™™™É?i
   c         C` s¢   t  j d |  d ƒ d d … d f } t  j d |  d ƒ } t  j |  d |  d f ƒ } | d | | | | d d … d d … f <d | d <t  j | d d ƒS(   s6   Compute the matrix for changing the differences array.i   Ni    t   axis(   t   npt   aranget   Nonet   zerost   cumprod(   t   ordert   factort   It   Jt   M(    (    s7   lib/python2.7/site-packages/scipy/integrate/_ivp/bdf.pyt	   compute_R   s    &,
c         C` sV   t  | | ƒ } t  | d ƒ } | j | ƒ } t j | j |  | d  ƒ |  | d *d S(   s<   Change differences array in-place when step size is changed.i   N(   R   t   dotR   t   T(   t   DR   R   t   Rt   Ut   RU(    (    s7   lib/python2.7/site-packages/scipy/integrate/_ivp/bdf.pyt   change_D   s    c	         C` sN  d }	 | j  ƒ  }
 d } t } xt t ƒ D]} |  | |
 ƒ } t j t j | ƒ ƒ s\ Pn  | | | | | |	 ƒ } t | | ƒ } | d k rœ d } n
 | | } | d k	 râ | d k sÞ | t | d | | | k râ Pn  |
 | 7}
 |	 | 7}	 | d k s&| d k	 r0| d | | | k  r0t	 } Pn  | } q+ W| | d |
 |	 f S(   s5   Solve the algebraic system resulting from BDF method.i    i   N(
   t   copyR   t   Falset   ranget   NEWTON_MAXITERR   t   allt   isfiniteR   t   True(   t   funt   t_newt	   y_predictt   ct   psit   LUt   solve_lut   scalet   tolt   dt   yt   dy_norm_oldt	   convergedt   kt   ft   dyt   dy_normt   rate(    (    s7   lib/python2.7/site-packages/scipy/integrate/_ivp/bdf.pyt   solve_bdf_system%   s0    	
 

$
t   BDFc           B` sJ   e  Z d  Z e j d d d d e d d „ Z d „  Z d „  Z	 d „  Z
 RS(   s^  Implicit method based on backward-differentiation formulas.

    This is a variable order method with the order varying automatically from
    1 to 5. The general framework of the BDF algorithm is described in [1]_.
    This class implements a quasi-constant step size as explained in [2]_.
    The error estimation strategy for the constant-step BDF is derived in [3]_.
    An accuracy enhancement using modified formulas (NDF) [2]_ is also implemented.

    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). 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 [4]_. 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] G. D. Byrne, A. C. Hindmarsh, "A Polyalgorithm for the Numerical
           Solution of Ordinary Differential Equations", ACM Transactions on
           Mathematical Software, Vol. 1, No. 1, pp. 71-96, March 1975.
    .. [2] L. F. Shampine, M. W. Reichelt, "THE MATLAB ODE SUITE", SIAM J. SCI.
           COMPUTE., Vol. 18, No. 1, pp. 1-22, January 1997.
    .. [3] E. Hairer, G. Wanner, "Solving Ordinary Differential Equations I:
           Nonstiff Problems", Sec. III.2.
    .. [4] 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 | ƒ ˆ  _ t | | ˆ  j ƒ \ ˆ  _	 ˆ  _
 ˆ  j ˆ  j ˆ  j ƒ } | d  k r¿ t ˆ  j ˆ  j ˆ  j | ˆ  j d ˆ  j	 ˆ  j
 ƒ ˆ  _ n t | | | ƒ ˆ  _ d  ˆ  _ d  ˆ  _ t d t | t d | d ƒ ƒ ˆ  _ d  ˆ  _ ˆ  j | |	 ƒ \ ˆ  _ ˆ  _ t ˆ  j ƒ r‡  f d †  } d „  } t ˆ  j d d	 d
 ˆ  j j ƒ} n6 ‡  f d †  } d „  } t  j! ˆ  j d
 ˆ  j j ƒ} | ˆ  _" | ˆ  _# | ˆ  _$ t  j% d d d d d d g ƒ } t  j& d t  j' d t  j( d t) d ƒ ƒ f ƒ ˆ  _* d | ˆ  j* ˆ  _+ | ˆ  j* d t  j( d t) d ƒ ˆ  _, t  j- t) d ˆ  j f d
 ˆ  j j ƒ} ˆ  j | d <| ˆ  j ˆ  j | d <| ˆ  _. d ˆ  _/ d ˆ  _0 d  ˆ  _1 d  S(   Nt   support_complexi   i
   g¸…ëQ¸ž?g      à?c         ` s   ˆ  j  d 7_  t |  ƒ S(   Ni   (   t   nluR   (   t   A(   t   self(    s7   lib/python2.7/site-packages/scipy/integrate/_ivp/bdf.pyt   luÐ   s    c         S` s   |  j  | ƒ S(   N(   t   solve(   R3   t   b(    (    s7   lib/python2.7/site-packages/scipy/integrate/_ivp/bdf.pyR4   Ô   s    t   formatt   csct   dtypec         ` s   ˆ  j  d 7_  t |  d t ƒS(   Ni   t   overwrite_a(   RC   R   R-   (   RD   (   RE   (    s7   lib/python2.7/site-packages/scipy/integrate/_ivp/bdf.pyRF   Ù   s    c         S` s   t  |  | d t ƒS(   Nt   overwrite_b(   R   R-   (   R3   RH   (    (    s7   lib/python2.7/site-packages/scipy/integrate/_ivp/bdf.pyR4   Ý   s    i    g®Gáz®Ç¿iÿÿÿÿi	   gýöuàœµ¿gsh‘í|?¥¿i   i   gÇqÇq¼¿(2   R   t   superRA   t   __init__R-   R
   t   max_stepR   t   nt   rtolt   atolR.   t   tR8   R   R   t	   directiont   h_absR   t	   h_abs_oldt   error_norm_oldt   maxR   t   mint
   newton_tolt
   jac_factort   _validate_jact   jacR   R   R   RK   R   t   identityRF   R4   R   t   arrayt   hstackt   cumsumR   t	   MAX_ORDERt   gammat   alphat   error_constt   emptyR"   R   t   n_equal_stepsR3   (   RE   R.   t   t0t   y0t   t_boundRP   RR   RS   R^   t   jac_sparsityt
   vectorizedt
   first_stept
   extraneousR<   RF   R4   R   t   kappaR"   (    (   RE   s7   lib/python2.7/site-packages/scipy/integrate/_ivp/bdf.pyRO   ¹   sJ    
!!			'		$				!5((			c         ` sí  ˆ j  } ˆ j ‰ ˆ  d  k r„ ˆ d  k	 r` t ˆ ƒ rE t ˆ ƒ ‰ n  t ˆ ƒ } ˆ | f ‰ n  ‡ ‡ f d †  } | | ˆ ƒ } n_t ˆ  ƒ r\ˆ  | ˆ ƒ } ˆ j d 7_ t | ƒ rç t | d ˆ j ƒ} ‡  ‡ ‡ f d †  } n- t	 j
 | d ˆ j ƒ} ‡  ‡ ‡ f d †  } | j ˆ j ˆ j f k rãt d j ˆ j ˆ j f | j ƒ ƒ ‚ qãn‡ t ˆ  ƒ r€t ˆ  d ˆ j ƒ} n t	 j
 ˆ  d ˆ j ƒ} | j ˆ j ˆ j f k rÝt d j ˆ j ˆ j f | j ƒ ƒ ‚ n  d  } | | f S(   Nc         ` sU   ˆ  j  d 7_  ˆ  j |  | ƒ } t ˆ  j |  | | ˆ  j ˆ  j ˆ ƒ \ } ˆ  _ | S(   Ni   (   t   njevt
   fun_singleR   t   fun_vectorizedRS   R\   (   RT   R8   R<   R   (   RE   t   sparsity(    s7   lib/python2.7/site-packages/scipy/integrate/_ivp/bdf.pyt   jac_wrappedÿ   s    i   RK   c         ` s+   ˆ j  d 7_  t ˆ  |  | ƒ d ˆ j ƒS(   Ni   RK   (   Rq   R   RK   (   RT   R8   (   R^   RE   Rj   (    s7   lib/python2.7/site-packages/scipy/integrate/_ivp/bdf.pyRu     s    c         ` s.   ˆ j  d 7_  t j ˆ  |  | ƒ d ˆ j ƒS(   Ni   RK   (   Rq   R   t   asarrayRK   (   RT   R8   (   R^   RE   Rj   (    s7   lib/python2.7/site-packages/scipy/integrate/_ivp/bdf.pyRu     s    s8   `jac` is expected to have shape {}, but actually has {}.(   RT   R8   R   R   R   R	   t   callableRq   RK   R   Rv   t   shapeRQ   t
   ValueErrorRI   (   RE   R^   Rt   Ri   t   groupsRu   R   (    (   R^   RE   Rt   Rj   s7   lib/python2.7/site-packages/scipy/integrate/_ivp/bdf.pyR]   ô   s:    			$	!c   &   
   C` s½  |  j  } |  j } |  j } d t j t j | |  j t j ƒ | ƒ } |  j | k rƒ | } t	 | |  j
 | |  j ƒ d |  _ nD |  j | k  r¾ | } t	 | |  j
 | |  j ƒ d |  _ n	 |  j } |  j } |  j } |  j
 } |  j }	 |  j }
 |  j } |  j } |  j } |  j d  k } t } x©| sÏ| | k  rFt |  j f S| |  j } | | } |  j | |  j d k r³|  j } t	 | | t j | | ƒ | ƒ d |  _ d  } n  | | } t j | ƒ } t j | | d  d d ƒ} | | t j | ƒ } t j | d | d !j |
 d | d !ƒ |	 | } t } | |	 | } x | sæ| d  k ry|  j |  j | | ƒ } n  t |  j | | | | | |  j | |  j  ƒ	 \ } } } } | sJ| rÂPn  |  j | | ƒ } d  } t! } qJqJW| s"d } | | 9} t	 | | | ƒ d |  _ d  } q'n  d d t" d d t" | } | | t j | ƒ } | | | } t# | | ƒ } | d k rÆt$ t% | | d | d ƒ } | | 9} t	 | | | ƒ d |  _ q't! } q'W|  j d 7_ | |  _  | |  _& | |  _ | |  _ | |  _ | | | d | | d <| | | d <x6 t' t( | d ƒ ƒ D] } | | c | | d 7<qKW|  j | d k  rŠt! d  f S| d k r¿| | d | | } t# | | ƒ }  n	 t j }  | t) k  r| | d | | d }! t# |! | ƒ }" n	 t j }" t j* |  | |" g ƒ }# |# d t j+ | | d	 ƒ }$ t j, |$ ƒ d }% | |% 7} | |  _
 t- t. | t j$ |$ ƒ ƒ } |  j | 9_ t	 | | | ƒ d |  _ d  |  _ t! d  f S(
   Ni
   i    i   R   g      à?gÍÌÌÌÌÌì?i   iÿÿÿÿi   (/   RT   R"   RP   R   t   abst	   nextafterRU   t   infRV   R&   R   Rh   RS   RR   Re   Rd   Rf   R   R3   R^   R   R(   t   TOO_SMALL_STEPRk   t   sumR    R!   RF   R   R@   R.   R4   R[   R-   R*   R   RY   t
   MIN_FACTORR8   t   reversedR)   Rc   R`   R   t   argmaxRZ   t
   MAX_FACTOR(&   RE   RT   R"   RP   t   min_stepRV   RS   RR   R   Re   Rd   Rf   R   R3   t   current_jact   step_acceptedt   hR/   R0   R5   R2   R:   R1   t   n_itert   y_newR7   R   t   safetyt   errort
   error_normt   it   error_mt   error_m_normt   error_pt   error_p_normt   error_normst   factorst   delta_order(    (    s7   lib/python2.7/site-packages/scipy/integrate/_ivp/bdf.pyt
   _step_impl)  sÌ    			-										
	!		
3	
	

					
		
			c         C` s=   t  |  j |  j |  j |  j |  j |  j |  j d  j ƒ  ƒ S(   Ni   (   t   BdfDenseOutputt   t_oldRT   RV   RU   R   R"   R'   (   RE   (    (    s7   lib/python2.7/site-packages/scipy/integrate/_ivp/bdf.pyt   _dense_output_impl·  s    N(   t   __name__t
   __module__t   __doc__R   R}   R   R(   RO   R]   R•   R˜   (    (    (    s7   lib/python2.7/site-packages/scipy/integrate/_ivp/bdf.pyRA   I   s   o9	5	ŽR–   c           B` s   e  Z d  „  Z d „  Z RS(   c         C` sl   t  t |  ƒ j | | ƒ | |  _ |  j | t j |  j ƒ |  _ | d t j |  j ƒ |  _ | |  _	 d  S(   Ni   (
   RN   R–   RO   R   RT   R   R   t   t_shiftt   denomR"   (   RE   R—   RT   R‡   R   R"   (    (    s7   lib/python2.7/site-packages/scipy/integrate/_ivp/bdf.pyRO   ½  s
    	 c         C` sá   | j  d k r5 | |  j |  j } t j | ƒ } nI | |  j d  d  … d  f |  j d  d  … d  f } t j | d d ƒ} t j |  j d j | ƒ } | j  d k r½ | |  j d 7} n  | |  j d d  d  … d  f 7} | S(   Ni    R   i   (	   t   ndimRœ   R   R   R   R   R    R"   R!   (   RE   RT   t   xt   pR8   (    (    s7   lib/python2.7/site-packages/scipy/integrate/_ivp/bdf.pyt
   _call_implÄ  s    4 (   R™   Rš   RO   R¡   (    (    (    s7   lib/python2.7/site-packages/scipy/integrate/_ivp/bdf.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   Rc   R*   R€   Rƒ   R   R&   R@   RA   R–   (    (    (    s7   lib/python2.7/site-packages/scipy/integrate/_ivp/bdf.pyt   <module>   s"   :	
		$ÿ t