In [35]:
from sympy import *
from IPython.display import display
init_printing(use_latex='mathjax')
height = lambda M: len(M[:,0])
width = lambda M: len(M[0,:])

In [36]:
Ai=Matrix([[1,2,1,0,0],[3,4,0,1,0],[5,6,0,0,1]])
h=height(Ai)
w=width(Ai)-height(Ai)
A=Ai[:,0:w]
display(A)
display(Ai)
Ar,pivots=Ai.rref()
display(Ar)
L=Ar[:,0:w]
R=Ar[:,w:w+h]
display(L.transpose(),R)
B=L.transpose()*R
display(B)
display(B*A)
display(A*B)

$$\left[\begin{matrix}1 & 2\\3 & 4\\5 & 6\end{matrix}\right]$$
$$\left[\begin{matrix}1 & 2 & 1 & 0 & 0\\3 & 4 & 0 & 1 & 0\\5 & 6 & 0 & 0 & 1\end{matrix}\right]$$
$$\left[\begin{matrix}1 & 0 & 0 & -3 & 2\\0 & 1 & 0 & \frac{5}{2} & - \frac{3}{2}\\0 & 0 & 1 & -2 & 1\end{matrix}\right]$$
$$\left[\begin{matrix}1 & 0 & 0\\0 & 1 & 0\end{matrix}\right]$$
$$\left[\begin{matrix}0 & -3 & 2\\0 & \frac{5}{2} & - \frac{3}{2}\\1 & -2 & 1\end{matrix}\right]$$
$$\left[\begin{matrix}0 & -3 & 2\\0 & \frac{5}{2} & - \frac{3}{2}\end{matrix}\right]$$
$$\left[\begin{matrix}1 & 0\\0 & 1\end{matrix}\right]$$
$$\left[\begin{matrix}0 & 2 & -1\\0 & 1 & 0\\0 & 0 & 1\end{matrix}\right]$$
In [32]:
Ai=Matrix([[1,2,3,1,0],[4,5,6,0,1]])
h=height(Ai)
w=width(Ai)-height(Ai)
A=Ai[:,0:w]
display(A)
display(Ai)
Ar,pivots=Ai.rref()
display(Ar)
P=Ar[:,0:w]
for i in range(h):
for j in range(w):
if j not in pivots:
P[i,j]=0
R=Ar[:,w:w+h]
display(P.transpose(),R)
B=P.transpose()*R
display(B)
display(A*B)
display(B*A)

$$\left[\begin{matrix}1 & 2 & 3\\4 & 5 & 6\end{matrix}\right]$$
$$\left[\begin{matrix}1 & 2 & 3 & 1 & 0\\4 & 5 & 6 & 0 & 1\end{matrix}\right]$$
$$\left[\begin{matrix}1 & 0 & -1 & - \frac{5}{3} & \frac{2}{3}\\0 & 1 & 2 & \frac{4}{3} & - \frac{1}{3}\end{matrix}\right]$$
$$\left[\begin{matrix}1 & 0\\0 & 1\\0 & 0\end{matrix}\right]$$
$$\left[\begin{matrix}- \frac{5}{3} & \frac{2}{3}\\\frac{4}{3} & - \frac{1}{3}\end{matrix}\right]$$
$$\left[\begin{matrix}- \frac{5}{3} & \frac{2}{3}\\\frac{4}{3} & - \frac{1}{3}\\0 & 0\end{matrix}\right]$$
$$\left[\begin{matrix}1 & 0\\0 & 1\end{matrix}\right]$$
$$\left[\begin{matrix}1 & 0 & -1\\0 & 1 & 2\\0 & 0 & 0\end{matrix}\right]$$
In [34]:
Ai=Matrix([[1,2,4,1,0,0],[3,5,6,0,1,0],[7,8,9,0,0,1]])
h=height(Ai)
w=width(Ai)-height(Ai)
A=Ai[:,0:w]
display(A)
display(Ai)
Ar,pivots=Ai.rref()
display(Ar)
L=Ar[:,0:w]
R=Ar[:,w:w+h]
display(L.transpose(),R)
B=L.transpose()*R
display(B)
display(B*A)
display(A*B)

$$\left[\begin{matrix}1 & 2 & 4\\3 & 5 & 6\\7 & 8 & 9\end{matrix}\right]$$
$$\left[\begin{matrix}1 & 2 & 4 & 1 & 0 & 0\\3 & 5 & 6 & 0 & 1 & 0\\7 & 8 & 9 & 0 & 0 & 1\end{matrix}\right]$$
$$\left[\begin{matrix}1 & 0 & 0 & \frac{3}{17} & - \frac{14}{17} & \frac{8}{17}\\0 & 1 & 0 & - \frac{15}{17} & \frac{19}{17} & - \frac{6}{17}\\0 & 0 & 1 & \frac{11}{17} & - \frac{6}{17} & \frac{1}{17}\end{matrix}\right]$$
$$\left[\begin{matrix}1 & 0 & 0\\0 & 1 & 0\\0 & 0 & 1\end{matrix}\right]$$
$$\left[\begin{matrix}\frac{3}{17} & - \frac{14}{17} & \frac{8}{17}\\- \frac{15}{17} & \frac{19}{17} & - \frac{6}{17}\\\frac{11}{17} & - \frac{6}{17} & \frac{1}{17}\end{matrix}\right]$$
$$\left[\begin{matrix}\frac{3}{17} & - \frac{14}{17} & \frac{8}{17}\\- \frac{15}{17} & \frac{19}{17} & - \frac{6}{17}\\\frac{11}{17} & - \frac{6}{17} & \frac{1}{17}\end{matrix}\right]$$
$$\left[\begin{matrix}1 & 0 & 0\\0 & 1 & 0\\0 & 0 & 1\end{matrix}\right]$$
$$\left[\begin{matrix}1 & 0 & 0\\0 & 1 & 0\\0 & 0 & 1\end{matrix}\right]$$
In []: