Suppose I have the variable expr1 = a[n] + b[n]
, and the list stencil={-1,0,1}
. I want to make a function f[expr_, stencil_]:= ...
that can give:
a[n-1] + b[n-1] + a[n] + b[n] + a[n+1] + b[n+1]
.
Basically I want to 'map' the values inside stencil
to n
of expr
. Any help would be appreciated.
Replace[expr1, $_ -> $ + #, {-1}] & /@stencil // Total
$\endgroup$Total[Function[n, a[n] + b[n]] /@ (n + {-1, 0, 1})]
. $\endgroup$