4
$\begingroup$

Say I have a list $E$ and want to build a list $F$ with the entries $F_j=E_j-2E_{j-1}+E_{j-2}$. I know that this happens to be achievable by iterating the function Differences , but is there a way how to generalize this to different linear combinations?

$\endgroup$
2
  • $\begingroup$ Related: (4061) $\endgroup$
    – Mr.Wizard
    Commented Jan 23, 2015 at 23:05
  • $\begingroup$ This looks like a finite difference stencil. See detailed docs for NDSolve for higher order schemes ...if that is what you mean by different linear combinations $\endgroup$ Commented Jan 24, 2015 at 0:08

3 Answers 3

9
$\begingroup$
ListConvolve[{1, -2, 1}, mylist]

Example

mylist = RandomInteger[10, {10}]

(* {5, 4, 10, 1, 7, 1, 7, 0, 8, 4} *)

ListConvolve[{1, -2, 1}, mylist]

(* {7, -15, 15, -12, 12, -13, 15, -12} *)

$\endgroup$
2
  • 3
    $\begingroup$ Perhaps an easier to follow example: mylist = f/@ Range@10; ListConvolve[{1, -2, 1}, mylist] $\endgroup$ Commented Jan 23, 2015 at 18:33
  • $\begingroup$ Yes. Quite clear. $\endgroup$ Commented Jan 23, 2015 at 18:41
1
$\begingroup$

Using BlockMap:

Clear[arr, e, f];
arr = Array[e, 10]
f = BlockMap[Apply[#3 - 2 #2 + #1 &], arr, 3, 1]

{e[1] - 2 e[2] + e[3], e[2] - 2 e[3] + e[4], e[3] - 2 e[4] + e[5],
e[4] - 2 e[5] + e[6], e[5] - 2 e[6] + e[7], e[6] - 2 e[7] + e[8],
e[7] - 2 e[8] + e[9], e[8] - 2 e[9] + e[10]}

$\endgroup$
1
$\begingroup$

Using Table:

arr = Array[e, 10];

Table[#[[i + 2]] - 2 #[[i + 1]] + #[[i]], {i, 1, Length[#] - 2}] &@arr

(*{e[1] - 2 e[2] + e[3], e[2] - 2 e[3] + e[4], e[3] - 2 e[4] + e[5], 
  e[4] - 2 e[5] + e[6], e[5] - 2 e[6] + e[7], e[6] - 2 e[7] + e[8], 
  e[7] - 2 e[8] + e[9], e[8] - 2 e[9] + e[10]}*)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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