Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Given a large (say 10x10) sparse Hermitian matrix with symbolic entries, I need four specific entries of the inverse matrix. It seems wasteful to me to compute 100 entries of which I only need a few in the end.

Is there a way to compute a small number of specific entries of the inverse matrix that is significantly more efficient than finding the inverse matrix and then extracting the elements?

Here's an example. N is half the dimension of the matrix. For N = 8, inverting takes 51 seconds on my university's computer, and we would ideally like to reach much larger N.

A = Table[If[Abs[i - j] == 1, (j - i) a, 0], {i, #}, {j, #}] &;
B = Table[If[Abs[i - j] == 1, b, 0], {i, #}, {j, #}] &;

\[Sigma]1[N_] := B@N + Table[
    If[Abs[i - j] == 0, 2 \[Mu], 0] + If[i == j == 1, -x, 0] + 
     If[i == j == N, -x, 0]
    , {i, N}, {j, N}];
\[Sigma]2[N_] := -B@N + Table[
    +If[i == j == 1, -y, 0] + If[i == j == N, -y, 0]
    , {i, N}, {j, N}];

(* Elements of M *)
el[N_, i_, j_] := Which[i <= N && j <= N,
  \[Sigma]1[N][[i, j]], i > N && j > N,
  \[Sigma]2[N][[i - N, j - N]], i > N && j <= N,
  A[N][[i - N, j]], i <= N && j > N,
  -A[N][[i, j - N]]] 

M[N_] := Table[el[N, i, j], {i, 2 N}, {j, 2 N}];

Daniel Lichtblau's idea LinearSolve[M@8,UnitVector[16,8]] takes 4.1 seconds, so it is certainly much faster than inverting the entire matrix, even if it has to be done seperately for each entry.

share|improve this question
If you have ACM access, search for Lin Lin et al SelInv paper. – ciao Mar 24 '14 at 21:22
As it is stated, the Q is off-topic. – Sektor Mar 24 '14 at 21:43
@bobthechemist I have to disagree here. It is clear that you can do an Inverse matrix calculation in Mathematica, and checking to see whether a fast version of Inverse[m][[{3,4},{2,1}]] exists is not unreasonable. It can be very advantageous. Many functions in Mathematica can be 'cut-off' to stop early and give a restricted set of solutions. Take, for instance, Ordering or Position. – Sjoerd C. de Vries Mar 24 '14 at 22:28
LinearSolve[mat,ej] will give the jth column only of the inverse, when ej is the jth coordinate axis unit vector. I would expect that to be 2-3 times faster than using Inverse. – Daniel Lichtblau Mar 24 '14 at 22:37
For multiple right hand sides, maybe do 'LinearSolve[mat]' and use the resulting 'LinearSolveFunction' on different vectors. – Daniel Lichtblau Mar 31 '14 at 3:22
up vote 0 down vote accepted

Looking at, the lines below avoid calculating more than needed (less than 1 sec on M[8]):

inverseElement[mat_, i_,  j_] /; (And @@ Thread[{i, j} <= Dimensions[mat]]) := 
 Cofactor[mat, j, i]/Det[mat];
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.