5
$\begingroup$

I've been working on creating tables of differences in MAA similar to the image below, but with a bit more detail:

Example difference Table

I've written a function that will take an anonymous function, a range of values and some optional arguments to create the table:

differenceTable[f_: Function[{g}, g], range_List, n_: 3, diff_: 3, p_: 2] :=
Module[
{a = N[f /@ range, n]}, 
Grid[
{{"x"} ~ Join ~ {"f[x]"} ~ Join ~ (Table[Superscript[\[CapitalDelta], k], {k, 1, diff}]),
{Column[range, Center, 1]} ~ Join ~ (Column[#, Center, 1] & /@ (SetPrecision[#, p] & /@ diffList[a, diff + 1]))},
Spacings -> 2, 
Dividers -> {{False, True}, {False, True}}
]
]

with,

diffList[x_List, n_] := 
  Module[{d = Differences[x]}, {x} ~ Join ~diffList[d, n - 1]];
diffList[x_List, 1] := {x}

For example:

differenceTable[Sin[#] &, Range[10, 90, 10] Degree]

gives

My Difference Table

Which is great. The problem I run into is that when the function values and the x values take on non-standard formatting (fractional notation and exponentials specifially) the spacing between rows is thrown off.

For example:

differenceTable[Sin[#] &, Range[Pi/18, Pi/2, Pi/18]]

gives:

my difference table 2

I'm not sure how to fix this without eliminating the taper in the table, which I would like to keep. Does anyone know of a fix for this?

$\endgroup$
  • $\begingroup$ Quick fix would be changing spacing for second column to 1.75. $\endgroup$ – swish May 27 '13 at 23:06
6
$\begingroup$

I've started from the beginning, but with Grid with number of rows equal to 2*Length[range] and SpanFromAbove inside:

     diffT[f_, range_, columns_] := 
      Module[{n = Length@range, RF, dif, grid, form},
      RF = Append[Evaluate@Riffle[#, SpanFromAbove], SpanFromAbove] &;
      form = NumberForm[N@#, {5, 4}] &;
      dif = NestList[Differences, f@# & /@ range, columns];
      grid = PadRight[#, 2 n, ""] & /@ (PadLeft[#, n + Length@#/2,""] & /@ (RF /@ (Map[form, dif, {2}])));
      Grid[
      Transpose[{RF@range, ##} & @@ grid]
      , ItemSize -> 4]
       ]

enter image description here

You can easily add all Your arguments or Headings.

It defenitely need to be optimized. Thanks in advance for remarks.

$\endgroup$

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.