3
$\begingroup$

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.

$\endgroup$
10
$\begingroup$
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 *)
$\endgroup$
6
  • 3
    $\begingroup$ If you are trying to be exhaustive, you might add Subtract @@@ Reverse /@ Partition[{a, b, c, d, e}, 2, 1] $\endgroup$ – m_goldberg Oct 8 '20 at 3:42
  • 3
    $\begingroup$ 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. $\endgroup$ – m_goldberg Oct 8 '20 at 4:27
  • $\begingroup$ @m_goldberg, thank you for both suggestions. It didn't occur to me FoldPairList would be so straightforward. $\endgroup$ – kglr Oct 8 '20 at 4:43
  • $\begingroup$ 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. $\endgroup$ – m_goldberg Oct 8 '20 at 7:04
  • $\begingroup$ @m_goldberg, it is also almost as efficient as Differences for numeric input. $\endgroup$ – kglr Oct 8 '20 at 7:47
4
$\begingroup$
MovingMap[Minus[Subtract @@ #] &, {a, b, c, d, e}, 1]
$\endgroup$
1
  • 1
    $\begingroup$ MovingMap[-(Subtract @@ #) &, {a, b, c, d, e}, 1] also do the job. $\endgroup$ – wuyudi Oct 8 '20 at 3:08
3
$\begingroup$

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.

$\endgroup$
3
$\begingroup$
Most@NDSolve`FiniteDifferenceDerivative[1,Range@Length@#,#,DifferenceOrder->1]&@{a,b,c,d,e}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.