# Is there a fast way to get the diagonal of the inverse of a banded Hermitian matrix?

Since inverting a matrix is often a bad idea, I am wondering if there is a better way to get just the diagonal of the inverse without inverting the whole thing. Note I need the whole diagonal, not the trace. For my application I need to compute the sums of pairs of terms on the diagonal (I'm not sure if there's a name for this, clearly not the same thing as the partial trace).

Edit:

If it makes a difference, the matrix is Hermitian. It has nonzero elements in the first three bands off the main diagonal, and ranges from 8×8 to 40×40. I'm also working with other matrices that have a similar form

$$\begin{pmatrix} 2 & 2\,c_{xy} & e^{-ix/4} & e^{-iy/4} & 0 & 0 & 0 & 0 \\[4pt] 2\,c_{xy} & 2 & e^{iy/4} & e^{ix/4} & 0 & 0 & 0 & 0 \\[4pt] e^{ix/4} & e^{-iy/4} & 2 & 2\,c_{xy} & e^{-ix/4} & e^{iy/4} & 0 & 0 \\[4pt] e^{iy/4} & e^{-ix/4} & 2\,c_{xy} & 2 & e^{-iy/4} & e^{ix/4} & 0 & 0\\[4pt] 0 & 0 & e^{ix/4} & e^{iy/4} & 2 & 2\,c_{xy} & e^{-ix/4} & e^{-iy/4} \\[4pt] 0 & 0 & e^{-iy/4} & e^{-ix/4} & 2\,c_{xy} & 2 & e^{iy/4} & e^{ix/4} \\[4pt] 0 & 0 & 0 & 0 & e^{ix/4} & e^{-iy/4} & 2 & 2\,c_{xy} \\[4pt] 0 & 0 & 0 & 0 & e^{iy/4} & e^{-ix/4} & 2\,c_{xy} & 2 \end{pmatrix}$$

where $c_{xy} = \cos\left(\frac{x+y}{4}\right)$.

• @Vsevolod, which changes the question to "what's a fast way to get the diagonal of the adjoint?". – J. M. will be back soon Mar 26 '18 at 5:02
• Does your matrix have anything special about it (sparse, symmetric, etc.)? – J. M. will be back soon Mar 26 '18 at 5:03
• While finding the adjoint of a matrix is not built-in, it seems to be defined in the Applications section of Minors. Not sure if it is the way to go though. – Kiro Mar 26 '18 at 5:23
• The complexity of Cramer's rule for all diagonal elements of the inverse of a general, dense $n \times n$ matrix A is n (n-1)^(n-1) + n^n while LU-decomposition has complexity n^3. The latter can be obtained with Diagonal[LinearSolve[A, IdentityMatrix[n, SparseArray]]]. – Henrik Schumacher Mar 26 '18 at 8:56
• But quite surprisingly, Diagonal[Inverse[A]] is faster... (n = 1000) – Henrik Schumacher Mar 26 '18 at 9:01

Proceed a similar way like @Vsevolod A. proposed.

Calculate the determinant of the submatrix of diagonal elements of the original matrix and divide by the total determinant.

subm[A_, diapos_] := Drop[A, {diapos}, {diapos}]

dialist[A_] := Table[Det[subm[A, pos]], {pos, 1, First@Dimensions[A]}]/Det[A]


Here the proof.

dialist[Array[Subscript[a, ##] &, {3, 3}]] ==
Diagonal[Inverse[Array[Subscript[a, ##] &, {3, 3}]]]

(*   True   *)

dialist[Array[Subscript[a, ##] &, {8, 8}]] ==
Diagonal[Inverse[Array[Subscript[a, ##] &, {8, 8}]]]

(*   True   *)

• Thanks, unfortunately this appears to be quite a bit slower for my purpose compared to simply inverting the matrix. I have added some detail to the question, I don't know if it will help to find a faster solution. – Kai Mar 26 '18 at 18:47