# Create simple table with function of column values

I just want to create a simple table:

Three input columns, one output column

Call the inputs {x,y,z}, each can take values {-1,0,1}

The output column should be some function f(x,y,z)

Sample table:

x  | y  | z  | f(x,y,z)

-1 | -1 | -1 | f(-1,-1,-1)

1  | -1 | -1 | f(1,-1,-1)


etc... (all possible combinations)

Thanks!

-
{##, f[##]} & @@@ Tuples[{-1, 0, 1}, {3}] but this is very likely a duplicate. Also, Hello :), have you tried anything? What with the documentation for "table"? Is it about Mathematica? I'm asking because your syntax does not seem to reflect it is so. – Kuba Oct 25 '13 at 19:50
@kuba Could be a duplicate, but that would be difficult too find. In the meantime, could you use your comment as an answer, so that our answer rate improves? ;-) – Sjoerd C. de Vries Oct 25 '13 at 20:07
@SjoerdC.deVries done :) Also, related - summation with constraints. – Kuba Oct 25 '13 at 20:37

Kuba has shown us how to generate the table you want, but didn't give any advice about displaying it in a way looks nice. I'm assuming you are interested in learning a little about formatting as well.

There are, of course, an unlimited number of ways to format data in Mathematica. I will discuss just two, one using TableForm and the other using Grid.

data = {##, f[##]} & @@@ Tuples[{-1, 0, 1}, {3}];


A quick and somewhat dirty method using TableForm:

TableForm[data,
TableHeadings -> {None, {"x", "y", "z", "f(x,y,z)"}},
TableAlignments -> Right]


This produces

The one glaring problem with TableForm is that the right-aligned last column is ugly. Unfortunately, TableForm AFAIK doesn't allow controlling the alignments on a column-by-column basis.

### Edit

Thanks to Kuba for pointing out that my formatting expression using Grid could be much simplified.

A better method using Grid:

Grid and its associated functions, such as Item and others, allow for much finer control over table formatting. But Grid is not always easy to use. Indeed, it can sometimes seem difficult even to accomplish what one hopes will be a trivial bit of formatting. I have seen posts cursing Grid as even dirtier than TableForm. In this case, Grid is easy and gives good results.

labeledData = Prepend[data, {"x", "y", "z", "f(x,y,z)"}];
Grid[labeledData,
Alignment -> {{Right, Right, Right, Left}},
Dividers -> {{-2 -> True}, {2 -> True}},
Spacings -> {2, Automatic}]


-
No need to Map with Item. Alignment -> {{Right, Right, Right, Left}} will do. +1. – Kuba Oct 26 '13 at 12:23
@Kuba. I missed that when reading the docs. Thanks for pointing it out. Will make edit to include this as it is a major improvement. – m_goldberg Oct 26 '13 at 12:53

Straightforward way to achieve this is, just after reading the documentation for Table:

v1 = Table[{i, j, k, f[i, j, k]}, {i, #}, {j, #}, {k, #}] &[{-1, 0, 1}] // Flatten[#, 2] &

{{-1,-1,-1,f[-1,-1,-1]},{-1,-1,0,f[-1,-1,0]},<<24>>,{1,1,1,f[1,1,1]}}


A little improved usage of Table:

v2 = Table[Flatten[{i, f @@ i}], {i, Tuples[{-1, 0, 1}, {3}]}];


or, what I usually do, not the king of performance but clear and compact Apply:

v3 = {##, f[##]} & @@@ Tuples[{-1, 0, 1}, {3}];


variation with Array:

v4 = Array[{##, f[##]} &, {3, 3, 3}, {{-1, 1}}] // Flatten[#, 2] &


v1 == v2 == v3 == v4

True

-
Wonderful, thanks!! Couldn't figure out the documentation.. – Kurt Oct 25 '13 at 20:24
@Kurt Press F1 and use it :) or use online documentation. Take a look at Table‌​, especially the 5th syntax. I'm glad you find it useful. Good luck :) – Kuba Oct 25 '13 at 20:32