
    (ph0                         S SK Jr  S SKrS SKJrJrJrJrJrJ	r	J
r
JrJrJr  S SKJr  SSKJr  SSKJrJr  S/rSS	 jrSS
 jrSS jrSS jrg)    )warnN)

atleast_2darange
zeros_likeimagdiagiscomplexobjtriltriuargsort
empty_like)ComplexWarning   )_asarray_validated)get_lapack_funcs_compute_lworkldlc                    [        [        XS95      nUR                  S   UR                  S   :w  a  [        S5      eUR                  S:X  a.  [        U5      [        U5      [        R                  " / [        S94$ UR                  S   n[        U5      (       a  [        O[        nU[        L aH  U(       aA  Su  p[        R                  " [        [        U5      5      5      (       a  [        S[         SS	9  OS
u  p[#        X4U45      u  p[%        XUS9nU
" X\UUS9u  pnUS:  a   [        UR'                  5        SU*  S35      e[)        XS9u  nn[+        UUXS9u  nn[-        UUUUS9u  nnUUU4$ )a  Computes the LDLt or Bunch-Kaufman factorization of a symmetric/
hermitian matrix.

This function returns a block diagonal matrix D consisting blocks of size
at most 2x2 and also a possibly permuted unit lower triangular matrix
``L`` such that the factorization ``A = L D L^H`` or ``A = L D L^T``
holds. If `lower` is False then (again possibly permuted) upper
triangular matrices are returned as outer factors.

The permutation array can be used to triangularize the outer factors
simply by a row shuffle, i.e., ``lu[perm, :]`` is an upper/lower
triangular matrix. This is also equivalent to multiplication with a
permutation matrix ``P.dot(lu)``, where ``P`` is a column-permuted
identity matrix ``I[:, perm]``.

Depending on the value of the boolean `lower`, only upper or lower
triangular part of the input array is referenced. Hence, a triangular
matrix on entry would give the same result as if the full matrix is
supplied.

Parameters
----------
A : array_like
    Square input array
lower : bool, optional
    This switches between the lower and upper triangular outer factors of
    the factorization. Lower triangular (``lower=True``) is the default.
hermitian : bool, optional
    For complex-valued arrays, this defines whether ``A = A.conj().T`` or
    ``A = A.T`` is assumed. For real-valued arrays, this switch has no
    effect.
overwrite_a : bool, optional
    Allow overwriting data in `A` (may enhance performance). The default
    is False.
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
-------
lu : ndarray
    The (possibly) permuted upper/lower triangular outer factor of the
    factorization.
d : ndarray
    The block diagonal multiplier of the factorization.
perm : ndarray
    The row-permutation index array that brings lu into triangular form.

Raises
------
ValueError
    If input array is not square.
ComplexWarning
    If a complex-valued array with nonzero imaginary parts on the
    diagonal is given and hermitian is set to True.

See Also
--------
cholesky, lu

Notes
-----
This function uses ``?SYTRF`` routines for symmetric matrices and
``?HETRF`` routines for Hermitian matrices from LAPACK. See [1]_ for
the algorithm details.

Depending on the `lower` keyword value, only lower or upper triangular
part of the input array is referenced. Moreover, this keyword also defines
the structure of the outer factors of the factorization.

.. versionadded:: 1.1.0

References
----------
.. [1] J.R. Bunch, L. Kaufman, Some stable methods for calculating
   inertia and solving symmetric linear systems, Math. Comput. Vol.31,
   1977. :doi:`10.2307/2005787`

Examples
--------
Given an upper triangular array ``a`` that represents the full symmetric
array with its entries, obtain ``l``, 'd' and the permutation vector `perm`:

>>> import numpy as np
>>> from scipy.linalg import ldl
>>> a = np.array([[2, -1, 3], [0, 2, 0], [0, 0, 1]])
>>> lu, d, perm = ldl(a, lower=0) # Use the upper part
>>> lu
array([[ 0. ,  0. ,  1. ],
       [ 0. ,  1. , -0.5],
       [ 1. ,  1. ,  1.5]])
>>> d
array([[-5. ,  0. ,  0. ],
       [ 0. ,  1.5,  0. ],
       [ 0. ,  0. ,  2. ]])
>>> perm
array([2, 1, 0])
>>> lu[perm, :]
array([[ 1. ,  1. ,  1.5],
       [ 0. ,  1. , -0.5],
       [ 0. ,  0. ,  1. ]])
>>> lu.dot(d).dot(lu.T)
array([[ 2., -1.,  3.],
       [-1.,  2.,  0.],
       [ 3.,  0.,  1.]])

)check_finiter   r   z%The input array "a" should be square.dtype)hetrfhetrf_lworkzscipy.linalg.ldl():
The imaginary parts of the diagonalare ignored. Use "hermitian=False" for factorization ofcomplex symmetric arrays.   )
stacklevel)sytrfsytrf_lwork)lower)lworkr   overwrite_azB exited with the internal error "illegal value in argument number z0". See LAPACK documentation for the error codes.)r   	hermitian)r   r   shape
ValueErrorsizer   nparrayintr	   complexfloatanyr   r   r   r   r   r   upper_ldl_sanitize_ipiv_ldl_get_d_and_l_ldl_construct_tri_factor)Ar   r!   r    r   anr_or_csslsolversolver_lworkr   ldupivinfoswap_arr	pivot_arrdluperms                        K/var/www/html/venv/lib/python3.13/site-packages/scipy/linalg/_decomp_ldl.pyr   r      sr   Z 	%aCDAwwqzQWWQZ@AAvv{!}jmRXXb-DDD	
A$QWUF Y&66$tAw-   -.<L '+QGaT:F<%8EA%(35NCdaxAGGI; '/04ug 600 1 	1 -S>HiS)5NEAr(XyNHBq$;    c                    U R                   n[        U5      n[        U[        S9nSnU(       a  SSSUS4O	SSUS-
  SS4u  pgpn
[	        XU
5       Hj  nU(       a  SnM  X   nUS:  a  XS-   :w  a	  X<S-
     X;'   SXK'   M/  US:  a,  XX-      :X  a"  U* US-   :w  a  X<* S-
     X;U-   '   SXKU-   '   SnMa  [        S5      e   X44$ )	a  
This helper function takes the rather strangely encoded permutation array
returned by the LAPACK routines ?(HE/SY)TRF and converts it into
regularized permutation and diagonal pivot size format.

Since FORTRAN uses 1-indexing and LAPACK uses different start points for
upper and lower formats there are certain offsets in the indices used
below.

Let's assume a result where the matrix is 6x6 and there are two 2x2
and two 1x1 blocks reported by the routine. To ease the coding efforts,
we still populate a 6-sized array and fill zeros as the following ::

    pivots = [2, 0, 2, 0, 1, 1]

This denotes a diagonal matrix of the form ::

    [x x        ]
    [x x        ]
    [    x x    ]
    [    x x    ]
    [        x  ]
    [          x]

In other words, we write 2 when the 2x2 block is first encountered and
automatically write 0 to the next entry and skip the next spin of the
loop. Thus, a separate counter or array appends to keep track of block
sizes are avoided. If needed, zeros can be filtered out later without
losing the block structure.

Parameters
----------
a : ndarray
    The permutation array ipiv returned by LAPACK
lower : bool, optional
    The switch to select whether upper or lower triangle is chosen in
    the LAPACK call.

Returns
-------
swap_ : ndarray
    The array that defines the row/column swap operations. For example,
    if row two is swapped with row four, the result is [0, 3, 2, 3].
pivots : ndarray
    The array that defines the block diagonal structure as given above.

r   Fr   r   r   TznWhile parsing the permutation array in "scipy.linalg.ldl", invalid entries found. The array syntax is invalid.)r$   r   r   r'   ranger#   )r0   r   r1   swap_pivotsskip_2x2xyrsreriindcur_vals                r?   r,   r,      s    ` 	
A1IES)FH +01aAb"ac2r5JA""RR H&Q;a%"19-
FKq[W#%0x3q5 $XaZ0!eFq5MH C D D- !2 =r@   c                    [        U 5      n[        [        U 5      5      nUR                  S   nSnU(       a  SOSu  pU(       a  [        U S5      O[	        U S5      n
[        U5      nSXU4'   XS:g      Hs  nX|-   nUS:X  ad  XU-   Xy-   4   XWU-   Xy-   4'   U(       a*  U(       a#  XU-   Xy-   4   R                  5       XWU	-   Xx-   4'   OXU-   Xy-   4   XWU	-   Xx-   4'   SXU-   Xy-   4'   UnMu     XZ4$ )aI  
Helper function to extract the diagonal and triangular matrices for
LDL.T factorization.

Parameters
----------
ldu : ndarray
    The compact output returned by the LAPACK routing
pivs : ndarray
    The sanitized array of {0, 1, 2} denoting the sizes of the pivots. For
    every 2 there is a succeeding 0.
lower : bool, optional
    If set to False, upper triangular part is considered.
hermitian : bool, optional
    If set to False a symmetric complex array is assumed.

Returns
-------
d : ndarray
    The block diagonal matrix.
lu : ndarray
    The upper/lower triangular matrix
r   )r   r   )r   r   rB   r   r   g        )r	   r   r"   r
   r   r   conj)r7   pivsr   r!   is_cr<   r1   blk_irG   rH   r=   	diag_indsblkincs                 r?   r-   r-      s   0 DT#YA	
AE 6DAc2T#q\Bq	I B)AI k!8"%Aguw&6"7AAguw 	&)'57*:&;&@&@&B'57"#&)'57*:&;'57"##%BQw  " 5Lr@   c                 v   U R                   S   n[        U5      nU(       a  US-
  SS4OSUS4u  pgn[        XgU5       Hl  n	X   n
X:w  d  M  U(       a  U	OSnU(       a  UOU	S-   nX)   U(       a  SOS:X  a  X(       a  SOS-  nX(       a  SOS-  nX	U
/X24   X
U	/X24'   XYU
/   XZU	/'   Mn     U [        U5      4$ )a!  
Helper function to construct explicit outer factors of LDL factorization.

If lower is True the permuted factors are multiplied as L(1)*L(2)*...*L(k).
Otherwise, the permuted factors are multiplied as L(k)*...*L(2)*L(1). See
LAPACK documentation for more details.

Parameters
----------
lu : ndarray
    The triangular array that is extracted from LAPACK routine call with
    ones on the diagonals.
swap_vec : ndarray
    The array that defines the row swapping indices. If the kth entry is m
    then rows k,m are swapped. Notice that the mth entry is not necessarily
    k to avoid undoing the swapping.
pivs : ndarray
    The array that defines the block diagonal structure returned by
    _ldl_sanitize_ipiv().
lower : bool, optional
    The boolean to switch between lower and upper triangular structure.

Returns
-------
lu : ndarray
    The square outer factor which satisfies the L * D * L.T = A
perm : ndarray
    The permutation vector that brings the lu to the triangular form

Notes
-----
Note that the original argument "lu" is overwritten.

r   r   rB   r   )r"   r   rC   r   )r=   swap_vecrP   r   r1   r>   rI   rJ   rK   rL   s_indcol_scol_es                r?   r.   r.   +  s    F 	A!9D"'!A#r2aAYJBBRR < CaEACEE y%QQ/u!+e*,.U|U[/H,IBs|U[()!%El!3D ! wt}r@   )TTFT)T)TT)warningsr   numpyr%   r   r   r   r   r   r	   r
   r   r   r   scipy._lib._utilr   _decompr   lapackr   r   __all__r   r,   r-   r.    r@   r?   <module>rb      sF     B B B + ' 4'NbRj5p6r@   