o
    oÇhn3  ã                   @   sê   d dl mZ d dlmZ ddlmZmZmZ ddlm	Z	 dd„ Z
dd	„ Zd
d„ Zd*dd„Ze	fdd„Ze	fdd„Ze	fdd„Ze	fdd„Ze	fdd„Ze	fdd„Ze	fdd„Zd+dd„Zd d!„ Zd,d#d$„Ze	fd%d&„Zd'e	dfd(d)„Zd'S )-é    )ÚDMNonInvertibleMatrixError)ÚEXé   )ÚMatrixErrorÚNonSquareMatrixErrorÚNonInvertibleMatrixError©Ú_iszeroc                 C   sH   | j r| jS | j| jkr| j | ¡ ¡  | j¡S | j |  | j¡ ¡ ¡S )a  Subroutine for full row or column rank matrices.

    For full row rank matrices, inverse of ``A * A.H`` Exists.
    For full column rank matrices, inverse of ``A.H * A`` Exists.

    This routine can apply for both cases by checking the shape
    and have small decision.
    )Úis_zero_matrixÚHÚrowsÚcolsÚmultiplyÚinv)ÚM© r   új/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/sympy/matrices/inverse.pyÚ_pinv_full_rank   s
   
r   c                 C   s2   | j r| jS |  ¡ \}}t|ƒ}t|ƒ}| |¡S )z¶Subroutine for rank decomposition

    With rank decompositions, `A` can be decomposed into two full-
    rank matrices, and each matrix can take pseudoinverse
    individually.
    )r
   r   Úrank_decompositionr   r   )r   ÚBÚCÚBpÚCpr   r   r   Ú_pinv_rank_decomposition   s   
r   c                 C   s¶   | j r| jS | }| j}zD| j| jkr1| |¡jdd\}}| dd„ ¡}| |¡ |j¡ |¡W S | |¡jdd\}}| dd„ ¡}| |¡ |¡ |j¡W S  tyZ   tdƒ‚w )zSubroutine using diagonalization

    This routine can sometimes fail if SymPy's eigenvalue
    computation is not reliable.
    T)Ú	normalizec                 S   ó   t | ƒrdS d|  S ©Nr   r   r   ©Úxr   r   r   Ú<lambda><   ó    z'_pinv_diagonalization.<locals>.<lambda>c                 S   r   r   r   r   r   r   r   r   C   r    z[pinv for rank-deficient matrices where diagonalization of A.H*A fails is not supported yet.)	r
   r   r   r   r   ÚdiagonalizeÚ	applyfuncr   ÚNotImplementedError)r   ÚAÚAHÚPÚDÚD_pinvr   r   r   Ú_pinv_diagonalization,   s&   

ÿÿÿr)   ÚRDc                 C   s<   | j r| jS |dkrt| ƒS |dkrt| ƒS tdt|ƒ ƒ‚)a  Calculate the Moore-Penrose pseudoinverse of the matrix.

    The Moore-Penrose pseudoinverse exists and is unique for any matrix.
    If the matrix is invertible, the pseudoinverse is the same as the
    inverse.

    Parameters
    ==========

    method : String, optional
        Specifies the method for computing the pseudoinverse.

        If ``'RD'``, Rank-Decomposition will be used.

        If ``'ED'``, Diagonalization will be used.

    Examples
    ========

    Computing pseudoinverse by rank decomposition :

    >>> from sympy import Matrix
    >>> A = Matrix([[1, 2, 3], [4, 5, 6]])
    >>> A.pinv()
    Matrix([
    [-17/18,  4/9],
    [  -1/9,  1/9],
    [ 13/18, -2/9]])

    Computing pseudoinverse by diagonalization :

    >>> B = A.pinv(method='ED')
    >>> B.simplify()
    >>> B
    Matrix([
    [-17/18,  4/9],
    [  -1/9,  1/9],
    [ 13/18, -2/9]])

    See Also
    ========

    inv
    pinv_solve

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Moore-Penrose_pseudoinverse

    r*   ÚEDzinvalid pinv method %s)r
   r   r   r)   Ú
ValueErrorÚrepr)r   Úmethodr   r   r   Ú_pinvL   s   6r/   c                    sj   | j stdƒ‚| jdd}| d¡}|du r-| jddd ‰t‡ ‡fdd	„tˆjƒD ƒƒ}|r3td
ƒ‚|S )zfInitial check to see if a matrix is invertible. Raises or returns
    determinant for use in _inv_ADJ.ú"A Matrix must be square to invert.Ú	berkowitz)r.   r   NT)Úsimplifyc                 3   ó     | ]}ˆ ˆ||f ƒV  qd S ©Nr   ©Ú.0Új©Ú
iszerofuncÚokr   r   Ú	<genexpr>™   ó   € z%_verify_invertible.<locals>.<genexpr>ú Matrix det == 0; not invertible.)	Ú	is_squarer   ÚdetÚequalsÚrrefÚanyÚranger   r   )r   r9   ÚdÚzeror   r8   r   Ú_verify_invertible   s   
rF   c                 C   s   t | |d}|  ¡ | S )z©Calculates the inverse using the adjugate matrix and a determinant.

    See Also
    ========

    inv
    inverse_GE
    inverse_LU
    inverse_CH
    inverse_LDL
    ©r9   )rF   Úadjugate)r   r9   rD   r   r   r   Ú_inv_ADJ    s   rI   c                    s†   ddl m} | jstdƒ‚| |  ¡ | | j¡¡}|jˆ ddd ‰t	‡ ‡fdd„t
ˆjƒD ƒƒr5td	ƒ‚|  ˆd
d
…|jd
…f ¡S )z™Calculates the inverse using Gaussian elimination.

    See Also
    ========

    inv
    inverse_ADJ
    inverse_LU
    inverse_CH
    inverse_LDL
    r   )ÚMatrixr0   T)r9   r2   r   c                 3   r3   r4   r   r5   ©r9   Úredr   r   r;   Æ   r<   z_inv_GE.<locals>.<genexpr>r=   N)ÚdenserJ   r>   r   ÚhstackÚ
as_mutableÚeyer   rA   rB   rC   r   Ú_new)r   r9   rJ   Úbigr   rK   r   Ú_inv_GE±   s   rS   c                 C   s6   | j stdƒ‚| jrt| |d | j|  | j¡tdS )z•Calculates the inverse using LU decomposition.

    See Also
    ========

    inv
    inverse_ADJ
    inverse_GE
    inverse_CH
    inverse_LDL
    r0   rG   )r>   r   Úfree_symbolsrF   ÚLUsolverP   r   r	   ©r   r9   r   r   r   Ú_inv_LUË   s
   rW   c                 C   ó   t | |d |  |  | j¡¡S )z›Calculates the inverse using cholesky decomposition.

    See Also
    ========

    inv
    inverse_ADJ
    inverse_GE
    inverse_LU
    inverse_LDL
    rG   )rF   Úcholesky_solverP   r   rV   r   r   r   Ú_inv_CHß   ó   rZ   c                 C   rX   )z•Calculates the inverse using LDL decomposition.

    See Also
    ========

    inv
    inverse_ADJ
    inverse_GE
    inverse_LU
    inverse_CH
    rG   )rF   ÚLDLsolverP   r   rV   r   r   r   Ú_inv_LDLð   r[   r]   c                 C   rX   )z•Calculates the inverse using QR decomposition.

    See Also
    ========

    inv
    inverse_ADJ
    inverse_GE
    inverse_CH
    inverse_LDL
    rG   )rF   ÚQRsolverP   r   rV   r   r   r   Ú_inv_QR  r[   r_   Fc                 C   s0   |   ¡ }|j}|s|jrdS |jr| t¡S |S )z.Try to convert a matrix to a ``DomainMatrix``.N)Úto_DMÚdomainÚis_EXRAWÚ
convert_tor   )r   Úuse_EXÚdMÚKr   r   r   Ú_try_DM  s   

rg   c                 C   s   | j s| jrdS | j S )z,Check whether to convert to an exact domain.F)Úis_RRÚis_CCÚis_Exact)Údomr   r   r   Ú_use_exact_domain   s   rl   Tc           
      C   s¼   | j \}}| j}||krtdƒ‚t|ƒ}|r| ¡ }|  |¡} z|  ¡ \}}W n ty2   tdƒ‚w |r@| |¡}| 	||¡}|rR|jj
sJ| ¡ }||  ¡ }	|	S | ¡ |j |¡ }	|	S )zÌCalculates the inverse using ``DomainMatrix``.

    See Also
    ========

    inv
    inverse_ADJ
    inverse_GE
    inverse_CH
    inverse_LDL
    sympy.polys.matrices.domainmatrix.DomainMatrix.inv
    r0   r=   )Úshapera   r   rl   Ú	get_exactrc   Úinv_denr   r   Úconvert_fromÚis_FieldÚto_fieldÚ	to_MatrixÚto_sympy)
re   ÚcancelÚmÚnrk   Ú	use_exactÚ	dom_exactÚdMiÚdenÚMir   r   r   Ú_inv_DM+  s.   

ÿ
þr}   c                 C   sV  ddl m} | jd }|dkr| jdtdS | d|d …d|d …f }| d|d …|d d…f }| |d d…d|d …f }| |d d…|d d…f }zt|ƒ}W n tyd   | jdtd Y S w || }	|	| }
||
 }zt|ƒ}W n ty‡   | jdtd Y S w | |	 }|| }| | }|||   }|||g||ggƒ ¡ }|S )z˜Calculates the inverse using BLOCKWISE inversion.

    See Also
    ========

    inv
    inverse_ADJ
    inverse_GE
    inverse_CH
    inverse_LDL
    r   )ÚBlockMatrixé   ÚLU©r.   r9   Né   )Ú&sympy.matrices.expressions.blockmatrixr~   rm   r   r	   Ú
_inv_blockr   Úas_explicit)r   r9   r~   Úir$   r   r   r'   ÚD_invÚB_D_iÚBDCÚA_nÚB_nÚdcÚC_nÚD_nÚnnr   r   r   r„   Y  s6   
ÿÿ

r„   Nc                 C   s‚  ddl m}m} | jstdƒ‚|r*|  ¡ }g }|D ]}| |j||d¡ q||Ž S |du r?|tu r?t	| dd}	|	dur>d}n
|d	v rIt	| d
d}	|du rWt
| |ƒrUd}nd}|dkr`t|	ƒ}
n\|dkrkt|	dd}
nQ|dkrv| j|d}
nF|dkr| j|d}
n;|dkrŒ| j|d}
n0|dkr—| j|d}
n%|dkr¢| j|d}
n|dkr­| j|d}
n|dkr¸| j|d}
ntdƒ‚|  |
¡S )ac	  
    Return the inverse of a matrix using the method indicated. The default
    is DM if a suitable domain is found or otherwise GE for dense matrices
    LDL for sparse matrices.

    Parameters
    ==========

    method : ('DM', 'DMNC', 'GE', 'LU', 'ADJ', 'CH', 'LDL', 'QR')

    iszerofunc : function, optional
        Zero-testing function to use.

    try_block_diag : bool, optional
        If True then will try to form block diagonal matrices using the
        method get_diag_blocks(), invert these individually, and then
        reconstruct the full inverse matrix.

    Examples
    ========

    >>> from sympy import SparseMatrix, Matrix
    >>> A = SparseMatrix([
    ... [ 2, -1,  0],
    ... [-1,  2, -1],
    ... [ 0,  0,  2]])
    >>> A.inv('CH')
    Matrix([
    [2/3, 1/3, 1/6],
    [1/3, 2/3, 1/3],
    [  0,   0, 1/2]])
    >>> A.inv(method='LDL') # use of 'method=' is optional
    Matrix([
    [2/3, 1/3, 1/6],
    [1/3, 2/3, 1/3],
    [  0,   0, 1/2]])
    >>> A * _
    Matrix([
    [1, 0, 0],
    [0, 1, 0],
    [0, 0, 1]])
    >>> A = Matrix(A)
    >>> A.inv('CH')
    Matrix([
    [2/3, 1/3, 1/6],
    [1/3, 2/3, 1/3],
    [  0,   0, 1/2]])
    >>> A.inv('ADJ') == A.inv('GE') == A.inv('LU') == A.inv('CH') == A.inv('LDL') == A.inv('QR')
    True

    Notes
    =====

    According to the ``method`` keyword, it calls the appropriate method:

        DM .... Use DomainMatrix ``inv_den`` method
        DMNC .... Use DomainMatrix ``inv_den`` method without cancellation
        GE .... inverse_GE(); default for dense matrices
        LU .... inverse_LU()
        ADJ ... inverse_ADJ()
        CH ... inverse_CH()
        LDL ... inverse_LDL(); default for sparse matrices
        QR ... inverse_QR()

    Note, the GE and LU methods may require the matrix to be simplified
    before it is inverted in order to properly detect zeros during
    pivoting. In difficult cases a custom zero detection function can
    be provided by setting the ``iszerofunc`` argument to a function that
    should return True if its argument is zero. The ADJ routine computes
    the determinant and uses that to detect singular matrices in addition
    to testing for zeros on the diagonal.

    See Also
    ========

    inverse_ADJ
    inverse_GE
    inverse_LU
    inverse_CH
    inverse_LDL

    Raises
    ======

    ValueError
        If the determinant of the matrix is zero.
    r   )ÚdiagÚSparseMatrixr0   r   NF)rd   ÚDM)r’   ÚDMNCTÚLDLÚGEr“   )ru   rG   r€   ÚADJÚCHÚQRÚBLOCKzInversion method unrecognized)Úsympy.matricesr   r‘   r>   r   Úget_diag_blocksÚappendr   r	   rg   Ú
isinstancer}   Ú
inverse_GEÚ
inverse_LUÚinverse_ADJÚ
inverse_CHÚinverse_LDLÚ
inverse_QRÚinverse_BLOCKr,   rQ   )r   r.   r9   Útry_block_diagr   r‘   ÚblocksÚrÚblockre   Úrvr   r   r   Ú_inv  sP   Y€


rª   )r*   )F)T)Úsympy.polys.matrices.exceptionsr   Úsympy.polys.domainsr   Ú
exceptionsr   r   r   Ú	utilitiesr	   r   r   r)   r/   rF   rI   rS   rW   rZ   r]   r_   rg   rl   r}   r„   rª   r   r   r   r   Ú<module>   s(    
 A

.&