ó
î&]\c           @` s¯   d  Z  d d l m Z m Z m Z d d l Z d d l m Z d d l m	 Z	 d d d	 g Z
 d
 „  Z e d d e e d „ Z d e e e e d „ Z e d d e d „ Z d S(   s   QR decomposition functions.i    (   t   divisiont   print_functiont   absolute_importNi   (   t   get_lapack_funcs(   t   _datacopiedt   qrt   qr_multiplyt   rqc         O` s    | j  d d ƒ } | d k r[ d | d <|  | | Ž  } | d d j j t j ƒ | d <n  |  | | Ž  } | d d k  r˜ t d | d | f ƒ ‚ n  | d  S(   s[   Call a LAPACK routine, determining lwork automatically and handling
    error return valuest   lworkiÿÿÿÿiþÿÿÿi    s.   illegal value in %d-th argument of internal %sN(   Niÿÿÿÿ(   t   gett   Nonet   realt   astypet   numpyt   intt
   ValueError(   t   ft   namet   argst   kwargsR   t   ret(    (    s5   lib/python2.7/site-packages/scipy/linalg/decomp_qr.pyt   safecall   s    
$t   fullc      	   C` s©  | d k r t  d ƒ ‚ n  | r3 t j |  ƒ } n t j |  ƒ } t | j ƒ d k rf t  d ƒ ‚ n  | j \ } } | p‡ t | |  ƒ } | rÓ t d | f ƒ \ }	 t |	 d	 | d
 | ƒ\ }
 } } | d 8} n9 t d | f ƒ \ } t | d | d | d
 | ƒ\ }
 } | d k s$| | k  r6t j	 |
 ƒ } n% t j	 |
 d | … d d … f ƒ } | rp| | f } n	 | f } | d k r‰| S| d k r¦|
 | f f | St d |
 f ƒ \ } | | k  rt | d |
 d d … d | … f | d | d
 d ƒ\ } nš | d k r7t | d |
 | d | d
 d ƒ\ } ng |
 j
 j } t j | | f d | ƒ} |
 | d d … d | … f <t | d | | d | d
 d ƒ\ } | f | S(   s@  
    Compute QR decomposition of a matrix.

    Calculate the decomposition ``A = Q R`` where Q is unitary/orthogonal
    and R upper triangular.

    Parameters
    ----------
    a : (M, N) array_like
        Matrix to be decomposed
    overwrite_a : bool, optional
        Whether data in a is overwritten (may improve performance)
    lwork : int, optional
        Work array size, lwork >= a.shape[1]. If None or -1, an optimal size
        is computed.
    mode : {'full', 'r', 'economic', 'raw'}, optional
        Determines what information is to be returned: either both Q and R
        ('full', default), only R ('r') or both Q and R but computed in
        economy-size ('economic', see Notes). The final option 'raw'
        (added in Scipy 0.11) makes the function return two matrices
        (Q, TAU) in the internal format used by LAPACK.
    pivoting : bool, optional
        Whether or not factorization should include pivoting for rank-revealing
        qr decomposition. If pivoting, compute the decomposition
        ``A P = Q R`` as above, but where P is chosen such that the diagonal
        of R is non-increasing.
    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
    -------
    Q : float or complex ndarray
        Of shape (M, M), or (M, K) for ``mode='economic'``.  Not returned
        if ``mode='r'``.
    R : float or complex ndarray
        Of shape (M, N), or (K, N) for ``mode='economic'``.  ``K = min(M, N)``.
    P : int ndarray
        Of shape (N,) for ``pivoting=True``. Not returned if
        ``pivoting=False``.

    Raises
    ------
    LinAlgError
        Raised if decomposition fails

    Notes
    -----
    This is an interface to the LAPACK routines dgeqrf, zgeqrf,
    dorgqr, zungqr, dgeqp3, and zgeqp3.

    If ``mode=economic``, the shapes of Q and R are (M, K) and (K, N) instead
    of (M,M) and (M,N), with ``K=min(M,N)``.

    Examples
    --------
    >>> from scipy import random, linalg, dot, diag, all, allclose
    >>> a = random.randn(9, 6)

    >>> q, r = linalg.qr(a)
    >>> allclose(a, np.dot(q, r))
    True
    >>> q.shape, r.shape
    ((9, 9), (9, 6))

    >>> r2 = linalg.qr(a, mode='r')
    >>> allclose(r, r2)
    True

    >>> q3, r3 = linalg.qr(a, mode='economic')
    >>> q3.shape, r3.shape
    ((9, 6), (6, 6))

    >>> q4, r4, p4 = linalg.qr(a, pivoting=True)
    >>> d = abs(diag(r4))
    >>> all(d[1:] <= d[:-1])
    True
    >>> allclose(a[:, p4], dot(q4, r4))
    True
    >>> q4.shape, r4.shape, p4.shape
    ((9, 9), (9, 6), (6,))

    >>> q5, r5, p5 = linalg.qr(a, mode='economic', pivoting=True)
    >>> q5.shape, r5.shape, p5.shape
    ((9, 6), (6, 6), (6,))

    R   R   t   rt   economict   raws>   Mode argument should be one of ['full', 'r','economic', 'raw']i   s   expected 2D arrayt   geqp3t   overwrite_ai   t   geqrfR   Nt   orgqrs   gorgqr/gungqrt   dtype(   R   R   R   R   R   (   R   (   R   (   R   R   (   R   (   R   R   t   asarray_chkfinitet   asarrayt   lent   shapeR   R   R   t   triuR   t   chart   empty(   t   aR   R   t   modet   pivotingt   check_finitet   a1t   Mt   NR   R   t   jpvtt   tauR   t   Rt   Rjt
   gor_un_gqrt   Qt   tt   qqr(    (    s5   lib/python2.7/site-packages/scipy/linalg/decomp_qr.pyR      sN    ]!%	(t   rightc      
   C` sŒ  | d k r$ t  d j | ƒ ƒ ‚ n  t j | ƒ } | j d k  rr t } t j | ƒ } | d k rx | j } qx n t } t j t j	 |  ƒ ƒ }  |  j
 \ } }	 | d k r÷ | j
 d t | |	 | | |	 ƒ k r+t  d j |  j
 | j
 ƒ ƒ ‚ q+n4 | | j
 d k r+t  d j | j
 |  j
 ƒ ƒ ‚ n  t |  | d d	 | ƒ }
 |
 d \ } } t d | f ƒ \ } | j d k r€d } n d } | d d … d t | |	 ƒ … f } | |	 k rˆ| d k rˆ| rˆ| rt j | j
 d | f d | j d d ƒ} | j | d d … d |	 … f <nM t j | | j
 d f d | j d d ƒ} | | d |	 … d d … f <d } | ryd } n d } t } nm | j d r¡| d k s§| rÎ| j } | d k rÅd } qõd } n' d } | } | d k rïd } n d } t | d | | | | | d | ƒ\ } | d k r4| j } n  | d k rh| d d … d t | |	 ƒ … f } n  | r}| j ƒ  } n  | f |
 d S(   s‘	  
    Calculate the QR decomposition and multiply Q with a matrix.

    Calculate the decomposition ``A = Q R`` where Q is unitary/orthogonal
    and R upper triangular. Multiply Q with a vector or a matrix c.

    Parameters
    ----------
    a : (M, N), array_like
        Input array
    c : array_like
        Input array to be multiplied by ``q``.
    mode : {'left', 'right'}, optional
        ``Q @ c`` is returned if mode is 'left', ``c @ Q`` is returned if
        mode is 'right'.
        The shape of c must be appropriate for the matrix multiplications,
        if mode is 'left', ``min(a.shape) == c.shape[0]``,
        if mode is 'right', ``a.shape[0] == c.shape[1]``.
    pivoting : bool, optional
        Whether or not factorization should include pivoting for rank-revealing
        qr decomposition, see the documentation of qr.
    conjugate : bool, optional
        Whether Q should be complex-conjugated. This might be faster
        than explicit conjugation.
    overwrite_a : bool, optional
        Whether data in a is overwritten (may improve performance)
    overwrite_c : bool, optional
        Whether data in c is overwritten (may improve performance).
        If this is used, c must be big enough to keep the result,
        i.e. ``c.shape[0]`` = ``a.shape[0]`` if mode is 'left'.

    Returns
    -------
    CQ : ndarray
        The product of ``Q`` and ``c``.
    R : (K, N), ndarray
        R array of the resulting QR factorization where ``K = min(M, N)``.
    P : (N,) ndarray
        Integer pivot array. Only returned when ``pivoting=True``.

    Raises
    ------
    LinAlgError
        Raised if QR decomposition fails.

    Notes
    -----
    This is an interface to the LAPACK routines ``?GEQRF``, ``?ORMQR``,
    ``?UNMQR``, and ``?GEQP3``.

    .. versionadded:: 0.11.0

    Examples
    --------
    >>> from scipy.linalg import qr_multiply, qr
    >>> A = np.array([[1, 3, 3], [2, 3, 2], [2, 3, 3], [1, 3, 2]])
    >>> qc, r1, piv1 = qr_multiply(A, 2*np.eye(4), pivoting=1)
    >>> qc
    array([[-1.,  1., -1.],
           [-1., -1.,  1.],
           [-1., -1., -1.],
           [-1.,  1.,  1.]])
    >>> r1
    array([[-6., -3., -5.            ],
           [ 0., -1., -1.11022302e-16],
           [ 0.,  0., -1.            ]])
    >>> piv1
    array([1, 0, 2], dtype=int32)
    >>> q2, r2, piv2 = qr(A, mode='economic', pivoting=1)
    >>> np.allclose(2*q2 - qc, np.zeros((4, 3)))
    True

    t   leftR5   s8   Mode argument can only be 'left' or 'right' but not '{}'i   i    s=   Array shapes are not compatible for Q @ c operation: {} vs {}i   s=   Array shapes are not compatible for c @ Q operation: {} vs {}R   t   ormqrt   st   dt   Tt   CNR   t   ordert   FR,   R/   t   Lt   C_CONTIGUOUSs   gormqr/gunmqrt   overwrite_c(   R6   R5   (   R7   (   R8   R9   (   R   t   formatR   R   t   ndimt   Truet
   atleast_2dR:   t   FalseR    R"   t   minR   R
   R   t   typecodet   zerosR   t   flagsR   t   ravel(   R&   t   cR'   R(   t	   conjugateR   R@   t   onedimR+   R,   R   R2   R.   t
   gor_un_mqrt   transt   cct   lrt   cQ(    (    s5   lib/python2.7/site-packages/scipy/linalg/decomp_qr.pyR   °   sp    K	(			%+"+						(c      	   C` s  | d k r t  d ƒ ‚ n  | r3 t j |  ƒ } n t j |  ƒ } t | j ƒ d k rf t  d ƒ ‚ n  | j \ } } | p‡ t | |  ƒ } t d | f ƒ \ } t | d | d | d	 | ƒ\ }	 }
 | d k sÜ | | k  rõ t j	 |	 | | ƒ } n' t j	 |	 | d
 … | d
 … f ƒ } | d k r,| St d |	 f ƒ \ } | | k  ryt | d |	 | |
 d | d	 d ƒ\ } n€ | d k r¬t | d |	 |
 d | d	 d ƒ\ } nM t j
 | | f d |	 j ƒ} |	 | | )t | d | |
 d | d	 d ƒ\ } | | f S(   sš  
    Compute RQ decomposition of a matrix.

    Calculate the decomposition ``A = R Q`` where Q is unitary/orthogonal
    and R upper triangular.

    Parameters
    ----------
    a : (M, N) array_like
        Matrix to be decomposed
    overwrite_a : bool, optional
        Whether data in a is overwritten (may improve performance)
    lwork : int, optional
        Work array size, lwork >= a.shape[1]. If None or -1, an optimal size
        is computed.
    mode : {'full', 'r', 'economic'}, optional
        Determines what information is to be returned: either both Q and R
        ('full', default), only R ('r') or both Q and R but computed in
        economy-size ('economic', see Notes).
    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
    -------
    R : float or complex ndarray
        Of shape (M, N) or (M, K) for ``mode='economic'``.  ``K = min(M, N)``.
    Q : float or complex ndarray
        Of shape (N, N) or (K, N) for ``mode='economic'``.  Not returned
        if ``mode='r'``.

    Raises
    ------
    LinAlgError
        If decomposition fails.

    Notes
    -----
    This is an interface to the LAPACK routines sgerqf, dgerqf, cgerqf, zgerqf,
    sorgrq, dorgrq, cungrq and zungrq.

    If ``mode=economic``, the shapes of Q and R are (K, N) and (M, K) instead
    of (N,N) and (M,N), with ``K=min(M,N)``.

    Examples
    --------
    >>> from scipy import linalg
    >>> a = np.random.randn(6, 9)
    >>> r, q = linalg.rq(a)
    >>> np.allclose(a, r @ q)
    True
    >>> r.shape, q.shape
    ((6, 9), (9, 9))
    >>> r2 = linalg.rq(a, mode='r')
    >>> np.allclose(r, r2)
    True
    >>> r3, q3 = linalg.rq(a, mode='economic')
    >>> r3.shape, q3.shape
    ((6, 6), (6, 9))

    R   R   R   s8   Mode argument should be one of ['full', 'r', 'economic']i   s   expected matrixt   gerqfR   R   Nt   orgrqs   gorgrq/gungrqi   R   (   R   R   R   (   RS   (   RT   (   R   R   R   R    R!   R"   R   R   R   R#   R%   R   (   R&   R   R   R'   R)   R*   R+   R,   RS   R   R.   R/   t
   gor_un_grqR2   t   rq1(    (    s5   lib/python2.7/site-packages/scipy/linalg/decomp_qr.pyR   C  s<    ?'(   t   __doc__t
   __future__R    R   R   R   t   lapackR   t   miscR   t   __all__R   RE   R
   RC   R   R   R   (    (    (    s5   lib/python2.7/site-packages/scipy/linalg/decomp_qr.pyt   <module>   s   	“	’