o
    oh+&                     @   s~   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
 d dlmZ d dlmZ d dlmZ G d	d
 d
Ze ZZdS )    )Expr)Dummy_sympify)CoercionFailed)Polyparallel_poly_from_expr)QQ)DomainMatrix)DomainScalarc                       s&  e Zd ZdZddddZedd Ze fdd	Zd
d ZeddddZ	dd Z
dd Zedd Zedd Zedd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zed2d3 Zed4d5 Zd=d7d8Zd9d: Z d;d< Z!  Z"S )>MutablePolyDenseMatrixax  
    A mutable matrix of objects from poly module or to operate with them.

    Examples
    ========

    >>> from sympy.polys.polymatrix import PolyMatrix
    >>> from sympy import Symbol, Poly
    >>> x = Symbol('x')
    >>> pm1 = PolyMatrix([[Poly(x**2, x), Poly(-x, x)], [Poly(x**3, x), Poly(-1 + x, x)]])
    >>> v1 = PolyMatrix([[1, 0], [-1, 0]], x)
    >>> pm1*v1
    PolyMatrix([
    [    x**2 + x, 0],
    [x**3 - x + 1, 0]], ring=QQ[x])

    >>> pm1.ring
    ZZ[x]

    >>> v1*pm1
    PolyMatrix([
    [ x**2, -x],
    [-x**2,  x]], ring=QQ[x])

    >>> pm2 = PolyMatrix([[Poly(x**2, x, domain='QQ'), Poly(0, x, domain='QQ'), Poly(1, x, domain='QQ'),             Poly(x**3, x, domain='QQ'), Poly(0, x, domain='QQ'), Poly(-x**3, x, domain='QQ')]])
    >>> v2 = PolyMatrix([1, 0, 0, 0, 0, 0], x)
    >>> v2.ring
    QQ[x]
    >>> pm2*v2
    PolyMatrix([[x**2]], ring=QQ[x])

    N)ringc                   s  |s|d u r
t dddg df\} }}nt|d trX|d |dd  }}|s2ddg } }nt|d ttfrNt|t|d } dd |D }nht|d} |}n^dd |d d D tttgkr}|d |d |d	 |dd  f\} }}n9d
d |d d D tttdd gkr|d |d |d	 |dd  f\} } fddt|D }nt dt|dkrt|d tr|d }| | |||S )Nz6The ring needs to be specified for an empty PolyMatrixr       c                 S   s   g | ]	}|D ]}|qqS r   r   ).0rower   r   j/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/sympy/polys/polymatrix.py
<listcomp>?   s    z2MutablePolyDenseMatrix.__new__.<locals>.<listcomp>c                 S      g | ]}t |qS r   typer   ar   r   r   r   D             c                 S   r   r   r   r   r   r   r   r   G   r   c                   S   s   dS Nr   r   r   r   r   r   <lambda>G   s    z0MutablePolyDenseMatrix.__new__.<locals>.<lambda>c                    s$   g | ]}t  D ]}||qqS r   range)r   ijcolsfuncr   r   r   J   s   $ zInvalid arguments)		TypeError
isinstancelisttuplelenintr   r    	from_list)clsr   argsrowsitemsgenselementsr   r#   r   __new__0   s,    *((zMutablePolyDenseMatrix.__new__c                    s>  dd |D }|rt dd |D rd}nd}|d ur*t|tr)tdt |dj}n0|rG|d }|d	d  D ]	}||\}}	q6|j|j }nt||dd
\}}
|
d |
d  }d}|rztd|j	|jd|j
jfddfdd|D n|jfdd|D  fddt|D }t|| f|}| |S )Nc                 S   r   r   r   r   itemr   r   r   r   Y   r   z4MutablePolyDenseMatrix.from_list.<locals>.<listcomp>c                 s   s    | ]}t |tV  qd S N)r'   r   r4   r   r   r   	<genexpr>Z   s    z3MutablePolyDenseMatrix.from_list.<locals>.<genexpr>TFr   domainr   )fieldr9   r1   c                    s   |   d j S r   )unifyrepto_list)p)p_ringto_ringr   r   r   r       z2MutablePolyDenseMatrix.from_list.<locals>.<lambda>c                       g | ]} |qS r   r   r   r>   )convert_polyr   r   r   s   r   c                    s   g | ]} |  qS r   )as_expr)r   r   )convert_exprr   r   r   v   rA   c                    s&   g | ]  fd dt D qS )c                    s   g | ]
}  |  qS r   r   )r   r"   )r$   r2   r!   r   r   r   y       z?MutablePolyDenseMatrix.from_list.<locals>.<listcomp>.<listcomp>r   )r   )r$   r2   )r!   r   r   y   s   & )allr'   strr   r   r9   r;   r1   r   symbolsr   r,   
from_sympyr    r
   from_dm)r-   r/   r$   r0   r1   r   polysr>   p2_infoelements_loldmr   )r$   rF   rD   r2   r?   r@   r   r,   U   s4   

z MutablePolyDenseMatrix.from_listc                    s:   t  | }| }|j}||_||_|j|_|j|_|S r6   )superr3   	to_sparser9   _dmr   rJ   r1   )r-   rR   objR	__class__r   r   rL   }   s   zMutablePolyDenseMatrix.from_dmc                 C   s
   | j  S r6   )rU   	to_Matrixselfr   r   r   rZ      s   
z MutablePolyDenseMatrix.to_Matrixc                G   s"   | g |j | |R d|iS )Nr   )shapeflat)r-   otherr   r1   r   r   r   from_Matrix   s   "z"MutablePolyDenseMatrix.from_Matrixc                 C   s   |  |  |S r6   )r`   rZ   )r\   r1   r   r   r   set_gens      zMutablePolyDenseMatrix.set_gensc                 C   sP   | j | j rdt|  d d  d| j d S d| j  d| j d| j dS )Nr   z, ring=)zPolyMatrix(z, z, [], ring=)r/   r$   reprrZ   r   r[   r   r   r   __repr__   s   &zMutablePolyDenseMatrix.__repr__c                 C   s   | j jS r6   )rU   r]   r[   r   r   r   r]      s   zMutablePolyDenseMatrix.shapec                 C   
   | j d S r   r]   r[   r   r   r   r/         
zMutablePolyDenseMatrix.rowsc                 C   rg   Nr   rh   r[   r   r   r   r$      ri   zMutablePolyDenseMatrix.colsc                 C   s   | j | j S r6   )r/   r$   r[   r   r   r   __len__   s   zMutablePolyDenseMatrix.__len__c                    s    fdd j }t|tr| | }fdd|D S t|tr5t| j\}}|||f }|jS |\}}t|trLt|trL|||f jS  |||f S )Nc                    s&    j jj} j jj}t|  ||dS )Nr8   )rU   r9   rJ   r   to_dict)vgroundr1   r[   r   r   to_poly   s   

z3MutablePolyDenseMatrix.__getitem__.<locals>.to_polyc                    rB   r   r   r4   )ro   r   r   r      r   z6MutablePolyDenseMatrix.__getitem__.<locals>.<listcomp>)	rU   r'   slicer^   r+   divmodr$   elementrL   )r\   keyrR   r0   r!   r"   r   r   )r\   ro   r   __getitem__   s   


z"MutablePolyDenseMatrix.__getitem__c                 C   s   t | t|s	tS | j|jkS r6   )r'   r   NotImplementedrU   r\   r_   r   r   r   __eq__   s   zMutablePolyDenseMatrix.__eq__c                 C   s$   t |t| r| | j|j S tS r6   r'   r   rL   rU   ru   rv   r   r   r   __add__      zMutablePolyDenseMatrix.__add__c                 C   s$   t |t| r| | j|j S tS r6   rx   rv   r   r   r   __sub__   rz   zMutablePolyDenseMatrix.__sub__c              	   C   s   t |t| r| | j|j S t |trt|}t |trD| j}z
t|	||}W n t
tfy;   t	|}Y nw | | j| S tS r6   )r'   r   rL   rU   r+   r   r   r   r   rK   r   
ValueErrorru   )r\   r_   Kxother_dsr   r   r   __mul__   s   

zMutablePolyDenseMatrix.__mul__c                 C   s:   t |tr	t|}t |trt|}| || j S tS r6   )	r'   r+   r   r   r   rK   rL   rU   ru   )r\   r_   r~   r   r   r   __rmul__   s   


zMutablePolyDenseMatrix.__rmul__c                 C   st   t |tr
| }n	t |trt|}t |tstS | j|}| j	
d| | j}t|| j	}| j| }| |S rj   )r'   r   rE   r+   r   r   ru   r9   rK   r   convert_fromr   rU   rL   )r\   r_   inverserR   r   r   r   __truediv__   s   





z"MutablePolyDenseMatrix.__truediv__c                 C   s   |  | j S r6   )rL   rU   r[   r   r   r   __neg__   s   zMutablePolyDenseMatrix.__neg__c                 C   s   |  | j S r6   )rL   rU   	transposer[   r   r   r   r      rb   z MutablePolyDenseMatrix.transposec                 C      t | j|j}| |S r6   )r
   hstackrU   rL   r\   r_   rR   r   r   r   row_join      
zMutablePolyDenseMatrix.row_joinc                 C   r   r6   )r
   vstackrU   rL   r   r   r   r   col_join   r   zMutablePolyDenseMatrix.col_joinc                 C   s   |   |}| || jS r6   )rZ   	applyfuncr`   r1   )r\   r%   Mr   r   r   r      s   z MutablePolyDenseMatrix.applyfuncc                 C   s   |  t|t| S r6   )rL   r
   eyer	   )r-   nr1   r   r   r   r     s   zMutablePolyDenseMatrix.eyec                 C   s   |  t||ft| S r6   )rL   r
   zerosr	   )r-   mr   r1   r   r   r   r     s   zMutablePolyDenseMatrix.zerosignorec                 C   s\   | j jrtdd | D std| j}||j j }| \}}||j }| ||fS )Nc                 s       | ]}|j V  qd S r6   	is_groundrC   r   r   r   r7         z.MutablePolyDenseMatrix.rref.<locals>.<genexpr>z1PolyMatrix rref is only for ground field elements)r9   is_FieldrH   r|   rU   
convert_torrefrL   )r\   simplifynormalize_lastrR   	dm_grounddm_rrefpivotsr   r   r   r     s   zMutablePolyDenseMatrix.rrefc                    s   j jrtdd D stdj}j j}}||jdd|}|   fddt	 j
d D }fd	d|D S )
Nc                 s   r   r6   r   rC   r   r   r   r7     r   z3MutablePolyDenseMatrix.nullspace.<locals>.<genexpr>z6PolyMatrix nullspace is only for ground field elementsT)divide_lastc                    s   g | ]
} d d |f qS r6   r   )r   r!   )dm_nullr   r   r     rG   z4MutablePolyDenseMatrix.nullspace.<locals>.<listcomp>r   c                    s   g | ]}  |qS r   )rL   )r   dmvecr[   r   r   r     s    )r9   r   rH   r|   rU   r   r   	nullspacer   r    r]   )r\   rR   Kr}   dm_null_rowsdm_basisr   )r   r\   r   r     s   z MutablePolyDenseMatrix.nullspacec                 C   s   | j t|   S r6   )r$   r*   r   r[   r   r   r   rank!  s   zMutablePolyDenseMatrix.rank)r   r   )#__name__
__module____qualname____doc__r3   classmethodr,   rL   rZ   r`   ra   rf   propertyr]   r/   r$   rk   rt   rw   ry   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__r   r   rX   r   r      sL    "%
'







r   N)sympy.core.exprr   sympy.core.symbolr   sympy.core.sympifyr   sympy.polys.polyerrorsr   sympy.polys.polytoolsr   r   sympy.polys.domainsr	   sympy.polys.matricesr
   !sympy.polys.matrices.domainscalarr   r   MutablePolyMatrix
PolyMatrixr   r   r   r   <module>   s      