DiagonalMatrixQ[m,k]
gives True if m has nonzero elements only on the k
diagonal, and False otherwise.
DiagonalMatrixQ
DiagonalMatrixQ[m,k]
gives True if m has nonzero elements only on the k
diagonal, and False otherwise.
Details and Options
- DiagonalMatrixQ[m,k] works even if m is not a square matrix.
- In DiagonalMatrixQ[m,k], positive k refers to superdiagonals above the main diagonal and negative k refers to subdiagonals below the main diagonal.
- DiagonalMatrixQ works with SparseArray and structured array objects.
- The following option can be given:
-
Tolerance Automatic numerical tolerance to use - For approximate matrices, the option Tolerance->t can be used to indicate that all entries Abs[mij]≤t are taken to be zero. »
Examples
open all close allBasic Examples (3)
DiagonalMatrixQ[(| | | |
| - | - | - |
| a | 0 | 0 |
| 0 | b | 0 |
| 0 | 0 | c |)]DiagonalMatrixQ[(| | | |
| - | - | - |
| 1 | 0 | 0 |
| 0 | 0 | 2 |
| 3 | 0 | 0 |)]MatrixForm[{{0, a, 0}, {0, 0, b}, {0, 0, 0}}]DiagonalMatrixQ[%, 1]MatrixForm[{{0, 0, 0}, {a, 0, 0}, {0, b, 0}}]DiagonalMatrixQ[%, -1]Scope (12)
Basic Uses (8)
Test rectangular diagonal matrices:
(mat23 = {{1, 0, 0}, {0, 2, 0}})//MatrixFormDiagonalMatrixQ[mat23](mat32 = {{1, 0}, {0, 2}, {0, 0}})//MatrixFormDiagonalMatrixQ[mat32]Use DiagonalMatrixQ with a symbolic matrix:
DiagonalMatrixQ[{{a, b}, {c, d}}]The matrix becomes diagonal when
:
Block[{b = c = 0}, DiagonalMatrixQ[{{a, b}, {c, d}}]]Test if a real machine-precision matrix is diagonal:
m = {{1.2, 0, 0}, {0, 2.4, 0}, {0, 0, 3.5}};DiagonalMatrixQ[m]Test if a complex matrix is diagonal:
m = {{1, -3I}, {0, 4}};DiagonalMatrixQ[m]The real part of the matrix is diagonal and the imaginary part is superdiagonal:
{DiagonalMatrixQ[Re[m]], DiagonalMatrixQ[Im[m], 1]}Test if an exact matrix is diagonal:
m = (| | | |
| - | - | ------- |
| 0 | 5 | 4 |
| 3 | 1 | Sqrt[2] |
| 3 | 5 | Pi |);DiagonalMatrixQ[m]Use DiagonalMatrixQ with an arbitrary-precision matrix:
m = RandomReal[5, {3, 3}, WorkingPrecision -> 15]A random matrix is typically non-diagonal:
DiagonalMatrixQ[m]Test if matrices have nonzero entries only on a particular superdiagonal:
DiagonalMatrixQ[(| | | |
| - | - | - |
| 0 | 1 | 0 |
| 0 | 0 | 2 |
| 0 | 0 | 0 |), 1]DiagonalMatrixQ[(| | | |
| - | - | - |
| 0 | 1 | 0 |
| 0 | 0 | 2 |
| 0 | 0 | 0 |), 2]Note that the matrix is not diagonal:
DiagonalMatrixQ[(| | | |
| - | - | - |
| 0 | 1 | 0 |
| 0 | 0 | 2 |
| 0 | 0 | 0 |)]Test if matrices have nonzero entries only on a particular subdiagonal:
DiagonalMatrixQ[(| | | |
| - | - | - |
| 0 | 0 | 0 |
| 0 | 0 | 0 |
| 3 | 0 | 0 |), -1]DiagonalMatrixQ[(| | | |
| - | - | - |
| 0 | 0 | 0 |
| 0 | 0 | 0 |
| 3 | 0 | 0 |), -2]Note that the matrix is not diagonal:
DiagonalMatrixQ[(| | | |
| - | - | - |
| 0 | 0 | 0 |
| 0 | 0 | 0 |
| 3 | 0 | 0 |)]Special Matrices (4)
Use DiagonalMatrixQ with sparse matrices:
SparseArray[{{1, 3} -> 1, {2, 2} -> 2, {3, 1} -> 3}, {3, 3}]DiagonalMatrixQ[%]SparseArray[{{x_, y_} /; x == y -> 3}, {5, 5}]DiagonalMatrixQ[%]Use DiagonalMatrixQ with structured matrices:
SymmetrizedArray[{{1, 1} -> 4, {2, 2} -> 5, {3, 3} -> 6}, {3, 3}, Symmetric[All]]DiagonalMatrixQ[%]Use with a QuantityArray structured matrix:
QuantityArray[{{1, 0}, {0, 5}}, "Meters"]DiagonalMatrixQ[%]The identity matrix is diagonal:
DiagonalMatrixQ[IdentityMatrix[5]]HilbertMatrix is not diagonal:
DiagonalMatrixQ[HilbertMatrix[5]]Options (1)
Tolerance (1)
m = {{1., 10 ^ -12, 0}, {0, 2., 10 ^ -13}, {0, 0, 3.}};
DiagonalMatrixQ[m]Add the Tolerance option to consider numbers smaller than 10-12 to be zero:
DiagonalMatrixQ[m, Tolerance -> 10 ^ -12]Applications (2)
A real-valued matrix is orthogonally similar to a diagonal matrix iff it is normal and has real eigenvalues:
m = N[HilbertMatrix[3]];
NormalMatrixQ[m]Compute the eigenvalues and eigenvectors of m:
{vals, vecs} = Eigensystem[m];vals∈RealsVerify that m is equivalent to a diagonal matrix via its eigenvectors:
DiagonalMatrixQ[vecs.m.Transpose[vecs]]Verify that the eigenvector matrix is orthogonal:
OrthogonalMatrixQ[vecs]A matrix is diagonalizable if and only if its canonical Jordan matrix is diagonal:
mat1 = {{1, 2}, {3, 4}};
(j1 = Last[JordanDecomposition[mat1]])//MatrixForm{DiagonalizableMatrixQ[mat1], DiagonalMatrixQ[j1]}mat2 = {{27, 48, 81}, {-6, 0, 0}, {1, 0, 3}};
(j2 = Last[JordanDecomposition[mat2]])//MatrixForm{DiagonalizableMatrixQ[mat2], DiagonalMatrixQ[j2]}Properties & Relations (10)
DiagonalMatrixQ returns False for inputs that are not matrices:
DiagonalMatrixQ[1]DiagonalMatrixQ[{}]Matrices of dimensions {n,0} are diagonal:
DiagonalMatrixQ[{{}, {}}]DiagonalMatrix creates a diagonal matrix:
(m = DiagonalMatrix[{a, b, c, d}])//MatrixFormDiagonalMatrixQ[m]Any identity matrix is diagonal:
DiagonalMatrixQ[IdentityMatrix[5]]Inverses of diagonal matrices are diagonal:
mat = DiagonalMatrix[Range[5]];DiagonalMatrixQ[Inverse[mat]]This extends to arbitrary powers and functions:
DiagonalMatrixQ[MatrixPower[mat, n]]DiagonalMatrixQ[MatrixFunction[f, mat]]The product of two (or more) diagonal matrices is diagonal:
m1 = DiagonalMatrix[RandomReal[1., {5}]];
m2 = DiagonalMatrix[RandomReal[1., {5}]];DiagonalMatrixQ[m1.m2]A diagonal matrix is both upper and lower triangular:
mat = DiagonalMatrix[RandomReal[1., {5}]];{DiagonalMatrixQ[m1], UpperTriangularMatrixQ[m1], LowerTriangularMatrixQ[m1]}DiagonalMatrixQ[m,0] is equivalent to DiagonalMatrixQ[m]:
{DiagonalMatrixQ[{{1, 0}, {0, 2}}], DiagonalMatrixQ[{{1, 0}, {0, 2}}, 0]}{DiagonalMatrixQ[{{1, 3}, {4, 2}}], DiagonalMatrixQ[{{1, 3}, {4, 2}}, 0]}A matrix
with only subdiagonals or superdiagonals is nilpotent, meaning
for some
:
m1 = DiagonalMatrix[Range[4], -1];
DiagonalMatrixQ[m1, -1]MatrixPower[m1, 5]m2 = DiagonalMatrix[Range[4], 1];
DiagonalMatrixQ[m2, 1]MatrixPower[m2, 5]Band can be used to construct a k-diagonal sparse matrix:
k = RandomInteger[5]m = SparseArray[Band[{1, 1 + k}] -> Range[5], (5 + k){1, 1}]DiagonalMatrixQ[m, k]See Also
Diagonal DiagonalMatrix UpperTriangularMatrixQ LowerTriangularMatrixQ DiagonalizableMatrixQ Tr IdentityMatrix
Function Repository: AntidiagonalMatrixQ
Tech Notes
Related Guides
History
Text
Wolfram Research (2019), DiagonalMatrixQ, Wolfram Language function, https://reference.wolfram.com/language/ref/DiagonalMatrixQ.html.
CMS
Wolfram Language. 2019. "DiagonalMatrixQ." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/DiagonalMatrixQ.html.
APA
Wolfram Language. (2019). DiagonalMatrixQ. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/DiagonalMatrixQ.html
BibTeX
@misc{reference.wolfram_2026_diagonalmatrixq, author="Wolfram Research", title="{DiagonalMatrixQ}", year="2019", howpublished="\url{https://reference.wolfram.com/language/ref/DiagonalMatrixQ.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_diagonalmatrixq, organization={Wolfram Research}, title={DiagonalMatrixQ}, year={2019}, url={https://reference.wolfram.com/language/ref/DiagonalMatrixQ.html}, note=[Accessed: 12-June-2026]}