TITLE: Determinant of matrix with $2$'s and the pattern $3\ 1\ 3$
QUESTION [9 upvotes]: Let for $n\ge 1, D_n$ be the determinant of the $n\times n$ matrix $A_n$ where the entries along the main diagonal (i.e. of the form $(i,i)$ for $1\leq i\leq n$) are all $3$, entries of the form $(i,i+1)$ are equal to $1$, entries of the form $(i, i+2)$ are equal to $3$, and all other entries are $2$. Find, with proof, a formula for the determinant of $D_n$.

For instance, the matrix  $A_n$ for $n=4$  is shown below.
$$A_4 =\begin{pmatrix} 3 & 1 & 3 & 2 \\
2 & 3 & 1 & 3\\
2 & 2 & 3 & 1\\
2 & 2 & 2 & 3\end{pmatrix}.$$
I found the following formula after a bit of experimentation, but I wasn't able to prove it: $$D_n = 1 + 12\lfloor \frac{n}6\rfloor + \begin{cases}2,&\text{ if $n\equiv 1\bmod 6$}\\
6,&\text{ if $n\equiv 2\bmod 6$}\\
10, &\text{ if $n\equiv 3\bmod 6$}\\
12, &\text{ if $n\equiv 4 $ or $5\bmod 6$}\\
0,&\text{ otherwise}\end{cases}.$$
One can define $D_0 := 1$. I know $D_2 = 7$ and using row operations. So far, I've come up with the following sequence of operations, but I seem to be stuck after the last step. In the description below, $R_i$ represents the $i$th row and $C_i$ represents the ith column of $A_n$. Suppose $n > 2$.
Perform the operation $R_i \mapsto R_i - R_n$ for $2\leq i < n.$ Then the resulting matrix has all zeroes for entries of the form $(i, j)$ for $i < j$ that are not on the last row. The entries on the last row are $n-1$ $2$'s followed by a $3$. Also, entries along the main diagonal are all $1$'s and the only entry of the form $(i,i+1)$ where $2\leq i <n$ that does not equal $-1$ is $(n-1,n)$, and that entry equals $-2$. Entries of the form $(i,i+2)$ where $2\leq i < n-2$ are equal to one while entry $(n-2,n)$ equals $0$ if it exists.
Now perform the operations $C_i \mapsto C_i - C_1$ for $2\leq i < n$. This removes all the $2$'s on the last row except for the first, and the entries on the top row are $3, -2,0$, followed by $-1$'s and ending with $2$ (the sequence stops whenever $n$ is reached, so if $n=3$, the entries would be $3,-2,0$).
Finally, perform the operations $R_i \mapsto R_i + R_{i+1}$ for $2\leq i\le n-2$.
After performing these operations, for $n=5$, the following matrix is obtained:
$$\begin{pmatrix}
3& -2 & 0 & -1 & 2\\
0 & 1 & 0 & 0 & 2\\
0 & 0 & 1 & 0  & -2\\
0 & 0 & 0 & 1 & -2\\
2 & 0 & 0 & 0 & 3
\end{pmatrix}.$$
I can't seem to "zero out" the $2$ in the bottom left corner, despite trying various row and column operations. I think I should split the proof into cases based on the remainder when $n$ is divided by $6$, which is suggested by the formula at the beginning.
I tried considering other approaches such as finding eigenvalues and deriving a recurrence relation for $D_n$, but those approaches didn't seem to help much.

REPLY [3 votes]: Let $e$ be the vector of ones and $N$ be the full-sized nilpotent Jordan block. Since $A_n$ is a rank-one update of $B_n=I-N+N^2$, by Sylvester's determinant lemma we get
$$
\det(A_n)=\det(B_n+2ee^T)=\det(B_n)(1+2e^TB_n^{-1}e)=1+2e^TB_n^{-1}e.
$$
So, we need to find the sum of all entries in $B_n^{-1}$. Since $B_n$ is a polynomial in $N$, so must be its inverse. Let $B_n^{-1}=c_0I+c_1N+c_2N^2+\cdots+c_{n-1}N^{n-1}$. The equation $B_nB_n^{-1}=I$ gives the recurrence relation $c_0=c_1=1$ and $c_k=c_{k-1}-c_k$, whose solution is given by a repetition of the pattern $(1,1,0,-1,-1,0)$. So, when $n=6q+r$, we have
\begin{aligned}
B_n^{-1}
&=(I+N-N^3-N^4)+(N^6+N^7-N^9-N^{10})+\cdots\\
&\quad+(N^{6q-6}+N^{6q-5}-N^{6q-3}-N^{6q-2})\\
&\quad+
\begin{cases}
0&\text{if }r=0,\\
N^{6q}&\text{if }r=1,\\
N^{6q}+N^{6q+1}&\text{if }r=2,3,\\
N^{6q}+N^{6q+1}-N^{6q+3}&\text{if }r=4,\\
N^{6q}+N^{6q+1}-N^{6q+3}-N^{6q+4}&\text{if }r=5.\\
\end{cases}
\end{aligned}
Since the sum of all entries in $N^k+N^{k+1}-N^{k+2}-N^{k+3}$ is equal to $6$, we get
\begin{aligned}
e^TB_n^{-1}e
&=\begin{cases}
6q&\text{if }r=0,\\
6q+r&\text{if }r=1,\\
6q+r+(r-1)&\text{if }r=2,3,\\
6q+r+(r-1)-(r-3)&\text{if }r=4,\\
6q+r+(r-1)-(r-3)-(r-4)&\text{if }r=5\\
\end{cases}\\
&=\begin{cases}
n&\text{if }r=0,1,\\
n+1&\text{if }r=2,5,\\
n+2&\text{if }r=3,4.\\
\end{cases}
\end{aligned}
Thus
\begin{aligned}
\det(A_n)=1+2e^TB_n^{-1}e
&=\begin{cases}
2n+1&\text{if }r=0,1,\\
2n+3&\text{if }r=2,5,\\
2n+5&\text{if }r=3,4.\\
\end{cases}
\end{aligned}
PS. The form $B_n=I-N+N^2$ suggests that there might be a simpler solution that uses generating functions, but I am satisfied with my current answer here.