# How to use other methods to realize Differences

I know the following method can get the desired result {b-a,c-b,d-c,e-d}:

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


But I want to know more about how to do this.

MovingMap[Subtract, {a, b, c, d, e}, 1]


Please provide as many methods as possible.

Rest @ # - Most @ # & @ {a, b, c, d, e}
#[[2 ;;]] - #[[;; -2]]& @ {a, b, c, d, e}
{1, -1}.Through @ {Rest, Most} @ {a, b, c, d, e}
Most[RotateLeft @ # - #]& @ {a, b, c, d, e}
ListConvolve[{1, -1}, {a, b, c, d, e}]
ListCorrelate[{-1, 1}, {a, b, c, d, e}]
MovingMap[#.{-1, 1} &, {a, b, c, d, e}, 1]
#2 - # & @@@ Subsequences[{a, b, c, d, e}, {2}]
Subsequences[{a, b, c, d, e}, {2}].{-1, 1}
#2 - # & @@@ Partition[{a, b, c, d, e}, 2, 1]
Partition[{a, b, c, d, e}, 2, 1].{-1, 1}
BlockMap[#.{-1, 1} &, {a, b, c, d, e}, 2, 1]
BlockMap[#2- # & @@ #&, {a, b, c, d, e}, 2, 1]
Partition[{a, b, c, d, e}, 2, 1, {1, -1}, {}, Minus @* Subtract]
ReplaceList[{w___, x_, y_, z___} :> y - x] @ {a, b, c, d, e}
SequenceCases[{a, b, c, d, e}, {a_, b_} :> b - a, Overlaps -> True]
Table[#[[i]] - #[[i - 1]], {i, 2, Length@#}] &@{a, b, c, d, e}
Function[x, Array[x[[# + 1]] - x[[#]] &, Length[x] - 1]] @ {a, b, c, d,  e}
{a, b, c, d, e}.SparseArray[{Band[{1, 1}, {4, 4}] -> -1, Band[{2, 1}, {5, 4}] -> 1}]
FoldPairList[{#2 - #1, #2} &, {a, b, c, d, e}] (* thanks: m_goldberg *)
Subtract @@@ Reverse /@ Partition[{a, b, c, d, e}, 2, 1] (* thanks: m_goldberg *)

• If you are trying to be exhaustive, you might add Subtract @@@ Reverse /@ Partition[{a, b, c, d, e}, 2, 1] – m_goldberg Oct 8 '20 at 3:42
• People tend to forget about FoldPairList, but it works nicely for this problem. FoldPairList[{#2 - #1, #2} &, {a, b, c, d, e}]. You might want to add this one. – m_goldberg Oct 8 '20 at 4:27
• @m_goldberg, thank you for both suggestions. It didn't occur to me FoldPairList would be so straightforward. – kglr Oct 8 '20 at 4:43
• Your 2nd example is my favorite. Beside being the most concise example, as an old Lisp programmer, I find it an a very intuitive representation of the operation being implemented. – m_goldberg Oct 8 '20 at 7:04
• @m_goldberg, it is also almost as efficient as Differences for numeric input. – kglr Oct 8 '20 at 7:47
MovingMap[Minus[Subtract @@ #] &, {a, b, c, d, e}, 1]

• MovingMap[-(Subtract @@ #) &, {a, b, c, d, e}, 1] also do the job. – wuyudi Oct 8 '20 at 3:08

First , we can see that

MovingMap[ # &, {a, b, c, d, e}, 1]


{{a, b}, {b, c}, {c, d}, {d, e}}

So how to make {a,b} -> b-a ?

For example:

MovingMap[#[[2]] - #[[1]] &, {a, b, c, d, e}, 1]


Or

MovingMap[(Last@# - First@#) &, {a, b, c, d, e}, 1]


Or

MovingMap[Apply[#2 - #1 &], {a, b, c, d, e}, 1]


and so on.

Most@NDSolveFiniteDifferenceDerivative[1,Range@Length@#,#,DifferenceOrder->1]&@{a,b,c,d,e}
`