Skip to main content
deleted 30 characters in body
Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929

Update: a simpler version for OP's specific case:

ClearAll[f2]
f2[exp_, p_] := exp /. v[x__] :> v[x][[p]] MapAt[1 + # &, v[x], {p}]

f2[10exp = 10 v[0, 0, 2, 0] + v[1, 1, 1, 1] + k v[1, 2, 3, 4]4];
f2[exp , 2]

v[1, 2, 1, 1] + 2 k v[1, 3, 3, 4]

f2[10 v[0, 0, 2, 0] + v[1, 1, 1, 1] + k v[1, 2, 3, 4]f2[exp, 3]

20 v[0, 0, 3, 0] + v[1, 1, 2, 1] + 3 k v[1, 2, 4, 4]

Original post:

ClearAll[foo]
foo = # /. a : #2[__] :> a[[#4]] MapAt[#3, a, {#4}] &;
(* or foo[exp_, func_, tr_, p_]:= exp/. a: func[__] :> a[[p]] MapAt[tr, a, {p}] *)

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]]

Update: a simpler version for OP's specific case:

ClearAll[f2]
f2[exp_, p_] := exp /. v[x__] :> v[x][[p]] MapAt[1 + # &, v[x], {p}]

f2[10 v[0, 0, 2, 0] + v[1, 1, 1, 1] + k v[1, 2, 3, 4], 2]

v[1, 2, 1, 1] + 2 k v[1, 3, 3, 4]

f2[10 v[0, 0, 2, 0] + v[1, 1, 1, 1] + k v[1, 2, 3, 4], 3]

20 v[0, 0, 3, 0] + v[1, 1, 2, 1] + 3 k v[1, 2, 4, 4]

Original post:

ClearAll[foo]
foo = # /. a : #2[__] :> a[[#4]] MapAt[#3, a, {#4}] &;
(* or foo[exp_, func_, tr_, p_]:= exp/. a: func[__] :> a[[p]] MapAt[tr, a, {p}] *)

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]]

Update: a simpler version for OP's specific case:

ClearAll[f2]
f2[exp_, p_] := exp /. v[x__] :> v[x][[p]] MapAt[1 + # &, v[x], {p}]

exp = 10 v[0, 0, 2, 0] + v[1, 1, 1, 1] + k v[1, 2, 3, 4];
f2[exp , 2]

v[1, 2, 1, 1] + 2 k v[1, 3, 3, 4]

f2[exp, 3]

20 v[0, 0, 3, 0] + v[1, 1, 2, 1] + 3 k v[1, 2, 4, 4]

Original post:

ClearAll[foo]
foo = # /. a : #2[__] :> a[[#4]] MapAt[#3, a, {#4}] &;
(* or foo[exp_, func_, tr_, p_]:= exp/. a: func[__] :> a[[p]] MapAt[tr, a, {p}] *)

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]]

added 406 characters in body
Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929

Update: a simpler version for OP's specific case:

ClearAll[f2]
f2[exp_, p_] := exp /. v[x__] :> v[x][[p]] MapAt[1 + # &, v[x], {p}]

f2[10 v[0, 0, 2, 0] + v[1, 1, 1, 1] + k v[1, 2, 3, 4], 2]

v[1, 2, 1, 1] + 2 k v[1, 3, 3, 4]

f2[10 v[0, 0, 2, 0] + v[1, 1, 1, 1] + k v[1, 2, 3, 4], 3]

20 v[0, 0, 3, 0] + v[1, 1, 2, 1] + 3 k v[1, 2, 4, 4]

Original post:

ClearAll[foo]
foo = # /. a : #2[__] :> a[[#4]] MapAt[#3, a, {#4}] &;
(* or foo[exp_, func_, tr_, p_]:= exp/. a: func[__] :> a[[p]] MapAt[tr, a, {p}] *)

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]]

ClearAll[foo]
foo = # /. a : #2[__] :> a[[#4]] MapAt[#3, a, {#4}] &;
(* or foo[exp_, func_, tr_, p_]:= exp/. a: func[__] :> a[[p]] MapAt[tr, a, {p}] *)

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]]

Update: a simpler version for OP's specific case:

ClearAll[f2]
f2[exp_, p_] := exp /. v[x__] :> v[x][[p]] MapAt[1 + # &, v[x], {p}]

f2[10 v[0, 0, 2, 0] + v[1, 1, 1, 1] + k v[1, 2, 3, 4], 2]

v[1, 2, 1, 1] + 2 k v[1, 3, 3, 4]

f2[10 v[0, 0, 2, 0] + v[1, 1, 1, 1] + k v[1, 2, 3, 4], 3]

20 v[0, 0, 3, 0] + v[1, 1, 2, 1] + 3 k v[1, 2, 4, 4]

Original post:

ClearAll[foo]
foo = # /. a : #2[__] :> a[[#4]] MapAt[#3, a, {#4}] &;
(* or foo[exp_, func_, tr_, p_]:= exp/. a: func[__] :> a[[p]] MapAt[tr, a, {p}] *)

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]]

added 87 characters in body
Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929
ClearAll[foo]
foo = # /. a : #2[___]#2[__] :> a[[#4]] MapAt[#3, a, {#4}] &;
(* or foo[exp_, func_, tr_, p_]:= exp/. a: func[__] :> a[[p]] MapAt[tr, a, {p}] *)

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]]

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]]

ClearAll[foo]
foo = # /. a : #2[__] :> a[[#4]] MapAt[#3, a, {#4}] &;
(* or foo[exp_, func_, tr_, p_]:= exp/. a: func[__] :> a[[p]] MapAt[tr, a, {p}] *)

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]]

Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929
Loading