pgfplotstable:“使用时创建”的问题,getprevrow(类似于“Horner 方案”的表格)

pgfplotstable:“使用时创建”的问题,getprevrow(类似于“Horner 方案”的表格)

我把解释文字写到图中,希望这样更容易解释:

在此处输入图片描述

没有内容的图片:

在此处输入图片描述

\documentclass[a4paper]{article}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.16}

\usepackage{xcolor, colortbl}

\pgfkeys{/pgf/number format/.cd,
fixed, 1000 sep={\,},
}

\begin{document}
\pgfmathtruncatemacro{\Divisor}{857}
\pgfmathtruncatemacro{\Base}{10}
\pgfmathtruncatemacro{\Exponent}{20}
% Text commands
\newcommand\SignForEmptyMultiplierShow{/}


\section{My modulo}
\pgfmathsetmacro{\BinExponent}{bin(\Exponent)}
\pgfmathsetmacro{\DimOfBinExponent}{dim(\BinExponent)}
\pgfmathtruncatemacro{\Binarydigits}{\DimOfBinExponent-1}


\colorbox{pink}{Example (!): $10^{\Exponent} \bmod \Divisor = \,?$} \par
Binary representation of the exponent:  $%~\big\vert
 \Exponent=\colorbox{cyan}{\pgfmathprintnumber{\BinExponent}}_{\,(2)}
% ,~~\dim(\BinExponent_{(2)}) = \DimOfBinExponent
$ (we can find these numbers again in the column "Occupation".)

\section{pgfplotstable try}
\pgfplotstableset{create on use/Binarydigit/.style={
create col/set list={\Binarydigits,...,0}},
columns/my new col/.style={string type}}

\pgfplotstableset{create on use/Occupation/.style={
create col/assign/.code={
\getthisrow{Binarydigit}\BinarydigitNo
\pgfmathtruncatemacro{\Binarydigit}{array({\BinExponent},\Binarydigits-\BinarydigitNo)}%
\pgfkeyslet{/pgfplots/table/create col/next content}\Binarydigit}}}

\pgfplotstableset{create on use/MultiplierShow/.style={
create col/assign/.code={
\getthisrow{Occupation}\occupier
\pgfmathsetmacro{\multiplier}{\occupier==1 ? "$\cdot \Base$" : "\SignForEmptyMultiplierShow"}%
\pgfkeyslet{/pgfplots/table/create col/next content}\multiplier}}}


\pgfplotstableset{create on use/MultiplierIs/.style={
create col/assign/.code={
\getthisrow{Occupation}\occupier
\pgfmathsetmacro{\multiplier}{\occupier==1 ? \Base : 1}%
\pgfkeyslet{/pgfplots/table/create col/next content}\multiplier}}}

\pgfplotstableset{create on use/Results/.style={
create col/assign/.code={
\getthisrow{Binarydigit}\BinarydigitNo
\getthisrow{Occupation}\occupier
\getthisrow{MultiplierIs}\multiplierX
\getthisrow{ModI}\modI
\pgfmathsetmacro{\results}{\BinarydigitNo==\Binarydigits ? \occupier*\Base : 1}%
\pgfkeyslet{/pgfplots/table/create col/next content}\results}}}

\pgfplotstableset{create on use/ModI/.style={
create col/assign/.code={
\getthisrow{Binarydigit}\BinarydigitNo
\getthisrow{Occupation}\occupier
\getthisrow{Results}\results
\pgfmathsetmacro{\ModI}{\BinarydigitNo==\Binarydigits ? mod(\results,\Divisor) : 1}%
\pgfkeyslet{/pgfplots/table/create col/next content}\ModI}}}


\pgfplotstableset{create on use/Square/.style={
create col/assign/.code={
\getthisrow{Binarydigit}\BinarydigitNo
\getthisrow{Occupation}\occupier
\getthisrow{Results}\results
\getthisrow{ModI}\modI
\pgfmathsetmacro{\Square}{\BinarydigitNo==\Binarydigits ? \modI*\modI : 1}%
\pgfkeyslet{/pgfplots/table/create col/next content}\Square}}}

\pgfplotstableset{create on use/ModII/.style={
create col/assign/.code={
\getthisrow{Binarydigit}\BinarydigitNo
\getthisrow{Occupation}\occupier
\getthisrow{Results}\results
\getthisrow{Square}\square
\pgfmathsetmacro{\ModI}{\BinarydigitNo==\Binarydigits ? mod(\square,\Divisor) : 1}%
\pgfkeyslet{/pgfplots/table/create col/next content}\ModI}}}




\pgfplotstablenew[columns={Binarydigit, Occupation, MultiplierShow, MultiplierIs, Results, ModI, Square, ModII}]{\DimOfBinExponent}\modulotable

\noindent\pgfplotstabletypeset[%string type, 
columns={Binarydigit, Occupation, MultiplierShow, MultiplierIs, Results, ModI, Square, ModII},
1000 sep={\,}, 
columns/Binarydigit/.style={column name=Binarydigit},
columns/Occupation/.style={column name=Occupation},
columns/MultiplierShow/.style={string type, column name=MultiplierShow},
columns/ModI/.style={column name=$\bmod\, \Divisor$},
columns/Square/.style={column name=Square},
columns/ModII/.style={column name=$\bmod\, \Divisor$},
]{\modulotable}

\section{This is what it should be}
\newcommand\R{\cellcolor{red}}
\newcommand\g{\cellcolor{green}}
\newcommand\G{\cellcolor{green!50!black}}
\begin{tabular}{c c c c c c c c }
Binarydigit & Occupation & Multiplier & Results & mod 857 & Square & mod 857  \\  
4 & 1 & $\cdot$10 & 10         & 10        & 100       & \g 100  \\
3 & 0 & /               &  \G 100 &  \G 100 & 10000    & \g 573  \\
2 & 1 & $\cdot$10 &   5730   &   588     & 345744  & \g 373  \\
1 & 0 & /               & \G  373 & \G  373 &  139129  &   295  \\
0 & 0 & /               & \R 295  & \R 295  & \R 87025 & \R 468  \\
%1 & 2 & 3 & 4 & 5 & 6 & 7  \\
\end{tabular}
\colorbox{pink}{$\Rightarrow 10^{20} \bmod{857} = 295$}

\begin{itemize}
\item The dark green cells does not need to be shown, so let them empty - same content in green cells!  {\color{red} How to get these previous cells for the "Square"-column  with pgfplots?}

\item The red part has to be completely  empty - a bigger modulo (468) than the previous (295) {\color{red}[I think\dots]}.
\end{itemize}
\end{document}

相关内容