Assume I have defined the following function:

FF[{x_, y_}] := {x^2 + y^3, 3.5*x*y}

I would like to apply FF on a list of input such as:

XX = {xa, xb, xc}
YY = {ya, yb, yc}

In order to do it I input the following syntax:

MapThread[FF, {XX, YY}]

but as output I get just the first element of the function:

{xa^2+ya^3, xb^2+yb^3, xc^2+yc^3}

what am I doing wrong here?

  • 9
    $\begingroup$ Try MapThread[FF[{##}] &, {XX, YY}]; alternatively, redefine FF[]: FF[x_, y_] := {x^2 + y^3, 3.5*x*y}. $\endgroup$ – J. M. will be back soon Sep 5 '12 at 7:00
  • $\begingroup$ Thanks a lot kguler this works!!! now to better understand, what is ACTUALLY the differrence between FF[x_, y_] := {x^2 + y^3, 3.5*xy} and the original FF[{x_, y_}] := {x^2 + y^3, 3.5*xy} as Mathematica understand it? Doron $\endgroup$ – Doron Sep 5 '12 at 7:31
  • $\begingroup$ Doron, I guess the 'thank you" and the follow up question were meant for @J.M.:) $\endgroup$ – kglr Sep 5 '12 at 7:42
  • 3
    $\begingroup$ MapThread[FF, {XX, YY}] gives {FF[xa, ya], FF[xb, yb], FF[xc, yc]}, not {xa^2+ya^3,xb^2+yb^3,xc^2+yc^3}. That is, it returns FF unevaluated, because FF requires a single list as its argument, so it is not defined when it is passed a Sequence of two arguments. $\endgroup$ – kglr Sep 5 '12 at 7:46

I would not use MapThread in this case. From the point of functional programming I consider this

FF /@ Transpose[{XX, YY}]

as more consistent. This is because the signature of FF, namely taking a tuple and returning a tuple, is exactly what you want to express mathematically. Therefore, I would not use the solution

MapThread[FF[{##}] &, {XX, YY}]

because although it works, it has to recreate the tuple parameter by wrapping FF with a pure function.

Another point: Maybe you know that most basic operations like multiplication, powers, etc work with list arguments. Thats why you could spare the Map (/@) of my first example and simply write

Transpose[FF[{XX, YY}]]
  • $\begingroup$ I think I get it. nevertheless (and I might again be worng...), I think thak fore a more complicate case such as, for example FFFF[x_, y_] := {x^2 + y^3, 3.5*xy, xy^0.5} the Only thing that will work is MapThread[FFFF, {XX, YY}]. right? $\endgroup$ – Doron Sep 5 '12 at 14:01
  • $\begingroup$ You have almost always several possibilities. For an f[x_, y_] := {x^2 + y^3, 3.5*x*y, x*y^0.5} you could write f @@@ Transpose[{XX, YY}] too. $\endgroup$ – halirutan Sep 5 '12 at 14:08
  • 1
    $\begingroup$ @Doron, I did not want to force you in one direction which is the best (because it always depends). I wanted to show you that since we have this huge amount of functional operations in Mathematica, you should try to implement your idea. $\endgroup$ – halirutan Sep 5 '12 at 14:12

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.