4

I would like to achieve the next result in tikz: enter image description here

However, I am only able to draw the matrices without the mathematical operations between them i.e. the dot product and the equal signs (having the same height for the parenthesis is a plus). Below I am attaching part of my code and how far I have reached so far.

Any help would be much appreciated

Thanks

\documentclass[usenames,dvipsnames]{beamer}
\usepackage{tikz}
    \usetikzlibrary{arrows.meta}
    \usetikzlibrary{decorations.pathreplacing}
    \usetikzlibrary{fit, calc, matrix, positioning, arrows.meta, intersections, through, backgrounds, patterns}
\usepackage{pgfplots}
    \pgfplotsset{compat = newest}
    \pgfplotsset{overwrite option/.style args={#1 with #2}{#1=#2,#1/.code=}}

\begin{frame}{MATRIX MULTIPLICATION} More graphically:

\begin{tikzpicture}
\scriptsize
    \matrix (m)[
    matrix of math nodes,
    nodes in empty cells,
    %minimum width=width("998888"),
    left delimiter=(,
    right delimiter=)
    ] {
    a_{11} && \cdots && a_{1k} && \cdots &&  a_{1n} \\
    \vdots &&        && \vdots &&        &&\vdots  \\
    a_{i1} && \cdots && a_{ik} && \cdots &&  a_{in} \\
    \vdots &&        && \vdots &&        && \vdots  \\
    a_{m1} && \cdots && a_{mk} && \cdots &&  a_{mn} \\
    } ;

    \draw (m-3-1.south west) rectangle (m-3-5.north east);

\begin{scope}[xshift=95pt]

    \matrix (m)[
    matrix of math nodes,
    nodes in empty cells,
    %minimum width=width("998888"),
    left delimiter=(,
    right delimiter=)
    ] {
    b_{11} && \cdots && b_{1j} && \cdots &&  b_{1p} \\
    \vdots &&        && \vdots &&        &&\vdots  \\
    b_{k1} && \cdots && b_{kj} && \cdots &&  b_{kp} \\
    \vdots &&        && \vdots &&        && \vdots  \\
    b_{n1} && \cdots && b_{nj} && \cdots &&  b_{np} \\
    } ;

    \draw (m-5-3.south west) rectangle (m-1-3.north east);
\end{scope}
\begin{scope}[xshift=200pt]

    \matrix (m)[
    matrix of math nodes,
    nodes in empty cells,
    %minimum width=width("998888"),
    left delimiter=(,
    right delimiter=)
    ] {
    c_{11} && \cdots && c_{1j} && \cdots &&  c_{1p} \\
    \vdots &&        && \vdots &&        &&\vdots  \\
    c_{i1} && \cdots && c_{ij} && \cdots &&  c_{ip} \\
    \vdots &&        && \vdots &&        && \vdots  \\
    c_{m1} && \cdots && c_{mj} && \cdots &&  c_{mp} \\
    } ;

    \draw (m-3-3.south west) rectangle (m-3-3.north east);
\end{scope}    
\end{tikzpicture}

\end{frame}

enter image description here

3 Answers3

4

First of all I'd use the positioning library to place the matrices next to each other, no need then to figure out the right xshift. (You could also do \matrix (m2) at (95pt,0) ... instead of the scope.)

Having named the matrices m, m2 and m3, you can for example do

\path (m) -- node {$\cdot$} (m2)
      (m2) -- node {$=$} (m3);

to place the symbols midway between them.

enter image description here

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning, matrix}
\begin{document}
\begin{tikzpicture}
    \scriptsize
        \matrix (m)[
        matrix of math nodes,
        nodes in empty cells,
        %minimum width=width("998888"),
        left delimiter=(,
        right delimiter=)
        ] {
        a_{11} && \cdots && a_{1k} && \cdots &&  a_{1n} \\
        \vdots &&        && \vdots &&        &&\vdots  \\
        a_{i1} && \cdots && a_{ik} && \cdots &&  a_{in} \\
        \vdots &&        && \vdots &&        && \vdots  \\
        a_{m1} && \cdots && a_{mk} && \cdots &&  a_{mn} \\
        } ;
    \draw (m-3-1.south west) rectangle (m-3-5.north east);



    \matrix (m2) [
    matrix of math nodes,
    nodes in empty cells,
    %minimum width=width("998888"),
    left delimiter=(,
    right delimiter=),
    right=of m
    ] {
    b_{11} && \cdots && b_{1j} && \cdots &&  b_{1p} \\
    \vdots &&        && \vdots &&        &&\vdots  \\
    b_{k1} && \cdots && b_{kj} && \cdots &&  b_{kp} \\
    \vdots &&        && \vdots &&        && \vdots  \\
    b_{n1} && \cdots && b_{nj} && \cdots &&  b_{np} \\
    } ;

    \draw (m-5-3.south west) rectangle (m-1-3.north east);


    \matrix (m3)[
    matrix of math nodes,
    nodes in empty cells,
    %minimum width=width("998888"),
    left delimiter=(,
    right delimiter=),
    right=of m2
    ] {
    c_{11} && \cdots && c_{1j} && \cdots &&  c_{1p} \\
    \vdots &&        && \vdots &&        &&\vdots  \\
    c_{i1} && \cdots && c_{ij} && \cdots &&  c_{ip} \\
    \vdots &&        && \vdots &&        && \vdots  \\
    c_{m1} && \cdots && c_{mj} && \cdots &&  c_{mp} \\
    } ;

    \draw (m-3-3.south west) rectangle (m-3-3.north east);

    \path (m) -- node {$\cdot$} (m2)
          (m2) -- node {$=$} (m3);
\end{tikzpicture}

\end{document}

Addendum

Note that in beamer you can't use a matrix directly, you need a fragile frame or use ampersand replacement, see for example "Single ampersand used with wrong catcode" error using tikz matrix in beamer

Below I made the frame fragile. I also made a new style pmat that I applied to all the matrices, and I modified things a bit so that they actually fit in the frame.

enter image description here

\documentclass[usenames,dvipsnames]{beamer}
\usepackage{tikz}
    \usetikzlibrary{arrows.meta}
    \usetikzlibrary{decorations.pathreplacing}
    \usetikzlibrary{fit, calc, matrix, positioning, arrows.meta, intersections, through, backgrounds, patterns}
\usepackage{pgfplots}
    \pgfplotsset{compat = newest}
    \pgfplotsset{overwrite option/.style args={#1 with #2}{#1=#2,#1/.code=}}
\begin{document}

\begin{frame}[fragile]{MATRIX MULTIPLICATION} More graphically:

\begin{tikzpicture}[ pmat/.style={ matrix of math nodes, nodes={font=\footnotesize\strut, inner sep=1.5pt}, left delimiter=(, right delimiter=), } ]

    \matrix (m)[pmat] {
    a_{11} && \cdots && a_{1k} && \cdots &&  a_{1n} \\
    \vdots &&        && \vdots &&        &&\vdots  \\
    a_{i1} && \cdots && a_{ik} && \cdots &&  a_{in} \\
    \vdots &&        && \vdots &&        && \vdots  \\
    a_{m1} && \cdots && a_{mk} && \cdots &&  a_{mn} \\
    } ;

    \draw (m-3-1.south west) rectangle (m-3-5.north east);


    \matrix (m2) [pmat,right=of m] {
    b_{11} && \cdots && b_{1j} && \cdots &&  b_{1p} \\
    \vdots &&        && \vdots &&        &&\vdots  \\
    b_{k1} && \cdots && b_{kj} && \cdots &&  b_{kp} \\
    \vdots &&        && \vdots &&        && \vdots  \\
    b_{n1} && \cdots && b_{nj} && \cdots &&  b_{np} \\
    } ;

    \draw (m-5-3.south west) rectangle (m-1-3.north east);


    \matrix (m3)[pmat,right=of m2] {
    c_{11} && \cdots && c_{1j} && \cdots &&  c_{1p} \\
    \vdots &&        && \vdots &&        &&\vdots  \\
    c_{i1} && \cdots && c_{ij} && \cdots &&  c_{ip} \\
    \vdots &&        && \vdots &&        && \vdots  \\
    c_{m1} && \cdots && c_{mj} && \cdots &&  c_{mp} \\
    } ;

    \draw (m-3-3.south west) rectangle (m-3-3.north east);

    \path (m) -- node {$\cdot$} (m2)
          (m2) -- node {$=$} (m3);
\end{tikzpicture}

\end{frame} \end{document}

Torbjørn T.
  • 206,688
1

enter image description here

With use of the \vphantom{b_{ij}} beginning of each rows with variables in the first and last matrix:

\documentclass[usenames,dvipsnames]{beamer}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
                backgrounds, 
                calc, 
                decorations.pathreplacing,
                fit, 
                intersections, 
                matrix, 
                positioning, patterns,
                through
                }

\begin{document} \begin{frame}[fragile] \frametitle{MATRIX MULTIPLICATION}

More graphically:

[ \begin{tikzpicture}[ every matrix/.style = {matrix of math nodes, nodes in empty cells, nodes = {inner sep=2pt, font=\scriptsize}, left delimiter=(, right delimiter=), inner sep=0pt }, ]
\matrix (m1) { \vphantom{b_{ij}} a_{11} & \cdots & a_{1k} & \cdots & a_{1n} \ \vdots & & \vdots & & \vdots \ \vphantom{b_{ij}} a_{i1} & \cdots & a_{ik} & \cdots & a_{in} \ \vdots & & \vdots & & \vdots \ \vphantom{b_{ij}} a_{m1} & \cdots & a_{mk} & \cdots & a_{mn} \ } ; \draw (m1-3-1.south west) rectangle (m1-3-5.north east); \node (m1a) [right=2mm of m1] {$\cdot$}; \matrix (m2)[right=2mm of m1a] { b_{11} & \cdots & b_{1j} & \cdots & b_{1p} \ \vdots & & \vdots & & \vdots \ b_{k1} & \cdots & b_{kj} & \cdots & b_{kp} \ \vdots & & \vdots & & \vdots \ b_{n1} & \cdots & b_{nj} & \cdots & b_{np} \ } ; \draw (m2-5-3.south west) rectangle (m2-1-3.north east); \node (m2a) [right=2mm of m2] {$=$}; \matrix (m3) [right=2mm of m2a] { \vphantom{b_{ij}} c_{11} & \cdots & c_{1j} & \cdots & c_{1p} \ \vdots & & \vdots & &\vdots \ \vphantom{b_{ij}} c_{i1} & \cdots & c_{ij} & \cdots & c_{ip} \ \vdots & & \vdots & & \vdots \ \vphantom{b_{ij}} c_{m1} & \cdots & c_{mj} & \cdots & c_{mp} \ } ; \draw (m3-3-3.south west) rectangle (m3-3-3.north east); \end{tikzpicture} ] \end{frame} \end{document}

Zarko
  • 296,517
1

You can do that with {pNiceMatrix} of nicematrix. This environment is similar to {pmatrix} of amsmath but creates PGF/Tikz nodes under the cells of the array. It's easy to use these nodes to draw the rectangles with Tikz.

\documentclass[usenames,dvipsnames]{beamer}
\usepackage{tikz}
\usepackage{nicematrix}

\begin{document}

\begin{frame}[fragile]{MATRIX MULTIPLICATION} More graphically:

\NiceMatrixOptions{small,create-large-nodes,columns-width=4mm}

$\begin{pNiceArray}{ccccc<{\vphantom{b_{np}}}} a_{11} & \cdots & a_{1j} & \cdots & a_{1p} \ \vdots & & \vdots & &\vdots \ a_{i1} & \cdots & a_{ij} & \cdots & a_{ip} \ \vdots & & \vdots & & \vdots \ a_{m1} & \cdots & a_{mj} & \cdots & a_{mp} \CodeAfter \tikz \draw (3-1-large.south west) rectangle (3-5-large.north east); \end{pNiceArray} \cdot \begin{pNiceArray}{ccccc} b_{11} & \cdots & b_{1j} & \cdots & b_{1p} \ \vdots & & \vdots & &\vdots \ b_{k1} & \cdots & b_{kj} & \cdots & b_{kp} \ \vdots & & \vdots & & \vdots \ b_{n1} & \cdots & b_{nj} & \cdots & b_{np} \CodeAfter \tikz \draw (5-3-large.south west) rectangle (1-3-large.north east); \end{pNiceArray} = \begin{pNiceArray}{ccccc<{\vphantom{b_{np}}}} c_{11} & \cdots & c_{1j} & \cdots & c_{1p} \ \vdots & & \vdots & &\vdots \ c_{i1} & \cdots & c_{ij} & \cdots & c_{ip} \ \vdots & & \vdots & & \vdots \ c_{m1} & \cdots & c_{mj} & \cdots & c_{mp} \CodeAfter \tikz \draw (3-3-large.south west) rectangle (3-3-large.north east); \end{pNiceArray}$ \end{frame}

\end{document}

Output of the above code

F. Pantigny
  • 40,250