2

Can you help me to write code for the following matrix... I have done one part of the code:

\begin{displaymath}
\begin{pmatrix}
a_{11} & a_{12} & a_{13} \\
a_{12} & a_{22} & a_{23} \\
a_{31} & a_{32} & a_{33}
\end{pmatrix}=
\begin{pmatrix}
\end{pmatrix}
\end{displaymath}

1

5 Answers 5

8

You can nest the matrix environment:

\documentclass{article}
\usepackage{amsmath}

\begin{document}
    \begin{equation}
        \mathop{\mathbf{adj}}
        \begin{pmatrix}
            a_{11} & a_{12} & a_{13} \\
            a_{12} & a_{22} & a_{23} \\
            a_{31} & a_{32} & a_{33}
        \end{pmatrix}
        =
        \begin{pmatrix}
            +
            \begin{vmatrix}
                a_{22} & a_{23} \\
                a_{32} & a_{33}
            \end{vmatrix} &
            -
            \begin{vmatrix}
                a_{12} & a_{13} \\
                a_{32} & a_{33}
            \end{vmatrix} &
            +
            \begin{vmatrix}
                a_{12} & a_{13} \\
                a_{22} & a_{23} \\
            \end{vmatrix} \\[1.5em]
            -
            \begin{vmatrix}
                a_{21} & a_{23} \\
                a_{31} & a_{33}
            \end{vmatrix} &
            +
            \begin{vmatrix}
                a_{11} & a_{13} \\
                a_{31} & a_{33}
            \end{vmatrix}&
            -
            \begin{vmatrix}
                a_{11} &  a_{13} \\
                a_{21} &  a_{23}
            \end{vmatrix} \\[1.5em]
            +
            \begin{vmatrix}
                a_{21} & a_{22} \\
                a_{31} & a_{32}
            \end{vmatrix} &
            -
            \begin{vmatrix}
            a_{11} & a_{12} \\
            a_{21} & a_{22}
            \end{vmatrix} &
            +
            \begin{vmatrix}
                a_{11} & a_{12} \\
                a_{21} & a_{22}
            \end{vmatrix}
        \end{pmatrix}
    \end{equation}
\end{document}

enter image description here

4
  • Turns out there was no need to use the suggestions in tex.stackexchange.com/q/14071/177 May 8, 2020 at 16:34
  • Thanks so much, what I should do to make a spacing between rows inside matrice? :)
    – user215058
    May 8, 2020 at 17:03
  • @Amela112 you can use \\[2mm] for example
    – Black Mild
    May 8, 2020 at 17:06
  • the spacing between the rows is achieved with \\[1.5em], with 1.5em a length of your liking (that is, you can change this value). For a more elaborate solution, have a loot at tex.stackexchange.com/q/14071/177 May 8, 2020 at 17:06
5

There are many possibilities to create the complete code for your answer. I have chosen a short classic way: put on every row 3 matrix. I have given a small vertical space with [3ex].

\documentclass{article}
\usepackage{amsmath}
\DeclareMathOperator{\adj}{adj}
\begin{document}
\[
\adj \begin{pmatrix}
a_{11} & a_{12} & a_{13} \\
a_{12} & a_{22} & a_{23} \\
a_{31} & a_{32} & a_{33}
\end{pmatrix}=
\begin{pmatrix}
+\begin{vmatrix} a_{22} & a_{23} \\ a_{32} & a_{33} \end{vmatrix} &
-\begin{vmatrix} a_{12} & a_{13} \\ a_{32} & a_{33} \end{vmatrix} &
+\begin{vmatrix} a_{12} & a_{13} \\ a_{22} & a_{23} \end{vmatrix} \\[3ex]
-\begin{vmatrix} a_{21} & a_{23} \\ a_{31} & a_{33} \end{vmatrix} &
+\begin{vmatrix} a_{11} & a_{13} \\ a_{31} & a_{33} \end{vmatrix} &
-\begin{vmatrix} a_{11} & a_{13} \\ a_{21} & a_{23} \end{vmatrix} \\[3ex]
+\begin{vmatrix} a_{21} & a_{22} \\ a_{31} & a_{32} \end{vmatrix} &
-\begin{vmatrix} a_{11} & a_{12} \\ a_{21} & a_{22} \end{vmatrix} &
+\begin{vmatrix} a_{11} & a_{12} \\ a_{21} & a_{22} \end{vmatrix} 
\end{pmatrix}
\]
\end{document}    

This is my output:

enter image description here

4
  • 1
    +1. I'd leave a bit more space between the right-hand edge of a vmatrix and the next + or - symbol, as otherwise uninitiated readers might assume that the determinants in a given row are added (or subtracted).
    – Mico
    May 8, 2020 at 21:46
  • 1
    @Mico You, like so many other users, are always welcome. The best users can edit, delete, improve what they want of my answers. I am a modest user and I want to be thus. :-)
    – Sebastiano
    May 8, 2020 at 21:49
  • 1
    I've taken you up on your offer. :-)
    – Mico
    May 8, 2020 at 22:10
  • @Mico Very good, and I thank you and everyone.
    – Sebastiano
    May 8, 2020 at 22:12
5

As suggested in other answers, it's better to use some shorthands.

\documentclass{article}
\usepackage{amsmath}

\newcommand{\minor}[4]{%
  \begin{vmatrix} a_{#1} & a_{#2} \\ a_{#3} & a_{#4} \end{vmatrix}
}

\begin{document}

\[
\operatorname{adj}
\begin{pmatrix}
a_{11} & a_{12} & a_{13} \\
a_{21} & a_{22} & a_{23} \\
a_{31} & a_{32} & a_{33}
\end{pmatrix}
=
\begin{pmatrix}
+\minor{22}{23}{32}{33} & -\minor{12}{13}{32}{33} & +\minor{12}{13}{22}{23}
\\ \noalign{\vspace{1ex}}
-\minor{21}{23}{31}{33} & +\minor{11}{13}{31}{33} & -\minor{11}{13}{21}{23}
\\ \noalign{\vspace{1ex}}
+\minor{21}{22}{31}{32} & -\minor{11}{12}{31}{32} & +\minor{11}{12}{21}{22}
\end{pmatrix}
\]

\end{document}

enter image description here

The challenge is now to make TeX compute the adjoint itself. Here's a set of macros tha do it.

\documentclass{article}
\usepackage{amsmath,xparse}

\ExplSyntaxOn
\NewDocumentCommand{\adjointmatrix}{O{a}m}
 {% #1 = entry, #2 = number of rows
  \tl_clear:N \l__amela_matrix_body_tl
  \__amela_matrix_adjoint_make:nn { #1 } { #2 }
  \begin{pmatrix}
  \tl_use:N \l__amela_matrix_body_tl
  \end{pmatrix}
 }

\cs_new_protected:Nn \__amela_matrix_adjoint_make:nn
 {
  \int_step_inline:nn { #2 }
   {
    \int_step_inline:nn { #2 }
     {
      \int_compare:nF { ####1 = 1 } { \tl_put_right:Nn \l__amela_matrix_body_tl { & } }
      \tl_put_right:Nx \l__amela_matrix_body_tl
       {
        \int_if_odd:nTF { ##1+####1 } { - } { + }
       }
      \tl_put_right:Nn \l__amela_matrix_body_tl { \begin{vmatrix} }
      \__amela_matrix_adjoint_minor:nnnn { #1 } { #2 } { ####1 } { ##1 }
      \tl_put_right:Nn \l__amela_matrix_body_tl { \end{vmatrix} }
     }
    \int_compare:nT { ##1 < #2 }
     {
      \tl_put_right:Nn \l__amela_matrix_body_tl { \\ \noalign{\vspace{1ex}} }
     }
   }
 }

\cs_new_protected:Nn \__amela_matrix_adjoint_minor:nnnn
 {% #1 = entry, #2 = size, #3 = row index, #4 = column index
  \tl_clear:N \l__amela_matrix_minor_body_tl
  \int_step_inline:nn { #2 }
   {
    \int_compare:nF { ##1 = #3 }
     {
      \tl_put_right:Nn \l__amela_matrix_minor_body_tl { \use_none:n } % remove the initial &
      \int_step_inline:nn { #2 }
       {
        \int_compare:nF { ####1 = #4 }
         {
          \tl_put_right:Nn \l__amela_matrix_minor_body_tl { & #1\sb{##1####1} }
         }
       }
      \tl_put_right:Nn \l__amela_matrix_minor_body_tl { \\ }
     }
   }
  \tl_put_right:NV \l__amela_matrix_body_tl \l__amela_matrix_minor_body_tl
 }

\NewDocumentCommand{\squarematrix}{O{a}m}
 {% #1 = entry, #2 = number of rows
  \tl_clear:N \l__amela_matrix_body_tl
  \__amela_matrix_make:nn { #1 } { #2 }
  \begin{pmatrix}
  \tl_use:N \l__amela_matrix_body_tl
  \end{pmatrix}
 }

\tl_new:N \l__amela_matrix_body_tl
\tl_new:N \l__amela_matrix_minor_body_tl

\cs_new_protected:Nn \__amela_matrix_make:nn
 {
  \int_step_inline:nn { #2 }
   {
    \int_step_inline:nn { #2 }
     {
      \int_compare:nF { ####1 = 1 } { \tl_put_right:Nn \l__amela_matrix_body_tl { & } }
      \tl_put_right:Nn \l__amela_matrix_body_tl { #1\sb{##1 ####1} }
     }
    \tl_put_right:Nn \l__amela_matrix_body_tl { \\ }
   }
 }

\ExplSyntaxOff

\begin{document}

\[
\squarematrix{3} \squarematrix[b]{4}
\]

\[
\operatorname{adj}\squarematrix{3}=\adjointmatrix{3}
\]

\[
\operatorname{adj}\squarematrix{2}=\adjointmatrix{2}
\]

\[
\adjointmatrix[b]{4}
\]

\end{document}

enter image description here

This is the output of \adjointmatrix{5} (you need large paper size for it).

enter image description here

1
  • Impressive :-) code and creative.
    – Sebastiano
    May 8, 2020 at 22:10
4

Just for fun: one can let LaTeX figure out the entries.

\documentclass{article}
\usepackage{amsmath}
\usepackage{pgf}
\makeatletter
\newcommand\AdjEntry{%
\stepcounter{imat}%
\pgfmathtruncatemacro{\irow}{(\value{imat}+2)/3}%
\pgfmathtruncatemacro{\icol}{Mod(\value{imat}-1,3)+1}%
\pgfmathsetmacro{\isign}{ifthenelse(pow(-1,\value{imat}+1)<0,"-","+")}%
\edef\temp{\noexpand\@removeelement{\icol}{1,2,3}{\noexpand\collst}%
\noexpand\@removeelement{\irow}{1,2,3}{\noexpand\rowlst}}%
\temp
\pgfmathtruncatemacro{\ione}{{\rowlst}[0]}%
\pgfmathtruncatemacro{\itwo}{{\rowlst}[1]}%
\pgfmathtruncatemacro{\jone}{{\collst}[0]}%
\pgfmathtruncatemacro{\jtwo}{{\collst}[1]}%
\isign 
\begin{vmatrix}
                a_{\jone\ione} & a_{\jtwo\ione} \\
                a_{\jone\itwo} & a_{\jtwo\itwo}
\end{vmatrix}}
\newcommand\aentry{\stepcounter{imat}%
\pgfmathtruncatemacro{\irow}{(\value{imat}+2)/3}%
\pgfmathtruncatemacro{\icol}{Mod(\value{imat}-1,3)+1}%
a_{\irow\icol}}
\makeatother
\newcounter{imat}
\begin{document}
\[\setcounter{imat}{0}
\operatorname{adj}\begin{pmatrix}
 \aentry & \aentry & \aentry\\
 \aentry & \aentry & \aentry\\
 \aentry & \aentry & \aentry\\
\end{pmatrix}=\setcounter{imat}{0}
\begin{pmatrix}
 \AdjEntry & \AdjEntry & \AdjEntry\\[2.5ex]
 \AdjEntry & \AdjEntry & \AdjEntry\\[2.5ex]
 \AdjEntry & \AdjEntry & \AdjEntry\\
\end{pmatrix}
\]
\end{document}

enter image description here

9
  • Hi, there is a missing \mathbf{adj} before the first matrix.
    – Sebastiano
    May 8, 2020 at 20:03
  • 1
    @Sebastiano Nope, you are 2 seconds late. ;-)
    – user194703
    May 8, 2020 at 20:04
  • Did you read my mind? Hahahah. But it is in math-bold.
    – Sebastiano
    May 8, 2020 at 20:04
  • @Sebastiano No, my crystal ball got stolen.
    – user194703
    May 8, 2020 at 20:05
  • 1
    @Sebastiano I am a cat. I think the anteater has it.
    – user194703
    May 8, 2020 at 20:07
3

We are using TeX, so we can do a shortcuts:

$$
\def\subd#1#2#3#4{\left|\matrix{a_{#1#2}&a_{#1#4}\cr a_{#3#2}&a_{#3#4}}\right|}
\def\crs{\cr\noalign{\kern.7ex}}
{\bf adj} \pmatrix{a_{11}&a_{12}&a_{13}\cr a_{21}&a_{22}&a_{23}\cr a_{31}&a_{32}&a_{33}}
= \pmatrix{+\subd 22 33 & -\subd 12 33 & +\subd 12 23 \crs
           -\subd 21 33 & +\subd 11 33 & -\subd 11 23 \crs
           +\subd 21 32 & -\subd 11 32 & +\subd 11 22} 
$$

You must log in to answer this question.