0

Can the expression be simplified as "Total[funca[a,#]] & /@ {x,y}"? What does the double of "& /@" mean? Where can I find some reference?

Updated

According to Mr.Wizard's answer, I now know the result. But I still can not understand the combination use of #&/@.

For example, the result from Out[1] to Out[6] looks so weird to me. Who can help to explain that?

x = {q, r, s};
y = {t, u, v};
In[1]:= Total[funca[a, #]] & /@ {x, y}

Out[1]:= {{a + q, a + r, a + s}, {a + t, a + u, a + v}}

In[2]:= Total[funca[a, #] & /@ #] & /@ {x, y}

Out[2]:= {funca[a, q] + funca[a, r] + funca[a, s], funca[a, t] + funca[a, u] + funca[a, v]}

In[3]:= Total[funca[a, #] & /@ # & /@ #] & /@ {x, y}

Out[3]:= {q + r + s, t + u + v}

In[4]:= Total[{a, #}] & /@ {x, y}

Out[4]:= {{a + q, a + r, a + s}, {a + t, a + u, a + v}}

In[5]:= Total[{a, #} & /@ #] & /@ {x, y}

Out[5]:= {{3 a, q + r + s}, {3 a, t + u + v}}

In[6]:= Total[{a, #} & /@ # & /@ #] & /@ {x, y}

Out[6]:= {q + r + s, t + u + v}

xibinke
  • 411
  • 2
  • 9

1 Answers1

6
Total[funca[a,#] & /@ #] & /@ {x,y}

There are two Function expressions here which I will refer to as inner and outer. The inner function:

funca[a,#] &

Is Mapped to the sole argument of the outer function. It will transform a list or other expression like this:

funca[a,#] & /@ foo[1, 2, 3]
foo[funca[a,1], funca[a,2], funca[a,3]]

The outer function does this to its single argument and then Totals it, which for the above would be:

funca[a,1] + funca[a,2] + funca[a,3]
  • To clarify: in the snippet funca[a,#] & /@ # the left # (in funca[a,#]) will be filled by the argument of the inner function while the right # will be filled by the argument of the outer function.

This is done for each for x and y because it (the outer function) is also mapped across {x, y}.

A complete example:

x = {q, r, s};
y = {t, u, v};

Total[funca[a, #] & /@ #] & /@ {x, y}
{funca[a, q] + funca[a, r] + funca[a, s],
 funca[a, t] + funca[a, u] + funca[a, v]}

The operation could also be written:

Total /@ Map[funca[a, #] &, {x, y}, {2}]

Map[funca[a, #] &, {x, y}, {2}] ~Total~ {2}
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • ,Thank you. I know the result now but I still can not understand. – xibinke Nov 06 '14 at 06:25
  • @xibinke maybe you just need to read: what are those symbols? – Kuba Nov 06 '14 at 06:49
  • @xibinke Please read these documentation pages, then if you are still have trouble let me know: http://reference.wolfram.com/language/tutorial/PureFunctions.html, http://reference.wolfram.com/language/howto/WorkWithPureFunctions.html, http://reference.wolfram.com/language/ref/Function.html, – Mr.Wizard Nov 06 '14 at 07:21
  • @xibinke Also please read my answer to (46238) as I explain Map (of which /@ is the short form) in my own words. – Mr.Wizard Nov 06 '14 at 07:23
  • @Mr.Wizard, thank you. Your answer really helps me a lot. Now I can understand the result of In[2]. Also I can understand In[1][4][5] after I realized that Total gives the sum of the first level of list. And what about In[3][6], seems meaningless? – xibinke Nov 07 '14 at 07:12