Simple (possibly silly) question: why are IdentityMatrix, DiagonalMatrix, ConstantArray defined as List and not SparseArray by default?

I suppose it is due to symbolic computation but I am not sure.

Just to illustrate the question:

IdentityMatrix[10000]; // AbsoluteTiming
SparseArray[{{i_, i_} -> 1}, 10000]; // AbsoluteTiming

Of course, SparseArray objects also require much less memory.

{0.113563, Null}

{0.000081, Null}

  • 5
    $\begingroup$ Mathematica certainly had these functions before sparse arrays were introduced, so a desire for backwards compatibility might explain it. $\endgroup$
    – mikado
    Jun 8, 2016 at 19:32
  • 1
    $\begingroup$ If one is going to be doing many updates to a sparse array, they have their performance downsides.... $\endgroup$
    – ciao
    Jun 8, 2016 at 19:57
  • 10
    $\begingroup$ You can use IdentityMatrix[10000, SparseArray] or DiagonalMatrix[SparseArray[{1, 2}]].and ConstantArray[1, {3, 3}, SparseArray] $\endgroup$
    – user21
    Jun 8, 2016 at 21:14
  • 1
    $\begingroup$ @anderstood, that's why I put it in as a comment. $\endgroup$
    – user21
    Jun 8, 2016 at 22:04
  • 4
    $\begingroup$ The answer to your question is backwards compatibility. $\endgroup$
    – user21
    Jun 8, 2016 at 22:04

2 Answers 2


The fastest way to get the identity matrix as a sparse array is simply this:

IdentityMatrix[10000, SparseArray]; // AbsoluteTiming

Here are just some thoughts on why SparseArray is not the default for IdentityMatrix:

You don't always want SparseArray output when defining matrices, and it's not always possible to decide automatically whether a SparseArray should be cast into List form (simple example: TraditionalForm or other displays). Therefore, when SparseArray was introduced, it could not simply replace the existing representation of arrays as lists. In particular, this holds for IdentityMatrix which is after all of use not only in high-dimensional spaces but also at low dimensionality where SparseArray doesn't represent the most efficient way of storing and manipulating arrays. For more potential reasons justifying the default "non-sparseness" of IdentityMatrix, see the comments.

  • $\begingroup$ But is there evident reason why this is not the default behaviour? $\endgroup$
    – anderstood
    Jun 8, 2016 at 21:45
  • 1
    $\begingroup$ @anderstood I think it's a very reasonable behavior, but whether it's "evident" may be a matter of taste, see my update. $\endgroup$
    – Jens
    Jun 8, 2016 at 21:59

SparseArray objects are somewhat special and some of the access operations can be slower with them. IdentityMatrix though does return a more compact object than just a list of lists -- it returns a packed array.


 Table[PackedArrayQ[IdentityMatrix[n]], {n, Range[1, 1000, 10]}]]

(* {{True, 100}}*)

Also, it is really easy to covert the matrices of IdentityMatrix into sparse arrays:

Table[SparseArray[IdentityMatrix[n]], {n, Range[100, 1000, 100]}]

identity matrices


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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