ó
î&]\c           @` 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 m Z m Z m Z m Z m Z m Z m Z d d l m Z d d l m Z m Z d g Z e e e e d	 „ Z e d
 „ Z e e d „ Z e d „ Z d S(   i    (   t   divisiont   print_functiont   absolute_import(   t   warnN(   t
   atleast_2dt   ComplexWarningt   aranget
   zeros_liket   imagt   diagt   iscomplexobjt   trilt   triut   argsortt
   empty_likei   (   t   _asarray_validated(   t   get_lapack_funcst   _compute_lworkt   ldlc         C` sç  t  t |  d | ƒƒ } | j d | j d k rA t d ƒ ‚ n  | j d k rx t | ƒ t | ƒ t j g  d t ƒf S| j d } t	 | ƒ r— t
 n t } | t
 k rï | rï d \ } }	 t j t t | ƒ ƒ ƒ rû t d t d	 d
 ƒqû n d \ } }	 t | |	 f | f ƒ \ }
 } t | | d | ƒ} |
 | d | d | d | ƒ\ } } } | d k  rƒt d j | j ƒ  | ƒ ƒ ‚ n  t | d | ƒ\ } } t | | d | d | ƒ\ } } t | | | d | ƒ\ } } | | | f S(   sG   Computes the LDLt or Bunch-Kaufman factorization of a symmetric/
    hermitian matrix.

    This function returns a block diagonal matrix D consisting blocks of size
    at most 2x2 and also a possibly permuted unit lower triangular matrix
    ``L`` such that the factorization ``A = L D L^H`` or ``A = L D L^T``
    holds. If ``lower`` is False then (again possibly permuted) upper
    triangular matrices are returned as outer factors.

    The permutation array can be used to triangularize the outer factors
    simply by a row shuffle, i.e., ``lu[perm, :]`` is an upper/lower
    triangular matrix. This is also equivalent to multiplication with a
    permutation matrix ``P.dot(lu)`` where ``P`` is a column-permuted
    identity matrix ``I[:, perm]``.

    Depending on the value of the boolean ``lower``, only upper or lower
    triangular part of the input array is referenced. Hence a triangular
    matrix on entry would give the same result as if the full matrix is
    supplied.

    Parameters
    ----------
    a : array_like
        Square input array
    lower : bool, optional
        This switches between the lower and upper triangular outer factors of
        the factorization. Lower triangular (``lower=True``) is the default.
    hermitian : bool, optional
        For complex-valued arrays, this defines whether ``a = a.conj().T`` or
        ``a = a.T`` is assumed. For real-valued arrays, this switch has no
        effect.
    overwrite_a : bool, optional
        Allow overwriting data in ``a`` (may enhance performance). The default
        is False.
    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
    -------
    lu : ndarray
        The (possibly) permuted upper/lower triangular outer factor of the
        factorization.
    d : ndarray
        The block diagonal multiplier of the factorization.
    perm : ndarray
        The row-permutation index array that brings lu into triangular form.

    Raises
    ------
    ValueError
        If input array is not square.
    ComplexWarning
        If a complex-valued array with nonzero imaginary parts on the
        diagonal is given and hermitian is set to True.

    Examples
    --------
    Given an upper triangular array `a` that represents the full symmetric
    array with its entries, obtain `l`, 'd' and the permutation vector `perm`:

    >>> import numpy as np
    >>> from scipy.linalg import ldl
    >>> a = np.array([[2, -1, 3], [0, 2, 0], [0, 0, 1]])
    >>> lu, d, perm = ldl(a, lower=0) # Use the upper part
    >>> lu
    array([[ 0. ,  0. ,  1. ],
           [ 0. ,  1. , -0.5],
           [ 1. ,  1. ,  1.5]])
    >>> d
    array([[-5. ,  0. ,  0. ],
           [ 0. ,  1.5,  0. ],
           [ 0. ,  0. ,  2. ]])
    >>> perm
    array([2, 1, 0])
    >>> lu[perm, :]
    array([[ 1. ,  1. ,  1.5],
           [ 0. ,  1. , -0.5],
           [ 0. ,  0. ,  1. ]])
    >>> lu.dot(d).dot(lu.T)
    array([[ 2., -1.,  3.],
           [-1.,  2.,  0.],
           [ 3.,  0.,  1.]])

    Notes
    -----
    This function uses ``?SYTRF`` routines for symmetric matrices and
    ``?HETRF`` routines for Hermitian matrices from LAPACK. See [1]_ for
    the algorithm details.

    Depending on the ``lower`` keyword value, only lower or upper triangular
    part of the input array is referenced. Moreover, this keyword also defines
    the structure of the outer factors of the factorization.

    .. versionadded:: 1.1.0

    See also
    --------
    cholesky, lu

    References
    ----------
    .. [1] J.R. Bunch, L. Kaufman, Some stable methods for calculating
       inertia and solving symmetric linear systems, Math. Comput. Vol.31,
       1977. DOI: 10.2307/2005787

    t   check_finitei    i   s%   The input array "a" should be square.t   dtypet   hetrft   hetrf_lworks‡   scipy.linalg.ldl():
The imaginary parts of the diagonalare ignored. Use "hermitian=False" for factorization ofcomplex symmetric arrays.t
   stackleveli   t   sytrft   sytrf_lworkt   lowert   lworkt   overwrite_asv   {} exited with the internal error "illegal value in argument number {}". See LAPACK documentation for the error codes.t	   hermitian(   R   R   (   R   R   (   R   R   t   shapet
   ValueErrort   sizeR   t   npt   arrayt   intR
   t   complext   floatt   anyR   R	   R   R   R   R   t   formatt   uppert   _ldl_sanitize_ipivt   _ldl_get_d_and_lt   _ldl_construct_tri_factor(   t   AR   R   R   R   t   at   nt   r_or_ct   st   slt   solvert   solver_lworkR   t   ldut   pivt   infot   swap_arrt	   pivot_arrt   dt   lut   perm(    (    s7   lib/python2.7/site-packages/scipy/linalg/_decomp_ldl.pyR      s0    m(	!c         C` s_  |  j  } t | ƒ } t | d t ƒ} t } | rH d d d | d f n d d | d d d f \ } } } }	 }
 xâ t | |	 |
 ƒ D]Î } | r› t } qƒ n  |  | } | d k rã | | d k rÖ | | d | | <n  d | | <qƒ | d k  rE| |  | | k rE| | d k r.| | d | | | <n  d | | | <t } qƒ t d ƒ ‚ qƒ W| | f S(   s„  
    This helper function takes the rather strangely encoded permutation array
    returned by the LAPACK routines ?(HE/SY)TRF and converts it into
    regularized permutation and diagonal pivot size format.

    Since FORTRAN uses 1-indexing and LAPACK uses different start points for
    upper and lower formats there are certain offsets in the indices used
    below.

    Let's assume a result where the matrix is 6x6 and there are two 2x2
    and two 1x1 blocks reported by the routine. To ease the coding efforts,
    we still populate a 6-sized array and fill zeros as the following ::

        pivots = [2, 0, 2, 0, 1, 1]

    This denotes a diagonal matrix of the form ::

        [x x        ]
        [x x        ]
        [    x x    ]
        [    x x    ]
        [        x  ]
        [          x]

    In other words, we write 2 when the 2x2 block is first encountered and
    automatically write 0 to the next entry and skip the next spin of the
    loop. Thus, a separate counter or array appends to keep track of block
    sizes are avoided. If needed, zeros can be filtered out later without
    losing the block structure.

    Parameters
    ----------
    a : ndarray
        The permutation array ipiv returned by LAPACK
    lower : bool, optional
        The switch to select whether upper or lower triangle is chosen in
        the LAPACK call.

    Returns
    -------
    swap_ : ndarray
        The array that defines the row/column swap operations. For example,
        if row two is swapped with row four, the result is [0, 3, 2, 3].
    pivots : ndarray
        The array that defines the block diagonal structure as given above.

    R   i   i    iÿÿÿÿi   sn   While parsing the permutation array in "scipy.linalg.ldl", invalid entries found. The array syntax is invalid.(   R    R   R   R#   t   Falset   ranget   TrueR   (   R-   R   R.   t   swap_t   pivotst   skip_2x2t   xt   yt   rst   ret   rit   indt   cur_val(    (    s7   lib/python2.7/site-packages/scipy/linalg/_decomp_ldl.pyR)   Ÿ   s*    0	C
 	c         C` sx  t  |  ƒ } t t |  ƒ ƒ } | j d } d } | r= d n d \ } }	 | r^ t |  d ƒ n t |  d ƒ }
 t | ƒ } d |
 | | f <xâ | | d k D]Ð } | | } | d k rd|  | | | |	 f | | | | |	 f <| r| r|  | | | |	 f j ƒ  | | |	 | | f <n* |  | | | |	 f | | |	 | | f <d |
 | | | |	 f <n  | } qš W| |
 f S(   s™  
    Helper function to extract the diagonal and triangular matrices for
    LDL.T factorization.

    Parameters
    ----------
    ldu : ndarray
        The compact output returned by the LAPACK routing
    pivs : ndarray
        The sanitized array of {0, 1, 2} denoting the sizes of the pivots. For
        every 2 there is a succeeding 0.
    lower : bool, optional
        If set to False, upper triangular part is considered.
    hermitian : bool, optional
        If set to False a symmetric complex array is assumed.

    Returns
    -------
    d : ndarray
        The block diagonal matrix.
    lu : ndarray
        The upper/lower triangular matrix
    i    i   iÿÿÿÿi   g        (   i   i    (   i    i   (   R
   R	   R   R   R   R   t   conj(   R4   t   pivsR   R   t   is_cR9   R.   t   blk_iRB   RC   R:   t	   diag_indst   blkt   inc(    (    s7   lib/python2.7/site-packages/scipy/linalg/_decomp_ldl.pyR*   ô   s$    $
*3*
c         C` sO  |  j  d } t | ƒ } | r2 | d d d f n d | d f \ } } } xò t | | | ƒ D]Þ }	 | |	 }
 |
 |	 k r] | r… |	 n d } | r— | n |	 d } | |	 | r´ d n d k rì | | rÌ d n d 7} | | râ d n d 7} n  |  |	 |
 g | | … f |  |
 |	 g | | … f <| |	 |
 g | |
 |	 g <q] q] W|  t | ƒ f S(   s‹  
    Helper function to construct explicit outer factors of LDL factorization.

    If lower is True the permuted factors are multiplied as L(1)*L(2)*...*L(k).
    Otherwise, the permuted factors are multiplied as L(k)*...*L(2)*L(1). See
    LAPACK documentation for more details.

    Parameters
    ----------
    lu : ndarray
        The triangular array that is extracted from LAPACK routine call with
        ones on the diagonals.
    swap_vec : ndarray
        The array that defines the row swapping indices. If k'th entry is m
        then rows k,m are swapped. Notice that m'th entry is not necessarily
        k to avoid undoing the swapping.
    pivs : ndarray
        The array that defines the block diagonal structure returned by
        _ldl_sanitize_ipiv().
    lower : bool, optional
        The boolean to switch between lower and upper triangular structure.

    Returns
    -------
    lu : ndarray
        The square outer factor which satisfies the L * D * L.T = A
    perm : ndarray
        The permutation vector that brings the lu to the triangular form

    Notes
    -----
    Note that the original argument "lu" is overwritten.

    i    i   iÿÿÿÿi   (   R   R   R=   R   (   R:   t   swap_vecRJ   R   R.   R;   RD   RE   RF   RG   t   s_indt   col_st   col_e(    (    s7   lib/python2.7/site-packages/scipy/linalg/_decomp_ldl.pyR+   ,  s    #1
2!(   t
   __future__R    R   R   t   warningsR   t   numpyR!   R   R   R   R   R   R	   R
   R   R   R   R   t   decompR   t   lapackR   R   t   __all__R>   R<   R   R)   R*   R+   (    (    (    s7   lib/python2.7/site-packages/scipy/linalg/_decomp_ldl.pyt   <module>   s   L	‘U8