<s>[INST] What is inverse of a matrix whose diagonal elements are all zero. I am a science researcher and I got a problem to find the generic inverse of the following matrix:
$$ 
A_n = \left(\begin{array}{ccc}
        0 & a_2 & a_3 & ... & a_{n-1} & a_n \\
      a_1 &   0 & a_3 & ... & a_{n-1} & a_n \\
      a_1 & a_2 &   0 & ... & a_{n-1} & a_n \\
      ... & ... & ... & ... & ...     & ... \\
      ... & ... & ... & ... & ...     & ... \\
      a_1 & a_2 & a_3 & ... &       0 & a_n \\
      a_1 & a_2 & a_3 & ... & a_{n-1} &   0 \\
      \end{array}\right) 
$$
I figured out that for n=2,3
$$
A_2^{-1} = \left(\begin{array}{ccc}
        0 & a_2^{-1}  \\
      a_1^{-1} &   0  \\
      \end{array}\right) 
$$
$$
A_3^{-1} = \frac{1}{2} \left(\begin{array}{ccc}
        -a_1^{-1} &  a_1^{-1} &  a_1^{-1} \\
         a_2^{-1} & -a_2^{-1} &  a_2^{-1} \\
         a_3^{-1} &  a_3^{-1} & -a_3^{-1} \\
      \end{array}\right) 
$$
but can we extend it to a general case?
Can anyone help?? Thanks!
[later]
It looks like it is
$$
m_{ij} =  -\frac{n-2}{n-1} a_{i}^{-1}\ (i=j)  
$$
$$
       = \frac{1}{n-1} a_{i}^{-1}(else)
$$
 [/INST] Your matrix can be written as $D_n + u_n \, v_n^T$:
$$u_n = \left[ \begin{matrix}  1 & ... & 1 \end{matrix} \right]^T$$
$$v_n = \left[ \begin{matrix}  a_1 & ... & a_n \end{matrix} \right]^T$$
$$D_n = - \text{diag}(v_n) = - \left[ \begin{matrix}
a_1 & ... & 0 \\
\vdots & ... & \vdots \\
0 & ... & a_n \\
 \end{matrix} \right]$$
Which reminds me of the Sherman–Morrison formula:
$$\left(A + u \, v^T\right)^{-1} = A^{-1} - {A^{-1} \, u \, v^T \, A^{-1} \over 1 + v^T \, A^{-1} \, u}$$
[EDIT]
Your pattern seems to be right. Defining $w_n = \left[ \begin{matrix}  a_1^{-1} & ... & a_n^{-1} \end{matrix} \right]^T$:
$$ $$
$$1 + v^T \, A^{-1} \, u = 1-n$$
$$A^{-1} \, u = - w_n$$
$$ v^T \, A^{-1} = - u_n^T$$
$$\left(A + u \, v^T\right)^{-1} = - \text{diag}(w_n) - { w_n \, u_n^T \over 1-n}$$
$$ \left(A + u \, v^T\right)^{-1} =
- \left[ \begin{matrix}
a_1^{-1} & ... & 0 \\
\vdots & ... & \vdots \\
0 & ... & a_n^{-1} \\
 \end{matrix} \right]
-
\frac{1}{1-n} 
\left[ \begin{matrix}
a_1^{-1} & ... & a_1^{-1} \\
\vdots & ... & \vdots \\
a_n^{-1} & ... & a_n^{-1} \\
 \end{matrix} \right]
$$
$$ $$
$$
m_{ij} = -a_{i}^{-1} -\frac{a_{i}^{-1}}{1-n} = \frac{n-2}{1-n} \, a_{i}^{-1} = - \frac{n-2}{n-1} \, a_{i}^{-1} \, (i=j)
$$
$$
 = -\frac{a_{i}^{-1}}{1-n} = \frac{1}{n-1} \, a_{i}^{-1} \, (else) \hphantom{aaaaaaaaaaaaaaa}
$$
</s>