matrix_props.is_diagonal

Checks if the matrix is a diagonal matrix.

Functions

is_diagonal(mat)

Determine if a matrix is diagonal [1].

Module Contents

matrix_props.is_diagonal.is_diagonal(mat)

Determine if a matrix is diagonal [1].

A matrix is diagonal if the matrix is square and if the diagonal of the matrix is non-zero, while the off-diagonal elements are all zero.

The following is an example of a 3-by-3 diagonal matrix:

\[\begin{split}\begin{equation} \begin{pmatrix} 1 & 0 & 0 \\ 0 & 2 & 0 \\ 0 & 0 & 3 \end{pmatrix} \end{equation}\end{split}\]

This quick implementation is given by Daniel F. from StackOverflow in [2].

Examples

Consider the following diagonal matrix:

\[\begin{split}A = \begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix}.\end{split}\]

Our function indicates that this is indeed a diagonal matrix:

>>> from toqito.matrix_props import is_diagonal
>>> import numpy as np
>>> A = np.array([[1, 0], [0, 1]])
>>> is_diagonal(A)
np.True_

Alternatively, the following example matrix

\[\begin{split}B = \begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix}\end{split}\]

is not diagonal, as shown using toqito.

>>> from toqito.matrix_props import is_diagonal
>>> import numpy as np
>>> B = np.array([[1, 2], [3, 4]])
>>> is_diagonal(B)
np.False_

References

[1] (1,2)

Wikipedia. Diagonal matrix. URL: https://en.wikipedia.org/wiki/Diagonal_matrix.

[2]

Stack Overflow Post. Check if a large matrix is diagonal matrix in python. URL: https://stackoverflow.com/questions/43884189/.

Parameters:

mat (numpy.ndarray) – The matrix to check.

Returns:

Returns True if the matrix is diagonal and False otherwise.

Return type:

bool