
    \hn3                         S SK Jr  S SKJr  SSKJrJrJr  SSKJ	r	  S r
S rS rSS	 jr\	4S
 jr\	4S jr\	4S jr\	4S jr\	4S jr\	4S jr\	4S jrSS jrS rSS jr\	4S jrS\	S4S jrg)    )DMNonInvertibleMatrixError)EX   )MatrixErrorNonSquareMatrixErrorNonInvertibleMatrixError_iszeroc                 x   U R                   (       a  U R                  $ U R                  U R                  :  aB  U R                  R	                  U 5      R                  5       R	                  U R                  5      $ U R                  R	                  U R	                  U R                  5      R                  5       5      $ )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)Ms    N/var/www/auris/envauris/lib/python3.13/site-packages/sympy/matrices/inverse.py_pinv_full_rankr      sx     	ss
vvss||A""$--acc22ss||AJJqssO//122    c                     U R                   (       a  U R                  $ U R                  5       u  p[        U5      n[        U5      nUR	                  U5      $ )zSubroutine 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Cps        r   _pinv_rank_decompositionr      sH     	ss
!DA		B		B;;r?r   c                 l   U R                   (       a  U R                  $ U nU R                  n U R                  U R                  :  ak  UR	                  U5      R                  SS9u  p4UR                  S 5      nUR	                  U5      R	                  UR                  5      R	                  U5      $ UR	                  U5      R                  SS9u  p4UR                  S 5      nUR	                  U5      R	                  U5      R	                  UR                  5      $ ! [         a    [        S5      ef = f)zuSubroutine using diagonalization

This routine can sometimes fail if SymPy's eigenvalue
computation is not reliable.
T)	normalizec                 0    [        U 5      (       a  S$ SU -  $ Nr   r   r	   xs    r   <lambda>'_pinv_diagonalization.<locals>.<lambda><       

1+EA+Er   c                 0    [        U 5      (       a  S$ SU -  $ r    r	   r!   s    r   r#   r$   C   r%   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_pinvs         r   _pinv_diagonalizationr/   ,   s    	ss
	
A	
BD66QVV[[^//$/?DA[[!EFF::f%..qss3<<R@@ ZZ^//"& 0 (DA[[!EFF;;q>**62;;ACC@@ D!CD 	DDs   BD 2A*D D3c                     U R                   (       a  U R                  $ US:X  a  [        U 5      $ US:X  a  [        U 5      $ [	        S[        U5      -  5      e)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

RDEDzinvalid pinv method %s)r   r   r   r/   
ValueErrorrepr)r   methods     r   _pinvr6   L   sP    l 	ss
~'**	4$Q''1DL@AAr   c                 "  ^^ U R                   (       d  [        S5      eU R                  SS9nUR                  S5      nUc;  U R	                  SS9S   m[        UU4S j[        TR                  5       5       5      nU(       a  [        S5      eU$ )	zbInitial 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)r5   r   T)simplifyc              3   <   >#    U  H  nT" TX4   5      v   M     g 7fN ).0j
iszerofuncoks     r   	<genexpr>%_verify_invertible.<locals>.<genexpr>   s     @A:bh''    Matrix det == 0; not invertible.)		is_squarer   detequalsrrefanyranger   r   )r   r@   dzerorA   s    `  @r   _verify_invertiblerN      s{     ;;"#GHH555$A88A;D|vvtv$Q'@rww@@&'IJJHr   c                 :    [        XS9nU R                  5       U-  $ )zCalculates the inverse using the adjugate matrix and a determinant.

See Also
========

inv
inverse_GE
inverse_LU
inverse_CH
inverse_LDL
r@   )rN   adjugate)r   r@   rL   s      r   _inv_ADJrR      s     	14A::<!r   c                   ^^ SSK Jn  U R                  (       d  [        S5      eUR	                  U R                  5       UR                  U R                  5      5      nUR                  TSS9S   m[        UU4S j[        TR                  5       5       5      (       a  [        S5      eU R                  TS	S	2UR                  S	24   5      $ )
zyCalculates the inverse using Gaussian elimination.

See Also
========

inv
inverse_ADJ
inverse_LU
inverse_CH
inverse_LDL
r   )Matrixr8   T)r@   r:   r   c              3   <   >#    U  H  nT" TX4   5      v   M     g 7fr<   r=   )r>   r?   r@   reds     r   rB   _inv_GE.<locals>.<genexpr>   s     
:/Q:c!$i  /rD   rE   N)denserT   rF   r   hstack
as_mutableeyer   rI   rJ   rK   r   _new)r   r@   rT   bigrV   s    `  @r   _inv_GEr^      s     ;;"#GHH
--

166(:
;C
((j4(
8
;C

:%/
:::&'IJJ66#al#$$r   c                     U R                   (       d  [        S5      eU R                  (       a	  [        XS9  U R	                  U R                  U R                  5      [        S9$ )zuCalculates the inverse using LU decomposition.

See Also
========

inv
inverse_ADJ
inverse_GE
inverse_CH
inverse_LDL
r8   rP   )rF   r   free_symbolsrN   LUsolver[   r   r
   r   r@   s     r   _inv_LUrc      sE     ;;"#GHH~~1499QUU166]w977r   c                 h    [        XS9  U R                  U R                  U R                  5      5      $ )z{Calculates the inverse using cholesky decomposition.

See Also
========

inv
inverse_ADJ
inverse_GE
inverse_LU
inverse_LDL
rP   )rN   cholesky_solver[   r   rb   s     r   _inv_CHrf      s)     q0AEE!&&M**r   c                 h    [        XS9  U R                  U R                  U R                  5      5      $ )zuCalculates the inverse using LDL decomposition.

See Also
========

inv
inverse_ADJ
inverse_GE
inverse_LU
inverse_CH
rP   )rN   LDLsolver[   r   rb   s     r   _inv_LDLri      s'     q0::aeeAFFm$$r   c                 h    [        XS9  U R                  U R                  U R                  5      5      $ )zuCalculates the inverse using QR decomposition.

See Also
========

inv
inverse_ADJ
inverse_GE
inverse_CH
inverse_LDL
rP   )rN   QRsolver[   r   rb   s     r   _inv_QRrl     s'     q099QUU166]##r   Fc                     U R                  5       nUR                  nU(       d  UR                  (       a  gUR                  (       a  UR                  [        5      $ U$ )z.Try to convert a matrix to a ``DomainMatrix``.N)to_DMdomainis_EXRAW
convert_tor   )r   use_EXdMKs       r   _try_DMru     s?    	
B
		A ajj	
}}R  	r   c                 j    U R                   (       d  U R                  (       a  gU R                  (       + $ )z,Check whether to convert to an exact domain.F)is_RRis_CCis_Exact)doms    r   _use_exact_domainr{      s!    
 yyCII<<r   c                 P   U R                   u  p#U R                  nX#:w  a  [        S5      e[        U5      nU(       a!  UR	                  5       nU R                  U5      n  U R                  5       u  pxU(       a#  UR                  U5      nUR                  UW5      nU(       a?  UR                  R                  (       d  UR                  5       nXx-  R                  5       n	U	$ UR                  5       UR                  R                  U5      -  n	U	$ ! [         a    [        S5      ef = f)zCalculates the inverse using ``DomainMatrix``.

See Also
========

inv
inverse_ADJ
inverse_GE
inverse_CH
inverse_LDL
sympy.polys.matrices.domainmatrix.DomainMatrix.inv
r8   rE   )shapero   r   r{   	get_exactrq   inv_denr   r   convert_fromis_Fieldto_field	to_Matrixto_sympy)
rs   cancelmnrz   	use_exact	dom_exactdMidenMis
             r   _inv_DMr   +  s     88DA
))Cv"#GHH "#&IMMO	]]9%K::< nnS!sI.zz"",,.Ci""$
 I ]]_szz22377I! & K&'IJJKs   D D%c                 $   SSK Jn  U R                  S   nUS::  a  U R                  S[        S9$ U SUS-  2SUS-  24   nU SUS-  2US-  S24   nXS-  S2SUS-  24   nXS-  S2US-  S24   n [        U5      nXX-  n	X-  n
XJ-
  n [        U5      nU* U	-  nX-  nU* U-  nXU* -  -   nU" X/X//5      R                  5       nU$ ! [         a    U R                  S[        S9s $ f = f! [         a    U R                  S[        S9s $ f = f)zxCalculates the inverse using BLOCKWISE inversion.

See Also
========

inv
inverse_ADJ
inverse_GE
inverse_CH
inverse_LDL
r   )BlockMatrix   LUr5   r@   N   )&sympy.matrices.expressions.blockmatrixr   r}   r   r
   
_inv_blockr   as_explicit)r   r@   r   ir*   r   r   r-   D_invB_D_iBDCA_nB_ndcC_nD_nnns                    r   r   r   Y  se    C	
ABwuuDWu55	'16'6AE6/A	'16'167
A	q&'7AF7
A	q&'167
A61 GE
'C
'C6o $u*C	B#c'C
cT'/C	sj3*-	.	:	:	<BI $ 6uuDWu556 $ 6uuDWu556s$   3C
 C. 
C+*C+.DDNc                    SSK JnJn  U R                  (       d  [	        S5      eU(       a>  U R                  5       n/ nU H!  nUR                  UR                  XS95        M#     U" U6 $ Uc  U[        L a  [        U SS9n	U	b  SnOUS;   a
  [        U S	S9n	Uc  [        X5      (       a  S
nOSnUS:X  a  [        W	5      n
OUS:X  a  [        W	SS9n
OUS:X  a  U R                  US9n
OUS:X  a  U R                  US9n
OyUS:X  a  U R                  US9n
OcUS:X  a  U R                  US9n
OMUS
:X  a  U R!                  US9n
O7US:X  a  U R#                  US9n
O!US:X  a  U R%                  US9n
O['        S5      eU R)                  U
5      $ )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SparseMatrixr8   r   F)rr   DM)r   DMNCTLDLGEr   )r   rP   r   ADJCHQRBLOCKzInversion method unrecognized)sympy.matricesr   r   rF   r   get_diag_blocksappendr   r
   ru   
isinstancer   
inverse_GE
inverse_LUinverse_ADJ
inverse_CHinverse_LDL
inverse_QRinverse_BLOCKr3   r\   )r   r5   r@   try_block_diagr   r   blocksrblockrs   rvs              r   _invr     s   r 2;;"#GHH""$EHHUYYfYDE  Qx ~*/Qu%>F	>	!Qt$ ~a&&FF~R[	6	R&	4\\Z\0	4\\Z\0	5]]j]1	4\\Z\0	5]]j]1	4\\Z\0	7	__
_389966":r   )r1   )F)T)sympy.polys.matrices.exceptionsr   sympy.polys.domainsr   
exceptionsr   r   r   	utilitiesr
   r   r   r/   r6   rN   rR   r^   rc   rf   ri   rl   ru   r{   r   r   r   r=   r   r   <module>r      s    F " S S 3$$D@>BB &- & # " " %4 " 8( " +" # %" " $" ,\ % $L GE Mr   