TikZ 中的矩阵乘法图表

TikZ 中的矩阵乘法图表

我知道有几篇关于给矩阵的行和列着色的帖子,但我试图创建一个图表来显示矩阵乘法,结果得到了以下代码,这有点经验主义,也有点笨拙。我想知道是否有更智能的方法来做同样的事情。

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{matrix,positioning,calc}

\begin{document}
\begin{tikzpicture}[
    every node/.style={minimum height=14pt,minimum width=1em},
    every matrix/.style={matrix of math nodes,
        column sep=1mm,
        row sep=1mm,
        left delimiter=(,
        right delimiter=)}]
\fill[red!10] (-1,-6pt) rectangle (1,6pt);
\fill[red!10] (4.8,0) ++(-7pt,-1) rectangle +(13pt,2);
\fill[red!10] (10.3,0) ++(-7pt,-6pt) rectangle +(13pt,15pt);
\matrix (A) at (0,0) {
\phantom{\bullet}  &        &   \\
\bullet & \cdots    & \bullet \\
\phantom{\bullet}  &        &   \\
};
\matrix (B) at (4.5,0) {
\phantom{\bullet} & \phantom{\bullet}  &    \bullet & \phantom{*} \\
 &  & |[rotate=90]| \dots &  \\
 &  &   \bullet &  \\
};
\matrix (AB) at (10,0) {
\phantom{\bullet} & \phantom{\bullet}  &    \phantom{\bullet}   & \phantom{\bullet} \\
\phantom{\bullet} & \phantom{\bullet}  &    \bullet & \phantom{*} \\
 &  &  &  \\
\phantom{\bullet} &  &  &  \\
};
\node[left=3mm] at (A.west) {\tikz\draw[semithick,<->] (0,0) -- (0,2) node[midway,anchor=center,fill=white] {$m$};};
\node[above=-1mm] at (A.north) {\tikz\draw[semithick,<->] (0,0) -- (2,0) node[midway,anchor=center,fill=white] {$p$};};
\node[left=3mm] at (B.west) {\tikz\draw[semithick,<->] (0,0) -- (0,2) node[midway,anchor=center,fill=white] {$p$};};
\node[above=-1mm] at (B.north) {\tikz\draw[semithick,<->] (0,0) -- (2.5,0) node[midway,anchor=center,fill=white] {$n$};};
\node[left=3mm] at (AB.west) {\tikz\draw[semithick,<->] (0,0) -- (0,2) node[midway,anchor=center,fill=white] {$m$};};
\node[above=-1mm] at (AB.north) {\tikz\draw[semithick,<->] (0,0) -- (2.5,0) node[midway,anchor=center,fill=white] {$n$};};
\node at ($(A)!0.45!(B)$) {$\times$};
\node at ($(B)!0.45!(AB)$) {$=$};
\node[below] (a) at (A.south) {row $i$};
\node[below] (b) at (B.south) {column $j$};
\node[below] (ab) at (AB.south) {$(i,j)$-cell};
\node at ($(a)!0.45!(b)$) {$\times$};
\node at ($(b)!0.45!(ab)$) {$=$};
\end{tikzpicture}

\end{document}

感谢您提供任何帮助使情况变得更好。

答案1

我认为如果您对结果感到满意,代码就不那么重要了,但这里有另一个版本。

它使用的一些属性来matrix自动为所需的行或列着色。还带有nodes in empty cells you don't need\phantom`。

您使用嵌套tikz命令来绘制矩阵维度。我认为这是错误的。

在这种情况下,下方的表达式从右到左绘制,以便轻松对齐所有元素。

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{matrix,positioning,calc}

\begin{document}
\begin{tikzpicture}[
    every matrix/.style={matrix of math nodes,
        nodes in empty cells,
        nodes={minimum size=5mm, anchor=center},
        left delimiter=(,
        right delimiter=)}]

\matrix[row 2/.style={nodes={fill=red!10}}] (A) {
 &  &   \\
\bullet &  & \bullet \\
 &        &   \\
};
\matrix[column 3/.style={nodes={fill=red!10}}, right=2cm of A] (B) {
 &  &   \bullet & \\
 &  & &  \\
 &  &   \bullet &  \\
};
\matrix[right=2cm of B] (AB) {
 &  &  & \\
 &  &|[fill=red!10]| \bullet & \\
 &  &  &  \\
 &  &  &  \\
};

\node at (A-2-2) {$\dots$};
\node[rotate=90] at (B-2-3) {$\dots$};

\draw[semithick,<->] ([xshift=-1.5em]A.south west) -- ([xshift=-1.5em]A.north west) node[midway,fill=white, inner xsep=1pt] {$m$};
\draw[semithick,<->] ([xshift=-1.5em]B.south west) -- ([xshift=-1.5em]B.north west) node[midway,fill=white, inner xsep=1pt] {$p$};
\draw[semithick,<->] ([xshift=-1.5em]AB.south west) -- ([xshift=-1.5em]AB.north west) node[midway,fill=white, inner xsep=1pt] {$m$};

\draw[semithick,<->] ([yshift=.5em]A.north west) -- ([yshift=.5em]A.north east) node[midway,anchor=center,fill=white] {$p$};
\draw[semithick,<->] ([yshift=.5em]B.north west) -- ([yshift=.5em]B.north east) node[midway,anchor=center,fill=white] {$n$};
\draw[semithick,<->] ([yshift=.5em]AB.north west) -- ([yshift=.5em]AB.north east) node[midway,anchor=center,fill=white] {$n$};

\node[below=0pt of AB.south] (ab) {$(i,j)$-cell};
\node at (B.center|-ab) (j) {column $j$};
\node at (A.center|-ab) (i) {row $i$};

\node at ($(A)!0.45!(B)$) (times) {$\times$};
\node at ($(B)!0.45!(AB)$) (equal) {$=$};
\node at (i-|times) {$\times$};
\node at (i-|equal) {$=$};
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案2

2021/06/23:我编辑了这个答案,因为使用最新版本nicematrix(2021/06/20 的 5.16)可以避免使用透明度。


这是(和 Tikz 使用由 创建的 PGF/Tikz 节点{NiceTabular}的版本。nicematrix{NiceTabular}

\documentclass{article}
\usepackage{nicematrix}
\usepackage{tikz}
\usetikzlibrary{fit}
\usepackage{mathtools}
\usepackage{xcolor}

\begin{document}


\setcounter{MaxMatrixCols}{20}

\[\def\s{\hspace*{1em}}% only a shortcut to avoid lines of code too long
\begin{NiceMatrix}[nullify-dots,columns-width=4mm]
\CodeBefore [create-cell-nodes]
  \begin{tikzpicture} [every node/.style = {rectangle,fill=red!15,inner sep = 3pt}]
    \node [fit = (3-2)(3-4)] {} ;
    \node [fit = (2-8)(4-8)] {} ;
    \node [fit = (3-13)] {} ;
  \end{tikzpicture}
\Body
\\
&         &        &         &        && \s & \bullet & \s &   && \s &         &    \\[2mm]
& \bullet & \Cdots & \bullet & \times &&    & \Vdots  &    & = &&    & \bullet &    \\[2mm]
&         &        &         &        &&    & \bullet &    &   &&    &         & \s \\
&         & \mathclap{\text{row } i}
                   &         & \times &&    & \mathclap{\text{column } j}
                                                      &    & = &&    & \mathclap{(i,j)\text{-cell}} \\
\CodeAfter
  \SubMatrix({2-2}{4-4})
  \SubMatrix({2-7}{4-9})
  \SubMatrix({2-12}{4-14})
  \begin{tikzpicture} [every node/.style= {fill=white},every path/.style = <->]
    \draw [<->] (1.5-|2)  to node {$p$} (1.5-|5) ;
    \draw [<->] (1.5-|7)  to node {$n$} (1.5-|10) ;
    \draw [<->] (1.5-|12) to node {$n$} (1.5-|15) ;
    \draw [<->] (2-|1.5)  to node {$m$} (5-|1.5) ;
    \draw [<->] (2-|6.5)  to node {$p$} (5-|6.5) ;
    \draw [<->] (2-|11.5) to node {$m$} (5-|11.5) ;
  \end{tikzpicture}
\end{NiceMatrix}\]

\end{document}

您需要多次编译(因为 PGF/Tikz 节点是用 构建的remember picture)。

上述代码的输出

为了展示构造,这里是hvlines添加键时的输出{NiceTabular}(由于所有垂直规则的宽度,输出更宽)。

添加关键 hvlines 时的输出

相关内容