1
$\begingroup$

I have a variable of the general form

variable = 
  Subscript[f, {a, b}] Subscript[f, {c, d}] + 
    Subscript[f, {a, c}] Subscript[f, {b, d}]

I would like to transform it something of the form:

f[x_, y_]:= x + y
variable[a_, b_, c_, d_] := f[a, b] f[c, d] + f[a, c] f[b, d]

After seeing some of the answers, perhaps I should pinpoint my problem, the variable I used here is just the general form of a long expression I got after some other calculations and eventualy a Series expansion, I tried to use replacement rules but got the next notice: "SetDelayed: Tag SeriesData in (a lot of symbols) is Protected"

Will appreciate any help!

$\endgroup$
2
$\begingroup$

First you must correct your definition of f to

f[x_, y_] := x + y

Then given

var =
  Subscript[f, {a, b}] Subscript[f, {c, d}] + 
    Subscript[f, {a, c}] Subscript[f, {b, d}];

you can write

 v[a_, b_, c_, d_] = var /. Subscript[f_, {u_, v_}] -> f[u, v];

Then

 v[a, b, c, d]

gives

(a + c) (b + d) + (a + b) (c + d)

If really want use the name var to refer to v, you can write

var = v

making var an alias for v, but I don't recommend that because you will lose the original definition of var

$\endgroup$
1
$\begingroup$

If I'm understanding your question, a replacement should do well.

variable = Subscript[f,{a,b}] Subscript[f,{c,d}] + Subscript[f,{a,c}] Subscript[f,{b,d}]
result = ReplaceAll[Subscript[f_,{x_,y_}] -> f[x,y]][variable]

For a more functional approach:

variable[a_,b_,c_,d_]:= Subscript[f,{a,b}] Subscript[f,{c,d}] + Subscript[f,{a,c}] Subscript[f,{b,d}]
g[a_,b_,c_,d_]:= variable[a,b,c,d]//ReplaceAll[Subscript[f_,{x_,y_}] -> f[x,y]]

Substitute any function of your choice for f using the following:

g[a,b,c,d] /. {f -> MyFunction}
$\endgroup$
  • $\begingroup$ Thank you for answering, but it doesn't work for me. I've been able to use a replacement rule to insert the required function in place of the symbolic variable, but haven't been able to assign the result to a new function of all the possible variables (a,b,c,d in my example). I may add that "variable" is a result of a Series expansion from some initial result of another calculation $\endgroup$ – Gaby Fleurov Jul 21 '17 at 17:31
  • $\begingroup$ @GabyFleurov I just updated my answer, which should take into account your need for a function assignment. You'll need to use SetDelayed (:=) with variable in order to have a,b,c,d evaluated every time the symbol is used. $\endgroup$ – terrygarcia Jul 21 '17 at 17:37
  • $\begingroup$ thanks,I'll check it out $\endgroup$ – Gaby Fleurov Jul 21 '17 at 17:49
1
$\begingroup$

Update: For more general inputs:

ClearAll[makeFunction]
makeFunction[v_] := Module[{vars = Sort@DeleteDuplicates@
       Flatten@Cases[v, Subscript[_, x_] :> x, {0, Infinity}]},
       v /. Subscript -> (# @@ #2 &) /. Thread[vars -> {##}]] &

Examples:

variable = Subscript[f, {a, b}] Subscript[f, {c, d}] + 
   Subscript[f, {a, c}] Subscript[f, {b, d}];
variable2 = Subscript[f1, {a, b, c}] + Subscript[f2, {c, d, e}] + 
   Subscript[f3, {e, f, g}] Subscript[f4, {g, h, i}];
variable3 = Subscript[f1, {a, b, c}] + Subscript[f2, {c, d, e}] + 
   Subscript[f3, {e, f, g}] Subscript[f4, {g, h, i}]^2;

Column[{variable, makeFunction[variable][a, b, c, d]}]

enter image description here

Column[{variable2, makeFunction[variable2] @@ (Symbol["x" <> ToString[#]] & /@ Range[9])}]

enter image description here

Column[{variable2, makeFunction[variable2] @@ Range[9]}]

enter image description here

Column[{variable3, makeFunction[variable3] @@ Range[9]}]

enter image description here

Original answer:

ClearAll[vF]
vF[w_, x_, y_, z_] := variable /. Subscript->(# @@ #2 &) /. 
      Thread[{a, b, c, d} -> {w, x, y, z}]

vF[a, b, c, d]

f[a, c] f[b, d] + f[a, b] f[c, d]

vF[u, v, w, x]

f[u, w] f[v, x] + f[u, v] f[w, x]

vF[1, 2, 3, 4]

f[1, 3] f[2, 4] + f[1, 2] f[3, 4]

If f is pre-defined:

f[x_, y_] := x + y;
vF[a, b, c, d]

(a + c) (b + d) + (a + b) (c + d)

vF[u, v, w, x]

(u + w) (v + x) + (u + v) (w + x)

vF[1, 2, 3, 4]

45

$\endgroup$
  • $\begingroup$ thanks, would you care to explain the syntax in Subscript->(#@@#2&)? Also, I think you missed a closing } in the Thread function, is it after the d? $\endgroup$ – Gaby Fleurov Jul 21 '17 at 19:25
  • 1
    $\begingroup$ @GabyFleurov, Subscript->(#@@#2&) is another way of writing Subscript[a_,b_] :> a[b]). when it is fed two arguments (f and {x,y} , say) , the pure function #@@#2& produces f@@{x,y} ( which is f[x,y]). $\endgroup$ – kglr Jul 21 '17 at 19:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.