Skip to main content
1 of 4
kglr
  • 400.5k
  • 18
  • 488
  • 929
ClearAll[foo]
foo = # /. a : #2[___] :> a[[#4]] MapAt[#3, a, {#4}] &;

Examples:

example = v[1, 1, 1, 1] + 10*v[0, 0, 2, 0] + 5 w[1, 2, 3];

foo[example, v, 1 + # &, 2]

v[1, 2, 1, 1] + 5 w[1, 2, 3]

foo[example, v, 1 + # &, 3]

20 v[0, 0, 3, 0] + v[1, 1, 2, 1] + 5 w[1, 2, 3]

foo[example, v, h, 3]

20 v[0, 0, h[2], 0] + v[1, 1, h[1], 1] + 5 w[1, 2, 3]

foo[example, w, h, 3]

10 v[0, 0, 2, 0] + v[1, 1, 1, 1] + 15 w[1, 2, h[3]]

foo[example, w | v, h, 3]

20 v[0, 0, h[2], 0] + v[1, 1, h[1], 1] + 15 w[1, 2, h[3]]

kglr
  • 400.5k
  • 18
  • 488
  • 929