ó
Ê½÷Xc        	   @` s’  d  d l  m Z m Z m Z d  d l m Z d  d l Z d  d l m Z m	 Z	 m
 Z
 m Z d  d l m Z m Z m Z m Z m Z m Z d  d l m Z d d l m Z e Z y d  d l j Z Wn e k
 rÚ e Z n Xe Z d	 d
 d d d d d g Z d e f d „  ƒ  YZ  d „  Z! d „  Z" d e d „ Z$ d d d d d e% ƒ  d „ Z& d d d d d d d d d „ Z' d „  Z( e e e d „ Z) d S(   i    (   t   divisiont   print_functiont   absolute_import(   t   warnN(   t   asarrayt   emptyt   ravelt   nonzero(   t   isspmatrix_csct   isspmatrix_csrt
   isspmatrixt   SparseEfficiencyWarningt
   csc_matrixt
   csr_matrix(   t   LinAlgErrori   (   t   _superlut
   use_solvert   spsolvet   splut   spilut
   factorizedt   MatrixRankWarningt   spsolve_triangularc           B` s   e  Z RS(    (   t   __name__t
   __module__(    (    (    sB   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/linalg/dsolve/linsolve.pyR      s   c          K` s$   d |  k r  |  d t  ƒ  d <n  d S(   sƒ  
    Select default sparse direct solver to be used.

    Parameters
    ----------
    useUmfpack : bool, optional
        Use UMFPACK over SuperLU. Has effect only if scikits.umfpack is
        installed. Default: True

    Notes
    -----
    The default sparse solver is umfpack when available
    (scikits.umfpack is installed). This can be changed by passing
    useUmfpack = False, which then causes the always present SuperLU
    based solver to be used.

    Umfpack requires a CSR/CSC matrix to have sorted column/row indices. If
    sure that the matrix fulfills this, pass ``assumeSortedIndices=True``
    to gain some speed.

    t
   useUmfpackN(   t   globals(   t   kwargs(    (    sB   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/linalg/dsolve/linsolve.pyR      s    c         C` sC   i d d 6d d 6d d 6d d 6} |  j  j |  j j  j } | | S(   s8   Get umfpack family string given the sparse matrix dtype.t   dit   zit   Dit   dlt   zlt   Dl(   t   dtypet   chart   indices(   t   At   familyt   dt(    (    sB   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/linalg/dsolve/linsolve.pyt   _get_umf_family8   s    "c         C` sD  t  |  ƒ p t |  ƒ s4 t |  ƒ }  t d t ƒ n  t | ƒ } | sU t | ƒ } n  | j d k pƒ | j d k oƒ | j d d k } |  j	 ƒ  |  j
 ƒ  }  t j |  j | j ƒ } |  j | k rÕ |  j | ƒ }  n  | j | k rö | j | ƒ } n  |  j \ } } | | k r-t d | | f f ƒ ‚ n  | | j d k rct d |  j | j d f ƒ ‚ n  | olt } | r| r| r| j ƒ  }	 n | }	 t |	 d |  j ƒj ƒ  }	 t rÆt d ƒ ‚ n  |  j j d	 k rçt d
 ƒ ‚ n  t j t |  ƒ ƒ }
 |
 j t j |  |	 d t ƒ} n#| r>| r>| j ƒ  } t } n  | sët  |  ƒ rYd } n d } t d | ƒ } t j | |  j  |  j! |  j" |  j# | | d | ƒ\ } } | d k rÓt d t$ ƒ | j% t j& ƒ n  | r@| j ƒ  } q@nUt' |  ƒ } t  | ƒ st d t ƒ t | ƒ } n  g  } g  } g  } x² t( | j d ƒ D] } | d d … | f j) j ƒ  } | | ƒ } t j* | ƒ } | j d } | j+ | ƒ | j+ t j, | d t- ƒ| ƒ | j+ t j | | d |  j ƒƒ qEWt j. | ƒ } t j. | ƒ } t j. | ƒ } |  j/ | | | f f d | j d |  j ƒ} | S(   s1  Solve the sparse linear system Ax=b, where b may be a vector or a matrix.

    Parameters
    ----------
    A : ndarray or sparse matrix
        The square matrix A will be converted into CSC or CSR form
    b : ndarray or sparse matrix
        The matrix or vector representing the right hand side of the equation.
        If a vector, b.shape must be (n,) or (n, 1).
    permc_spec : str, optional
        How to permute the columns of the matrix for sparsity preservation.
        (default: 'COLAMD')

        - ``NATURAL``: natural ordering.
        - ``MMD_ATA``: minimum degree ordering on the structure of A^T A.
        - ``MMD_AT_PLUS_A``: minimum degree ordering on the structure of A^T+A.
        - ``COLAMD``: approximate minimum degree column ordering
    use_umfpack : bool, optional
        if True (default) then use umfpack for the solution.  This is
        only referenced if b is a vector and ``scikit-umfpack`` is installed.

    Returns
    -------
    x : ndarray or sparse matrix
        the solution of the sparse linear equation.
        If b is a vector, then x is a vector of size A.shape[1]
        If b is a matrix, then x is a matrix of size (A.shape[1], b.shape[1])

    Notes
    -----
    For solving the matrix expression AX = B, this solver assumes the resulting
    matrix X is sparse, as is often the case for very sparse inputs.  If the
    resulting X is dense, the construction of this sparse result will be
    relatively expensive.  In that case, consider converting A to a dense
    matrix and using scipy.linalg.solve or its variants.
    s.   spsolve requires A be CSC or CSR matrix formati   i   s$   matrix must be square (has shape %s)i    s)   matrix - rhs dimension mismatch (%s - %s)R"   s   Scikits.umfpack not installed.t   dDsZ   convert matrix data to double, please, using .astype(), or set linsolve.useUmfpack = Falset   autoTransposet   ColPermt   optionss   Matrix is exactly singularsC   spsolve is more efficient when sparse b is in the CSC matrix formatNt   shape(0   R   R	   R   R   R   R
   R   t   ndimR-   t   sort_indicest   asfptypet   npt   promote_typesR"   t   astypet
   ValueErrorR   t   toarrayR   t   noScikitt   RuntimeErrorR#   t   umfpackt   UmfpackContextR(   t   linsolvet	   UMFPACK_At   Truet   Falset   dictR   t   gssvt   nnzt   dataR$   t   indptrR   t   fillt   nanR   t   rangeR%   t   flatnonzerot   appendt   onest   intt   concatenatet	   __class__(   R%   t   bt
   permc_spect   use_umfpackt   b_is_sparset   b_is_vectort   result_dtypet   Mt   Nt   b_vect   umft   xt   flagR,   t   infot
   Afactsolvet	   data_segst   row_segst   col_segst   jt   bjt   xjt   wt   segment_lengtht   sparse_datat
   sparse_rowt
   sparse_col(    (    sB   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/linalg/dsolve/linsolve.pyR   >   sŽ    %
1
		! 'c   
   
   C` sÖ   t  |  ƒ s( t |  ƒ }  t d t ƒ n  |  j ƒ  |  j ƒ  }  |  j \ } } | | k rh t d ƒ ‚ n  t d | d | d | d | ƒ }	 | d	 k	 r¥ |	 j
 | ƒ n  t j | |  j |  j |  j |  j d t d |	 ƒS(
   sú  
    Compute the LU decomposition of a sparse, square matrix.

    Parameters
    ----------
    A : sparse matrix
        Sparse matrix to factorize. Should be in CSR or CSC format.
    permc_spec : str, optional
        How to permute the columns of the matrix for sparsity preservation.
        (default: 'COLAMD')

        - ``NATURAL``: natural ordering.
        - ``MMD_ATA``: minimum degree ordering on the structure of A^T A.
        - ``MMD_AT_PLUS_A``: minimum degree ordering on the structure of A^T+A.
        - ``COLAMD``: approximate minimum degree column ordering

    diag_pivot_thresh : float, optional
        Threshold used for a diagonal entry to be an acceptable pivot.
        See SuperLU user's guide for details [1]_
    drop_tol : float, optional
        (deprecated) No effect.
    relax : int, optional
        Expert option for customizing the degree of relaxing supernodes.
        See SuperLU user's guide for details [1]_
    panel_size : int, optional
        Expert option for customizing the panel size.
        See SuperLU user's guide for details [1]_
    options : dict, optional
        Dictionary containing additional expert options to SuperLU.
        See SuperLU user guide [1]_ (section 2.4 on the 'Options' argument)
        for more details. For example, you can specify
        ``options=dict(Equil=False, IterRefine='SINGLE'))``
        to turn equilibration off and perform a single iterative refinement.

    Returns
    -------
    invA : scipy.sparse.linalg.SuperLU
        Object, which has a ``solve`` method.

    See also
    --------
    spilu : incomplete LU decomposition

    Notes
    -----
    This function uses the SuperLU library.

    References
    ----------
    .. [1] SuperLU http://crd.lbl.gov/~xiaoye/SuperLU/

    s   splu requires CSC matrix formats   can only factor square matricest   DiagPivotThreshR+   t	   PanelSizet   Relaxt   iluR,   N(   R   R   R   R   R/   R0   R-   R4   R>   t   Nonet   updateR   t   gstrfR@   RA   R$   RB   R=   (
   R%   RM   t   diag_pivot_thresht   drop_tolt   relaxt
   panel_sizeR,   RR   RS   t   _options(    (    sB   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/linalg/dsolve/linsolve.pyR   Ä   s    7
$c	         C` sè   t  |  ƒ s( t |  ƒ }  t d t ƒ n  |  j ƒ  |  j ƒ  }  |  j \ }	 }
 |	 |
 k rh t d ƒ ‚ n  t d | d | d | d | d | d | d	 | ƒ } | d k	 r· | j
 | ƒ n  t j |
 |  j |  j |  j |  j d
 t d | ƒS(   s  
    Compute an incomplete LU decomposition for a sparse, square matrix.

    The resulting object is an approximation to the inverse of `A`.

    Parameters
    ----------
    A : (N, N) array_like
        Sparse matrix to factorize
    drop_tol : float, optional
        Drop tolerance (0 <= tol <= 1) for an incomplete LU decomposition.
        (default: 1e-4)
    fill_factor : float, optional
        Specifies the fill ratio upper bound (>= 1.0) for ILU. (default: 10)
    drop_rule : str, optional
        Comma-separated string of drop rules to use.
        Available rules: ``basic``, ``prows``, ``column``, ``area``,
        ``secondary``, ``dynamic``, ``interp``. (Default: ``basic,area``)

        See SuperLU documentation for details.

    Remaining other options
        Same as for `splu`

    Returns
    -------
    invA_approx : scipy.sparse.linalg.SuperLU
        Object, which has a ``solve`` method.

    See also
    --------
    splu : complete LU decomposition

    Notes
    -----
    To improve the better approximation to the inverse, you may need to
    increase `fill_factor` AND decrease `drop_tol`.

    This function uses the SuperLU library.

    s   splu requires CSC matrix formats   can only factor square matricest   ILU_DropRulet   ILU_DropTolt   ILU_FillFactorRe   R+   Rf   Rg   Rh   R,   N(   R   R   R   R   R/   R0   R-   R4   R>   Ri   Rj   R   Rk   R@   RA   R$   RB   R<   (   R%   Rm   t   fill_factort	   drop_ruleRM   Rl   Rn   Ro   R,   RR   RS   Rp   (    (    sB   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/linalg/dsolve/linsolve.pyR     s     +
$c         ` sÃ   t  r² t r t d ƒ ‚ n  t ˆ  ƒ sC t ˆ  ƒ ‰  t d t ƒ n  ˆ  j ƒ  ˆ  j ƒ  ‰  ˆ  j	 j
 d k rz t d ƒ ‚ n  t j t ˆ  ƒ ƒ ‰ ˆ j ˆ  ƒ ‡  ‡ f d †  } | St ˆ  ƒ j Sd S(   sÂ  
    Return a function for solving a sparse linear system, with A pre-factorized.

    Parameters
    ----------
    A : (N, N) array_like
        Input.

    Returns
    -------
    solve : callable
        To solve the linear system of equations given in `A`, the `solve`
        callable should be passed an ndarray of shape (N,).

    Examples
    --------
    >>> from scipy.sparse.linalg import factorized
    >>> A = np.array([[ 3. ,  2. , -1. ],
    ...               [ 2. , -2. ,  4. ],
    ...               [-1. ,  0.5, -1. ]])
    >>> solve = factorized(A) # Makes LU decomposition.
    >>> rhs1 = np.array([1, -2, 0])
    >>> solve(rhs1) # Uses the LU factors.
    array([ 1., -2., -2.])

    s   Scikits.umfpack not installed.s   splu requires CSC matrix formatR)   sZ   convert matrix data to double, please, using .astype(), or set linsolve.useUmfpack = Falsec         ` s   ˆ j  t j ˆ  |  d t ƒS(   NR*   (   t   solveR8   R;   R<   (   RL   (   R%   RU   (    sB   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/linalg/dsolve/linsolve.pyRv   }  s    N(   R   R6   R7   R   R   R   R   R/   R0   R"   R#   R4   R8   R9   R(   t   numericR   Rv   (   R%   Rv   (    (   R%   RU   sB   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/linalg/dsolve/linsolve.pyR   N  s    
c         C` s~  t  |  ƒ s( t d t ƒ t |  ƒ }  n | s= |  j ƒ  }  n  |  j d |  j d k rr t d j |  j ƒ ƒ ‚ n  |  j ƒ  |  j	 ƒ  t
 j | ƒ } | j d k r¿ t d j | j ƒ ƒ ‚ n  |  j d | j d k rú t d j |  j | j ƒ ƒ ‚ n  | r	| } n | j ƒ  } | r0t t | ƒ ƒ } n t t | ƒ d d d ƒ } x+| D]#} |  j | } |  j | d }	 | r|	 d }
 t | |	 d ƒ } n | }
 t | d |	 ƒ } |	 | k sÕ|  j |
 | k  rít d	 j | ƒ ƒ ‚ n  |  j |
 | k r"t d
 j | |  j |
 ƒ ƒ ‚ n  |  j | } |  j | } | | c t
 j | | j | ƒ 8<| | c |  j |
 <qSW| S(   sc  
    Solve the equation `A x = b` for `x`, assuming A is a triangular matrix.

    Parameters
    ----------
    A : (M, M) sparse matrix
        A sparse square triangular matrix. Should be in CSR format.
    b : (M,) or (M, N) array_like
        Right-hand side matrix in `A x = b`
    lower : bool, optional
        Whether `A` is a lower or upper triangular matrix.
        Default is lower triangular matrix.
    overwrite_A : bool, optional
        Allow changing `A`. The indices of `A` are going to be sorted and zero
        entries are going to be removed.
        Enabling gives a performance gain. Default is False.
    overwrite_b : bool, optional
        Allow overwriting data in `b`.
        Enabling gives a performance gain. Default is False.

    Returns
    -------
    x : (M,) or (M, N) ndarray
        Solution to the system `A x = b`.  Shape of return matches shape of `b`.

    Raises
    ------
    LinAlgError
        If `A` is singular or not triangular.
    ValueError
        If shape of `A` or shape of `b` do not match the requirements.

    Notes
    -----
    .. versionadded:: 0.19.0
    s8   CSR matrix format is required. Converting to CSR matrix.i    i   s.   A must be a square matrix but its shape is {}.i   s,   b must have 1 or 2 dims but its shape is {}.sˆ   The size of the dimensions of A must be equal to the size of the first dimension of b but the shape of A is {} and the shape of b is {}.iÿÿÿÿs%   A is singular: {}th diagonal is zero!s5   A is no triangular matrix: entry [{},{}] is not zero!(   i   i   (   R	   R   R   R   t   copyR-   R4   t   formatt   eliminate_zerosR/   R1   t
   asanyarrayR.   RE   t   lenRB   t   sliceR$   R   RA   t   dott   T(   R%   RL   t   lowert   overwrite_At   overwrite_bRV   t   row_indicest   it   indptr_startt   indptr_stopt   A_diagonal_index_row_it   A_off_diagonal_indices_row_it   A_column_indices_in_row_it   A_values_in_row_i(    (    sB   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/linalg/dsolve/linsolve.pyR   …  sR    '

		
		#(*   t
   __future__R    R   R   t   warningsR   t   numpyR1   R   R   R   R   t   scipy.sparseR   R	   R
   R   R   R   t   scipy.linalgR   t    R   R=   R6   t   scikits.umfpackR8   t   ImportErrorR<   R   t   __all__t   UserWarningR   R   R(   Ri   R   R>   R   R   R   R   (    (    (    sB   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/linalg/dsolve/linsolve.pyt   <module>   s0   ".
		†I?	7