U
    L?h@                     @   sx  d dl mZ d dlmZmZ d dlmZmZ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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  d d	l!m"Z"m#Z#m$Z$m%Z% d d
l&m'Z'm(Z(m)Z)m*Z* d dl+m,Z,m-Z- d dl.m/Z/m0Z0m1Z1 e-ddd\Z2Z3Z4Z5Z6Z7Z8ede7e7Z9ede7e7Z:ede7e7Z;ede7e7Z<ede7e7Z=ede7e7Z>e
e=e>ggZ?e
e=ge>ggZ@dd ZAdd ZBdd ZCdd ZDdd ZEd d! ZFd"d# ZGd$d% ZHd&d' ZId(d) ZJd*d+ ZKd,d- ZLd.d/ ZMd0d1 ZNed2d3 ZOd4d5 ZPd6d7 ZQd8d9 ZRd:d; ZSd<d= ZTd>d? ZUd@dA ZVdBdC ZWdDdE ZXdFdG ZYdHdI ZZdJdK Z[dLdM Z\dNdO Z]dPdQ Z^dRdS Z_dTS )U    )Trace)raisesslow)block_collapse	bc_matmulbc_block_plus_identBlockDiagMatrixBlockMatrixbc_dist	bc_mataddbc_transpose
bc_inverseblockcutreblock_2x2deblock)MatrixSymbolIdentitytracedet
ZeroMatrix	OneMatrix)Inverse)MatPow)	Transpose)NonInvertibleMatrixError)MatrixImmutableMatrixImmutableSparseMatrixzeros)TupleExprSFunction)Symbolsymbols)	transposeimrezi:n, pT)integerABCDGHc                   C   s@   t tt t t ttt t tt t  t ggks<td S N)r   r.   b1b2r-   r	   AssertionError r3   r3   c/var/www/html/venv/lib/python3.8/site-packages/sympy/matrices/expressions/tests/test_blockmatrix.pytest_bc_matmul   s    r5   c                   C   s>   t tttggtttgg ttt tt ggks:td S r/   )r   r	   r-   r.   r2   r3   r3   r3   r4   test_bc_matadd    s    r6   c                   C   s@   t ttttgttggttjtjgtjtjggks<td S r/   )	r   r   r	   r)   r*   r+   r,   Tr2   r3   r3   r3   r4   test_bc_transpose$   s    r8   c                  C   s^   t dtt} t dtt}t dtt}t| ||}t|| td|  d| d| sZtd S )Nr)   r*   r+      )r   nmlr   r
   equalsr2   r)   r*   r+   Xr3   r3   r4   test_bc_dist_diag(   s
    r@   c                  C   s   t dtt} t dtt}t dtt}t dtt}t| |g||gg}t dtt tt }t|ttt  | ttttt| | kstd S )Nr)   r*   r+   r,   Z)r   r:   r;   r	   r   r   r   r2   )r)   r*   r+   r,   r?   rA   r3   r3   r4   test_block_plus_ident0   s    rB   c                  C   s  t dtt} t dtt}t dtt}t dtt}t dtt t}t dtt tt }tt| |g||gg}|j|j	 |kst
t dtt}t| d|  | d|  kst
t d	tt}t|j|  | |j|  | kst
|jtt tt fkst
|jd
kst
t|tt| j|jg|j|jggks4t
t|j|jd d d ksTt
|| jsdt
||jsvt
|| jtt tfkst
|| jst
||jst
|| j|jkst
t dtd}t d	td}tt|g|gg}	||	 jtt dfkst
t||	 jd | | ||  ks:t
t||	 jd || ||  ksbt
tt||	 tt||	 kst
tt||	 d| t||	 td| fkst
t| gg}
t d| j }t|
| | | kst
d S )Nr)   r*   r+   r,   MNEr9   Fr9   r9      )r   r   )rI   r   rA   )rA   )r   r:   r;   kr<   pr	   r   	__class__argsr2   r   r7   shapeZ
blockshaper%   	is_MatMul	_blockmul	is_MatAdd	_blockaddblocksr   )r)   r*   r+   r,   rC   rD   r?   rE   rF   YZAbrA   r3   r3   r4   test_BlockMatrix:   sH     $. ((&
 
rU   c                  C   s\   t ddgddgg} tt| gg| ks,ttddgddgg} tt| gg| ksXtd S NrI   r9         )r   r   r	   r2   r   r)   r3   r3   r4   %test_block_collapse_explicit_matricesk   s    rZ   c                  C   s   t ddd} tdd}t| |g||gg}t|| t| d |g||ggksRtt|| | t| d |g||ggks~td S )Nar9   rW   )r   r   r	   r   r2   )r[   zbr3   r3   r4   test_issue_17624r   s
    
(r^   c                  C   s8   t dddgdddgddd	gg} | t t| ks4td S )
NrI   r9   rW   rX               	   )r   r   r2   rY   r3   r3   r4   test_issue_18618y   s     rd   c                  C   sf   dd dD \} }}}t | |g||gg}t|t| t| ksFttt tttgdksbtd S )Nc                 S   s   g | ]}t |d d qS rW   r   .0sr3   r3   r4   
<listcomp>~   s     z*test_BlockMatrix_trace.<locals>.<listcomp>ABCDr   )r	   r   r2   r   r:   r)   r*   r+   r,   r?   r3   r3   r4   test_BlockMatrix_trace}   s    rm   c               	   C   s   dd dD \} }}}t | |g||gg}ddlm} ddlm} ||| ( t|t| t|d ksttW 5 Q R X t	t|t
sttt | gt| ksttt tttgdkstd S )Nc                 S   s   g | ]}t |d d qS re   rf   rg   r3   r3   r4   rj      s     z0test_BlockMatrix_Determinant.<locals>.<listcomp>rk   r   )Q)assumingr)   )r	   Zsympy.assumptions.askrn   Zsympy.assumptions.assumero   Z
invertibler   schurr2   
isinstancer    r   r:   )r)   r*   r+   r,   r?   rn   ro   r3   r3   r4   test_BlockMatrix_Determinant   s    ,rr   c                  C   s0  t dtt} t dtt}t dtt}t dtt}t| |g||gg}t| gg}|jsZt|ttt  }t|t| tt |g||tt ggkst|t dtt tt  jst|t dtt tt  j	stt|j
| j
kstt| tst|jrtttt|g||gg}|jr,td S )Nr)   r*   r+   r,   rn   )r   r:   r;   r	   	is_squarer2   r   r   rQ   rO   Irq   inverser   Zis_Identity)r)   r*   r+   r,   r?   rT   rn   rA   r3   r3   r4   test_squareBlockMatrix   s$    
"rv   c               	   C   sl  t dtt} t dttt }t dtt t}t dtt tt }t| |g||gg}|jrh|jttfkslttt	|j
tstt dtt} t dtt}t dtt}ttt}t| |g||gg}t	| t| j
| j
| |dj
 | | j
  | j
 | |dj
 g|dj
 | | j
 |dj
ggks:tt dtt} t dtt}ttt}t dtt}t| |g||gg}t	| t|dj
 | |j
 |dj
g|j
|j
|  |dj
 | |j
  |j
 |  |dj
 ggkstt dtt} ttt}t dtt}t dtt}t| |g||gg}t	| t|j
 | |dj
 |j
|j
| |dj
 |  |j
  g|dj
|dj
 |  |j
 ggkstttt} t dtt}t dtt}t dtt}t| |g||gg}t	| t|dj
|dj
 | |j
 g|j
 | |dj
 |j
|j
| |dj
 | |j
  ggkshtd S Nr)   r*   r+   r,   )r   r:   r;   rJ   r	   rs   rN   r2   rq   r   rt   r   r   ru   rp   rl   r3   r3   r4   %test_BlockMatrix_2x2_inverse_symbolic   sV    
<"
"<
<"
"<rx   c                  C   s  t ddgddgg} t ddgddgg}t ddgddgg}t ddgddgg}| |   krz|   krzdksn t||  ||    kr||    krdksn tt| |g||gg}t|  |  kstt|| g||gg}t|  |  ks&tt||g| |gg}t|  |  ks\tt||g|| gg}t|  |  kstdS )z>Test 2x2 block matrix inversion numerically for all 4 formulasrI   r9   rW   rX   rc      N)r   Zrankr2   r	   r   invas_explicit)rC   ZD1ZD2ZD3Kr3   r3   r4   $test_BlockMatrix_2x2_inverse_numeric   s    0< ""r}   c                     sF   t ttf} ttt f t fdd| D }t|j}t|tsBtd S )Nc                    s   g | ]  fd dD qS )c                    s    g | ]}t d  |f  |qS )zM%s%srf   )rh   colsrowsr3   r4   rj      s     z<test_BlockMatrix_3x3_symbolic.<locals>.<listcomp>.<listcomp>r3   rh   colblocksizesr   r4   rj      s   z1test_BlockMatrix_3x3_symbolic.<locals>.<listcomp>)r:   r;   rJ   r	   r   rt   rq   r2   )rowblocksizesr|   Zcollapser3   r   r4   test_BlockMatrix_3x3_symbolic   s    


r   c                     s&  t dtt t dttt dttt dtt t tt t } t t d d }jd ksrtjtt t tt t fkstt fdd	t	dD stj
j kst  fkstttj tstt t     ks$tt t     ksNtt td  d d sztt| t   d  d  kstt| td  d d
 kstd|   jstd|   jst| jst| js"td S )Nr)   r*   r+   rC   r9   rW   )rI   rI   c                 3   sL   | ]D}t d D ]6}||kr*j||f jnj||f  fkV  qqdS )rW   N)rangerS   Zis_ZeroMatrix)rh   ijr>   r3   r4   	<genexpr>  s    
 z'test_BlockDiagMatrix.<locals>.<genexpr>rX   )r   r:   r;   r<   r   rS   r2   rN   allr   rL   rM   Zget_diag_blocksrq   r   rt   r   r   r=   rO   rQ   rP   rR   )rC   rT   r3   r>   r4   test_BlockDiagMatrix  s.    "**,2*r   c                     s   t dtt t dtt} t | }|jtt tt fks<t|jtt tt fksVt|jttgksht|j	ttgksztt dttt dtt}t|}t
|| t  | | kstt
||j t j | |j ksttt fdd d S )Nr)   r*   r+   r,   c                      s   t  j S r/   )r   r7   ru   r3   r)   r+   r3   r4   <lambda>2      z0test_BlockDiagMatrix_nonsquare.<locals>.<lambda>)r   r:   r;   rJ   r<   r   rN   r2   r   r   r   r7   r   r   )r*   r?   r,   rT   r3   r   r4   test_BlockDiagMatrix_nonsquare%  s    

"(r   c                  C   s   t dtt} t dtt}tt dks*ttt| t| ksBttt| |t| t| ksdtt dtt}t dtt}tt||dkstd S )Nr)   r*   rI   r+   r,   r   )r   r:   r;   r   r   r2   r)   r*   r+   r,   r3   r3   r4    test_BlockDiagMatrix_determinant4  s    "r   c                  C   s   t t dkstt ttttdks,ttdtt} t t| t | ksPttdtt}t t| |t | t | ks~ttdtt}tdtt}tt t||tstd S )Nr   r)   r*   r+   r,   )	r   r   r2   r   r:   r   r;   rq   r   r   r3   r3   r4   test_BlockDiagMatrix_trace@  s    "r   c                  C   sj   t dtt} t dtt}tt t ks,ttt| t| jksFttt| |t| j|jksftd S )Nr)   r*   )	r   r:   r;   rJ   r<   r%   r   r2   r7   )r)   r*   r3   r3   r4   test_BlockDiagMatrix_transposeM  s
    r   c                  C   s^   t ttgttg} t ttgttg}t| | t ttt gttt gksZtd S r/   )r   r   r   r   rJ   r<   r   r2   )Zbdm1Zbdm2r3   r3   r4   test_issue_2460T  s    r   c                  C   s
  t dtt} t| td td ftd td f}|t| d td d td f | d td td d f g| td d d td f | td d td d f ggksttddtd}t|dd}|t|kstt|dd}t|jd tddggkstd S )	Nr)   r9   rX   ry   rG   )rI   rW   )r   rI   rW   )	r   r:   r;   r   r	   r2   r   r   rS   )r)   r*   rC   r3   r3   r4   test_blockcutY  s    $:6r   c                  C   sf   t dd tdD } | jjdks&tt| }|jjdks>t| j|jksNt|  | ksbtd S )Nc                    s"   g | ]  fd dt dD qS )c                    s    g | ]}t d  |f ddqS )A_%d%dr9   rf   rh   r   r   r3   r4   rj   g  s   z/test_reblock_2x2.<locals>.<listcomp>.<listcomp>rW   r   r   r3   r   r4   rj   g  s   
z$test_reblock_2x2.<locals>.<listcomp>rW   )rW   rW   rG   )r	   r   rS   rN   r2   r   r{   )r*   BBr3   r3   r4   test_reblock_2x2f  s    r   c                  C   s.   t dd tdD } tt| | ks*td S )Nc                    s"   g | ]  fd dt dD qS )c                    s    g | ]}t d  |f ttqS )r   )r   r:   r   r   r3   r4   rj   s  s   z+test_deblock.<locals>.<listcomp>.<listcomp>rX   r   r   r3   r   r4   rj   s  s   
z test_deblock.<locals>.<listcomp>rX   )r	   r   r   r   r2   )r*   r3   r3   r4   test_deblockr  s    r   c                  C   s   t tdgtdg} t tdgtdg}| jjt ks<tt| | jt ksRttt| jt kshttt| jt ks~ttt| jt kstt	t| jt kstd S rV   )
r   r   r7   rL   r2   r   r   r   r   r   )Zbm1Zbm2r3   r3   r4   test_block_collapse_typey  s    r   c                   C   s<   t tdd  t tdd  t tdd  t tdd  d S )Nc                   S   s   t tdtdggS )Nr9   r_   )r	   r   r3   r3   r3   r4   r     s   z+test_invalid_block_matrix.<locals>.<lambda>c                   S   s   t ttttggS r/   )r	   r   r:   r;   r3   r3   r3   r4   r     s   c                   S   s4   t ttttttgtttd tttd ggS NrI   r	   r   r:   r3   r3   r3   r4   r     s   c                   S   s4   t ttd ttttgttd ttttggS r   r   r3   r3   r3   r4   r     s   )r   
ValueErrorr3   r3   r3   r4   test_invalid_block_matrix  s    r   c                  C   s   t dtt} t dtt}t dtt}t dtt}t| |g||gg}| \}}}t|| | |ksjt| \}}}t|| | |kst| \}}t|| |kstd S rw   )	r   r:   r;   r	   ZLDUdecompositionr   r2   ZUDLdecompositionZLUdecomposition)r)   r*   r+   r,   r?   LUr3   r3   r4   test_block_lu_decomposition  s    r   c                  C   s   d} t | }t| | }t||||g||||g||||g||||gg}t| }t||||g||||g||||g| |||gg}||kstd S )N
   )r   r   r	   r   rz   r2   )r:   rt   Or)   ZAinvZAinvTr3   r3   r4   test_issue_21866  s    





r   c                  C   s   t d} tdd}tdd}t d}t| |g||gg}t| tj| g||gg}| t| tddgtdd|ggks~tt||kst| t| tddgtj tdd |ggkstt	|ttddtddgtddtddggkstd S )NrW   r9   )
r   r   r   r	   r!   ZImaginaryUnitZadjointr2   r'   r&   )r)   r*   r+   r,   r?   ZX2r3   r3   r4   !test_adjoint_and_special_matrices  s    

,4r   c                     s   t d tdd fddtdD } t| d dd df | d ddf g| dd df | dd df gg}t| |   tddkstd S )NxrW   c                    s   g | ]}t d |  qS )r[   )r"   )rh   r   r   r3   r4   rj     s     z0test_block_matrix_derivative.<locals>.<listcomp>rc   r9   )r$   r   r   r	   diffr   r2   )r)   bcr3   r   r4   test_block_matrix_derivative  s    Hr   c                  C   s   t d} t| }t| | }t||g||gg}t|  |ksFtt|  |ks^ttt| dt|dks~ttt|d t|dkstd S )Nr:   )	r#   r   r   r	   r   r%   ru   r2   r   )r:   rt   rA   r)   r3   r3   r4   test_transpose_inverse_commute  s    
 r   N)`Z sympy.matrices.expressions.tracer   Zsympy.testing.pytestr   r   Z&sympy.matrices.expressions.blockmatrixr   r   r   r   r	   r
   r   r   r   r   r   r   Zsympy.matrices.expressionsr   r   r   r   r   r   Z"sympy.matrices.expressions.inverser   Z!sympy.matrices.expressions.matpowr   Z$sympy.matrices.expressions.transposer   Zsympy.matrices.exceptionsr   Zsympy.matricesr   r   r   r   Z
sympy.corer   r    r!   r"   Zsympy.core.symbolr#   r$   Zsympy.functionsr%   r&   r'   r   r   rJ   r<   r;   r:   rK   r)   r*   r+   r,   r-   r.   r0   r1   r5   r6   r8   r@   rB   rU   rZ   r^   rd   rm   rr   rv   rx   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r3   r3   r3   r4   <module>   sh   8 
16
 