Tikz 矩阵内部的简单计算

Tikz 矩阵内部的简单计算

我有这个相当复杂的矩阵,如果能直接在 LaTeX 中做一些计算就太好了,因为我需要为准备的每个问题生成几次迭代。

计算很简单,我想计算每个单元格右下角的数字(蓝色)。它们的计算方式为 cij-(ui+vj),例如左上角的数字减去相交行和列的红色数字之和。例如,第一个单元格:1-(0+1)=0

我知道有几个用于计算的包,但我不知道哪一个最合适。

样本矩阵

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{matrix,fit,calc}
\usepackage{adjustbox}

\begin{document}

 \begin{adjustbox}{max totalsize={.9\textwidth}{.9\textheight},center}

\begin{tikzpicture}

\matrix(m) [matrix of math nodes, nodes in empty cells, nodes={minimum size=1cm, outer sep=0pt, text height=1.5ex, text depth=.25ex}]
{
 1  &                   & 2 &                   & 3 &                   &   5    &                   & 0 &                  \\
    &\textcolor{blue}{0}&   &\textcolor{blue}{1}&   &\textcolor{blue}{2}&        &\textcolor{blue}{4}&   &\textcolor{blue}{0}\\
0   &                   & 1 &                   & 5 &                   &   3    &                   & 3 &                  \\
    &\textcolor{blue}{0}&   &\textcolor{blue}{1}&   &\textcolor{blue}{5}&        &\textcolor{blue}{3}&   &\textcolor{blue}{4}\\
3   &                   & 0 &                   & 4 &                   &   0    &                   & 3 &                  \\
    &\textcolor{blue}{3}&   &\textcolor{blue}{0}&   &\textcolor{blue}{4}&        &\textcolor{blue}{0}&   &\textcolor{blue}{4}\\
5   &                   & 7 &                   & 0 &                   &  0     &                   & 2 &                  \\
    &\textcolor{blue}{5}&   &\textcolor{blue}{7}&   &\textcolor{blue}{0}&        &\textcolor{blue}{0}&   &\textcolor{blue}{3}\\
 3  &                   & 1 &                   & 4 &                   & 5      &                   & 0 &                  \\
    &\textcolor{blue}{3}&   &\textcolor{blue}{0}&   &\textcolor{blue}{3}&        &\textcolor{blue}{5}&   &\textcolor{blue}{0}\\
};

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



% horizontal lines
\foreach \i in {2,4,6,8,10} {
  \draw (m-\i-1.south west) -- (m-\i-10.south east);
}

%  vertical lines
\foreach \j in {2,4,6,8,10} {
  \draw (m-1-\j.north east) -- (m-10-\j.south east);
}

% Solution
\node[draw=blue,circle,fit=(m-1-9)(m-2-10),inner sep = -19pt] {1};
\node[draw=blue,circle,fit=(m-3-1)(m-4-2),inner sep = -19pt] {1};
\node[draw=blue,circle,fit=(m-5-7)(m-6-8),inner sep = -19pt] {1};
\node[draw=blue,circle,fit=(m-7-5)(m-8-6),inner sep = -19pt] {1};
\node[draw=blue,circle,fit=(m-9-3)(m-10-4),inner sep = -19pt] {1};

\node[draw=blue,circle,fit=(m-1-1)(m-2-2),inner sep = -19pt] {0};
\node[draw=blue,circle,fit=(m-5-3)(m-6-4),inner sep = -19pt] {0};
\node[draw=blue,circle,fit=(m-7-7)(m-8-8),inner sep = -19pt] {0};
\node[draw=blue,circle,fit=(m-9-9)(m-10-10),inner sep = -19pt] {0};

% dual ui
\node [font=\color{red}] at (6,4) {0};
\node [font=\color{red}] at (6,2) {-1};
\node [font=\color{red}] at (6,0) {-1};
\node [font=\color{red}] at (6,-2) {-1};
\node [font=\color{red}] at (6,-4) {0};

% dual vj
\node [font=\color{red}] at (-4,-6) {1};
\node [font=\color{red}] at (-2,-6) {1};
\node [font=\color{red}] at (0,-6) {1};
\node [font=\color{red}] at (2,-6) {1};
\node [font=\color{red}] at (4,-6) {0};


\end{tikzpicture}
\end{adjustbox}

\end{document}

答案1

我给你一个不使用矩阵库的解决方案。我做了一个\newcommand定义“正方形”的程序,画出它并将所有给定和计算的值放入其中。这是我的代码:

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usepackage{ifthen}

\def\l{1.2} % cell lenght
\newcommand{\square}[4] % x-position, y-position, main value, circled value (if any)
{%
  \begin{scope}[shift={(#1*\l,-#2*\l)}]
    \draw    (0,0) rectangle (\l,-\l);
    \node at (0.25*\l ,-0.25*\l) {#3};    
    % calculate bottom right corener:
    \pgfmathtruncatemacro{\temp}{#3-\bottom[#1]-\right[#2]}
    \node[blue] at (0.75*\l ,-0.75*\l) {$\temp$};
    % circled value
    \ifthenelse{\equal{#4}{}}
    {} % then do nothing
    {%   else... 
      \draw[blue] (0.5*\l ,-0.5*\l) circle (0.2);
      \node at (0.5*\l ,-0.5*\l) {$#4$};
    }
  \end{scope}
}

\begin{document}
\begin{tikzpicture}[line cap=round,line join=round]
  \def\right {{0,-1,-1,-1,0}} % right  red values (dual ui)
  \def\bottom{{1, 1, 1, 1,0}} % bottom red values (dual vj)
  % row 1
  \square{0}{0}{1}{0};
  \square{1}{0}{2}{};
  \square{2}{0}{3}{};
  \square{3}{0}{5}{};
  \square{4}{0}{0}{1};
  % row 2
  \square{0}{1}{0}{1};
  \square{1}{1}{1}{};
  \square{2}{1}{5}{};
  \square{3}{1}{3}{};
  \square{4}{1}{3}{};
  % row 3
  \square{0}{2}{3}{};
  \square{1}{2}{0}{0};
  \square{2}{2}{4}{};
  \square{3}{2}{0}{1};
  \square{4}{2}{3}{};
  % row 4
  \square{0}{3}{5}{};
  \square{1}{3}{7}{};
  \square{2}{3}{0}{1};
  \square{3}{3}{0}{0};
  \square{4}{3}{2}{};
  % row 5
  \square{0}{4}{3}{};
  \square{1}{4}{1}{1};
  \square{2}{4}{4}{};
  \square{3}{4}{5}{};
  \square{4}{4}{0}{0};
  % red values (dual ui and dual vj)
  \foreach\i in {0,...,4}
  {
    \pgfmathtruncatemacro{\temp}{\bottom[\i]}
    \node[red] at (\i*\l+0.5*\l,-5.5*\l) {$\temp$};
    \pgfmathtruncatemacro{\temp}{\right[\i]}
    \node[red,left] at (5.75*\l,-\i*\l-0.5*\l) {$\temp$};
  }
\end{tikzpicture}
\end{document}

结果如下: 在此处输入图片描述

相关内容