§
    fŠtjD1  ã                   óÆ   — d dl mZ d dlZd dlmZmZmZmZmZm	Z	m
Z
mZmZmZ d dlmZ d dlmZ ddlmZ ddlmZmZ d	gZ ed
¦  «        dd„¦   «         Zdd„Zdd„Zdd„ZdS )é    )ÚwarnN)
Ú
atleast_2dÚarangeÚ
zeros_likeÚimagÚdiagÚiscomplexobjÚtrilÚtriuÚargsortÚ
empty_like)ÚComplexWarning)Ú_apply_over_batché   )Ú_asarray_validated)Úget_lapack_funcsÚ_compute_lworkÚldl)ÚAé   TFc                 ó€  — t          t          | |¬¦  «        ¦  «        }|j        d         |j        d         k    rt          d¦  «        ‚|j        dk    r8t          |¦  «        t          |¦  «        t          j        g t          ¬¦  «        fS |j        d         }t          |¦  «        rt          nt          }|t          u rM|rKd\  }}	t          j        t          t          |¦  «        ¦  «        ¦  «        rt          dt           d¬	¦  «         nd
\  }}	t#          ||	f|f¦  «        \  }
}t%          |||¬¦  «        } |
||||¬¦  «        \  }}}|dk     r(t          |                     ¦   «         › d| › d�¦  «        ‚t)          ||¬¦  «        \  }}t+          ||||¬¦  «        \  }}t-          ||||¬¦  «        \  }}|||fS )aG   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_lworkz‡scipy.linalg.ldl():
The imaginary parts of the diagonalare ignored. Use "hermitian=False" for factorization ofcomplex symmetric arrays.r   )Ú
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)r   r    r#   r"   r   ÚaÚnÚr_or_cÚsÚslÚsolverÚsolver_lworkr!   ÚlduÚpivÚinfoÚswap_arrÚ	pivot_arrÚdÚluÚperms                        úV/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/scipy/linalg/_decomp_ldl.pyr   r      sò  € õ\ 	Õ% a°lÐCÑCÔCÑDÔD€AØ„wˆq„z�Q”W˜Q”ZÒÐÝÐ@ÑAÔAÐAà„v�‚{€{Ý˜!‰}Œ}�j¨™mœm­R¬X°bÅÐ-DÑ-DÔ-DÐDÐDà	Œ�Œ
€AÝ$ Q™œÐ2�WˆW­U€Fð •ÐÐ˜YÐØ&‰ˆˆ2ÝŒ6•$•t˜A‘w”w‘-”-Ñ Ô ð 	LÝð -å.<ÈðLñ Lô Lð Løð '‰ˆˆ2å+¨Q°¨G°a°TÑ:Ô:Ñ€FˆLÝ˜<¨°%Ð8Ñ8Ô8€EØ�V˜A U°%Ø(3ð5ñ 5ô 5�N€Cˆˆdàˆa‚x€xÝ˜AŸGšG™IœIð 0ð 0Ø04¨uð0ð 0ð 0ñ 1ô 1ð 	1õ -¨S¸Ð>Ñ>Ô>Ñ€HˆiÝ˜S )°5ÀIÐNÑNÔN�E€A€rÝ(¨¨X°yÈÐNÑNÔN�H€Bˆàˆq�$ˆ;Ðó    c                 ó´  — | j         }t          |¦  «        }t          |t          ¬¦  «        }d}|rddd|dfn	dd|dz
  ddf\  }}}}	}
t	          ||	|
¦  «        D ]|}|rd}Œ| |         }|dk    r||dz   k    r||dz
           ||<   d||<   Œ2|dk     r6|| ||z            k    r'| |dz   k    r|| dz
           |||z   <   d|||z   <   d}Œnt          d¦  «        ‚||fS )	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%   )r1   r    r2   Úswap_ÚpivotsÚskip_2x2ÚxÚyÚrsÚreÚriÚindÚcur_vals                r@   r.   r.   ¡   sL  € ð` 	
Œ€AÝ�1‰IŒI€EÝ˜¥SÐ)Ñ)Ô)€FØ€Hð +0ÐJ˜˜1˜a  A��°b¸"¸aÀ¹cÀ2ÀrÐ5JÑ€A€qˆ"ˆb�"å�R˜˜RÑ Ô ð Dð Dˆàð 	ØˆHØà�C”&ˆà�QŠ;ˆ;Ø˜#˜a™%ÒÐà" 7¨1¡9Ô-��c‘
ØˆF�3‰KˆKà�qŠ[ˆ[˜W¨¨#¨a©%¬Ò0Ð0àˆx˜3˜q™5Ò Ð à$ g X¨a¡ZÔ0��c˜!‘e‘ØˆF�3�q‘5‰MØˆHˆHåð Cñ Dô Dð Dð �&ˆ=ÐrA   c                 ó"  — t          | ¦  «        }t          t          | ¦  «        ¦  «        }|j        d         }d}|rdnd\  }}	|rt          | d¦  «        nt	          | d¦  «        }
t          |¦  «        }d|
||f<   ||dk             D ]„}||z   }|dk    ru| ||z   ||	z   f         |||z   ||	z   f<   |r0|r.| ||z   ||	z   f                              ¦   «         |||	z   ||z   f<   n| ||z   ||	z   f         |||	z   ||z   f<   d|
||z   ||	z   f<   |}Œ…||
fS )a™  
    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   rC   r   r   g        )r	   r   r$   r
   r   r   Úconj)r8   Úpivsr    r#   Úis_cr=   r2   Úblk_irH   rI   r>   Ú	diag_indsÚblkÚincs                 r@   r/   r/   ö   sk  € õ0 ˜ÑÔ€DÝ�T�#‰YŒY‰Œ€AØ	Œ�Œ
€AØ€Eð Ð&ˆ6ˆ6 �D€A€qàÐ	1�ˆc�2‰Œˆ¥T¨#¨q¡\¤\€BÝ�q‘	”	€IØ €B€y�)ÐÑà�D˜A’IŒð ð ˆð �c‰kˆà�!Š8ˆ8Ø"% e¨A¡g¨u°Q©wÐ&6Ô"7ˆAˆe�A‰g�u˜Q‘wÐÑð ð <˜	ð <Ø&)¨%°©'°5¸±7Ð*:Ô&;×&@Ò&@Ñ&BÔ&B��%˜‘'˜5 ™7Ð"Ñ#Ð#à&)¨%°©'°5¸±7Ð*:Ô&;��%˜‘'˜5 ™7Ð"Ñ#à#%ˆBˆu�Q‰w˜˜a™ÐÑ Øˆˆàˆbˆ5€LrA   c                 ót  — | j         d         }t          |¦  «        }|r|dz
  ddfnd|df\  }}}t          |||¦  «        D ]g}	||	         }
|
|	k    rW|r|	nd}|r|n|	dz   }||	         |rdndk    r||rdndz  }||rdndz  }| |	|
g||…f         | |
|	g||…f<   ||	|
g         ||
|	g<   Œh| t          |¦  «        fS )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   rC   r   )r$   r   rD   r   )r>   Úswap_vecrQ   r    r2   r?   rJ   rK   rL   rM   Ús_indÚcol_sÚcol_es                r@   r0   r0   .  s  € ðF 	Œ�Œ€AÝ�!‰9Œ9€Dà"'Ð6�!�A‘#�r˜2��¨a°°A¨Y�J€BˆˆBå�R˜˜RÑ Ô ð 4ð 4ˆØ˜”ˆØ�CŠ<ˆ<à Ð'�C�C aˆEØÐ)�A�A C¨¡EˆEð �CŒy %Ð.˜Q˜Q¨QÒ/Ð/Ø˜uÐ+˜˜¨!Ñ+�Ø˜eÐ*˜˜¨Ñ*�Ø,.°°U¨|¸UÀ5¸[Ð/HÔ,IˆB��sˆ|˜U 5˜[Ð(Ñ)Ø!% s¨E lÔ!3ˆD�%˜�Ñøà�w�t‰}Œ}ÐÐrA   )TTFT)T)TT)Úwarningsr   Únumpyr'   r   r   r   r   r   r	   r
   r   r   r   Únumpy.exceptionsr   Úscipy._lib._utilr   Ú_decompr   Úlapackr   r   Ú__all__r   r.   r/   r0   © rA   r@   ú<module>rd      s…  ðØ Ð Ð Ð Ð Ð à Ð Ð Ð ðBð Bð Bð Bð Bð Bð Bð Bð Bð Bð Bð Bð Bð Bð Bð Bð Bð Bð Bð Bð Bð Bð Bð Bà +Ð +Ð +Ð +Ð +Ð +à .Ð .Ð .Ð .Ð .Ð .Ø 'Ð 'Ð 'Ð 'Ð 'Ð 'Ø 4Ð 4Ð 4Ð 4Ð 4Ð 4Ð 4Ð 4àˆ'€ð Ð�8ÑÔðNð Nð Nñ ÔðNðbRð Rð Rð Rðj5ð 5ð 5ð 5ðp6ð 6ð 6ð 6ð 6ð 6rA   