# Opposite of Differences

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?

• – Karsten 7. Nov 1 '15 at 3:11
• 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. – rhermans Nov 6 '15 at 14:18
• 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. – rhermans Nov 6 '15 at 14:23

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.

ListConvolve[]/ListCorrelate[] do the job:

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


Can be done with Plus and Partition:

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


{6, 9, 12, 14, 10}

• Just so it is equivalent to Differences you can define it in functional form as sums = Plus @@@ Partition[#, 2, 1] & – Jason B. Oct 30 '15 at 13:08

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

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

{6, 9, 12, 14, 10}

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}

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.

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)

list = {2, 4, 5, 7, 7, 3};

diff = Differences[list];

FoldList[Plus, First[list], diff]


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