L i<dZddlZddlmZddlmZddlmZgdZ dZ ed  dd Z ed d  dd Z ed dd Z y)zQR decomposition functions.N)_apply_over_batch)get_lapack_funcs) _datacopied)qr qr_multiplyrqc|jdd}|dvr?d|d<||i|}|ddjjtj|d<||i|}|ddkrt d|d d||ddS) z[Call a LAPACK routine, determining lwork automatically and handling error return valueslworkN)Nr rzillegal value in zth argument of internal )getrealastypenpint_ ValueError)fnameargskwargsr rets ]/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/scipy/linalg/_decomp_qr.pysafecallr s JJw %E w  b'!*//009w T V C 2w{,c"gXJ6NtfUVV s8O)ac|dvr td|rtj|}ntj|}t |j dk7r td|j \}}|j dk(rt||} |dvrGtj|||f} tj|| d<tj|} n2tj||| f} tj|| |f} |r(| tj|tj f} n| f} |d k(r| S|d k(r9tj|||f} tj|| f}| |ff| zS| f| zS|xs t||}|r(td |f\}t|d ||\} }}|dz}n"td|f\}t|d|||\} }|dvs||krtj | } ntj | d|ddf} |r| f} n| f} |d k(r| S|d k(r| |ff| zStd| f\}||krt|d| ddd|f||d\} nf|dk(rt|d| ||d\} nM| j"j$}tj&||f| }| |ddd|f<t|d|||d\} | f| zS)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. Equivalently, albeit less efficiently, an explicit P matrix may be formed explicitly by permuting the rows or columns (depending on the side of the equation on which it is to be used) of an identity matrix. See Examples. 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'``. Replaced by tuple ``(Q, TAU)`` if ``mode='raw'``. R : float or complex ndarray Of shape (M, N), or (K, N) for ``mode in ['economic', 'raw']``. ``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 >>> P = np.eye(p4.size)[p4] >>> np.allclose(a, np.dot(q4, r4) @ P) True >>> np.allclose(a @ P.T, 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,)) >>> P = np.eye(6)[:, p5] >>> np.allclose(a @ P, np.dot(q5, r5)) True )fullrreconomicrawz?Mode argument should be one of ['full', 'r', 'economic', 'raw']rzexpected a 2-D arrayr)r!r"shape.dtyper r")geqp3r') overwrite_ar)geqrfr)r r(N)orgqrz gorgqr/gungqrr!)rrasarray_chkfiniteasarraylenr$sizemin empty_likeidentityarangeint32 zeros_likerrrtriur&charempty)rr(r modepivoting check_finitea1MNKQRRjrtaur'jpvtr) gor_un_gqrtqqrs rrrs\ 99./ /  ! !! $ ZZ] 288}/00 88DAq ww!| 1I * * bA/A[[^AcF b!A bA/A bA/A BIIarxx00BB 3;I U]r!Q0B--1$/CI<"$ $tby5+b!"4K!*re4 M D#  !*re45'2U'24C &&!a% GGBK GGBrr1uI  W R s{ S |b  ":u5KJ1u j/2a!e9c!q2   j/2s%"#% HHMMhh1vQ'ArrE j/35"#% 4"9r)cz1|2c |dvrtd|dtj|}|jdkr)d}tj|}|dk(r|j }nd}tjtj |}|j\}} |dk(rJ|jdt|| ||| z zzk7r\td |jd |j||jd k7r%td |jd |jt||d d|} | d\} } |jdk(rtj|f| d d zStd| f\} | jdvrd}nd}| d d d t|| f} || kDr|dk(r|s|rGtj|jd |f|jd}|j |d d d | f<n>tj||jd f|jd}||d | d d f<d}|rd}nd}d}n;|j dr|dk(s|r|j }|dk(rd}nd}nd}|}|dk(rd}nd}t#| d||| | ||\}|dk7r |j }|dk(r|d d d t|| f}|r|j%}|f| d d zS)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 )leftrightz5Mode argument can only be 'left' or 'right' but not ''rTrJFrz5Array shapes are not compatible for Q @ c operation: z vs rz5Array shapes are not compatible for c @ Q operation: Nr")ormqr)sdTCF)r&orderr>rAL C_CONTIGUOUSz gormqr/gunmqr) overwrite_crK)rrr,ndim atleast_2drPr-r$r0rr/r1rtypecodezerosr&flagsrravel)rrHr9r: conjugater(rVonedimr=r>r"r@rC gor_un_mqrtranscclrcQs rrrsZ $$!!%a)* * QAvvz MM!  6>A bjjm$A 77DAq v~ 771:QK1$5 56 6,,-GG9D CD D  ?,,-GG9D CD D Q T5( 3C VFAs vv{ a "SW,,":t4KJj( !Zc!QiZ-A1u 1771:q/DBBq"1"uI1aggaj/DBBrr1uIE BB  Uc\Y SS 6>BB  6>BB :E1c2* ,CB | TT w :C1I:  XXZ 53qr7?rc6|dvr td|rtj|}ntj|}t |j dk7r td|j \}}|j dk(rt||}|dk(sGtj|} tj|||f} tj|| d<n2tj|||f} tj|||f} |d k(r| S| | fS|xs t||}td |f\} t| d ||| \} } |dk(r||krtj| ||z } n tj| | d | d f} |d k(r| Std| f\}||krt|d| | d | |d \} | | fS|dk(rt|d| | |d \} | | fStj||f| j}| || d t|d|| |d \} | | fS)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)) )rr r!z8Mode argument should be one of ['full', 'r', 'economic']rzexpected matrixrr!r#.r )gerqfrer*N)orgrqz gorgrq/gungrqrr%)rrr,r-r.r$r/r0r1r2rrrr6r8r&)rr(r r9r;r<r=r>r?rAr@rer rC gor_un_grqrq1s rr r ts=D ,,KM M  ! !! $ ZZ] 288}*++ 88DAq ww!| 1Iz! b!A bA/A[[^AcF bA/A bA/A 3;H!t 5+b!"4K j2% 0FEugr#.0GB : Q GGB!  GGBrsQBCxL ! s{":u5KJ1u j/2qbc7Cu"#% a4K   j/2s%"#% a4K hh1vRXX.QBC j/35"#% a4Kr)FNrFT)rKFFFF)FNrT)__doc__numpyrscipy._lib._utilrlapackr_miscr__all__rrrr rrrps!.% % 8?D||~8\*>C/4U+Up8yyr