§
    µ³Ùg™5  ã                   ó‚   — d Z ddlZddlmZmZmZmZ ddlmZm	Z	 ddl
mZ g d¢Z	 	 dd
„Zdd„Zdd„Zdd„Zdd„Zdd„ZdS )z!Cholesky decomposition functions.é    N)Úasarray_chkfiniteÚasarrayÚ
atleast_2dÚ
empty_likeé   )ÚLinAlgErrorÚ_datacopied)Úget_lapack_funcs)ÚcholeskyÚ
cho_factorÚ	cho_solveÚcholesky_bandedÚcho_solve_bandedFTc                 ó~  — |rt          | ¦  «        nt          | ¦  «        }t          |¦  «        }|j        dk    rt	          d|j        › d�¦  «        ‚|j        d         |j        d         k    rt	          d|j        › d�¦  «        ‚|j        dk    r@t          t          j	        d|j
        ¬¦  «        ¦  «        j
        }t          ||¬¦  «        |fS |pt          || ¦  «        }t          d	|f¦  «        \  } |||||¬
¦  «        \  }}	|	dk    rt          d|	z  ¦  «        ‚|	dk     rt	          d|	 › d�¦  «        ‚||fS )z,Common code for cholesky() and cho_factor().é   z*Input array needs to be 2D but received a zd-array.r   r   z8Input array is expected to be square but has the shape: ú.©Údtype)Úpotrf)ÚlowerÚoverwrite_aÚcleanz9%d-th leading minor of the array is not positive definitez$LAPACK reported an illegal value in z -th argumenton entry to "POTRF".)r   r   r   ÚndimÚ
ValueErrorÚshapeÚsizer   ÚnpÚeyer   r   r	   r
   r   )
Úar   r   r   Úcheck_finiteÚa1Údtr   ÚcÚinfos
             úS/granary/edev/quantum/lib/python3.11/site-packages/scipy/linalg/_decomp_cholesky.pyÚ	_choleskyr&      s…  € ð ".Ð	=Õ	˜1Ñ	Ô	Ð	µ7¸1±:´:€BÝ	�B‰Œ€Bð 
„w�!‚|€|ÝÐWÀbÄgÐWÐWÐWÑXÔXÐXà	„x�„{�b”h˜q”kÒ!Ð!Ýð 3Ø')¤xð3ð 3ð 3ñ 4ô 4ð 	4ð 
„w�!‚|€|Ý•b”f˜Q b¤hÐ/Ñ/Ô/Ñ0Ô0Ô6ˆÝ˜" BÐ'Ñ'Ô'¨Ð.Ð.àÐ3¥¨R°Ñ!3Ô!3€KÝ˜j¨2¨%Ñ0Ô0�F€EØˆe�B˜e°ÀEÐJÑJÔJ�G€A€tØˆa‚x€xÝð %Ø'+ñ,ñ -ô -ð 	-àˆa‚x€xÝð 0ÀÀð 0ð 0ð 0ñ 1ô 1ð 	1àˆeˆ8€Oó    c                 ó4   — t          | ||d|¬¦  «        \  }}|S )aU  
    Compute the Cholesky decomposition of a matrix.

    Returns the Cholesky decomposition, :math:`A = L L^*` or
    :math:`A = U^* U` of a Hermitian positive-definite matrix A.

    Parameters
    ----------
    a : (M, M) array_like
        Matrix to be decomposed
    lower : bool, optional
        Whether to compute the upper- or lower-triangular Cholesky
        factorization. During decomposition, only the selected half of the
        matrix is referenced. Default is upper-triangular.
    overwrite_a : bool, optional
        Whether to overwrite data in `a` (may improve performance).
    check_finite : bool, optional
        Whether to check that the entire 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
    -------
    c : (M, M) ndarray
        Upper- or lower-triangular Cholesky factor of `a`.

    Raises
    ------
    LinAlgError : if decomposition fails.

    Notes
    -----
    During the finiteness check (if selected), the entire matrix `a` is
    checked. During decomposition, `a` is assumed to be symmetric or Hermitian
    (as applicable), and only the half selected by option `lower` is referenced.
    Consequently, if `a` is asymmetric/non-Hermitian, `cholesky` may still
    succeed if the symmetric/Hermitian matrix represented by the selected half
    is positive definite, yet it may fail if an element in the other half is
    non-finite.

    Examples
    --------
    >>> import numpy as np
    >>> from scipy.linalg import cholesky
    >>> a = np.array([[1,-2j],[2j,5]])
    >>> L = cholesky(a, lower=True)
    >>> L
    array([[ 1.+0.j,  0.+0.j],
           [ 0.+2.j,  1.+0.j]])
    >>> L @ L.T.conj()
    array([[ 1.+0.j,  0.-2.j],
           [ 0.+2.j,  5.+0.j]])

    T©r   r   r   r    ©r&   ©r   r   r   r    r#   s        r%   r   r   .   s-   € õn ˜ %°[ÈØ&2ð4ñ 4ô 4�H€A€uà€Hr'   c                 ó8   — t          | ||d|¬¦  «        \  }}||fS )aª
  
    Compute the Cholesky decomposition of a matrix, to use in cho_solve

    Returns a matrix containing the Cholesky decomposition,
    ``A = L L*`` or ``A = U* U`` of a Hermitian positive-definite matrix `a`.
    The return value can be directly used as the first parameter to cho_solve.

    .. warning::
        The returned matrix also contains random data in the entries not
        used by the Cholesky decomposition. If you need to zero these
        entries, use the function `cholesky` instead.

    Parameters
    ----------
    a : (M, M) array_like
        Matrix to be decomposed
    lower : bool, optional
        Whether to compute the upper or lower triangular Cholesky factorization.
        During decomposition, only the selected half of the matrix is referenced.
        (Default: upper-triangular)
    overwrite_a : bool, optional
        Whether to overwrite data in a (may improve performance)
    check_finite : bool, optional
        Whether to check that the entire 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
    -------
    c : (M, M) ndarray
        Matrix whose upper or lower triangle contains the Cholesky factor
        of `a`. Other parts of the matrix contain random data.
    lower : bool
        Flag indicating whether the factor is in the lower or upper triangle

    Raises
    ------
    LinAlgError
        Raised if decomposition fails.

    See Also
    --------
    cho_solve : Solve a linear set equations using the Cholesky factorization
                of a matrix.

    Notes
    -----
    During the finiteness check (if selected), the entire matrix `a` is
    checked. During decomposition, `a` is assumed to be symmetric or Hermitian
    (as applicable), and only the half selected by option `lower` is referenced.
    Consequently, if `a` is asymmetric/non-Hermitian, `cholesky` may still
    succeed if the symmetric/Hermitian matrix represented by the selected half
    is positive definite, yet it may fail if an element in the other half is
    non-finite.

    Examples
    --------
    >>> import numpy as np
    >>> from scipy.linalg import cho_factor
    >>> A = np.array([[9, 3, 1, 5], [3, 7, 5, 1], [1, 5, 9, 2], [5, 1, 2, 6]])
    >>> c, low = cho_factor(A)
    >>> c
    array([[3.        , 1.        , 0.33333333, 1.66666667],
           [3.        , 2.44948974, 1.90515869, -0.27216553],
           [1.        , 5.        , 2.29330749, 0.8559528 ],
           [5.        , 1.        , 2.        , 1.55418563]])
    >>> np.allclose(np.triu(c).T @ np. triu(c) - A, np.zeros((4, 4)))
    True

    Fr)   r*   r+   s        r%   r   r   j   s1   € õN ˜ %°[ÈØ&2ð4ñ 4ô 4�H€A€uàˆeˆ8€Or'   c                 óÞ  — | \  }}|rt          |¦  «        }t          |¦  «        }nt          |¦  «        }t          |¦  «        }|j        dk    s|j        d         |j        d         k    rt	          d¦  «        ‚|j        d         |j        d         k    r t	          d|j        › d|j        › d�¦  «        ‚|j        dk    rZt          t          j        d|j	        ¬¦  «        d	ft          j
        d|j	        ¬¦  «        ¦  «        j	        }t          ||¬¦  «        S |pt          ||¦  «        }t          d
||f¦  «        \  } |||||¬¦  «        \  }	}
|
dk    rt	          d|
 z  ¦  «        ‚|	S )aM  Solve the linear equations A x = b, given the Cholesky factorization of A.

    Parameters
    ----------
    (c, lower) : tuple, (array, bool)
        Cholesky factorization of a, as given by cho_factor
    b : array
        Right-hand side
    overwrite_b : bool, optional
        Whether to overwrite data in b (may improve 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
        The solution to the system A x = b

    See Also
    --------
    cho_factor : Cholesky factorization of a matrix

    Examples
    --------
    >>> import numpy as np
    >>> from scipy.linalg import cho_factor, cho_solve
    >>> A = np.array([[9, 3, 1, 5], [3, 7, 5, 1], [1, 5, 9, 2], [5, 1, 2, 6]])
    >>> c, low = cho_factor(A)
    >>> x = cho_solve((c, low), [1, 1, 1, 1])
    >>> np.allclose(A @ x - [1, 1, 1, 1], np.zeros(4))
    True

    r   r   r   z$The factored matrix c is not square.zincompatible dimensions (z and ú)r   T)Úpotrs©r   Úoverwrite_bz0illegal value in %dth argument of internal potrs)r   r   r   r   r   r   r   r   r   r   Úonesr   r	   r
   )Úc_and_lowerÚbr1   r    r#   r   Úb1r"   r/   Úxr$   s              r%   r   r   ¶   sˆ  € ðH �J€QˆØð Ý˜qÑ!Ô!ˆÝ˜aÑ Ô ˆˆå�Q‰ZŒZˆÝ�A‰JŒJˆà„v�‚{€{�a”g˜a”j A¤G¨A¤JÒ.Ð.ÝÐ?Ñ@Ô@Ð@Ø„wˆq„z�R”X˜a”[Ò Ð ÝÐN°Q´WÐNÐNÀ2Ä8ÐNÐNÐNÑOÔOÐOð 
„w�!‚|€|Ý�œ˜q¨¬Ð1Ñ1Ô1°4Ð8Ýœ ¨¬Ð1Ñ1Ô1ñ3ô 3Ü38ð 	å˜" BÐ'Ñ'Ô'Ð'àÐ3¥¨R°Ñ!3Ô!3€Kå˜j¨1¨b¨'Ñ2Ô2�F€EØˆe�A�r °KÐ@Ñ@Ô@�G€A€tØˆq‚y€yÝÐKØ ˜5ñ!ñ "ô "ð 	"à€Hr'   c                 ó�  — |rt          | ¦  «        } nt          | ¦  «        } | j        dk    rDt          t	          j        ddgddgg| j        ¬¦  «        ¦  «        j        }t          | |¬¦  «        S t          d| f¦  «        \  } || ||¬¦  «        \  }}|dk    rt          d|z  ¦  «        ‚|dk     rt          d| z  ¦  «        ‚|S )ai  
    Cholesky decompose a banded Hermitian positive-definite matrix

    The matrix a is stored in ab either in lower-diagonal or upper-
    diagonal ordered form::

        ab[u + i - j, j] == a[i,j]        (if upper form; i <= j)
        ab[    i - j, j] == a[i,j]        (if lower form; i >= j)

    Example of ab (shape of a is (6,6), u=2)::

        upper form:
        *   *   a02 a13 a24 a35
        *   a01 a12 a23 a34 a45
        a00 a11 a22 a33 a44 a55

        lower form:
        a00 a11 a22 a33 a44 a55
        a10 a21 a32 a43 a54 *
        a20 a31 a42 a53 *   *

    Parameters
    ----------
    ab : (u + 1, M) array_like
        Banded matrix
    overwrite_ab : bool, optional
        Discard data in ab (may enhance performance)
    lower : bool, optional
        Is the matrix in the lower form. (Default is upper form)
    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
    -------
    c : (u + 1, M) ndarray
        Cholesky factorization of a, in the same banded format as ab

    See Also
    --------
    cho_solve_banded :
        Solve a linear set equations, given the Cholesky factorization
        of a banded Hermitian.

    Examples
    --------
    >>> import numpy as np
    >>> from scipy.linalg import cholesky_banded
    >>> from numpy import allclose, zeros, diag
    >>> Ab = np.array([[0, 0, 1j, 2, 3j], [0, -1, -2, 3, 4], [9, 8, 7, 6, 9]])
    >>> A = np.diag(Ab[0,2:], k=2) + np.diag(Ab[1,1:], k=1)
    >>> A = A + A.conj().T + np.diag(Ab[2, :])
    >>> c = cholesky_banded(Ab)
    >>> C = np.diag(c[0, 2:], k=2) + np.diag(c[1, 1:], k=1) + np.diag(c[2, :])
    >>> np.allclose(C.conj().T @ C - A, np.zeros((5, 5)))
    True

    r   r   r   )Úpbtrf)r   Úoverwrite_abz)%d-th leading minor not positive definitez1illegal value in %d-th argument of internal pbtrf)r   r   r   r   r   Úarrayr   r   r
   r   r   )Úabr9   r   r    r"   r8   r#   r$   s           r%   r   r   ÷   sì   € ðx ð Ý˜rÑ"Ô"ˆˆå�R‰[Œ[ˆð 
„w�!‚|€|Ý�RœX¨¨1 v°°1¨vÐ&6¸b¼hÐGÑGÔGÑHÔHÔNˆÝ˜" BÐ'Ñ'Ô'Ð'å˜j¨2¨%Ñ0Ô0�F€EØˆe�B˜e°,Ð?Ñ?Ô?�G€A€tØˆa‚x€xÝÐEÈÑLÑMÔMÐMØˆa‚x€xÝÐLØ ˜5ñ!ñ "ô "ð 	"à€Hr'   c                 ó†  — | \  }}|rt          |¦  «        }t          |¦  «        }nt          |¦  «        }t          |¦  «        }|j        d         |j        d         k    rt          d¦  «        ‚|j        dk    rot          t          j        ddgddgg|j        ¬¦  «        ¦  «        }t          |dft          j
        d|j        ¬¦  «        ¦  «        j        }t          ||¬¦  «        S t          d||f¦  «        \  } |||||¬	¦  «        \  }	}
|
dk    rt          d
|
z  ¦  «        ‚|
dk     rt          d|
 z  ¦  «        ‚|	S )a‘  
    Solve the linear equations ``A x = b``, given the Cholesky factorization of
    the banded Hermitian ``A``.

    Parameters
    ----------
    (cb, lower) : tuple, (ndarray, bool)
        `cb` is the Cholesky factorization of A, as given by cholesky_banded.
        `lower` must be the same value that was given to cholesky_banded.
    b : array_like
        Right-hand side
    overwrite_b : bool, optional
        If True, the function will overwrite the values in `b`.
    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
        The solution to the system A x = b

    See Also
    --------
    cholesky_banded : Cholesky factorization of a banded matrix

    Notes
    -----

    .. versionadded:: 0.8.0

    Examples
    --------
    >>> import numpy as np
    >>> from scipy.linalg import cholesky_banded, cho_solve_banded
    >>> Ab = np.array([[0, 0, 1j, 2, 3j], [0, -1, -2, 3, 4], [9, 8, 7, 6, 9]])
    >>> A = np.diag(Ab[0,2:], k=2) + np.diag(Ab[1,1:], k=1)
    >>> A = A + A.conj().T + np.diag(Ab[2, :])
    >>> c = cholesky_banded(Ab)
    >>> x = cho_solve_banded((c, False), np.ones(5))
    >>> np.allclose(A @ x - np.ones(5), np.zeros(5))
    True

    éÿÿÿÿr   z&shapes of cb and b are not compatible.r   r   Tr   )Úpbtrsr0   z(%dth leading minor not positive definitez0illegal value in %dth argument of internal pbtrs)r   r   r   r   r   r   r   r:   r   r   r2   r   r
   r   )Úcb_and_lowerr4   r1   r    Úcbr   Úmr"   r>   r6   r$   s              r%   r   r   G  s]  € ð\ �K€RˆØð Ý˜rÑ"Ô"ˆÝ˜aÑ Ô ˆˆå�R‰[Œ[ˆÝ�A‰JŒJˆð 
„x�„|�q”w˜q”zÒ!Ð!ÝÐAÑBÔBÐBð 	„v�‚{€{Ý�BœH q¨! f¨q°!¨fÐ%5¸R¼XÐFÑFÔFÑGÔGˆÝ˜q $˜i­¬°¸!¼'Ð)BÑ)BÔ)BÑCÔCÔIˆÝ˜! 2Ð&Ñ&Ô&Ð&å˜j¨2¨q¨'Ñ2Ô2�F€EØˆe�B˜ °KÐ@Ñ@Ô@�G€A€tØˆa‚x€xÝÐDÀtÑKÑLÔLÐLØˆa‚x€xÝÐKØ ˜5ñ!ñ "ô "ð 	"à€Hr'   )FFTT)FFT)FT)Ú__doc__Únumpyr   r   r   r   r   Ú_miscr   r	   Úlapackr
   Ú__all__r&   r   r   r   r   r   © r'   r%   ú<module>rH      s  ðØ 'Ð 'à Ð Ð Ð Ø DÐ DÐ DÐ DÐ DÐ DÐ DÐ DÐ DÐ DÐ DÐ Dð ,Ð +Ð +Ð +Ð +Ð +Ð +Ð +Ø $Ð $Ð $Ð $Ð $Ð $ðð ð €ð 8<Øðð ð ð ð@9ð 9ð 9ð 9ðxIð Ið Ið IðX>ð >ð >ð >ðBMð Mð Mð Mð`Gð Gð Gð Gð Gð Gr'   