Diagonal Matrix

Author

Eric W. Weisstein
January 3, 2021

This notebook downloaded from http://mathworld.wolfram.com/notebooks/LinearAlgebra/DiagonalMatrix.nb.

For more information, see Eric's MathWorld entry http://mathworld.wolfram.com/DiagonalMatrix.html.

©2021 Wolfram Research, Inc. except for portions noted otherwise

Example

DiagonalMatrix[Array[a,5]]/.a[i_]:>Subscript[a,i]//TraditionalForm

Sparse diagonal matrix

With[{d=Range[5]},SparseArray[{i_,i_}:>d[[i]],Length[d] {1,1}]
]

Normal[%]

Matrix Power

DiagonalMatrix[Array[a,5]]^4==DiagonalMatrix[Array[a,5]^4]

Matrix Exponential

MatrixExp[DiagonalMatrix[Array[a,5]]]==DiagonalMatrix[Exp[Array[a,5]]]

Determinants

Det/@DiagonalMatrix/@Reverse[NestList[Most,Range[10],9]]

Edge case

MatrixQ[{{}}]

DiagonalMatrixQ[{{}}]

Tuples[{0,1},0]

Tuples[{0,1},1]

Tuples[{0,1},{0}]

Tuples[{0,1},{1,1}]

Tuples[{0,1},{0,0}]

Tuples[{0,1},{0,0,0}]

From mtrott 2020-01-03:

No strong opinion.

I would have expected {} for both as as there are no tuples here.

but I could also see an argument for {{}} if we consider {} a 0-element tuple.

And I could also argue for your {{{}}}.

(0, 1)-matrices

Closed form

A000079

2^Range[0,20]

Table[2^n,{n,0,20}]

Compute

Table[Count[Tuples[{0,1},{n,n}],_?DiagonalMatrixQ],{n,0,4}]

Table[MatrixForm/@Select[Tuples[{0,1},{n,n}],DiagonalMatrixQ],{n,3}]//Column

Do[Print[{n,MaxMemoryUsed[c=Count[Tuples[{0,1},{n,n}],_?DiagonalMatrixQ]]1.`3*^-9GB//Timing,c}],{n,5}]

(-1, 1)-matrices

Closed form

Piecewise[{{2,n==1}}]

Compute

Table[Count[Tuples[{-1,1},{n,n}],_?DiagonalMatrixQ],{n,0,4}]

Table[MatrixForm/@Select[Tuples[{-1,1},{n,n}],DiagonalMatrixQ],{n,3}]//Column

Do[Print[{n,MaxMemoryUsed[c=Count[Tuples[{-1,1},{n,n}],_?DiagonalMatrixQ]]1.`3*^-9GB//Timing,c}],{n,5}]

(-1, 0, 1)-matrices

Closed form

A047656

Compute

Table[Count[Tuples[{-1,0,1},{n,n}],_?DiagonalMatrixQ],{n,3}]

Table[MatrixForm/@Select[Tuples[{-1,0,1},{n,n}],DiagonalMatrixQ],{n,3}]//Column

Do[Print[{n,MaxMemoryUsed[c=Count[Tuples[{-1,0,1},{n,n}],_?DiagonalMatrixQ]]1.`3*^-9GB//Timing,c}],{n,4}]