0

is there a package that will automate the typesetting of the triangular table used to calculate the coefficients used in Newton's divided differences algorithm? but in a table like the following image

enter image description here

  • There may not be a package but if you spell out what this algorithm should do, your chances of getting what you're after will increase. –  Jan 13 '20 at 00:26
  • 1
    Package, which typeset such a table directly doesn't exist. Your table can designed by use multirow packages. What you try so far? – Zarko Jan 13 '20 at 00:27
  • https://tex.stackexchange.com/questions/118219/newton-finite-differences-with-tikz – js bibra Jan 13 '20 at 01:20

1 Answers1

1

With TikZ it is fairly easy to create something that computes the entries. This answer defines such a pic. All you need to say is

\begin{tikzpicture}
 \pic{difftable={0,1,3,5}{3,2,4,-1}};
\end{tikzpicture}

where the first list contains the xi and the second the fi.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,patterns}
\usepackage{eqparbox}
\newbox\eqnodebox
\tikzset{equal size/.style={execute at begin
    node={\setbox\eqnodebox=\hbox\bgroup},
    execute at end node={\egroup\eqmakebox[#1][c]{\copy\eqnodebox}}},
    equal size/.default=A,}
\newcounter{difftable}  
\makeatletter
\pgfmathdeclarefunction{Dim}{1}{%
\begingroup%
\pgfutil@tempcnta0%
\@for\pgfutil@tempa:=#1\do{\advance\pgfutil@tempcnta1}%
\edef\pgfmathresult{\the\pgfutil@tempcnta}%
\pgfmathsmuggle\pgfmathresult\endgroup%
}
\pgfmathdeclarefunction{diffquots}{2}{%
\begingroup%
\pgfutil@tempcnta1%
\pgfmathtruncatemacro{\pgfutil@tempa}{min(Dim("#1"),Dim("#2"))}%\typeout{dim=\pgfutil@tempa}%
\pgfutil@loop
\pgfmathsetmacro{\pgfutil@tempb}{{#1}[\the\pgfutil@tempcnta]-{#1}[\the\numexpr\pgfutil@tempcnta-1]}%
\pgfmathsetmacro{\pgfutil@tempc}{{#2}[\the\pgfutil@tempcnta]-{#2}[\the\numexpr\pgfutil@tempcnta-1]}%
\ifdim\pgfutil@tempc pt=0pt\relax
\tikzerror{You asked me to divide by zero. Terminating.}%
\fi
\pgfmathsetmacro{\pgfutil@tempd}{\pgfutil@tempb/\pgfutil@tempc}%
\pgfmathsetmacro{\pgfutil@tempe}{{#2}[\the\pgfutil@tempcnta]/2+{#2}[\the\numexpr\pgfutil@tempcnta-1]/2}%
\ifnum\pgfutil@tempcnta=1\relax
\edef\pgfutil@tempf{\pgfutil@tempd}%
\edef\pgfutil@tempg{\pgfutil@tempe}%
\else
\edef\pgfutil@tempf{\pgfutil@tempf,\pgfutil@tempd}%
\edef\pgfutil@tempg{\pgfutil@tempg,\pgfutil@tempe}%
\fi
\advance\pgfutil@tempcnta1%
\ifnum\pgfutil@tempcnta<\the\numexpr\pgfutil@tempa-1%
\pgfutil@repeat
\edef\pgfmathresult{"\pgfutil@tempf","\pgfutil@tempg"}%
\pgfmathsmuggle\pgfmathresult\endgroup%
}%
\makeatother
\tikzset{result/.style={draw,text height=1em,text depth=0.35ex,
 inner xsep=0.5ex,align=center,equal size=\pgfkeysvalueof{/tikz/difftable/id}-#1},
 pics/difftable/.style 2 args={code={\stepcounter{difftable}
   \tikzset{difftable/id=\number\value{difftable}}
   \node[result=1] (xi-1) {$x_i$}; %
   \path foreach \X [count=\Y] in {#1}
    {node[result=1,below={-\pgflinewidth} of xi-\Y,alias=xi-last] 
      (xi-\the\numexpr\Y+1)   {\pgfmathprintnumber{\X}}};
   % 
   \node[result=2,right={-\pgflinewidth} of xi-1] (fi-1) {$f(x_i)$}; 
   \path foreach \X [count=\Y] in {#2}
    {node[result=2,below={-\pgflinewidth} of fi-\Y,alias=fi-last] (fi-\the\numexpr\Y+1) 
    {\pgfmathprintnumber{\X}}};
   % 
   \pgfmathsetmacro{\myresultA}{diffquots("#1","#2")}
   \pgfmathsetmacro{\lstdfi}{{\myresultA}[0]}
   \pgfmathsetmacro{\lstdxi}{{\myresultA}[1]}
   \node[result=3,right={-\pgflinewidth} of fi-1] (dfi-1) {$DD1$};  
   \path foreach \X [count=\Y starting from 2] in \lstdfi
    {node[result=3,alias=dfi-last]  (dfi-\Y) at 
    (dfi-1|-fi-\Y.south east)  {\pgfmathprintnumber{\X}}};
   %
   \pgfmathsetmacro{\myresultB}{diffquots("\lstdfi","\lstdxi")}
   \pgfmathsetmacro{\lstddfi}{{\myresultB}[0]}
   \pgfmathsetmacro{\lstddxi}{{\myresultB}[1]}
   \node[result=4,right={-\pgflinewidth} of dfi-1] (ddfi-1) {$DD2$};  
   \path foreach \X [count=\Y starting from 2] in \lstddfi
    {node[result=4,alias=ddfi-last]  (ddfi-\Y) at 
    (ddfi-1|-dfi-\Y.south east)  {\pgfmathprintnumber{\X}}};
   %
   \pgfmathsetmacro{\myresultC}{diffquots("\lstddfi","\lstddxi")}
   \pgfmathsetmacro{\lstdddfi}{{\myresultC}[0]}
   \pgfmathsetmacro{\lstdddxi}{{\myresultC}[1]}
   \node[result=5,right={-\pgflinewidth} of ddfi-1] (dddfi-1) {$DD3$};  
   \path foreach \X [count=\Y starting from 2] in \lstdddfi
    {node[result=5,alias=dddfi-last]  (dddfi-\Y) at 
    (dddfi-1|-ddfi-\Y.south east)  {\pgfmathprintnumber{\X}}};
   %
   \path[pattern=north east lines] (fi-2.north east) 
    foreach \X in {d,dd,ddd}
    {|- (\X fi-2.north east) }
    |- cycle
    (fi-last.south east) 
    foreach \X in {d,dd,ddd}
    {|- (\X fi-last.south east) }
    |- cycle;
   \draw[thick] (xi-1.north west) rectangle (xi-last.south-|dddfi-1.east);
   }},
   difftable/.cd,id/.initial=0}
\begin{document}
\begin{tikzpicture}
 \pic{difftable={0,1,3,5}{3,2,4,-1}};
\end{tikzpicture}
\bigskip

\begin{tikzpicture}
 \pic{difftable={0,1,3,5}{2,5,-3,1}};
\end{tikzpicture}

\end{document}

enter image description here