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?
8 Answers
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}
There's also the MovingAverage (of length 2),
2 MovingAverage[{2, 4, 5, 7, 7, 3}, 2]
{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}
-
$\begingroup$ Just so it is equivalent to
Differences
you can define it in functional form assums = Plus @@@ Partition[#, 2, 1] &
$\endgroup$– Jason B.Commented Oct 30, 2015 at 13:08
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}
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)
list = {2, 4, 5, 7, 7, 3};
diff = Differences[list];
FoldList[Plus, First[list], diff]
{2, 4, 5, 7, 7, 3}
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}
FoldList
as an inverse ofDifferences
. $\endgroup$