I want simple syntax that results in this:
{{f[1, 2], g[1, 2]}, {f[3, 4], g[3, 4]}}
though I'm willing to settle for:
{{f[{1, 2}], g[{1, 2}]}, {f[{3, 4}], g[{3, 4}]}}
if that's easier somehow. (And for completeness, and Re: the answer I accepted, I'm also fine with the form
{{f[1, 2], f[3, 4]}, {g[1, 2], g[3, 4]}}
which differs from the original form by a simple Transpose
anyway.)
I would expect the function Through
to help here, but it doesn't seem able to work with functions that take multiple arguments. My naive attempt looks like this:
Through@{f, g}[{{1, 2}, {3, 4}}]
{f[{{1, 2}, {3, 4}}], g[{{1, 2}, {3, 4}}]} (*out*)
which is clearly not what I want (as it does no threading over the arguments). If a function is defined to take one argument, then Through makes the most of that:
f[x_] := x^2
g[x_] := x^3
Through@{f, g}[{{1, 2}, {3, 4}}]
{{{1, 4}, {9, 16}}, {{1, 8}, {27, 64}}} (*out*)
(f and g now get applied to the lowest level, where there's only 1 argument.) So what I want is a way to get Through to 'make the most' of functions that take multiple arguments. Instead it gives me:
Clear[f, g]
f[x_, y_] := x^2 + y
g[x_, y_] := x^3 + y
Through@{f, g}[{{1, 2}, {3, 4}}]
{f[{{1, 2}, {3, 4}}], g[{{1, 2}, {3, 4}}]} (*out*)
Through @* {f, g} /@ {{1, 2}, {3, 4}}
. $\endgroup$f
andg
with only one argument each (the monomial case) you get the desired result not becauseThrough
works like you'd expect but because Mathematica can raise lists to (integer) powers $\endgroup$@*
instead of application@
; the former shortcut only became available in recent versions. What version are you using? $\endgroup$Through@*{Apply[f], Apply[g]} /@ {{1, 2}, {3, 4}}
to get rid of the inner lists. $\endgroup$