ó
î&]\c           @` sÄ   d  Z  d d l m Z m Z m Z d d l m Z d d l m Z m	 Z	 d d l
 m Z m Z d d l m Z d d l m Z d	 d
 d g Z e e d „ Z d e e d „ Z e e e d „ Z d S(   s   LU decomposition functions.i    (   t   divisiont   print_functiont   absolute_import(   t   warn(   t   asarrayt   asarray_chkfinitei   (   t   _datacopiedt   LinAlgWarning(   t   get_lapack_funcs(   t   get_flinalg_funcst   lut   lu_solvet	   lu_factorc         C` sô   | r t  |  ƒ } n t |  ƒ } t | j ƒ d k sP | j d | j d k r_ t d ƒ ‚ n  | pq t | |  ƒ } t d
 | f ƒ \ } | | d | ƒ\ } } } | d k  rÄ t d | ƒ ‚ n  | d k rê t d | t d	 d ƒn  | | f S(   s­  
    Compute pivoted LU decomposition of a matrix.

    The decomposition is::

        A = P L U

    where P is a permutation matrix, L lower triangular with unit
    diagonal elements, and U upper triangular.

    Parameters
    ----------
    a : (M, M) array_like
        Matrix to decompose
    overwrite_a : bool, optional
        Whether to overwrite data in A (may increase performance)
    check_finite : bool, optional
        Whether to check that the input matrix contains only finite numbers.
        Disabling may give a performance gain, but may result in problems
        (crashes, non-termination) if the inputs do contain infinities or NaNs.

    Returns
    -------
    lu : (N, N) ndarray
        Matrix containing U in its upper triangle, and L in its lower triangle.
        The unit diagonal elements of L are not stored.
    piv : (N,) ndarray
        Pivot indices representing the permutation matrix P:
        row i of matrix was interchanged with row piv[i].

    See also
    --------
    lu_solve : solve an equation system using the LU factorization of a matrix

    Notes
    -----
    This is a wrapper to the ``*GETRF`` routines from LAPACK.

    Examples
    --------
    >>> from scipy.linalg import lu_factor
    >>> from numpy import tril, triu, allclose, zeros, eye
    >>> A = np.array([[2, 5, 8, 7], [5, 2, 2, 8], [7, 5, 6, 6], [5, 4, 4, 8]])
    >>> lu, piv = lu_factor(A)
    >>> piv
    array([2, 2, 3, 3], dtype=int32)
    
    Convert LAPACK's ``piv`` array to NumPy index and test the permutation 
    
    >>> piv_py = [2, 0, 3, 1]
    >>> L, U = np.tril(lu, k=-1) + np.eye(4), np.triu(lu)
    >>> np.allclose(A[piv_py] - L @ U, np.zeros((4, 4)))
    True
    i   i    i   s   expected square matrixt   getrft   overwrite_as=   illegal value in %d-th argument of internal getrf (lu_factor)s4   Diagonal number %d is exactly zero. Singular matrix.t
   stacklevel(   R   (	   R   R   t   lent   shapet
   ValueErrorR   R   R   R   (   t   aR   t   check_finitet   a1R   R
   t   pivt   info(    (    s5   lib/python2.7/site-packages/scipy/linalg/decomp_lu.pyR      s    7/
c         C` sÌ   |  \ } } | r! t  | ƒ } n t | ƒ } | p? t | | ƒ } | j d | j d k rk t d ƒ ‚ n  t d | | f ƒ \ } | | | | d | d | ƒ\ }	 }
 |
 d k r· |	 St d |
 ƒ ‚ d S(	   s  Solve an equation system, a x = b, given the LU factorization of a

    Parameters
    ----------
    (lu, piv)
        Factorization of the coefficient matrix a, as given by lu_factor
    b : array
        Right-hand side
    trans : {0, 1, 2}, optional
        Type of system to solve:

        =====  =========
        trans  system
        =====  =========
        0      a x   = b
        1      a^T x = b
        2      a^H x = b
        =====  =========
    overwrite_b : bool, optional
        Whether to overwrite data in b (may increase performance)
    check_finite : bool, optional
        Whether to check that the input matrices contain only finite numbers.
        Disabling may give a performance gain, but may result in problems
        (crashes, non-termination) if the inputs do contain infinities or NaNs.

    Returns
    -------
    x : array
        Solution to the system

    See also
    --------
    lu_factor : LU factorize a matrix

    Examples
    --------
    >>> from scipy.linalg import lu_factor, lu_solve
    >>> A = np.array([[2, 5, 8, 7], [5, 2, 2, 8], [7, 5, 6, 6], [5, 4, 4, 8]])
    >>> b = np.array([1, 1, 1, 1])
    >>> lu, piv = lu_factor(A)
    >>> x = lu_solve((lu, piv), b)
    >>> np.allclose(A @ x - b, np.zeros((4,)))
    True

    i    s   incompatible dimensions.t   getrst   transt   overwrite_bs5   illegal value in %d-th argument of internal gesv|posvN(   R   (   R   R   R   R   R   R   (   t
   lu_and_pivt   bR   R   R   R
   R   t   b1R   t   xR   (    (    s5   lib/python2.7/site-packages/scipy/linalg/decomp_lu.pyR   Z   s    .$c   
      C` sÐ   | r t  |  ƒ } n t |  ƒ } t | j ƒ d k rE t d ƒ ‚ n  | pW t | |  ƒ } t d | f ƒ \ } | | d | d | ƒ\ } } } }	 |	 d k  r³ t d |	 ƒ ‚ n  | rÃ | | f S| | | f S(	   sþ  
    Compute pivoted LU decomposition of a matrix.

    The decomposition is::

        A = P L U

    where P is a permutation matrix, L lower triangular with unit
    diagonal elements, and U upper triangular.

    Parameters
    ----------
    a : (M, N) array_like
        Array to decompose
    permute_l : bool, optional
        Perform the multiplication P*L  (Default: do not permute)
    overwrite_a : bool, optional
        Whether to overwrite data in a (may improve performance)
    check_finite : bool, optional
        Whether to check that the input matrix contains only finite numbers.
        Disabling may give a performance gain, but may result in problems
        (crashes, non-termination) if the inputs do contain infinities or NaNs.

    Returns
    -------
    **(If permute_l == False)**

    p : (M, M) ndarray
        Permutation matrix
    l : (M, K) ndarray
        Lower triangular or trapezoidal matrix with unit diagonal.
        K = min(M, N)
    u : (K, N) ndarray
        Upper triangular or trapezoidal matrix

    **(If permute_l == True)**

    pl : (M, K) ndarray
        Permuted L matrix.
        K = min(M, N)
    u : (K, N) ndarray
        Upper triangular or trapezoidal matrix

    Notes
    -----
    This is a LU factorization routine written for Scipy.

    Examples
    --------
    >>> from scipy.linalg import lu
    >>> A = np.array([[2, 5, 8, 7], [5, 2, 2, 8], [7, 5, 6, 6], [5, 4, 4, 8]])
    >>> p, l, u = lu(A)
    >>> np.allclose(A - p @ l @ u, np.zeros((4, 4)))
    True

    i   s   expected matrixR
   t	   permute_lR   i    s4   illegal value in %d-th argument of internal lu.getrf(   R
   (   R   R   R   R   R   R   R	   (
   R   R   R   R   R   t   flut   pt   lt   uR   (    (    s5   lib/python2.7/site-packages/scipy/linalg/decomp_lu.pyR
   ™   s    9$
N(   t   __doc__t
   __future__R    R   R   t   warningsR   t   numpyR   R   t   miscR   R   t   lapackR   t   flinalgR	   t   __all__t   Falset   TrueR   R   R
   (    (    (    s5   lib/python2.7/site-packages/scipy/linalg/decomp_lu.pyt   <module>   s   I?