How to write a diagonal matrix in LaTeX?

A diagonal matrix is a special type of square matrix where all the elements outside the main diagonal are zero. The only nonzero elements appear along the main diagonal (from the top left to the bottom right).

\documentclass{article}
\usepackage{amsmath}
\begin{document}
 \[ \begin{bmatrix}
   a_{1} &  &  & \\ 
   & a_{2} &  & \\ 
   &  &  \ddots & \\ 
   &  &   & a_{3} 
 \end{bmatrix} \]
\end{document}

Output :

define diagonal matrix in latex

Using the physics package for diagonal matrices

The physics package in LaTeX provides convenient commands to represent diagonal matrices in multiple ways.

\documentclass{article}
\usepackage{physics}
\begin{document}
  \[ \mqty[ a_{1} & 0 & 0\\ 0 & a_{2} & 0\\ 0 & 0 & a_{3} ] \]
\end{document}

Output :

use mqty command to define diagonal matrix in latex

Second, you can use the \dmat command. However, this \dmat command must be passed as an argument in the \mqty command without using direct.

\documentclass{article}
\usepackage{physics}
\begin{document} 
  \[ \mqty[\dmat{1,2,3,4}] \]
\end{document}

Output :

use \dmat command to define diagonal matrix in latex

With that, the elements of the diagonal matrix have to be passed into \dmat command.

To fill in any position other than the diagonal position of the matrix by zero, you need to pass zero in the optional argument with \dmat command.

\documentclass{article}
\usepackage{physics}
\begin{document}
  \[ \mqty[\dmat[0]{1,2,3,4}] \]
\end{document}

Output :

use mqty command to define diagonal matrix in latex

Inverse diagonal matrix

If you want to arrange the elements of the matrix along the inverse diagonal you need to use \admat command instead of \dmat command.

\documentclass{article}
\usepackage{physics}
\begin{document}
  \[ \mqty[\admat{1,2,3,4}] \]
  \[ \mqty[\admat[0]{1,2,3,4}] \]
\end{document}

Output :

use mqty command to define inverse diagonal matrix in latex

Share tutorial

Leave a Comment

Your email address will not be published. Required fields are marked *