How to write a diagonal matrix in LaTeX?

Elements of the diagonal matrix will be along the diagonal. All the other positions except the diagonal position of the matrix will be filled by zero.

\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

And diagonal matrix is only valid in the case of a square matrix.

Use physics package for diagonal matrix

In Latex, there are various arguments within the physics package that allow you to easily represent the diagnostic matrix in a variety of 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 comand 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 comand to define diagonal matrix in latex

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

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

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

Output :

use mqty comand to define diagonal matrix in latex

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

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

Output :

use mqty comand to define inverse diagonal matrix in latex

One request!

Don't forget to share this tutorial if I have added any value to your education life with this tutorial.

facebook icontwitter iconwhatsapp iconlinkdin icon

Leave a Comment

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