
     h
                     n    d Z ddlmZ ddlmZmZ ddlmZmZ ddl	m
Z
 ddlmZ g dZddZddZddZdS )zLU decomposition functions.    )warn)asarrayasarray_chkfinite   )_datacopiedLinAlgWarning)get_lapack_funcs)get_flinalg_funcs)lulu_solve	lu_factorFTc                 *   |rt          |           }nt          |           }|pt          ||           }t          d|f          \  } |||          \  }}}|dk     rt	          d| z            |dk    rt          d|z  t          d           ||fS )aJ  
    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 : (N,) ndarray
        Pivot indices representing the permutation matrix P:
        row i of matrix was interchanged with row piv[i].

    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.

    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

    >>> piv_py = [2, 0, 3, 1]
    >>> L, U = np.tril(lu, k=-1) + np.eye(4), np.triu(lu)
    >>> np.allclose(A[piv_py] - L @ U, np.zeros((4, 4)))
    True
    )getrf)overwrite_ar   z<illegal value in %dth argument of internal getrf (lu_factor)z4Diagonal number %d is exactly zero. Singular matrix.   )
stacklevel)r   r   r   r	   
ValueErrorr   r   )ar   check_finitea1r   r   pivinfos           S/var/www/html/Sam_Eipo/venv/lib/python3.11/site-packages/scipy/linalg/_decomp_lu.pyr   r      s    t  q!!QZZ5+b!"4"4Kj2%00FEE"+666MBTaxx 69=> ? ? 	?axxCdJq	* 	* 	* 	*s7N    c                    | \  }}|rt          |          }nt          |          }|pt          ||          }|j        d         |j        d         k    r-t	          d                    |j        |j                            t          d||f          \  } ||||||          \  }	}
|
dk    r|	S t	          d|
 z            )a  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
    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 {} and b {} are incompatible)getrs)transoverwrite_bz4illegal value in %dth argument of internal gesv|posv)r   r   r   shaper   formatr	   )
lu_and_pivbr   r   r   r   r   b1r   xr   s              r   r   r   Y   s    ^ IR q!!QZZ3R!3!3K	x{bhqk!!D &28446 6 	6 j2r(33FEeBRu+FFFGAtqyy
Ku   r   c                 J   |rt          |           }nt          |           }t          |j                  dk    rt	          d          |pt          ||           }t          d|f          \  } ||||          \  }}}}	|	dk     rt	          d|	 z            |r||fS |||fS )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
        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.

    Returns
    -------
    **(If permute_l == False)**

    p : (M, M) ndarray
        Permutation matrix
    l : (M, K) ndarray
        Lower triangular or trapezoidal matrix with unit diagonal.
        K = min(M, N)
    u : (K, N) ndarray
        Upper triangular or trapezoidal matrix

    **(If permute_l == True)**

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

    Notes
    -----
    This is a LU factorization routine written for SciPy.

    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, np.zeros((4, 4)))
    True

    r   zexpected matrix)r   )	permute_lr   r   z3illegal value in %dth argument of internal lu.getrf)r   r   lenr   r   r   r
   )
r   r&   r   r   r   fluplur   s
             r   r   r      s    t  q!!QZZ
28}}*+++5+b!"4"4KWre,,DCCi[IIIMAq!Taxx -04u5 6 6 	6 !ta7Nr   N)FT)r   FT)FFT)__doc__warningsr   numpyr   r   _miscr   r   lapackr	   _flinalg_pyr
   __all__r   r   r    r   r   <module>r4      s    ! !       , , , , , , , , . - - - - - - - $ $ $ $ $ $ * * * * * *
)
)
)G G G GT> > > >BH H H H H Hr   