({{, (RefLink[Differences, paclet:ref/Differences])[list]
gives the successive differences of elements in list. }, {, (RefLink[Differences, paclet:ref/Differences])[list,n]
gives the n(^th) differences of list. }, {, (RefLink[Differences, paclet:ref/Differences])[list,n,s]
gives the differences of elements step s apart.}, {, (RefLink[Differences, paclet:ref/Differences])[list,{(Subscript[n, 1]),(Subscript[n, 2]),…}]
gives the successive (Subscript[n, k])(^th) differences at level k in a nested list. }})

OpenersNotesSection(Spacer1)Details

(RefLink[Differences, paclet:ref/Differences])[list] is equivalent to (RefLink[ListConvolve, paclet:ref/ListConvolve])[{1,-1},list].

If list has length l, (RefLink[Differences, paclet:ref/Differences])[list,n] has length l-n. »

If list has length l, (RefLink[Differences, paclet:ref/Differences])[list,n,s] has length l-n|s|.

(RefLink[Differences, paclet:ref/Differences])[m,{1,0}] or (RefLink[Differences, paclet:ref/Differences])[m,1] finds the successive differences of rows in a 2D array or matrix m. (RefLink[Differences, paclet:ref/Differences])[m,{0,1}] finds the differences of columns.

(RefLink[Differences, paclet:ref/Differences])[list,{(Subscript[n, 1]),(Subscript[n, 2])}] is equivalent to (RefLink[Differences, paclet:ref/Differences])[Differences[list,(Subscript[n, 1])],{0,(Subscript[n, 2])}].

OpenersPrimaryExamplesSection(Spacer1)Examples  (15)

OpenersExampleSection0(Spacer1)Basic Examples  (2)

First differences:

Differences[{a,b,c,d,e}]

Second differences:

Differences[{a,b,c,d,e},2]

OpenersExampleSection1(Spacer1)Scope  (3)

Differences of rows, within each column:

Differences[(({{(Subscript[a, 11]), (Subscript[a, 12]), (Subscript[a, 13])}, {(Subscript[a, 21]), (Subscript[a, 22]), (Subscript[a, 23])}, {(Subscript[a, 31]), (Subscript[a, 32]), (Subscript[a, 33])}}))]//MatrixForm

Differences[(({{(Subscript[a, 11]), (Subscript[a, 12]), (Subscript[a, 13])}, {(Subscript[a, 21]), (Subscript[a, 22]), (Subscript[a, 23])}, {(Subscript[a, 31]), (Subscript[a, 32]), (Subscript[a, 33])}})),{1,0}]//MatrixForm

Differences of columns, within each row:

Differences[(({{(Subscript[a, 11]), (Subscript[a, 12]), (Subscript[a, 13])}, {(Subscript[a, 21]), (Subscript[a, 22]), (Subscript[a, 23])}, {(Subscript[a, 31]), (Subscript[a, 32]), (Subscript[a, 33])}})),{0,1}]//MatrixForm

Differences of a regularly sampled (RefLink[TimeSeries, paclet:ref/TimeSeries]):

v={1,3,9,3,2};
ts=TimeSeries[v,Automatic]

ts["Path"]

diff=Differences[ts]

diff["Path"]

Compare to differencing the values:

Differences[v]==diff["Values"]

OpenersExampleSection2(Spacer1)Applications  (3)

Deduce the degree of a sequence by seeing when successive differences become constant:

Table[n^5+2n-1,{n,8}]

FixedPointList[Differences,%]

Finite difference approximation to the Laplacian of a Gaussian:

t=Table[Exp[-x^2-y^2],{x,-2,2,0.1},{y,-2,2,0.1}];

ListDensityPlot[Drop[Differences[t,{0,2}],2,0]+Drop[Differences[t,{2,0}],0,2],PlotRange->All,DataRange->2]

Prices of a stock at close:

ts=TimeSeries@FinancialData["SBUX","Close",{2013,1,1}]

DateListPlot[ts]

Find the daily differences between stock values at close:

diff=Differences[ts]

DateListPlot[diff]

Find the proportion of days with increase in price at close:

res=Tally[Sign[diff]["Values"]]

Select[res,First[#]==1&][[1,2]]/ts["PathLength"]//N

OpenersExampleSection3(Spacer1)Properties & Relations  (4)

First differences are constant for a linear function:

Table[3i+6,{i,10}]

Differences[%]

Second differences are constant for a quadratic function:

Table[3i^2+6,{i,10}]

Differences[%]

Differences[%]

Successive differences are progressively shorter:

Column[Table[Differences[Range[10],n],{n,10}]]

(RefLink[FoldList, paclet:ref/FoldList]) acts as an inverse of (RefLink[Differences, paclet:ref/Differences]):

Differences[{a,b,c,d,e}]

FoldList[Plus,a,%]

OpenersExampleSection4(Spacer1)Possible Issues  (1)

Differences of an irregularly sampled (RefLink[TimeSeries, paclet:ref/TimeSeries]) may differ from differences of the values:

v={1,3,9,3,2};
t={0,.3,.4,1.3,1.5};
ts=TimeSeries[v,{t}]

RegularlySampledQ[ts]

diff=Differences[ts]

diff["Path"]

Compare with (RefLink[Differences, paclet:ref/Differences]) of the values:

Differences[v]==diff["Values"]

Set (RefLink[TemporalRegularity, paclet:ref/TemporalRegularity]) to (RefLink[True, paclet:ref/True]) and compute differences of the new time series:

ts1=TimeSeries[ts,TemporalRegularity->True];

diff1=Differences[ts1];

Now the value differences agree:

diff1["Values"]==Differences[v]

OpenersExampleSection5(Spacer1)Neat Examples  (2)

Successive differences mod 2 of a sequence based on the primes:

ArrayPlot[Mod[PadLeft[NestList[Differences,Array[(Prime[#]-1)/2&,100],100]],2]]

Pattern resembling Pascal's triangle:

NestList[Differences,Normal[SparseArray[10->1,21]],11]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[Subtract, paclet:ref/Subtract])  ▪  (RefLink[Accumulate, paclet:ref/Accumulate])  ▪  (RefLink[Ratios, paclet:ref/Ratios])  ▪  (RefLink[ListConvolve, paclet:ref/ListConvolve])  ▪  (RefLink[FoldList, paclet:ref/FoldList])  ▪  (RefLink[DifferenceDelta, paclet:ref/DifferenceDelta])  ▪  (RefLink[RecurrenceTable, paclet:ref/RecurrenceTable])  ▪  (RefLink[EstimatedBackground, paclet:ref/EstimatedBackground])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Discrete & Integer Data, paclet:guide/DiscreteAndIntegerData])}, {▪(RefLinkPlain[Time Series Processes, paclet:guide/TimeSeriesProcesses])}, {▪(RefLinkPlain[List Manipulation, paclet:guide/ListManipulation])}, {▪(RefLinkPlain[Data Transforms and Smoothing, paclet:guide/DataTransformsAndSmoothing])}, {▪(RefLinkPlain[Arithmetic Functions, paclet:guide/ArithmeticFunctions])}, {▪(RefLinkPlain[Time Series Processing, paclet:guide/TimeSeries])}, {▪(RefLinkPlain[Discrete Calculus, paclet:guide/DiscreteCalculus])}, {▪(RefLinkPlain[Numerical Data, paclet:guide/NumericalData])}, {▪(RefLinkPlain[Math & Counting Operations on Lists, paclet:guide/MathematicalAndCountingOperationsOnLists])}, {▪(RefLinkPlain[Applying Functions to Lists, paclet:guide/ApplyingFunctionsToLists])}})}})