Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping two argument function in Mathematica

I want to archive:

Map2[f,{a,b,c,d}]
{f[a,b], f[b,c], f[c,d]}

But for some reason, I can only think of iterative approaches.

What's the functional way?

Edit:

Use of this, was to generate graph examples. If number list is given, following generates the graph of what it would mean, to sequentially visit all the nodes.

For example:

Map2 = # @@@ Partition[#2, 2, 1] &;
MG[elems_] := Graph[Map2[DirectedEdge, elems]]
nrs = RandomInteger[{1, 15}, 20]
MG[nrs]

{10,13,9,7,13,3,5,1,15,10,15,6,14,3,1,2,11,4,8,5}

Radial layout:

enter image description here

like image 813
Margus Avatar asked May 29 '11 13:05

Margus


1 Answers

Most simply:

Map2 = # @@@ Partition[#2, 2, 1] &;

Or, possibly using less memory, but running a bit slower:

Map2[f_, lst_] := Developer`PartitionMap[f @@ # &, lst, 2, 1]

Also, be aware of Differences and Accumulate, which are related.


Other ways:

Inner[#, Most@#2, Rest@#2, List] &

Notice that in this one List could be a different function.

MapThread[#, {Most@#2, Rest@#2}] &

Most@MapThread[#, {#2, RotateLeft@#2}] &

Rest@MapThread[#, {RotateRight@#2, #2}] &

Table[# @@ #2[[i ;; i + 1]], {i, Length@#2 - 1}] &

Now, the timings. I will make use of Timo's timeAvg.

f1 = # @@@ Partition[#2, 2, 1] &;
f2[f_, lst_] := Developer`PartitionMap[f @@ # &, lst, 2, 1]
f3 = Inner[#, Most@#2, Rest@#2, List] &;
f4 = MapThread[#, {Most@#2, Rest@#2}] &;
f5 = Most@MapThread[#, {#2, RotateLeft@#2}] &;
f6 = Table[# @@ #2[[i ;; i + 1]], {i, Length@#2 - 1}] &;

timings = 
  Table[
   list = RandomReal[99, 10^i];
   func = Plus;
   timeAvg[#[func, list]] & /@ {f1, f2, f3, f4, f5, f6},
   {i, 6}
  ];

TableForm[timings, 
 TableHeadings -> {10^Range@6, {"f1", "f2", "f3", "f4", "f5", "f6"}}]

ListLogPlot[timings\[Transpose], Joined -> True]

enter image description here

enter image description here

The numbers on the left side of the table are the length of the list of real numbers for each run.

Note that the graph is logarithmic.

Here are the bytes of memory used while processing the longest list (1,000,000):

enter image description here


After doing the timings above, I found I can make f6 faster by eliminating Apply (@@). It is now clearly the fastest on longer lists. I will not add it to the table above because that is already wide enough, but here is the function and its timings:

f7 = Table[#2[[i]] ~#~ #2[[i + 1]], {i, Length@#2 - 1}] &;

enter image description here

like image 88
Mr.Wizard Avatar answered Oct 08 '22 17:10

Mr.Wizard