
    xf5                     V    d Z ddlZddlmZ ddlmZ g dZd Z	 	 ddZ	 	 dd	Z	dd
Z
y)zQR decomposition functions.    N   )get_lapack_funcs)_datacopied)qrqr_multiplyrqc                     |j                  dd      }|dv r?d|d<    | |i |}|d   d   j                  j                  t        j                        |d<    | |i |}|d   dk  rt        d|d    |fz        |dd S )z[Call a LAPACK routine, determining lwork automatically and handling
    error return valueslworkN)Nr   r   z-illegal value in %dth argument of internal %s)getrealastypenumpyint_
ValueError)fnameargskwargsr
   rets         9/usr/lib/python3/dist-packages/scipy/linalg/_decomp_qr.pysafecallr      s     JJw%E
w  b'!*//00<w
T
V
C
2w{H WHd+, - 	-s8O    c                    |dvrt        d      |rt        j                  |       }nt        j                  |       }t	        |j
                        dk7  rt        d      |j
                  \  }}|xs t        ||       }|r(t        d|f      \  }	t        |	d||      \  }
}}|dz  }n"t        d	|f      \  }t        |d
|||      \  }
}|dvs||k  rt        j                  |
      }nt        j                  |
d|ddf         }|r|f}n|f}|dk(  r|S |dk(  r|
|ff|z   S t        d|
f      \  }||k  rt        |d|
ddd|f   ||d      \  }nf|dk(  rt        |d|
||d      \  }nM|
j                  j                  }t        j                  ||f|      }|
|ddd|f<   t        |d|||d      \  }|f|z   S )a  
    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 if
        `overwrite_a` is set to True by reusing the existing input data
        structure rather than creating a new one.)
    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
    --------
    >>> import numpy as np
    >>> from scipy import linalg
    >>> rng = np.random.default_rng()
    >>> a = rng.standard_normal((9, 6))

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

    >>> r2 = linalg.qr(a, mode='r')
    >>> np.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 = np.abs(np.diag(r4))
    >>> np.all(d[1:] <= d[:-1])
    True
    >>> np.allclose(a[:, p4], np.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,))

    )fullr   reconomicrawz>Mode argument should be one of ['full', 'r','economic', 'raw']   zexpected a 2-D array)geqp3r!   )overwrite_ar   )geqrfr#   r
   r"   )r   r   Nr   r   )orgqrzgorgqr/gungqrr   dtype)r   r   asarray_chkfiniteasarraylenshaper   r   r   triur'   charempty)ar"   r
   modepivotingcheck_finitea1MNr!   r   jpvttaur#   RRj
gor_un_gqrQtqqrs                       r   r   r      s   B 99 . / 	/ $$Q']]1
288}/0088DAq5+b!"4K!*re4 MD#	!*re45'2U'24C &&!a%JJrNJJr"1"a%y!WRs{		S	|b  ":u5KJ1uj/2a!e9c!q2		j/2s%"#% HHMMkk1a&*ArrE
j/35"#% 4"9r   c           
         |dvrt        dj                  |            t        j                  |      }|j                  dk  r)d}t        j
                  |      }|dk(  r|j                  }nd}t        j
                  t        j                  |             } | j                  \  }}	|dk(  rT|j                  d   t        ||	|||	z
  z  z         k7  rpt        dj                  | j                  |j                              ||j                  d	   k7  r/t        d
j                  |j                  | j                              t        | |dd|      }
|
d   \  }}t        d|f      \  }|j                  dv rd}nd}|dddt        ||	      f   }||	kD  r|dk(  r|s|rGt        j                  |j                  d	   |f|j                  d      }|j                  |ddd|	f<   n>t        j                  ||j                  d	   f|j                  d      }||d|	ddf<   d}|rd}nd}d}n;|j                  d   r|dk(  s|r|j                  }|dk(  rd}nd}nd}|}|dk(  rd}nd}t!        |d||||||      \  }|dk7  r|j                  }|dk(  r|dddt        ||	      f   }|r|j#                         }|f|
d	d z   S )a	  
    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
    --------
    >>> import numpy as np
    >>> 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

    )leftrightz8Mode argument can only be 'left' or 'right' but not '{}'r    Tr?   Fr   z=Array shapes are not compatible for Q @ c operation: {} vs {}r   z=Array shapes are not compatible for c @ Q operation: {} vs {}Nr   )ormqr)sdTCF)r'   orderr5   r8   LC_CONTIGUOUSzgormqr/gunmqr)overwrite_cr@   )r   formatr   r(   ndim
atleast_2drD   r)   r+   minr   r   typecodezerosr'   flagsr   ravel)r/   cr0   r1   	conjugater"   rJ   onedimr4   r5   r   r;   r7   
gor_un_mqrtranscclrcQs                     r   r   r      s   X $$ $$*F4L2 	2"AvvzQ6>Aq)*A77DAqv~771:QK1$5 566 44:F177AGG4LN N 
? 44:F177AGG4LN N QT5(
3CVFAs":t4KJj(	!Zc!QiZ-A1uaggaj!_AGG3GBBq"1"uIa_AGG3GBBrr1uIEBB	
	 Uc\YSS6>BB6>BB
:E1c2*,CB|TTw:C1I:XXZ53qr7?r   c                    |dvrt        d      |rt        j                  |       }nt        j                  |       }t	        |j
                        dk7  rt        d      |j
                  \  }}|xs t        ||       }t        d|f      \  }t        |d|||      \  }	}
|dk(  r||k  rt        j                  |	||z
        }n t        j                  |	| d	| d	f         }|d
k(  r|S t        d|	f      \  }||k  rt        |d|	| d	 |
|d      \  }||fS |dk(  rt        |d|	|
|d      \  }||fS t        j                  ||f|	j                        }|	|| d	 t        |d||
|d      \  }||fS )a  
    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
    --------
    >>> import numpy as np
    >>> from scipy import linalg
    >>> rng = np.random.default_rng()
    >>> a = rng.standard_normal((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   z8Mode argument should be one of ['full', 'r', 'economic']r    zexpected matrix)gerqfr\   r$   r   Nr   )orgrqzgorgrq/gungrqr   r&   )r   r   r(   r)   r*   r+   r   r   r   r,   r.   r'   )r/   r"   r
   r0   r2   r3   r4   r5   r\   r   r7   r8   
gor_un_grqr;   rq1s                  r   r   r   F  s   B ,,KM 	M $$Q']]1
288}*++88DAq5+b!"4Kj2%0FEugr#.0GB:QJJr1Q3JJr1"#rs(|$s{":u5KJ1uj/2qbc7Cu"#% a4K 
	j/2s%"#% a4K kk1a&1QBCj/35"#% a4Kr   )FNr   FT)r@   FFFF)FNr   T)__doc__r   lapackr   _miscr   __all__r   r   r   r    r   r   <module>re      sD    !  % 
% @EUp ?D/4Qhgr   