0
$\begingroup$

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?

$\endgroup$
3
$\begingroup$
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.

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

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
| improve this answer | |
$\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.