Iterating Composition over a list of tuples
Unless you really want the functions to take different numbers of arguments (which is not clear from the question):
a[z_] := z + 1/2
r[z_] := 1/z
tuples = Tuples[{a, r}, 2]
Composition[##][x] & @@@ tuples
{1 + x, 1/2 + 1/x, 1/(1/2 + x), x}
I will redefine your functions so that they both take two inputs, and the both output fraction
so that in the composition, we can keep track of what fraction
is, even though r
doesn't depend on it:
Clear[a, r]
a[{z_, frac_}] := {z + frac, frac}
r[{z_, frac_}] := {1/z, frac}
Then, we will define a function that takes as inputs z
and frac
, forms all compositions of the function, then applies the compositions to the inputs. Finally, at the end, we extract just the desired output:
applyComposition[z_, frac_] := First /@ With[
{f = Composition @@@ Tuples[{a, r}, 2]},
Through[f[{z, frac}]]
]
Then,
applyComposition[x, frac]
(* {2 frac + x, frac + 1/x, 1/(frac + x), x} *)
A pure operator version of corey's answer (i.e., a version without a pure function) could go:
tf = Through @* (Composition @@@ tuples)
Through@*{a@*a, a@*r, r@*a, r@*r}
Then:
tf[x]
{1 + x, 1/2 + 1/x, 1/(1/2 + x), x}