# Why is IdentityMatrix not defined as a SparseArray by default?

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}

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

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.

• But is there evident reason why this is not the default behaviour? – anderstood Jun 8 '16 at 21:45
• @anderstood I think it's a very reasonable behavior, but whether it's "evident" may be a matter of taste, see my update. – Jens Jun 8 '16 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.

Needs["Developer"]

Tally[
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]}]
`