# Inverse of Sparse Diagonal Array not Sparse

Kind of bummed that when I take the inverse of a matrix that is a diagonal sparse array, the result in not a sparse array. Further, the time to compute the inverse is the same as the time to compute the inverse of a normal array.

Just checking to see if I am missing some obvious switch or setting or method that will let MMa speed up the process. I know I can invert the elements of the list that I am building the diagonal matrix from prior, and get the result I want.

v = DiagonalMatrix@SparseArray[Range[5000] // N]
res = Inverse@v; // Timing
(* {3.6875, Null} *)


res is not a sparse array. The time to create it is the same as if it had never been sparse in the first place.

v = DiagonalMatrix@Range[5000] // N
res = Inverse@v; // Timing
(* {3.625, Null} *)


Manually inverting the list I am building the diagonal matrix from. This is the performance I was hoping to see. Also, was hoping to get a sparse matrix on the output.

res = SparseArray@(1/Range[50000]); // Timing
(* {0.125, Null} *)


res is by creation a sparse array.

• This seems relevant: Efficient way of sparse matrix inversion – Lukas Lang Jun 10 at 17:35
• Hm. I don't see why Inverse should check for diagonal matrices first: It is a rather seldom case in which we may expect the user to know that they are about to invert a diagonal matrix. In any case, I suggest to use LinearSolve instead of Inverse in almost all applications, where the matrix size is bigger than, say $5 \times 5$. – Henrik Schumacher Jun 10 at 18:18
• I've a problem that involves inverting a matrix (my diagonal one), adding it to another, inverting their sum, and then summing up the elements of that inverted matrix. So there's not really a place for LinearSolve in the problem. Not a great bother to work around it, was just mildly surprised that there was no way to signal that the Inverse was sparse too, and to tell Mathematica to attempt to keep it sparse. – MikeY Jun 10 at 18:33