# Function apply to three dimensions list

I defined a function

f[x_, y_] := {x - y, x + y}


and I have a list

d1 = {{1, 2}, {3, 4}, {5, 6}};


I know how to input the list in a simple way,

f @@ Transpose[d1]


return

{{-1, -1, -1}, {3, 7, 11}}


But I don't know how to apply a high dimensions list, for example,

d2 = {{{1, 2}, {3, 4}, {5, 6}}, {{1, 2}, {3, 4}, {5, 6}}, {{1, 2}, {3,
4}, {5, 6}}, {{1, 2}, {3, 4}, {5, 6}}};


Is there a simple way return results?

f @@@ Transpose /@ d2


{{{-1, -1, -1}, {3, 7, 11}}, {{-1, -1, -1}, {3, 7, 11}}, {{-1, -1, -1}, {3, 7, 11}}, {{-1, -1, -1}, {3, 7, 11}}}

• Thank you, it is a good way. Commented Jun 5, 2019 at 4:46

You can extend the definition of f to accommodate data like d2:

fx = (Transpose[#] &) /* Apply[f]


Then evaluating fx /@ d2 produces:

{ {{-1, -1, -1}, {3, 7, 11}}, {{-1, -1, -1}, {3, 7, 11}},
{{-1, -1, -1}, {3, 7, 11}}, {{-1, -1, -1}, {3, 7, 11}} }


Or you can simply use the third argument of Apply to apply the provided function at a desired level in an expression eg.

Apply[f, d2, {2}]


evaluates to

{ {{-1, 3}, {-1, 7}, {-1, 11}}, {{-1, 3}, {-1, 7}, {-1, 11}},
{{-1, 3}, {-1, 7}, {-1, 11}}, {{-1, 3}, {-1, 7}, {-1, 11}} }


which is identical to what you get if you evaluate fx /* Transpose /@ d2.

## A side note

The definition of f takes two parameters and outputs a list of two elements.

It would make sense (to my mind, at least) to expect that when f is applied on a list of pairs of elements, the output would be a list of pairs of elements.

In that sense, using Transpose in the first place in order to supply the proper number of arguments to f seems counterintuitive. It is definitely not wrong or something that doesn't evaluate or anything of that sort but it can cause complications.

Using the level specification in Apply seems a good way to apply f on data in deeper levels.