1

I am using the polynom package in order to create a horner scheme for two polynomials. The default scheme looks like this:

\documentclass[]{article}
\usepackage{amsmath}
\usepackage{polynom}

\begin{document}

\[ \polyhornerscheme[x=1]{x^3+3x^2-2x+5} \]

\end{document}

enter image description here

Is there a way, by using the polynom package, to custom the \polyhornerscheme command so it will change the position of number 1 as wel as the design of the scheme to look more like a tabular like the following picture?

enter image description here

  • Why not just write it as a tabular without polynom? – Werner Aug 4 '17 at 8:59
  • The polynom package computes automaticaly the coefficients. I don't know how to create such a command with tabular. – mac Aug 4 '17 at 9:05
2
+50

A modification of my code for Horner scheme with a tabular

The syntax is

\horner*[<length>]{<coefficients>}{<evaluated at>}

where the * means “hide the computation”, <length> is the width of the cells (default 2em), <coefficients> is the list of coefficients (only integers) of the polynomial and <evaluated at> is the point where the polynomial is evaluated at.

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

\newcolumntype{C}[1]{>{\centering$}p{#1}<{$}}

\ExplSyntaxOn
% from https://tex.stackexchange.com/questions/165517/
\NewDocumentCommand{\horner}{sO{2em}mm}
 {
  \IfBooleanTF{#1}
   { \bool_set_false:N \l_silke_show_bool }
   { \bool_set_true:N \l_silke_show_bool }
  \silke_horner:nnn { #2 } { #3 } { #4 }
 }

\bool_new:N \l_silke_show_bool
\seq_new:N \l_silke_top_seq
\seq_new:N \l_silke_middle_seq
\seq_new:N \l_silke_bottom_seq
\seq_new:N \l_silke_temp_seq
\int_new:N \l_silke_degree_int
\tl_new:N \l_silke_remainder_tl

\cs_new_protected:Npn \silke_horner:nnn #1 #2 #3
 {
  \seq_set_split:Nnn \l_silke_top_seq { , } { #2 }
  \int_set:Nn \l_silke_degree_int { \seq_count:N \l_silke_top_seq }
  \seq_clear:N \l_silke_middle_seq
  \seq_clear:N \l_silke_bottom_seq
  \seq_put_right:Nn \l_silke_middle_seq { }
  \seq_put_right:Nx \l_silke_bottom_seq
   {
    \int_to_arabic:n { \seq_item:Nn \l_silke_top_seq { 1 } }
   }
  \int_step_inline:nnnn { 2 } { 1 } { \l_silke_degree_int }
   {
    \seq_put_right:Nx \l_silke_middle_seq
     {
      \int_to_arabic:n { \seq_item:Nn \l_silke_bottom_seq { ##1 - 1 } * #3 }
     }
    \seq_put_right:Nx \l_silke_bottom_seq
     {
      \int_to_arabic:n
       {
        \seq_item:Nn \l_silke_top_seq { ##1 }
        +
        \seq_item:Nn \l_silke_middle_seq { ##1 }
       }
     }
   }
  \silke_print_scheme:nn { #1 } { #3 }
 }

\cs_new_protected:Npn \silke_print_scheme:nn #1 #2
 {
  \bool_if:NF \l_silke_show_bool
   {
    \silke_phantom:N \l_silke_middle_seq
    \silke_phantom:N \l_silke_bottom_seq
   }
  \seq_pop_right:NN \l_silke_bottom_seq \l_silke_remainder_tl
  \begin{tabular}{ *{\int_eval:n {\l_silke_degree_int-1}}{|C{#1}} || C{#1} | C{#1} | }
  \hline
  \seq_use:Nn \l_silke_top_seq { & } & #2 \tabularnewline
  \hline
  \seq_use:Nn \l_silke_middle_seq { & } \tabularnewline
  \cline{1-\l_silke_degree_int}
  \seq_use:Nn \l_silke_bottom_seq { & } & 
  \l_silke_remainder_tl \tabularnewline
  \cline{1-\l_silke_degree_int}
  \end{tabular}
 }

\cs_new_protected:Npn \silke_phantom:N #1
 {
  \seq_clear:N \silke_temp_seq
  \seq_map_inline:Nn #1 { \seq_put_right:Nn \silke_temp_seq { \phantom{##1} } }
  \seq_set_eq:NN #1 \silke_temp_seq
 }

\ExplSyntaxOff

\begin{document}

\horner{1,3,-2,5}{1}

\bigskip

\horner{3,2,-5,-10}{2}

\bigskip

\horner*{3,2,-5,-10}{2}

\bigskip

\horner[3em]{1,5,100,100,5,1}{-1}

\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.