o
    8Va&                     @   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   8/usr/lib/python3/dist-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   )Zfieldr9   r1   c                    s   |   d jjS r   )unifyZrep)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   s    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   Zpolysr;   Zp2_infoZelements_loldmr   )r$   rC   rA   r2   r<   r=   r   r,   U   s4   

z MutablePolyDenseMatrix.from_listc                    s:   t  | }| }|j}||_||_|j|_|j|_|S r6   )superr3   Z	to_sparser9   _dmr   rG   r1   )r-   rL   objR	__class__r   r   rI   }   s   zMutablePolyDenseMatrix.from_dmc                 C   s
   | j  S r6   )rN   	to_Matrixselfr   r   r   rS      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   )rY   rS   )rU   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$   reprrS   r   rT   r   r   r   __repr__   s   &zMutablePolyDenseMatrix.__repr__c                 C   s   | j jS r6   )rN   rV   rT   r   r   r   rV      s   zMutablePolyDenseMatrix.shapec                 C   
   | j d S r   rV   rT   r   r   r   r/         
zMutablePolyDenseMatrix.rowsc                 C   r`   Nr   ra   rT   r   r   r   r$      rb   zMutablePolyDenseMatrix.colsc                 C   s   | j | j S r6   )r/   r$   rT   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   )rN   r9   rG   r   Zto_dict)vZgroundr1   rT   r   r   to_poly   s   

z3MutablePolyDenseMatrix.__getitem__.<locals>.to_polyc                    r?   r   r   r4   )rf   r   r   r      r   z6MutablePolyDenseMatrix.__getitem__.<locals>.<listcomp>)	rN   r'   slicerW   r+   divmodr$   elementrI   )rU   keyrL   r0   r!   r"   r   r   )rU   rf   r   __getitem__   s   


z"MutablePolyDenseMatrix.__getitem__c                 C   s   t | t|s	tS | j|jkS r6   )r'   r   NotImplementedrN   rU   rX   r   r   r   __eq__   s   zMutablePolyDenseMatrix.__eq__c                 C   s$   t |t| r| | j|j S tS r6   r'   r   rI   rN   rl   rm   r   r   r   __add__      zMutablePolyDenseMatrix.__add__c                 C   s$   t |t| r| | j|j S tS r6   ro   rm   r   r   r   __sub__   rq   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   rI   rN   r+   r   r   r   r   rH   r   
ValueErrorrl   )rU   rX   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   rH   rI   rN   rl   )rU   rX   ru   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 rc   )r'   r   rB   r+   r   r   rl   r9   rH   r   Zconvert_fromr   rN   rI   )rU   rX   ZinverserL   r   r   r   __truediv__   s   





z"MutablePolyDenseMatrix.__truediv__c                 C   s   |  | j S r6   )rI   rN   rT   r   r   r   __neg__   s   zMutablePolyDenseMatrix.__neg__c                 C   s   |  | j S r6   )rI   rN   	transposerT   r   r   r   rz      r[   z MutablePolyDenseMatrix.transposec                 C      t | j|j}| |S r6   )r
   ZhstackrN   rI   rU   rX   rL   r   r   r   row_join      
zMutablePolyDenseMatrix.row_joinc                 C   r{   r6   )r
   ZvstackrN   rI   r|   r   r   r   col_join   r~   zMutablePolyDenseMatrix.col_joinc                 C   s   |   |}| || jS r6   )rS   	applyfuncrY   r1   )rU   r%   Mr   r   r   r      s   z MutablePolyDenseMatrix.applyfuncc                 C   s   |  t|t| S r6   )rI   r
   eyer	   )r-   nr1   r   r   r   r     s   zMutablePolyDenseMatrix.eyec                 C   s   |  t||ft| S r6   )rI   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   Z	is_groundr@   r   r   r   r7         z.MutablePolyDenseMatrix.rref.<locals>.<genexpr>z1PolyMatrix rref is only for ground field elements)r9   is_FieldrE   rs   rN   
convert_torrefrI   )rU   ZsimplifyZnormalize_lastrL   Z	dm_groundZdm_rrefZpivotsr   r   r   r     s   zMutablePolyDenseMatrix.rrefc                    s   j jrtdd D stdj}j j}}|| |}|   fddt	 j
d D }fdd|D S )Nc                 s   r   r6   r   r@   r   r   r   r7     r   z3MutablePolyDenseMatrix.nullspace.<locals>.<genexpr>z6PolyMatrix nullspace is only for ground field elementsc                    s   g | ]
} d d |f qS r6   r   )r   r!   )dm_nullr   r   r     rD   z4MutablePolyDenseMatrix.nullspace.<locals>.<listcomp>r   c                    s   g | ]}  |qS r   )rI   )r   ZdmvecrT   r   r   r     r>   )r9   r   rE   rs   rN   r   r   	nullspacerz   r    rV   )rU   rL   Krt   Zdm_null_rowsZdm_basisr   )r   rU   r   r     s   z MutablePolyDenseMatrix.nullspacec                 C   s   | j t|   S r6   )r$   r*   r   rT   r   r   r   rank!  s   zMutablePolyDenseMatrix.rank)r   r   )#__name__
__module____qualname____doc__r3   classmethodr,   rI   rS   rY   rZ   r_   propertyrV   r/   r$   rd   rk   rn   rp   rr   rv   rw   rx   ry   rz   r}   r   r   r   r   r   r   r   __classcell__r   r   rQ   r   r      sL    "%
'







r   N)Zsympy.core.exprr   Zsympy.core.symbolr   Zsympy.core.sympifyr   Zsympy.polys.polyerrorsr   Zsympy.polys.polytoolsr   r   Zsympy.polys.domainsr	   Zsympy.polys.matricesr
   Z!sympy.polys.matrices.domainscalarr   r   ZMutablePolyMatrixZ
PolyMatrixr   r   r   r   <module>   s      