
    MZd"                        d Z ddlmZ ddlmZ ddlmZmZmZ ddl	m
Z
mZ ddlmZ  ed      Z ed	e
      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 Zd Zd Zd Zd Zd Zy) 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]]

    )annotations)mul   )DMShapeErrorDMNonInvertibleMatrixErrorDMNonSquareMatrixError)SequenceTypeVar)RingElementTR)boundc                @    t        t        t         t        |              S )zmatrix transpose)listmapzip)matrixs    </usr/lib/python3/dist-packages/sympy/polys/matrices/dense.pyddm_transposer   4   s    D#v,'((    c                l    t        | |      D ]%  \  }}t        |      D ]  \  }}||xx   |z  cc<    ' y)za += bNr   	enumerateabaibijbijs         r   ddm_iaddr!   9   ?    a) Bm 	FAsqESLE	r   c                l    t        | |      D ]%  \  }}t        |      D ]  \  }}||xx   |z  cc<    ' y)za -= bNr   r   s         r   ddm_isubr$   @   r"   r   c                D    | D ]  }t        |      D ]  \  }}| ||<     y)z
a  <--  -aNr   )r   r   r   aijs       r   ddm_inegr(   G   s3     m 	FAsDBqE	r   c                H    | D ]  }t        |      D ]  \  }}||z  ||<     y Nr&   r   r   r   r   r'   s        r   ddm_imulr,   N   s5     m 	FAs!GBqE	r   c                H    | D ]  }t        |      D ]  \  }}||z  ||<     y r*   r&   r+   s        r   	ddm_irmulr.   T   s5     m 	FAsGBqE	r   c           	         t        t        |       }t        ||       D ]9  \  }}t        |      D ]&  \  }}t        t	        t
        ||      ||         ||<   ( ; y)z
a += b @ cN)r   r   r   sumr   r   )r   r   ccTr   r   r   cTjs           r   ddm_imatmulr4   Z   s^     
c1gBa) 2Bm 	2FAsCS)2a51BqE	22r   c           	     ~    t               }|sg S t         d         }d}g }t        |      D ]
  |r,t        t        ||       fd      } |    |   c |<    |<    |      }|s2t        |dz   |      D ]  } |      }|s |    |   c |<    |<    n n |   }|dz  }	t        |      D ]  }
||
xx   |	z  cc<    t               D ]I  \  }}||k(  s|   s|   }|xx   |z  cc<   t        dz   |      D ]  }
||
xx   |||
   z  z  cc<    K |j	                         |dz  }||k\  s
 |S  |S )za  <--  rref(a)r   c                &    t        |             S r*   )abs)ipr   r   s    r   <lambda>zddm_irref.<locals>.<lambda>w   s    QrU1X r   )keyr   )lenrangemaxr   append)r   _partial_pivotmnipivotsr8   r'   r   aijinvlkakakjr   s   `             @r   	ddm_irrefrJ   e   s    	AA	AaD	A	AF1X . U1a[&>?BB%1KAaD!B% d1g AaCm eAh"#B%1KAaD!B%  qTbq! 	AqEVOE	 q\ 	%EArAvRUQ%CqESLE1Q3] %1r!u$%	% 	a	Q 6Ma.` Mr   c           	     4   t        |       }|s|j                  S t        | d         }|j                  }|j                  }t        |dz
        D ]  }| |   |   s@t        |dz   |      D ]   }| |   |   s| |   | |   c| |<   | |<   | } n |j                  c S |r| |dz
     |dz
     n|j                  }t        |dz   |      D ]F  }t        |dz   |      D ]2  }	 || |   |	   | |   |   z  | |   |   | |   |	   z  z
  |      | |   |	<   4 H  || d   d   z  S )za  <--  echelon(a); return detr   r   r;   )r<   oneexquor=   zero)
r   KrA   rB   rM   ufrG   rC   akkm1r   s
             r   ddm_idetrR      sX    	AAuuAaD	AGGE	
B1Q3Z JtAw1Q3] Q47!"1qtJAaD!A$B	 vv !A#qsaeeqsA 	JA1Q3] J!Q!Q!A$q'!A$q'/ A5I!QJ	JJ  "b	>r   c           	        |j                   st        d      t        |      }|sy t        |d         }||k7  rt        t	        |      D cg c]6  }t	        |      D cg c]  }||k(  r|j
                  n|j                  ! c}8 }}}t        ||      D 	cg c]
  \  }}	||	z    }
}}	t        |
      }|t        t	        |            k7  rt        d      |
D cg c]  }||d  	 c}| d d  y c c}w c c}}w c c}	}w c c}w )NzNot a fieldr   z Matrix det == 0; not invertible.)is_Field
ValueErrorr<   r   r=   rL   rN   r   rJ   r   r   )ainvr   rO   rA   rB   rC   r   eyeroweyerowAaugrD   s               r   ddm_iinvr[      s    ::'' 	AAAaD	AAv$$BG(
KQuQx8!QTAEEqvv%8
KC
K,/3K8[S&C&L8D8t_FeAh()KLL"&'3s12w'DG 9
K8 (s$   C5$C0C5C;D0C5c                    t        |      }|sg S t        |d         }t        |      }|j                  gt        ||      z  }t	        d|      D ]'  }t        ||      }||   d| | |   d| |d| ||   d| ) |S )zL, U  <--  LU(U)r   r   N)r<   ddm_ilurN   minr=   )	LUrO   rA   rB   swapszerosrC   r   s	            r   ddm_ilu_splitrc      s    AA	AaD	AAJEVVHs1ay E1a[ 1IQ48!Ra!9!Ra
 Lr   c           	        t        |       }|sg S t        | d         }g }t        t        ||            D ]  }| |   |   sCt        |dz   |      D ]0  }| |   |   s|j                  ||f       | |   | |   c| |<   | |<    n Nt        |dz   |      D ]H  }| |   |   | |   |   z  }|| |   |<   t        |dz   |      D ]  }| |   |xx   || |   |   z  z  cc<    J  |S )za  <--  LU(a)r   r   )r<   r=   r^   r?   )	r   rA   rB   ra   rC   r8   r   l_jirG   s	            r   r]   r]      s   AA	AaD	AE3q!9 *tAwAaCm R58LL!R)"#B%1KAaD!B%	 qsA 	*AQ47QqT!W$DAaDG1Q3] *!Q4!A$q'>)*	**  Lr   c                r   t        |      }|syt        |d         }t        |      }|st        d      t        |d         }||k7  rt        d      ||k  rt        d      |r-|D 	cg c]  }	|	dd 	 }}	|D ]  \  }
}||   ||
   c||
<   ||<    t        |      D cg c]  }dg|z  
 }}t        |      D ]F  }t        |      D ]6  }||   |   }t        |      D ]  }|||   |   ||   |   z  z  } |||   |<   8 H ||kD  r/t        ||      D ]   }t        |      D ]  }||   |   st         " t        |      D ]j  }t        t        |            D ]Q  }||   |   st        ||   |   }t        |dz   |      D ]  }|||   |   | |   |   z  z  } |||   |   z  | |   |<   S l yc c}	w c c}w )zx  <--  solve(L*U*x = swaps(b))Nr   zShape mismtchUnderdeterminedr   )r<   r   NotImplementedErrorr=   r   reversed)xr_   r`   ra   r   rA   rB   m2orX   i1i2_yrG   rC   rhsr   s                     r   ddm_ilu_solverr     s(   AAAaD	A	QB?++AaD	ABw?++1u!"344 !SV!! 	(FBR5!B%LAbE1R5	( #1X&$!&A&1X q 	AA$q'C1X )qtAw1a(()AaDG		 	1uq! 	5A1X 5Q47445	5 1X $%(# 	$AQ4700A$q'C1Q3] )qtAw1a(()AaDGmAaDG	$$) "
 	's   )F/F4c                   t        |       }|s|j                  ggS t        | d         }||k7  rt        d      |dk(  r|j                  g| d   d    ggS | d   d   }| d   dd  g}| dd  D cg c]  }|d   g
 }}| dd  D cg c]  }|dd  	 }}t        ||      }	t	        |dz         D 
cg c]  }
|j
                  g|z   }}
t	        |      D ]   }|j                  ||   |<   | ||dz      |<   " t	        d|dz         D ]w  }|dk(  r|}n)}|D cg c]  }|j
                  g }}t        |||       |j
                  gg}t        |||       t	        d|dz   |z
        D ]  }|d   d    |||z      |<    y t	        |dz         D 
cg c]  }
|j
                  g }}
t        |||	       |S c c}w c c}w c c}
w c c}w c c}
w )Nr   z
Not squarer      )r<   rL   r   ddm_berkr=   rN   r4   )MrO   rA   rB   r   r   rX   CAqro   r   rC   AnCRAnCr   qouts                    r   ru   ru   7  s   AAyAaD	AAv<((Av1Q47($$	!QA	
1ab
AqrU#c#a&#A#abE"SQR"A"AA$QqSz*!!&&A*A*1X %%!QB!A#q	 1ac] 
$6CA'()AFF8)C)Q"zD!S!q!A#a% 	$AaAacF1I	$
$ $AaCj)QVVH)D)aK/ 	$" 	+ * *s   .F3F8.F=GGN)r   zSequence[Sequence[T]]returnzlist[list[T]])r   list[list[R]]r   Sequence[Sequence[R]]r}   None)r   r~   r}   r   )r   r~   r   r   r}   r   )r   r~   r   r   r1   r   r}   r   )F)__doc__
__future__r   operatorr   
exceptionsr   r   r   typingr	   r
   sympy.polys.matrices._typingr   r   r   r   r!   r$   r(   r,   r.   r4   rJ   rR   r[   rc   r]   rr   ru    r   r   <module>r      s   #H #  
 % 4 CLC{#)
22.23H2	2;|D(($8-$`%r   