Math 455.1 • 25 February 2009

Displaying tables

The Mathematica examples below involve the sum (∑_(j=1)^n)j , that is, 1+2+⋯ + n, of the first n positive integers

Begin an "experiment" to see what such sums are:

Table[{n,Sum[j,{j,1,n}]},{n,1,10}]

If you like, you may use the Classroom Assistant from the Palettes menu in Mathematica 7 (or the Basic Input palette in Mathematica 6) to obtain the familiar sigma notation for the sum instead of Sum[j, {j, 1, n}]}:

Table[{n,(∑_(j=1)^n)j},{n,1,10}]

Display this as a table whose first column has values of n and whose second column has the sum (∑_(j=1)^n)j as directly calculated by Mathematica:

Table[{n,Sum[j,{j,1,n}]},{n,1,10}] // TableForm

You might see the pattern here—the "closed-form" formula  (∑_(j=1)^n)j=((n(n+1))/2) . Here's the same table, but with an additional column where we calculate the sum using our conjectured "closed-form" formula:

calculations=Table[{n,Sum[j,{j,1,n}],((n(n+1))/2)},{n,1,10}]

And the same thing displayed as a table:

calculations//TableForm

We should really label the columns with column headers. Form the list of the headers:

headers={"n","(∑_(j = 1)^n)j","((n (n + 1))/2)"};

And now display the table again, but with these headers for the columns (and no headings for the rows):

TableForm[calculations,TableHeadings->{None,headers}]

That's already easy to understand. But to make it even better, we want to use traditional mathematical notation, in which variable names are set in italic. The idea behind doing this is to evaluate TraditionalForm at each of the three headers.

{TraditionalForm[n],TraditionalForm[(∑_(j=1)^n)j],TraditionalForm[((n(n+1))/2)]}

Oops: Mathematica went right ahead and evaluated the sum as (1/2)n(n+1): it knows the closed-form formula! To prevent it from doing so—and to prevent it from inadventently using some global value you might have assigned previously to n, wrap each of the three expression with HoldForm to prevent it from being evaluated:

{TraditionalForm[HoldForm[n]],TraditionalForm[HoldForm[(∑_(j=1)^n)j]],TraditionalForm[HoldForm[((n(n+1))/2)]]}

The result is really nice, although the input has become rather verbose. To shorten the input  expression, use Map to evaluate TraditionalForm automatically at each member of the list of three HoldForm exressions:

Map[TraditionalForm,{HoldForm[n],HoldForm[(∑_(j=1)^n)j],HoldForm[((n(n+1))/2)]}]

To make the input even more concise, use the abbreviation f/@lis for Map[f,lis]:

niceHeaders=TraditionalForm/@{HoldForm[n],HoldForm[(∑_(j=1)^n)j],HoldForm[((n(n+1))/2)]}

So here is the final version of the table:

TableForm[calculations,TableHeadings->{None,niceHeaders}]

Another way to display a table is to use Grid, which easily allows insertion of rules to separate the rows and columns:

Grid[Prepend[calculations,niceHeaders],Frame->All]