My graph looks like:
\begin{equation*} X =
\begin{pmatrix}
1 & -1 & 0 & 0 \\
1 & -1 & 0 & 0 \\
1 & 0 & 0 & -1 \\
1 & 0 & 0 & -1 \\
0 & 1 & -1 & 0 \\
0 & 1 & 0 & -1 \\
0 & 0 & 1 & -1 \\
0 & 0 & 1 & -1
\end{pmatrix}
\end{equation*}
\begin{equation*}
Y =
\begin{pmatrix}
1 \\
4 \\
28 \\
4 \\
1 \\
28 \\
7 \\
2
\end{pmatrix}
\end{equation*}
\begin{equation*}
M =
\begin{pmatrix}
4 & -2 & -1 & -1 \\
-2 & 4 & 1 & 1 \\
-1 & -1 & 4 & -2 \\
1 & 1 & 1 & 1 \\
\end{pmatrix}
\end{equation*}
\begin{equation*}
P =
\begin{pmatrix}
37 \\
24 \\
-20 \\
0
\end{pmatrix}
\end{equation*}
\begin{equation*}
r =
\begin{pmatrix}
8.70833333333 \\
6.54166666667 \\
-5.875 \\
-9.375
\end{pmatrix}
\end{equation*}
Below is my code:
X = Matrix([
[1,-1,0,0],
[1,-1,0,0],
[1,0,-1,0],
[1,0,0,-1],
[0,1,-1,0],
[0,1,0,-1],
[0,0,1,-1],
[0,0,1,-1]
])
y = Matrix([[1,4,28,4,1,28,7,2]]).transpose()
XT = X.transpose()
M = XTX
n = M.dimensions()[0]
for i in range(n):
M[n-1,i] = 1
p = XTy
p[n-1,0] = 0
r = M.solve_right(p)
pretty_print(“M=”, M)
pretty_print(“p=”, p)
pretty_print(“r=”, r)