表格 Ruffini-Horner 算法的命令

表格 Ruffini-Horner 算法的命令

我正在尝试用 LaTeX 编写一个命令来输入用于计算 Ruffini-Horner 算法的表格。您可以在上面的 Wikipedia 链接中看到,这些是相当简单的表格。在第一列之后的开头有一条垂直线,在最后一行之前,结尾有一条水平线。

我想要一个命令,可以传递表中的数字条目并绘制其余部分。

这个怎么做?

我希望\ruffini{...}在条目中输入数字列表,可能用逗号分隔。或者甚至可以输入带有分隔符的数字,&\\格式化它们在表格中的位置。但我不想考虑表格中的行。我希望命令能够处理行。

答案1

我们可以逐行构建表格;语法是:

\ruffini{<list of coefficients>}
        {<divisor>}
        {<list of numbers for the computation>}
        {<list of coefficients for the result>}

以下是宏:

\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\ruffini}{mmmm}
 {% #1 = polynomial, #2 = divisor, #3 = middle row, #4 = result
  \franklin_ruffini:nnnn { #1 } { #2 } { #3 } { #4 }
 }

\seq_new:N \l_franklin_temp_seq
\tl_new:N \l_franklin_scheme_tl
\int_new:N \l_franklin_degree_int

\cs_new_protected:Npn \franklin_ruffini:nnnn #1 #2 #3 #4
 {
  % Start the first row
  \tl_set:Nn \l_franklin_scheme_tl { #2 & }
  % Split the list of coefficients
  \seq_set_split:Nnn \l_franklin_temp_seq { , } { #1 }
  % Remember the number of columns
  \int_set:Nn \l_franklin_degree_int { \seq_count:N \l_franklin_temp_seq }
  % Fill the first row
  \tl_put_right:Nx \l_franklin_scheme_tl
   { \seq_use:Nnnn \l_franklin_temp_seq { & } { & } { & } }
  % End the first row and leave two empty places in the next
  \tl_put_right:Nn \l_franklin_scheme_tl { \\ & & }
  % Split the list of coefficients and fill the second row
  \seq_set_split:Nnn \l_franklin_temp_seq { , } { #3 }
  \tl_put_right:Nx \l_franklin_scheme_tl
   { \seq_use:Nnnn \l_franklin_temp_seq { & } { & } { & } }
  % End the second row
  \tl_put_right:Nn \l_franklin_scheme_tl { \\ }
  % Compute the \cline command
  \tl_put_right:Nx \l_franklin_scheme_tl
   {
    \exp_not:N \cline { 2-\int_to_arabic:n { \l_franklin_degree_int + 1 } }
   }
  % Leave an empty place in the third row (no rule either)
  \tl_put_right:Nn \l_franklin_scheme_tl { \multicolumn{1}{r}{} & }
  % Split and fill the third row
  \seq_set_split:Nnn \l_franklin_temp_seq { , } { #4 }
  \tl_put_right:Nx \l_franklin_scheme_tl
   { \seq_use:Nnnn \l_franklin_temp_seq { & } { & } { & } }
  % Start the array (with \use:x because the array package
  % doesn't expand the argument)
  \use:x
   {
    \exp_not:n { \begin{array} } { r | *{\int_use:N \l_franklin_degree_int} { r } }
   }
  % Body of the array and finish
  \tl_use:N \l_franklin_scheme_tl
  \end{array}
 }
\ExplSyntaxOff

\begin{document}
\[
\ruffini{1,-6,11,-6}{2}{2,-8,6}{1,-4,3,0}
\]
\end{document}

在此处输入图片描述

“意大利风格”方案的变体。

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

\ExplSyntaxOn

\NewDocumentCommand{\ruffini}{mmmm}
 {% #1 = polynomial, #2 = divisor, #3 = middle row, #4 = result
  \franklin_ruffini:nnnn { #1 } { #2 } { #3 } { #4 }
 }

\seq_new:N \l_franklin_temp_seq
\tl_new:N \l_franklin_scheme_tl
\int_new:N \l_franklin_degree_int

\cs_new_protected:Npn \franklin_ruffini:nnnn #1 #2 #3 #4
 {
  % Start the first row
  \tl_set:Nn \l_franklin_scheme_tl { & }
  % Split the list of coefficients
  \seq_set_split:Nnn \l_franklin_temp_seq { , } { #1 }
  % Remember the number of columns
  \int_set:Nn \l_franklin_degree_int { \seq_count:N \l_franklin_temp_seq }
  % Fill the first row
  \tl_put_right:Nx \l_franklin_scheme_tl
   { \seq_use:Nn \l_franklin_temp_seq { & } }
  % End the first row and leave two empty places in the next
  \tl_put_right:Nn \l_franklin_scheme_tl { \\ #2 & & }
  % Split the list of coefficients and fill the second row
  \seq_set_split:Nnn \l_franklin_temp_seq { , } { #3 }
  \tl_put_right:Nx \l_franklin_scheme_tl
   { \seq_use:Nn \l_franklin_temp_seq { & } }
  % End the second row
  \tl_put_right:Nn \l_franklin_scheme_tl { \\ \hline }
  % Split and fill the third row
  \seq_set_split:Nnn \l_franklin_temp_seq { , } { #4 }
  \tl_put_right:Nx \l_franklin_scheme_tl
   { & \seq_use:Nn \l_franklin_temp_seq { & } }
  % Start the array (with \use:x because the array package
  % doesn't expand the argument)
  \use:x
   {
    \exp_not:n { \begin{array} } { r | *{\int_eval:n { \l_franklin_degree_int - 1 }} { r } | r }
   }
  % Body of the array and finish
  \tl_use:N \l_franklin_scheme_tl
  \end{array}
 }
\ExplSyntaxOff

\begin{document}
\[
\ruffini{1,-6,11,-6}{2}{2,-8,6}{1,-4,3,0}
\]
\end{document}

在此处输入图片描述

答案2

有一种更简单的方法可以实现这一点:使用polynom包,如下:

\usepackage{polynom}

\begin{center}
  \polyhornerscheme[x=2]{x^3-6x^2+11x-6}
\end{center}

相关内容