Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

One thing I would like to do in many instances is arrange input/output in columns for comparison. I expect that I need to use Grid to do this, but have not been able to figure out the right commands. For example, if I have the following type of function and two different possibilities:

Table[GCD[fun1[c^a], fun1[c^b]], {a, 1, 3}, {b, 1, 3}, {c, 1, 3}]

Table[GCD[fun2[c^a], fun2[c^b]], {a, 1, 3}, {b, 1, 3}, {c, 1, 3}]

Here, I have two custom functions fun1 and fun2 which take multiple inputs over ranges (27 possibilities in all). I want the output to be rearranged so I have the following columns:

a, b, c, GCD[fun1[c^a], fun1[c^b]], GCD[fun2[c^a], fun1[c^b]], difference

In otherwords I want to see the combination of inputs being used in the leftmost columns, the ouptuts from the two possible expressions, one using fun1, the other using fun2, then optionally a final column showing the difference between the two possible outputs for each of the 27 possibilities.

How can I do this?

share|improve this question

2 Answers 2

Table[{a, b, c, GCD[fun1[c^a], fun1[c^b]], GCD[fun2[c^a], fun2[c^b]]}, 
    {a, 1, 3}, {b, 1, 3}, {c, 1, 3}] // TableForm

and with the final differences:

Table[{a, b, c, x = GCD[fun1[c^a], fun1[c^b]], y = GCD[fun2[c^a], fun2[c^b]], x - y}, 
     {a, 1, 3}, {b, 1, 3}, {c, 1, 3}] // TableForm

You can reformat the table by

Flatten[Table[{a, b, c, x = GCD[fun1[c^a], fun1[c^b]],  
   y = GCD[fun2[c^a], fun2[c^b]], x - y}, {a, 1, 3}, {b, 1, 3}, {c, 1, 3}], 2] //TableForm

as suggested by cormullion to get it into single columns.

share|improve this answer
    
This does not make a single set of columns of the results. It creates 3 separate columns which wrap around. –  Tyler Durden Oct 21 '13 at 15:34
    
@TylerDurden Try Flatten[Table[...], 2] // TableForm perhaps? –  cormullion Oct 21 '13 at 15:56

If I understand you:

{fun1, fun2} = {Prime, Fibonacci};

Table[
   {a, b, c, #, #2, # - #2} &[
    GCD[fun1[c^a], fun1[c^b]],
    GCD[fun2[c^a], fun2[c^b]]
   ],
   {a, 3}, {b, 3}, {c, 3}
] ~Flatten~ 2 // Grid

Also look at Outer and Array. For the example above I needed to Flatten, so I might consider using Tuples instead, which always produces a flat list:

{fun1, fun2} = {Prime, Fibonacci};

row[{a_, b_, c_}] := {a, b, c, #, #2, # - #2} &[
  GCD[fun1[c^a], fun1[c^b]],
  GCD[fun2[c^a], fun2[c^b]]
 ]

row /@ Tuples[Range@{3, 3, 3}] // Grid
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.