Solution (source code)

= Solution

First add row 1 to rows 2 and 3. This is the product of two <elementary matrices>, written compactly as
$$
E_1=\begin{pmatrix}1&0&0&0\\1&1&0&0\\1&0&1&0\\0&0&0&1\end{pmatrix},\qquad E_1A=\begin{pmatrix}1&-1&0&1\\0&0&0&2\\0&-1&1&2\\0&-2&-1&2\end{pmatrix}.
$$
Swap rows 2 and 4 with $P$, then subtract half of the new row 2 from row 3 with $E_2$:
$$
P=\begin{pmatrix}1&0&0&0\\0&0&0&1\\0&0&1&0\\0&1&0&0\end{pmatrix},\qquad E_2=\begin{pmatrix}1&0&0&0\\0&1&0&0\\0&-1/2&1&0\\0&0&0&1\end{pmatrix}.
$$
This gives the upper triangular matrix
$$
\boxed{U=E_2PE_1A=\begin{pmatrix}1&-1&0&1\\0&-2&-1&2\\0&0&3/2&1\\0&0&0&2\end{pmatrix}.}
$$
Because $P^{-1}=P$, rearranging yields the convention \b[$A=PLU$], where
$$
\boxed{L=PE_1^{-1}PE_2^{-1}=\begin{pmatrix}1&0&0&0\\0&1&0&0\\-1&1/2&1&0\\-1&0&0&1\end{pmatrix}.}
$$
The conjugation by $P$ moves the earlier elimination multipliers to the appropriate rows. Ignoring this permutation when forming $L$ would give an incorrect <PLU factorization>. Here the same permutation is its own inverse, so $PA=LU$ also holds.