Command for the table Ruffini-Horner algorithm

We can build the table row by row; the syntax is:

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

Here are the macros:

\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}

enter image description here

A variation for the “Italian style” scheme.

\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}

enter image description here


There is a much easier way to get this: using the polynom package, as follows:

\usepackage{polynom}

and

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