
    doi4                         S r SSKJr  SSKJrJr  SSKrSSKJr  SSK	J
r
  SSKJrJr  SS	KJrJr  SS
KJr  / SQr\
" S5      SS j5       rSS jr\
" SSS5      S 5       r  SS jrg)zLU decomposition functions.    )warn)asarrayasarray_chkfiniteN)product)_apply_over_batch   )_datacopiedLinAlgWarning)get_lapack_funcs_normalize_lapack_dtype)lu_dispatcher)lulu_solve	lu_factor)a   c                    U(       a  [        U 5      nO[        U 5      nUR                  S:X  a<  [        R                  " U5      n[        R
                  " S[        R                  S9nXE4$ U=(       d    [        X05      n[        SU45      u  nU" X1S9u  pEnUS:  a  [        SU*  S35      eUS:  a  [        SU S3[        S	S
9  XE4$ )a\	  
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_aillegal value in z)th argument of internal getrf (lu_factor)zDiagonal number z" is exactly zero. Singular matrix.r   )
stacklevel)r   r   sizenp
empty_likearangeint32r	   r   
ValueErrorr   r
   )r   r   check_finitea1r   pivr   infos           S/var/www/html/land-ocr/venv/lib/python3.13/site-packages/scipy/linalg/_decomp_lu.pyr   r      s    l q!QZ 
ww!|]]2ii*w5+b"4Kj2%0FE"6MBTaxw&OP
 	
 axtf$FG	

 7N    c           	      "    U u  pV[        XVXUUS9$ )a  Solve an equation system, a x = b, given the LU factorization of a

The documentation is written assuming array arguments are of specified
"core" shapes. However, array argument(s) of this function may have additional
"batch" dimensions prepended to the core shape. In this case, the array is treated
as a batch of lower-dimensional slices; see :ref:`linalg_batch` for details.

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

)transoverwrite_br    )	_lu_solve)
lu_and_pivbr'   r(   r    r   r"   s          r$   r   r      s"    j IRRa+".0 0r%   )r   r   )r"   r   )r+   z1|2c                 D   U(       a  [        U5      nO[        U5      nU=(       d    [        Xb5      nU R                  S   UR                  S   :w  a&  [	        SU R                   SUR                   S35      eUR
                  S:X  ah  [        [        R                  " SU R                  S9SS/4[        R                  " SUR                  S95      n[        R                  " XgR                  S9$ [        SX45      u  nU" XXcUS	9u  pU
S:X  a  U	$ [	        S
U
*  S35      e)Nr   zShapes of lu z and b z are incompatibler   r   r   )getrs)r'   r(   r   z!th argument of internal gesv|posv)r   r   r	   shaper   r   r   r   eyer   onesr   r   )r   r"   r+   r'   r(   r    b1mr-   xr#   s              r$   r)   r)      s    q!QZ3R!3K	xx{bhhqk!=
'"((CTUVV 
ww!|bffQbhh/!Q8"''!177:ST}}Rww//j2(3FEBR+FGAqy
($/PQ
RRr%   c                 
   U(       a  [         R                  " U 5      O[         R                  " U 5      nUR                  S:  a  [	        S5      e[        XR5      u  pRUR                  Gt pgn[        Xx5      n	UR                  R                  S;   a  SOSn
[        UR                  6 S:X  a  U(       aM  [         R                  " / UQUPU	PUR                  S9n[         R                  " / UQU	PUPUR                  S9nX4$ U(       a'  [         R                  " / UQSP[         R                  S9O[         R                  " / UQSPSPU
S9n[         R                  " / UQUPU	PUR                  S9n[         R                  " / UQU	PUPUR                  S9nXU4$ UR                  S	S
 S:X  a  U(       a0  [         R                  " U5      U(       a  U4$ UR                  5       4$ U(       a  [         R                  " / UQUP[        S9O[         R                  " U5      nU[         R                  " U5      U(       a  U4$ UR                  5       4$ [!        XP5      (       d  U(       d  UR                  SS9nUR"                  S   (       a  UR"                  S   (       d  UR                  SS9nU(       dc  [         R                  " U[         R                  S9n[         R                  " X/UR                  S9n[%        UUX5        Xx:  a  XU4OUUU4u  pnGO[         R                  " / UQUP[         R                  S9nXx:  au  [         R                  " / UQU	PU	PUR                  S9n['        UR                  S
S	  Vs/ s H  n[)        U5      PM     sn6  H  n[%        UU   UU   UU   U5        M     UnOt[         R                  " / UQU	PU	PUR                  S9n['        UR                  S
S	  Vs/ s H  n[)        U5      PM     sn6  H  n[%        UU   UU   UU   U5        M     UnU(       d  U(       d  U(       ax  [         R                  " / UQUPUPU
S9n[         R*                  " U Vs/ s H  n[         R,                  " U5      PM     sn[         R,                  " U5      /-   6 nSU/ UQUP7'   UnO3[         R                  " Xw/U
S9nSU[         R,                  " U5      U4'   UnU(       a  X4$ XU4$ s  snf s  snf s  snf )a{  
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``.

Array argument(s) of this function may have additional
"batch" dimensions prepended to the core shape. In this case, the array is treated
as a batch of lower-dimensional slices; see :ref:`linalg_batch` for details.

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
-------
(p, l, u) | (pl, u):
    The tuple `(p, l, u)` is returned if `permute_l` is ``False`` (default) else
    the tuple `(pl, u)` is returned, where:

    p : (..., M, M) ndarray
        Permutation arrays or vectors depending on `p_indices`.
    l : (..., M, K) ndarray
        Lower triangular or trapezoidal array with unit diagonal, where the last
        dimension is ``K = min(M, N)``.
    pl : (..., M, K) ndarray
        Permuted L matrix with last dimension being ``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.fFfdr   )r.   r   r   N)r   r   C)orderC_CONTIGUOUS	WRITEABLEr   )r   r   r   ndimr   r   r.   minr   charemptyr   	ones_likecopyzerosintr	   flagsr   r   rangeix_r   )r   	permute_lr   r    	p_indicesr!   ndr2   nk
real_dcharPLUPLpur3   indPand_ixs                        r$   r   r      s5   @ %1		a	 bjjmB	ww{LMM .b>OBIRAA	A-3J BHH~"a288<B{{Q{{"((;A5L7@(B(("((3+B++1+Z8 {{Q{{"((;A{{Q{{"((;A7N 
xx}<<#KbGGRWWYGG8A"a4ll2& bll2&{JJ	JJ rs#B HH^$+)>WW3WHHQbhh'HHaV288,b!Q* !1!*Aq":a
 HHXrX1XRXX.52q!BHH5A288CR= A=aq= ABbgqvqvyA CA 2q!BHH5A288CR= A=aq= ABbgqvqvyA CA 	+B++1+Z8BFFB7BqbiilB71FHEB{{{OA1&
3B"#Bryy|QAA6-Q1I-1 !B !B 8s   8S4-S9* S>)FT)r   FT)FFTF)__doc__warningsr   numpyr   r   r   	itertoolsr   scipy._lib._utilr   _miscr	   r
   lapackr   r   _decomp_lu_cythonr   __all__r   r   r)   r    r%   r$   <module>ra      sy    !  ,   . . = , * 8n nb70t 9j,7S 8S. <@u.r%   