7
$\begingroup$

I want to add the successive the elements of a list. As we know the operator "Differences" performs the difference of the successive elements of a list. Is there a function which performs the opposite job of "differences" in mathematica?

$\endgroup$
3
  • 1
    $\begingroup$ Using FoldList as an inverse of Differences. $\endgroup$
    – Karsten7
    Commented Nov 1, 2015 at 3:11
  • $\begingroup$ There are things to do after your question is answered. Its a good idea to stay vigilant for some time, better approaches may come later improving over previous replies. Experienced users may point alternatives, caveats or limitations. New users should test answers before voting and wait 24 hours before accepting the best one. Participation is essential for the site, please come back to do your part. $\endgroup$
    – rhermans
    Commented Nov 6, 2015 at 14:18
  • 2
    $\begingroup$ Why have you not accepted answers in any of your 8 questions? Voting and accepting is central to the SE model of providing quality questions and answers; it is how good/bad content gets sorted, helpful users get reputation and privileges. $\endgroup$
    – rhermans
    Commented Nov 6, 2015 at 14:23

8 Answers 8

16
$\begingroup$

I interpret "opposite" to mean the inverse operation. Just like with differentiation and antidifferentiation, the inverse of Differences is defined up to a constant and is given by Accumulate:

list = {2, 4, 5, 7, 7, 3};
diff = Differences[list]                (* note its length is one shorter *)
Accumulate[diff]                        (* ...and so is the length here *)
Accumulate[Prepend[diff, First[list]]]  (* the real inverse: include the starting point *)
(*
  {2, 1, 2, 0, -4}
  {2, 3, 5, 5, 1}
  {2, 4, 5, 7, 7, 3}
*)

The last command is like $f(x) = f(a) + \int_a^x f'(x)\;dx$, where the starting value is included.

$\endgroup$
8
$\begingroup$

ListConvolve[]/ListCorrelate[] do the job:

ListCorrelate[{1, 1}, {2, 4, 5, 7, 7, 3}]
   {6, 9, 12, 14, 10}
$\endgroup$
7
$\begingroup$

There's also the MovingAverage (of length 2),

2 MovingAverage[{2, 4, 5, 7, 7, 3}, 2]

{6, 9, 12, 14, 10}
$\endgroup$
6
$\begingroup$

Can be done with Plus and Partition:

data = {2, 4, 5, 7, 7, 3};
Plus @@@ Partition[data, 2, 1]

{6, 9, 12, 14, 10}

$\endgroup$
1
  • $\begingroup$ Just so it is equivalent to Differences you can define it in functional form as sums = Plus @@@ Partition[#, 2, 1] & $\endgroup$
    – Jason B.
    Commented Oct 30, 2015 at 13:08
4
$\begingroup$
list = {2, 4, 5, 7, 7, 3};

There have been two interpretations of this question.

One is pairwise sum with overlap 1. Examples (inclusive of some given answers):

ListCorrelate[{1, 1}, list]
Plus @@@ Partition[list, 2, 1]
Partition[list, 2, 1].{1, 1}
{1, 1}.{Most@list, Rest@list}
MovingMap[Total, list, 1]

all yielding {6, 9, 12, 14, 10}

The other interpretation as per MichaelE2 cumulative sum,e.g.:

FoldList[Plus, list]
Accumulate[list]

yielding: {2, 6, 11, 18, 25, 28}

$\endgroup$
3
$\begingroup$

How about:

Plus[Prepend[list, 0], Append[list, 0]][[2 ;; -2]]

And inspired by ubpdqn:

Plus[Rest[list], Most[list]]

Which is the fastest I can find. I also observed speed differences if the list contains pure Integers or Reals.

Addition

Random integers gives this timing:

list = RandomInteger[{1, 100}, 100000];
{RepeatedTiming[Plus[Prepend[list, 0], Append[list, 0]][[2 ;; -2]];], 
  RepeatedTiming[Plus[Rest[list], Most[list]];], 
  RepeatedTiming[ListCorrelate[{1, 1}, list];], 
  RepeatedTiming[Plus @@@ Partition[list, 2, 1];], 
  RepeatedTiming[Partition[list, 2, 1].{1, 1};], 
  RepeatedTiming[{1, 1}.{Most@list, Rest@list};], 
  RepeatedTiming[MovingMap[Total, list, 1];]}[[All, 1]]

{0.00043, 0.000137, 0.000737, 0.0317, 0.0055, 0.0015, 0.0394}

Random reals gives this timing:

list = RandomReal[{1, 100}, 100000];
{RepeatedTiming[Plus[Prepend[list, 0], Append[list, 0]][[2 ;; -2]];], 
  RepeatedTiming[Plus[Rest[list], Most[list]];], 
  RepeatedTiming[ListCorrelate[{1, 1}, list];], 
  RepeatedTiming[Plus @@@ Partition[list, 2, 1];], 
  RepeatedTiming[Partition[list, 2, 1].{1, 1};], 
  RepeatedTiming[{1, 1}.{Most@list, Rest@list};], 
  RepeatedTiming[MovingMap[Total, list, 1];]}[[All, 1]]

{0.0298, 0.00014, 0.00144, 0.040, 0.00430, 0.00089, 0.052}

10.2.0 for Microsoft Windows (64-bit) (July 28, 2015)

$\endgroup$
3
$\begingroup$
list = {2, 4, 5, 7, 7, 3};

diff = Differences[list];

FoldList[Plus, First[list], diff]

{2, 4, 5, 7, 7, 3}

$\endgroup$
2
$\begingroup$
list = {2, 4, 5, 7, 7, 3};

Using SequenceCases (new in 10.1)

SequenceCases[list, {a_, b_} :> a + b, Overlaps -> True]

{6, 9, 12, 14, 10}

Using BlockMap (new in 10.2)

BlockMap[Total, list, 2, 1]

{6, 9, 12, 14, 10}

$\endgroup$

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.