a
    RG5dG   ã                   @   sš   d Z ddlmZ ddlmZmZmZ dd„ Zdd„ Zd	d
„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zd#dd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd"S )$a"  

Module for the ddm_* routines for operating on a matrix in list of lists
matrix representation.

These routines are used internally by the DDM class which also provides a
friendlier interface for them. The idea here is to implement core matrix
routines in a way that can be applied to any simple list representation
without the need to use any particular matrix class. For example we can
compute the RREF of a matrix like:

    >>> from sympy.polys.matrices.dense import ddm_irref
    >>> M = [[1, 2, 3], [4, 5, 6]]
    >>> pivots = ddm_irref(M)
    >>> M
    [[1.0, 0.0, -1.0], [0, 1.0, 2.0]]

These are lower-level routines that work mostly in place.The routines at this
level should not need to know what the domain of the elements is but should
ideally document what operations they will use and what functions they need to
be provided with.

The next-level up is the DDM class which uses these routines but wraps them up
with an interface that handles copying etc and keeps track of the Domain of
the elements of the matrix:

    >>> from sympy.polys.domains import QQ
    >>> from sympy.polys.matrices.ddm import DDM
    >>> M = DDM([[QQ(1), QQ(2), QQ(3)], [QQ(4), QQ(5), QQ(6)]], (2, 3), QQ)
    >>> M
    [[1, 2, 3], [4, 5, 6]]
    >>> Mrref, pivots = M.rref()
    >>> Mrref
    [[1, 0, -1], [0, 1, 2]]

é    )Úmulé   )ÚDMShapeErrorÚDMNonInvertibleMatrixErrorÚDMNonSquareMatrixErrorc                 C   s   t tt t| Ž ƒƒ}|S )zmatrix transpose)ÚlistÚmapÚzip)ÚaZaT© r   úV/var/www/html/django/DPS/env/lib/python3.9/site-packages/sympy/polys/matrices/dense.pyÚddm_transpose.   s    r   c                 C   s:   t | |ƒD ]*\}}t|ƒD ]\}}||  |7  < qq
dS )za += bN©r	   Ú	enumerate©r
   ÚbÚaiÚbiÚjÚbijr   r   r   Úddm_iadd4   s    r   c                 C   s:   t | |ƒD ]*\}}t|ƒD ]\}}||  |8  < qq
dS )za -= bNr   r   r   r   r   Úddm_isub;   s    r   c                 C   s*   | D ] }t |ƒD ]\}}| ||< qqdS )z
a  <--  -aN©r   )r
   r   r   Úaijr   r   r   Úddm_inegB   s    r   c                 C   s,   | D ]"}t |ƒD ]\}}|| ||< qqd S ©Nr   ©r
   r   r   r   r   r   r   r   Úddm_imulI   s    r   c                 C   s,   | D ]"}t |ƒD ]\}}|| ||< qqd S r   r   r   r   r   r   Ú	ddm_irmulO   s    r   c                 C   sP   t t|Ž ƒ}t|| ƒD ]4\}}t|ƒD ]"\}}ttt||ƒ|| ƒ||< q&qdS )z
a += b @ cN)r   r	   r   Úsumr   r   )r
   r   ÚcZcTr   r   r   ZcTjr   r   r   Úddm_imatmulU   s    r!   Fc              	      s‚  t ˆ ƒ}|sg S t ˆ d ƒ}d}g }t|ƒD ]N‰|rltt||ƒ‡ ‡fdd„d}ˆ | ˆ |  ˆ |< ˆ |< ˆ | ˆ }|sÀt|d |ƒD ]2}ˆ | ˆ }|rŠˆ | ˆ |  ˆ |< ˆ |<  qÀqŠq,ˆ | }|d }	tˆ|ƒD ]}
||
  |	9  < qÚtˆ ƒD ]b\}}||ksø|ˆ sqø|ˆ }|ˆ  |8  < tˆd |ƒD ]}
||
  |||
  8  < q:qø| ˆ¡ |d7 }||kr, q~q,|S )za  <--  rref(a)r   c                    s   t ˆ |  ˆ ƒS r   )Úabs)Úip©r
   r   r   r   Ú<lambda>p   ó    zddm_irref.<locals>.<lambda>)Úkeyr   éÿÿÿÿ)ÚlenÚrangeÚmaxr   Úappend)r
   Ú_partial_pivotÚmÚnÚiÚpivotsr#   r   r   ZaijinvÚlÚkÚakZakjr   r$   r   Ú	ddm_irref^   sD    
r5   c           
      C   s*  t | ƒ}|s|jS t | d ƒ}|j}|j}t|d ƒD ]â}| | | s”t|d |ƒD ]4}| | | rT| | | |  | |< | |< | } q”qT|j  S |r¬| |d  |d  n|j}t|d |ƒD ]V}t|d |ƒD ]B}	|| | |	 | | |  | | | | | |	   |ƒ| | |	< qÒqÀq6|| d d  S )za  <--  echelon(a); return detr   r   r(   )r)   ÚoneÚexquor*   Úzero)
r
   ÚKr.   r/   r7   Zufr3   r0   Zakkm1r   r   r   r   Úddm_idetœ   s&    
Dr:   c                    s    ˆ j stdƒ‚t|ƒ}|sd S t|d ƒ‰|ˆkr6t‚‡ ‡fdd„tˆƒD ƒ}dd„ t||ƒD ƒ}t|ƒ}|ttˆƒƒkr‚tdƒ‚‡fdd„|D ƒ| d d …< d S )NzNot a fieldr   c                    s$   g | ]‰ ‡‡ fd d„t ˆƒD ƒ‘qS )c                    s    g | ]}ˆ|krˆ j nˆ j‘qS r   )r6   r8   )Ú.0r   )r9   r0   r   r   Ú
<listcomp>Ê   r&   z'ddm_iinv.<locals>.<listcomp>.<listcomp>)r*   )r;   ©r9   r/   )r0   r   r<   Ê   r&   zddm_iinv.<locals>.<listcomp>c                 S   s   g | ]\}}|| ‘qS r   r   )r;   ÚrowZeyerowr   r   r   r<   Ë   r&   z Matrix det == 0; not invertible.c                    s   g | ]}|ˆ d … ‘qS r   r   ©r;   r>   )r/   r   r   r<   Ï   r&   )	Úis_FieldÚ
ValueErrorr)   r   r*   r	   r5   r   r   )Úainvr
   r9   r.   ÚeyeÚAaugr1   r   r=   r   Úddm_iinv¾   s    rE   c           	      C   sˆ   t |ƒ}|sg S t |d ƒ}t|ƒ}|jgt||ƒ }td|ƒD ]B}t||ƒ}|| d|… | | d|…< |d|… || d|…< q@|S )zL, U  <--  LU(U)r   r   N)r)   Úddm_ilur8   Úminr*   )	ÚLÚUr9   r.   r/   ÚswapsÚzerosr0   r   r   r   r   Úddm_ilu_splitÒ   s    
rL   c           	   	   C   sþ   t | ƒ}|sg S t | d ƒ}g }tt||ƒƒD ]Ê}| | | sŒt|d |ƒD ]<}| | | rL| ||f¡ | | | |  | |< | |<  qŒqLq.t|d |ƒD ]\}| | | | | |  }|| | |< t|d |ƒD ]$}| | |  || | |  8  < qÐqšq.|S )za  <--  LU(a)r   r   )r)   r*   rG   r,   )	r
   r.   r/   rJ   r0   r#   r   Zl_jir3   r   r   r   rF   ä   s&    &rF   c                    sØ  t |ƒ}|sdS t |d ƒ}t |ƒ}|s0tdƒ‚t |d ƒ‰ ||krLtdƒ‚||k r\tdƒ‚|r–dd„ |D ƒ}|D ]"\}}	||	 ||  ||< ||	< qr‡ fdd„t|ƒD ƒ}
tˆ ƒD ]T}t|ƒD ]F}|| | }t|ƒD ] }||| | |
| |  8 }qØ||
| |< qÀq´||krHt||ƒD ](}tˆ ƒD ]}|
| | r*t‚q*qtˆ ƒD ]‚}tt|ƒƒD ]n}|| | svt‚|
| | }t|d |ƒD ]"}||| | | | |  8 }q||| |  | | |< q`qPdS )	zx  <--  solve(L*U*x = swaps(b))Nr   zShape mismtchZUnderdeterminedc                 S   s   g | ]}|d d … ‘qS r   r   r?   r   r   r   r<     r&   z!ddm_ilu_solve.<locals>.<listcomp>c                    s   g | ]}d gˆ  ‘qS r   r   ©r;   Ú_©Úor   r   r<     r&   r   )r)   r   ÚNotImplementedErrorr*   r   Úreversed)ÚxrH   rI   rJ   r   r.   r/   Úm2Úi1Úi2Úyr3   r0   Úrhsr   r   rO   r   Úddm_ilu_solve   sH    
 rY   c                    sª  t | ƒ}|sˆ jggS t | d ƒ‰|ˆkr2tdƒ‚ˆdkrRˆ jg| d d  ggS | d d }| d dd … g}dd„ | dd … D ƒ}dd„ | dd … D ƒ}t|ˆ ƒ}‡ ‡fdd„tˆd ƒD ƒ}tˆƒD ]$}	ˆ j||	 |	< | ||	d  |	< qÊtdˆd ƒD ]€}	|	dkr|}
n"|
}‡ fd	d„|D ƒ}
t|
||ƒ ˆ jgg}t|||
ƒ tdˆd |	 ƒD ] }|d d  ||	|  |< q\qþ‡ fd
d„tˆd ƒD ƒ}t|||ƒ |S )Nr   z
Not squarer   c                 S   s   g | ]}|d  g‘qS )r   r   r?   r   r   r   r<   >  r&   zddm_berk.<locals>.<listcomp>c                 S   s   g | ]}|d d… ‘qS )r   Nr   r?   r   r   r   r<   ?  r&   c                    s   g | ]}ˆ j gˆ ‘qS r   ©r8   rM   r=   r   r   r<   C  r&   é   c                    s   g | ]}ˆ j g‘qS r   rZ   r?   ©r9   r   r   r<   L  r&   c                    s   g | ]}ˆ j g‘qS r   rZ   rM   r\   r   r   r<   S  r&   )r)   r6   r   Úddm_berkr*   r!   r8   )ÚMr9   r.   r
   ÚRÚCÚAÚqÚTr0   ZAnCZRAnCr   Zqoutr   r=   r   r]   0  s<    



 r]   N)F)Ú__doc__Úoperatorr   Ú
exceptionsr   r   r   r   r   r   r   r   r   r!   r5   r:   rE   rL   rF   rY   r]   r   r   r   r   Ú<module>   s    $	
>"0