Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to learn the functional style which seems to prevail in Mathematica. I'm having trouble accomplishing the following:

Let's say I have multiple lists of arbitrary data (using two lists here to keep it simple):

a = {1,2,3,4,5,6}
b = {1,1,2,2,3,3}

I also have multiple arbitrary functions (again using two to keep it simple) which all take the same inputs in the same order, but use them to generate different outputs:

f = Function[{u,v},{{u^2,u*v},{-u*v,v^2}}]
g = Function[{u,v},{{u^2,1},{-1,v^2}}]

I understand how to build lists of results, threading over lists a,b, with MapThread:

fOut = MapThread[f,{a,b}]
gOut = MapThread[g,{a,b}]

However, because in reality there are dozens of functions (f,g,etc.) and dozens of lists of data (a,b,etc.), I end up with dozens of MapThread commands, each with the same long list of arguments, creating a wall of text that is far from elegant-looking. So I tried to merge it into a single MapThread command, which didn't work:

{fOut,gOut} = MapThread[{f,g},{a,b}]

Is there a way to MapThread many functions onto many lists of data (assuming each function expects the data in the same order)? If possible, I would like to avoid creating new variables because the functions are intended to be compiled, and I think it would create redundant data.

share|improve this question
Does this generate the result you expect: (# @@@ Transpose@{a, b}) & /@ {f, g}? Or even: SetAttributes[{f, g}, Listable]; (# @@ {a, b}) & /@ {f, g}. – István Zachar Jan 11 '14 at 17:28
That first one does exactly what I want. Thanks! Now I just need to understand what the hell it's doing. – phosgene Jan 11 '14 at 17:35
You can do : Outer[#1 @@ #2 &, {f, g}, Transpose[{a, b}], 1] – andre Jan 11 '14 at 18:02
Thanks, this one also works! – phosgene Jan 11 '14 at 18:16
up vote 2 down vote accepted

It is not entirely clear to me what your larger problem looks like. If you have the same set of lists each time and merely want to MapThread a series of functions across them you can use a simple construct such as:

a = {1, 2, 3, 4, 5, 6};
b = {1, 1, 2, 2, 3, 3};

MapThread[#, {a, b}] & /@ {f, g}
{{f[1, 1], f[2, 1], f[3, 2], f[4, 2], f[5, 3], f[6, 3]},
 {g[1, 1], g[2, 1], g[3, 2], g[4, 2], g[5, 3], g[6, 3]}}

If you have a more involved operation in mind please describe it in greater detail.

share|improve this answer
That was exactly what I needed. Of the three suggestions I've seen so far (all of which work), this one has the most concise and easy-to-read syntax, and also the highest (uncompiled) performance by a small margin. – phosgene Jan 11 '14 at 18:23
@user2790167 If you give a more representative example of your actual operation we may be able to recommend a better approach. For example, MapThread will unpack packed arrays, and can therefore significantly limit performance in some cases. – Mr.Wizard Jan 11 '14 at 18:38
I'm not using any packed arrays (to my knowledge). I'm actually writing code inside a Compile command. The fundamental operations are floating-point linear algebra. – phosgene Jan 11 '14 at 18:59
...including construction of matrices from data (similar to functions f,g in my example). I've already written the whole thing in the procedural style (a wall of text to be sure), and I wanted to make the code shorter and easier to read. – phosgene Jan 11 '14 at 19:08
Map[MapThread[#,{a,b}]&,{f,g}] compiles seemingly without error. However, when it's called, it fails with this error: "Compiled expression Function[{u,v},{{u^2,u v},{-u v,v^2}}] should be a machine-size real number. >>", then executes the uncompiled version instead, which is an order of magnitude slower. A plain MapThread command makes a good compiled function, but it seems not to like nested Map[MapThread] commands for some reason. – phosgene Jan 11 '14 at 21:58

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.