o
    X۷iM                     @  s>   d dl mZ d dlZd dlmZ d dlZdd Zd
dd	ZdS )    )annotationsN)_utilc                 C  s@   ddl m} t|  t|  t|  t| }|| |S )a  Compute the inverse of a Hermitian matrix.

    This function computes a inverse of a real symmetric or complex hermitian
    positive-definite matrix using Cholesky factorization. If matrix ``a`` is
    not positive definite, Cholesky factorization fails and it raises an error.

    Args:
        a (cupy.ndarray): Real symmetric or complex hermitian maxtix.

    Returns:
        cupy.ndarray: The inverse of matrix ``a``.
    r   lapack)cupyxr   r   _assert_cupy_array
_assert_2d_assert_stacked_squarestacked_identity_likeposv)ar   b r   I/home/ubuntu/vllm_env/lib/python3.10/site-packages/cupyx/linalg/_solve.pyinvh	   s   



r   FTc                 C  s   ddl m} | \}}|r?|rt|jd dnt|jd d}t|d|d |d f  s4t	dt| s?t	d|s_|j
dkrS|jjrRtj|dd	d
}n|jjr_tj|dd	d
}|j|||dS )a  
    Solves the linear system Ax = b using the Cholesky factorization of A.
    Batched input arrays are also supported.

    Args:
        c (tuple of cupy.ndarray and bool): The first tuple item is the
            Cholesky factor of the matrix `A`, typically obtained from
            `cupy.linalg.cholesky`. The second item is a bool that indicates
            whether the Cholesky factor is stored in the lower-triangular
            part (when True) or in the upper-triangular part (when False).
        b (cupy.ndarray): Right-hand side array.
        overwrite_b (bool, optional): If True, the contents of `b` may be
            overwritten for efficiency. Default is False.
        check_finite (bool, optional): If True, checks whether the input arrays
            contain only finite numbers. Disabling this may improve performance
            but can lead to crashes or non-termination if NaNs or infs are
            present. Default is True.

    Returns:
        cupy.ndarray: Solution to the linear system Ax = b.

    See Also:
        cupy.linalg.cho_factor: Computes the Cholesky factorization.

    Examples:
        >>> import cupy as cp
        >>> from cupy.linalg import cholesky, cho_solve
        >>> A = cp.array([[9, 3, 1, 5],
        ...               [3, 7, 5, 1],
        ...               [1, 5, 9, 2],
        ...               [5, 1, 2, 6]])
        >>> c= cholesky(A)
        >>> x = cho_solve((c, True), cp.ones(4))
        >>> cp.allclose(A @ x, cp.ones(4))
        True
    r   r      .z%Input array contains NaN or infinity.   FT)ordercopyC)lower)r   r   numpytril_indicesshapetriu_indicescupyisfiniteall
ValueErrorndimflagsf_contiguousasarrayc_contiguouspotrs)c_and_lowerr   overwrite_bcheck_finiter   cr   indexesr   r   r   	cho_solve"   s$   % 
r,   )FT)
__future__r   r   cupy.linalgr   r   r   r,   r   r   r   r   <module>   s    