4
$\begingroup$

Is there a built-in or more efficient approach than using Partition to apply a binary operation to adjacent pairs of elements? That is, is it reasonable to be using the following for a binary operation f and a possibly long list (here, just {a,b,c}) the following approach?

Map[f @@ # &, Partition[{a, b, c}, 2, 1]]

Related: will the result of this use of Partition ever actually be constructed in full, or are the pairs just generated on demand? (And how can I anticipate the answer to this question?)

$\endgroup$
6
  • $\begingroup$ something like this? f @@ list[[# ;; # + 1]] & /@ Range[Length[list] - 1] $\endgroup$
    – george2079
    Nov 6, 2015 at 20:30
  • 1
    $\begingroup$ l = {a, b, c}; Inner[f, Most@l, Rest@l, List] $\endgroup$ Nov 6, 2015 at 20:30
  • $\begingroup$ @belisariushassettled If I'm willing to risk the construciton of Most and Rest, I can just use Thread. But that gets me back to my question of how I can know when such things will be constructed. $\endgroup$
    – Alan
    Nov 6, 2015 at 21:08
  • $\begingroup$ You can verify how Mathematica is calculating (more or less) by Map[f @@ # &, Partition[{a, b, c}, 2, 1]] // Trace $\endgroup$ Nov 6, 2015 at 21:22
  • 1
    $\begingroup$ Regarding the second question, Map has no Hold* attributes so the full Partition will be evaluated before Map sees it. The pairs are not generated on demand. $\endgroup$ Nov 6, 2015 at 21:23

1 Answer 1

3
$\begingroup$

For a packed numerical list it is both faster and less memory intensive to use ListConvolve:

list = RandomReal[{0, 1}, 10^7];
f = Plus;

AbsoluteTiming@MaxMemoryUsed[
  res1 = Map[f @@ # &, Partition[list, 2, 1]]]
(* {2.37724, 320005544} *)

AbsoluteTiming@MaxMemoryUsed[
  res2 = ListConvolve[{1, 1}, list, {-1, 1}, {}, Times, f]]
(* {0.290708, 80006000} *)

res1 == res2
(* True *)
$\endgroup$
2
  • $\begingroup$ While comparable in terms of memory compared to ListConvolve, Developer`PartitionMap takes about twice as long as Map on my machine. $\endgroup$
    – rcollyer
    Nov 6, 2015 at 22:03
  • $\begingroup$ Wonderful! PartitionMap is exactly what I was looking for. Presumably it will get faster ... But I'm also very happy to learn about this use of ListConvolve. I confess I had hopefully skimmed the documentation for it without discovering how to do this. $\endgroup$
    – Alan
    Nov 7, 2015 at 0:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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