§
    µ³Ùg�2  ã                   ó´   — d Z ddlmZ ddlmZmZ ddlZddlmZ ddl	m
Z
mZ ddlmZ dd	lmZ d
„ ej        d         D ¦   «         Zg d¢Zdd„Zdd„Z	 	 dd„ZdS )zLU decomposition functions.é    )Úwarn)ÚasarrayÚasarray_chkfiniteN)Úproducté   )Ú_datacopiedÚLinAlgWarning)Úget_lapack_funcs)Úlu_dispatcherc                 óT   ‡— i | ]$Š‰d                       ˆfd„dD ¦   «         ¦  «        “Œ%S )Ú c                 ó>   •— g | ]}t          j        ‰|¦  «        ¯|‘ŒS © )ÚnpÚcan_cast)Ú.0ÚyÚxs     €úM/granary/edev/quantum/lib/python3.11/site-packages/scipy/linalg/_decomp_lu.pyú
<listcomp>z<dictcomp>.<listcomp>   s*   ø€ ÐGÐGÐG aµR´[ÀÀAÑ5FÔ5FÐG ÐGÐGÐGó    ÚfdFD)Újoin©r   r   s    @r   ú
<dictcomp>r      sL   ø€ ð 2ð 2ð 2Øð �r—w’wÐGÐGÐGÐG¨6ÐGÑGÔGÑHÔHð 2ð 2ð 2r   ÚAll)ÚluÚlu_solveÚ	lu_factorFTc                 ó°  — |rt          | ¦  «        }nt          | ¦  «        }|j        dk    r8t          j        |¦  «        }t          j        dt          j        ¬¦  «        }||fS |pt          || ¦  «        }t          d|f¦  «        \  } |||¬¦  «        \  }}}|dk     rt          d| z  ¦  «        ‚|dk    rt          d|z  t          d¬¦  «         ||fS )	al
  
    Compute pivoted LU decomposition of a matrix.

    The decomposition is::

        A = P L U

    where P is a permutation matrix, L lower triangular with unit
    diagonal elements, and U upper triangular.

    Parameters
    ----------
    a : (M, N) array_like
        Matrix to decompose
    overwrite_a : bool, optional
        Whether to overwrite data in A (may increase performance)
    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
    -------
    lu : (M, N) ndarray
        Matrix containing U in its upper triangle, and L in its lower triangle.
        The unit diagonal elements of L are not stored.
    piv : (K,) ndarray
        Pivot indices representing the permutation matrix P:
        row i of matrix was interchanged with row piv[i].
        Of shape ``(K,)``, with ``K = min(M, N)``.

    See Also
    --------
    lu : gives lu factorization in more user-friendly format
    lu_solve : solve an equation system using the LU factorization of a matrix

    Notes
    -----
    This is a wrapper to the ``*GETRF`` routines from LAPACK. Unlike
    :func:`lu`, it outputs the L and U factors into a single array
    and returns pivot indices instead of a permutation matrix.

    While the underlying ``*GETRF`` routines return 1-based pivot indices, the
    ``piv`` array returned by ``lu_factor`` contains 0-based indices.

    Examples
    --------
    >>> import numpy as np
    >>> from scipy.linalg import lu_factor
    >>> A = np.array([[2, 5, 8, 7], [5, 2, 2, 8], [7, 5, 6, 6], [5, 4, 4, 8]])
    >>> lu, piv = lu_factor(A)
    >>> piv
    array([2, 2, 3, 3], dtype=int32)

    Convert LAPACK's ``piv`` array to NumPy index and test the permutation

    >>> def pivot_to_permutation(piv):
    ...     perm = np.arange(len(piv))
    ...     for i in range(len(piv)):
    ...         perm[i], perm[piv[i]] = perm[piv[i]], perm[i]
    ...     return perm
    ...
    >>> p_inv = pivot_to_permutation(piv)
    >>> p_inv
    array([2, 0, 3, 1])
    >>> L, U = np.tril(lu, k=-1) + np.eye(4), np.triu(lu)
    >>> np.allclose(A[p_inv] - L @ U, np.zeros((4, 4)))
    True

    The P matrix in P L U is defined by the inverse permutation and
    can be recovered using argsort:

    >>> p = np.argsort(p_inv)
    >>> p
    array([1, 3, 0, 2])
    >>> np.allclose(A - L[p] @ U, np.zeros((4, 4)))
    True

    or alternatively:

    >>> P = np.eye(4)[p]
    >>> np.allclose(A - P @ L @ U, np.zeros((4, 4)))
    True
    r   ©Údtype)Úgetrf)Úoverwrite_az<illegal value in %dth argument of internal getrf (lu_factor)z4Diagonal number %d is exactly zero. Singular matrix.é   )Ú
stacklevel)r   r   Úsizer   Ú
empty_likeÚarangeÚint32r   r
   Ú
ValueErrorr   r	   )Úar$   Úcheck_finiteÚa1r   Úpivr#   Úinfos           r   r   r      s  € ðj ð Ý˜qÑ!Ô!ˆˆå�Q‰ZŒZˆð 
„w�!‚|€|ÝŒ]˜2ÑÔˆÝŒi˜¥¤Ð*Ñ*Ô*ˆØ�3ˆwˆàÐ5¥+¨b°!Ñ"4Ô"4€Kå˜j¨2¨%Ñ0Ô0�F€EØ�E˜"¨+Ð6Ñ6Ô6�M€BˆˆTØˆa‚x€xÝð 6Ø9=¸ñ>ñ ?ô ?ð 	?àˆa‚x€xÝÐCÀdÑJÝ qð	*ñ 	*ô 	*ð 	*àˆsˆ7€Nr   c                 óF  — | \  }}|rt          |¦  «        }nt          |¦  «        }|pt          ||¦  «        }|j        d         |j        d         k    r t	          d|j        › d|j        › d�¦  «        ‚|j        dk    rat          t          j        d|j	        ¬¦  «        ddgft          j
        d|j	        ¬¦  «        ¦  «        }t          j        ||j	        ¬¦  «        S t          d||f¦  «        \  }	 |	|||||¬	¦  «        \  }
}|dk    r|
S t	          d
| z  ¦  «        ‚)aU  Solve an equation system, a x = b, given the LU factorization of a

    Parameters
    ----------
    (lu, piv)
        Factorization of the coefficient matrix a, as given by lu_factor.
        In particular piv are 0-indexed pivot indices.
    b : array
        Right-hand side
    trans : {0, 1, 2}, optional
        Type of system to solve:

        =====  =========
        trans  system
        =====  =========
        0      a x   = b
        1      a^T x = b
        2      a^H x = b
        =====  =========
    overwrite_b : bool, optional
        Whether to overwrite data in b (may increase performance)
    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
    -------
    x : array
        Solution to the system

    See Also
    --------
    lu_factor : LU factorize a matrix

    Examples
    --------
    >>> import numpy as np
    >>> from scipy.linalg import lu_factor, lu_solve
    >>> A = np.array([[2, 5, 8, 7], [5, 2, 2, 8], [7, 5, 6, 6], [5, 4, 4, 8]])
    >>> b = np.array([1, 1, 1, 1])
    >>> lu, piv = lu_factor(A)
    >>> x = lu_solve((lu, piv), b)
    >>> np.allclose(A @ x - b, np.zeros((4,)))
    True

    r   zShapes of lu z and b z are incompatibler%   r!   r   )Úgetrs)ÚtransÚoverwrite_bz4illegal value in %dth argument of internal gesv|posv)r   r   r   Úshaper+   r'   r   r   Úeyer"   Úonesr(   r
   )Ú
lu_and_pivÚbr3   r4   r-   r   r/   Úb1Úmr2   r   r0   s               r   r   r   �   s?  € ð` �I€RˆØð Ý˜qÑ!Ô!ˆˆå�Q‰ZŒZˆàÐ3¥¨R°Ñ!3Ô!3€Kà	„x�„{�b”h˜q”kÒ!Ð!ÝÐU¨¬ÐUÐU¸"¼(ÐUÐUÐUÑVÔVÐVð 
„w�!‚|€|Ý•b”f˜Q b¤hÐ/Ñ/Ô/°!°Q°Ð8½"¼'À!È1Ì7Ð:SÑ:SÔ:SÑTÔTˆÝŒ}˜R q¤wÐ/Ñ/Ô/Ð/å˜j¨2¨r¨(Ñ3Ô3�F€EØˆe�B˜˜R u¸+ÐFÑFÔF�G€A€tØˆq‚y€yØˆÝ
ÐKØ�uññ ô ð r   c                 óX	  — |rt          j        | ¦  «        nt          j        | ¦  «        }|j        dk     rt	          d¦  «        ‚|j        j        dvrNt          |j        j                 }|st          d|j        › d�¦  «        ‚| 	                    |d         ¦  «        }d}|j
        �^ }}}	t          ||	¦  «        }
|j        j        dv rd	nd
}t          |j
        Ž dk    rÑ|rFt          j        g |¢|‘|
‘|j        ¬¦  «        }t          j        g |¢|
‘|	‘|j        ¬¦  «        }||fS |r$t          j        g |¢d‘t           j        ¬¦  «        nt          j        g |¢d‘d‘|¬¦  «        }t          j        g |¢|‘|
‘|j        ¬¦  «        }t          j        g |¢|
‘|	‘|j        ¬¦  «        }|||fS |j
        dd…         dk    r�|r,t          j        |¦  «        |r|n|                     ¦   «         fS |rt          j        g |¢|‘t"          ¬¦  «        nt          j        |¦  «        }|t          j        |¦  «        |r|n|                     ¦   «         fS t%          || ¦  «        s|s|                     d¬¦  «        }|j        d         r|j        d         s|                     d¬¦  «        }|sdt          j        |t           j        ¬¦  «        }t          j        |
|
g|j        ¬¦  «        }t)          ||||¦  «         ||	k    r|||fn|||f\  }}}nÿt          j        g |¢|‘t           j        ¬¦  «        }||	k    rkt          j        g |¢|
‘|
‘|j        ¬¦  «        }t+          d„ |j
        dd…         D ¦   «         Ž D ]&}t)          ||         ||         ||         |¦  «         Œ'|}njt          j        g |¢|
‘|
‘|j        ¬¦  «        }t+          d„ |j
        dd…         D ¦   «         Ž D ]&}t)          ||         ||         ||         |¦  «         Œ'|}|sŽ|sŒ|rWt          j        g |¢|‘|‘|¬¦  «        }t          j        d„ |D ¦   «         t          j        |¦  «        gz   Ž }d|g |¢|‘R <   |}n3t          j        ||g|¬¦  «        }d|t          j        |¦  «        |f<   |}|r||fn|||fS )ar  
    Compute LU decomposition of a matrix with partial pivoting.

    The decomposition satisfies::

        A = P @ L @ U

    where ``P`` is a permutation matrix, ``L`` lower triangular with unit
    diagonal elements, and ``U`` upper triangular. If `permute_l` is set to
    ``True`` then ``L`` is returned already permuted and hence satisfying
    ``A = L @ U``.

    Parameters
    ----------
    a : (M, N) array_like
        Array to decompose
    permute_l : bool, optional
        Perform the multiplication P*L (Default: do not permute)
    overwrite_a : bool, optional
        Whether to overwrite data in a (may improve performance)
    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.
    p_indices : bool, optional
        If ``True`` the permutation information is returned as row indices.
        The default is ``False`` for backwards-compatibility reasons.

    Returns
    -------
    **(If `permute_l` is ``False``)**

    p : (..., M, M) ndarray
        Permutation arrays or vectors depending on `p_indices`
    l : (..., M, K) ndarray
        Lower triangular or trapezoidal array with unit diagonal.
        ``K = min(M, N)``
    u : (..., K, N) ndarray
        Upper triangular or trapezoidal array

    **(If `permute_l` is ``True``)**

    pl : (..., M, K) ndarray
        Permuted L matrix.
        ``K = min(M, N)``
    u : (..., K, N) ndarray
        Upper triangular or trapezoidal array

    Notes
    -----
    Permutation matrices are costly since they are nothing but row reorder of
    ``L`` and hence indices are strongly recommended to be used instead if the
    permutation is required. The relation in the 2D case then becomes simply
    ``A = L[P, :] @ U``. In higher dimensions, it is better to use `permute_l`
    to avoid complicated indexing tricks.

    In 2D case, if one has the indices however, for some reason, the
    permutation matrix is still needed then it can be constructed by
    ``np.eye(M)[P, :]``.

    Examples
    --------

    >>> import numpy as np
    >>> from scipy.linalg import lu
    >>> A = np.array([[2, 5, 8, 7], [5, 2, 2, 8], [7, 5, 6, 6], [5, 4, 4, 8]])
    >>> p, l, u = lu(A)
    >>> np.allclose(A, p @ l @ u)
    True
    >>> p  # Permutation matrix
    array([[0., 1., 0., 0.],  # Row index 1
           [0., 0., 0., 1.],  # Row index 3
           [1., 0., 0., 0.],  # Row index 0
           [0., 0., 1., 0.]]) # Row index 2
    >>> p, _, _ = lu(A, p_indices=True)
    >>> p
    array([1, 3, 0, 2], dtype=int32)  # as given by row indices above
    >>> np.allclose(A, l[p, :] @ u)
    True

    We can also use nd-arrays, for example, a demonstration with 4D array:

    >>> rng = np.random.default_rng()
    >>> A = rng.uniform(low=-4, high=4, size=[3, 2, 4, 8])
    >>> p, l, u = lu(A)
    >>> p.shape, l.shape, u.shape
    ((3, 2, 4, 4), (3, 2, 4, 4), (3, 2, 4, 8))
    >>> np.allclose(A, p @ l @ u)
    True
    >>> PL, U = lu(A, permute_l=True)
    >>> np.allclose(A, PL @ U)
    True

    r%   z1The input array must be at least two-dimensional.r   z
The dtype z5 cannot be cast to float(32, 64) or complex(64, 128).r   TÚfFÚfÚd)r5   r"   r!   éþÿÿÿN)r   r   ÚC)ÚorderÚC_CONTIGUOUSÚ	WRITEABLEc                 ó,   — g | ]}t          |¦  «        ‘ŒS r   ©Úranger   s     r   r   zlu.<locals>.<listcomp>m  ó   € Ð AÐ AÐ A¨a¥ q¡¤Ð AÐ AÐ Ar   c                 ó,   — g | ]}t          |¦  «        ‘ŒS r   rF   r   s     r   r   zlu.<locals>.<listcomp>s  rH   r   c                 ó6   — g | ]}t          j        |¦  «        ‘ŒS r   )r   r)   r   s     r   r   zlu.<locals>.<listcomp>}  s    € Ð7Ð7Ð7¨q�bœi¨™lœlÐ7Ð7Ð7r   r   )r   r   r   Úndimr+   r"   ÚcharÚlapack_cast_dictÚ	TypeErrorÚastyper5   ÚminÚemptyr*   Ú	ones_likeÚcopyÚzerosÚintr   Úflagsr   r   Úix_r)   )r,   Ú	permute_lr$   r-   Ú	p_indicesr.   Ú
dtype_charÚndr;   ÚnÚkÚ
real_dcharÚPLÚUÚPÚLÚpÚuÚindÚPaÚnd_ixs                        r   r   r   É   sE  € ð@ %1Ð	C�Ô	˜aÑ	 Ô	 Ð	 µb´jÀ±m´m€BØ	„w�‚{€{ÝÐLÑMÔMÐMð 
„x„}˜FÐ"Ð"Ý% b¤h¤mÔ4ˆ
Øð 	EÝð D¨¬ð Dð Dð Dñ Eô Eð Eð �YŠY�z !”}Ñ%Ô%ˆØˆà”�I€RˆˆAÝˆAˆq‰	Œ	€AØœœ¨Ð-Ð-��°3€Jõ ˆBŒH€~˜ÒÐØð 		Ý”  "  a ¨ °2´8Ð<Ñ<Ô<ˆBÝ”˜{ ˜{ Q˜{¨˜{°"´(Ð;Ñ;Ô;ˆAØ�q�5ˆLà7@ð 9•”˜(˜B˜( ˜(­"¬(Ð3Ñ3Ô3Ð3Ý”˜+˜B˜+ ˜+ 1˜+¨ZÐ8Ñ8Ô8ð å”˜{ ˜{ Q˜{¨˜{°"´(Ð;Ñ;Ô;ˆAÝ”˜{ ˜{ Q˜{¨˜{°"´(Ð;Ñ;Ô;ˆAØ�a˜�7ˆNð 
„x���„}˜ÒÐØð 	KÝ”< Ñ#Ô#¨KÐ&F b b¸R¿WºW¹Y¼YÐGÐGà8Að '•”  "  a µÐ4Ñ4Ô4Ð4Ý”l 2Ñ&Ô&ð à•b”l 2Ñ&Ô&¨{Ð)I¨¨ÀÇÂÁ	Ä	ÐJÐJõ �r˜1ÑÔð $Øð 	$à—’˜s�Ñ#Ô#ˆBð ŒH�^Ô$ð  ¨¬°+Ô)>ð  Ø�WŠW˜3ˆWÑÔˆàð åŒH�Q�bœhÐ'Ñ'Ô'ˆÝŒH�a˜�V 2¤8Ð,Ñ,Ô,ˆÝ�b˜!˜Q 	Ñ*Ô*Ð*Ø ! A¢ �1�b˜!�*�*¨A¨q°"¨:‰ˆˆ1ˆaˆaõ
 ŒH�X�r�X˜1�X¥R¤XÐ.Ñ.Ô.ˆàˆqŠ5ˆ5Ý”˜˜2˜˜q˜ !˜¨B¬HÐ5Ñ5Ô5ˆAÝÐ AÐ A°2´8¸C¸R¸C´=Ð AÑ AÔ AÐBð Bð B�Ý˜b œg q¨¤v¨q°¬v°yÑAÔAÐAÐAØˆAˆAõ ”˜˜2˜˜q˜ !˜¨B¬HÐ5Ñ5Ô5ˆAÝÐ AÐ A°2´8¸C¸R¸C´=Ð AÑ AÔ AÐBð Bð B�Ý˜b œg q¨¤v¨q°¬v°yÑAÔAÐAÐAØˆAð ð 
 	ð 
Øð 		Ý”˜+˜B˜+ ˜+ 1˜+¨ZÐ8Ñ8Ô8ˆBå”FÐ7Ð7°BÐ7Ñ7Ô7½¼À1¹¼¸ÑFÐHˆEØˆBˆ{�ˆ{˜ˆ{ˆ{‰OØˆAˆAå”˜1˜a˜&¨
Ð3Ñ3Ô3ˆBØ"#ˆB�rŒy˜‰|Œ|˜QˆÑØˆAàÐ-ˆAˆqˆ6ˆ6 Q¨¨1 IÐ-r   )FT)r   FT)FFTF)Ú__doc__Úwarningsr   Únumpyr   r   r   Ú	itertoolsr   Ú_miscr   r	   Úlapackr
   Ú_decomp_lu_cythonr   Ú	typecodesrM   Ú__all__r   r   r   r   r   r   ú<module>rq      s#  ðØ !Ð !à Ð Ð Ð Ð Ð à ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,Ø Ð Ð Ð Ø Ð Ð Ð Ð Ð ð .Ð -Ð -Ð -Ð -Ð -Ð -Ð -Ø $Ð $Ð $Ð $Ð $Ð $Ø ,Ð ,Ð ,Ð ,Ð ,Ð ,ð2ð 2Øœ\¨%Ô0ð2ñ 2ô 2Ð ð *Ð
)Ð
)€ðjð jð jð jðZEð Eð Eð EðP <@Øð|.ð |.ð |.ð |.ð |.ð |.r   