I have an expression V = (a[1] + a[2])b[1]
. How would I define a function of a[1]
, a[2]
and b[1]
? I'm looking for something like this f[a[1]_,a[2]_,b[1]_]=(a[1] + a[2])b[1]
but Mathematica isn't satisfied with that definition.
Answer
One way to do this, most likely not the most elegant, is to rename variables of the form a[n]
temporarily.
Suppose v = (a[1] + a[2]) b[1]
. Then define
f[a1_, a2_, b1_] := Evaluate[v /. {a_[n_] :> ToExpression[ToString[a] <> ToString[n]]}]
With this definition you get the desired result if you evaluate f[a[1],a[2],b[1]]
.
Comments
Post a Comment