Creating Difference Tables from Data

©2003Kenneth Levasseur
Mathematical Sciences
UMass Lowell
Kenneth_Levasseur@uml.edu

Definitions - initialization cells. Respond with "yes" if asked whether to evaluate them)

BeginPackage["Differences`"];

Differences::usage=
    "Differences[data_List] computes the list of first differences in data";
DifferencesTable::usage=
    "DifferencesTable[data_List] generates a formatted difference table for data.";
Interpolate::usage=
    "Interpolate[data_List,x] generates the polynomial function of x that has degree at most Length[data].  Interpolate[table] will do the same if table is an output from DifferenceTable.";
Delta::usage=
    "Delta[f_Function] generates the first forward difference function of f.";

Δ::usage="Δ is the differences operator symbol";
Begin["`Private`"];

Differences[input_List]:=Partition[input,2,1]/.{{a_,b_}->b-a};
DifferencesTable[input_List]:=
    DT[FixedPointList[Differences,input,Length[input]-1,
        SameTest->((Length[Union[#2]]==1)&)]];

\(Format[
        DT[e_]] := \(\(\(Map[PadRight[#, Length[e[\([1]\)]], " "] &, e] // 
              MapThread[
                  Prepend[#1, Δ\^"(" <> ToString[#2 - 1] <> ")"] &, {#, Range[Length[#]]}] &\) // 
            Prepend[#, Prepend[Range[0, Length[First[e]] - 1], "n"]] &\) // 
          Transpose\) // TableForm\)

Interpolate[DT[e_],x_]:=
    e//Map[First,#]&//
Inner[Times,#,Table[Binomial[x,k],{k,0,Length[#]-1}],Plus]&;

Interpolate[input_List,x_]:=Interpolate[DifferencesTable[input],x];

Delta[f_] := (f[#+1]-f[#])&; 
End[ ];
                   
EndPackage[ ];

Examples

data = Range[8]^2

{data, Differences[data], Differences[Differences[data]]}

dt = DifferencesTable[data]

p = Interpolate[dt, y]

Factor[p]

q = Interpolate[{1, 2, 4, 8, 16}, x]

Expand[q]

Factor[q]

Delta[Sin[#] &]

NestList[Delta[#] &, f, 4]

The output above is messy because it is a list of functions.  Here are formulae in terms of x for the first five difference columns (starting with the 0th column).

Map[#[x] &, NestList[Delta[#] &, f, 4]] // TableForm

w[x_] := x^3

Map[#[x] &, NestList[Delta, w, 4]] // TableForm

Map[#[x] &, NestList[Delta, w, 5]] // Simplify // 
      TableForm

Using this file as a package

If you save this file and the file Differences.m in the Mathematica directory   AddOns/Applications, then you can load the definitions that are in this Notebook by simply evaluating the expression

<< Differences`

or the expression

Needs["Differences`"]