1
|
11817 Views
|
12 Replies
|
30 Total Likes
View groups...
Share
GROUPS:

# Distributing a list of heads over a list of arguments

Posted 12 years ago
 Given two lists, one for the heads another for their argumentsheads = {f, g, h, k};arguments = {w, x, y, z};is there less clumsy way to do this?In[1]:= Diagonal[Through /@ Distribute[heads[arguments], List]]Out[1]= {f[w], g[x], h[y], k[z]}
12 Replies
Sort By:
Posted 11 years ago
Posted 11 years ago
Posted 11 years ago
 Dot[heads, arguments] /. Times -> (Operate[##, 0] &)
Posted 11 years ago
 Nice, but the Operate is slightly heavy handed.  I think the following is both shorter and easier to understand:In[7]:= Dot[heads, arguments] /. Times -> (#1[#2] &)Out[7]= f[w] + g[x] + h[y] + k[z]Of course, this will break if one of the arguments has head Times. arguments = {w, x, y, z b};  In[18]:= Dot[heads, arguments] /. Times -> (Operate[##, 0] &)  During evaluation of In[18]:= Operate::argt: Operate called with 4 arguments; 2 or 3 arguments are expected. >>  Out[18]= f[w] + g[x] + h[y] + Operate[b, k, z, 0]  In[19]:= Dot[heads, arguments] /. Times -> (#1[#2] &)Out[19]= b[k] + f[w] + g[x] + h[y]
Posted 11 years ago
 In[7]:= Dot[heads, arguments] /. Times -> (#1[#2] &)Out[7]= f + g + h + kOf course, this will break if one of the arguments has head Times. And while it's frequently safer to use Replace at a specific level is rather than ReplaceAll (/.) in code, even that won't help you in this case.  That's probably why Dan's answer used Inner instead of Dot.
Posted 12 years ago
 No worries, but for the inconvenience you have to suffer reading this code, in the category: Way clumsierresult = {};For[  i = 1,  i <= Max[Length /@ {heads, arguments}],  i += Length[IntegerPartitions[i]]/PartitionsP[i],  AppendTo[result, heads[[i]][arguments[[i]]]]  ];result
Posted 12 years ago
 @Simon Schmidt - Wow this is a real fun one ;-)  +1 !
Posted 12 years ago
Posted 12 years ago
 In[1]:= Inner[#1[#2] &, heads, arguments, List]Out[1]= {f[w], g[x], h[y], k[z]}
Posted 12 years ago
 Compose is currently superseded by composition, but that works slightly different, so another solution is:MapThread[#1@#2 &, {heads,arguments}]
Posted 12 years ago
 Sander Huisman, - this would be my solution as well.
Posted 12 years ago