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?
$\begingroup$
$\endgroup$
2
-
$\begingroup$ Related: (4061) $\endgroup$– Mr.WizardCommented 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$– Mike HoneychurchCommented Jan 24, 2015 at 0:08
Add a comment
|
3 Answers
$\begingroup$
$\endgroup$
2
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} *)
-
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$
$\endgroup$
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]}
$\begingroup$
$\endgroup$
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]}*)