# 'Mapping' the values of a list to variable

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.

• Use the ideas [email protected] lzd Replace[expr1, $_ ->$ + #, {-1}] & /@stencil // Total May 22 at 22:58
• It would have been much better if you had defined your expression as a function instead: Total[Function[n, a[n] + b[n]] /@ (n + {-1, 0, 1})]. May 23 at 14:14

Another approach could be:

Total[Thread /@ Map[# + stencil &, expr1, {-1}]]


Note that it's slightly faster than the @Syed answer but uses more memory.

Does this work correctly apart from your example?

f = Join @@ (# /. Table[sym -> Head[#][sym, i], {i, #2}, {sym, Variables[Level[#, {-1}]]}]) &;

f[expr1, stencil]
(* a[-1 + n] + a[n] + a[1 + n] + b[-1 + n] + b[n] + b[1 + n] *)

[email protected](expr1 /. n -> n + # & /@ stencil)


a[-1 + n] + a[n] + a[1 + n] + b[-1 + n] + b[n] + b[1 + n]

Distribute[{expr1, stencil},List,List,Plus,Replace[#1,n:>n+#2,2]&]

(* a[-1 + n] + a[n] + a[1 + n] + b[-1 + n] + b[n] + b[1 + n] *)


Another way:

[email protected][Map[Composition[Through, Map[Head, Variables[expr1]]],
Mean[Level[expr1, {-1}]] + stencil]] // AbsoluteTiming
(*{0.000063, a[-1 + n] + a[n] + a[1 + n] + b[-1 + n] + b[n] + b[1 + n]}*)

• I think you want Total instead of Sort. May 22 at 21:02
• Thanks, bbgodfrey! :) May 22 at 21:21

Outer also works.

Outer[#1/.n->n+#2&, [email protected]@(a[n] + b[n]),{-1,0,1}]//
Total//
Total


a[-1 + n] + a[n] + a[1 + n] + b[-1 + n] + b[n] + b[1 + n]

Here is the trick.

a[n]-b[n] // FullForm


Plus[a[n], Times[-1, b[n]]]

So the + or - will not affect the result.

Different way:

expr1 = a[n] + b[n]
stencil = {-1, 0, 1}
f[ex_, sten_] := Sum[Map[# + tp &, ex, {2}] /. tp -> sten[[i]], {i, 1, Length[sten]}]


Then, f[expr1, stencil] gives

a[-1 + n] + a[n] + a[1 + n] + b[-1 + n] + b[n] + b[1 + n]