-1

I have the following problem. I current can automatically create the variable positionPayload, for instance:

positionPayload=2.x+3.t^2

I need, however, to automatically make positionPayload a function of x and t, i.e. do something like positionPayload[x_,t_]. To identify the variables present in positionPayload, I do:

variableList=DeleteDuplicates[Variables[positionPayload]]

However, how do I now write something to the effect of positionPayload[variableList] such that Mathematica now understands that positionPayload is a function of those variables?

Update

My question really is: how can I convert positionPayload into positionPayload[x_,t_] using the list variableList.

space_voyager
  • 841
  • 4
  • 11

4 Answers4

5

Not that I would recommend this, but anyway:

positionPayload = 2. x + 3. t^2;
variableList = Variables[positionPayload];
positionPayload = Function[Evaluate@variableList, Evaluate@positionPayload];
positionPayload[q, r]
(* 3. q^2 + 2. r *)

Edit

positionPayload = 2. x[1] + 3. t[3]^2;
variableList = Variables[positionPayload];
ul = Unique[ConstantArray[\[FormalT], Length@variableList]];
positionPayload = Function @@ {ul, positionPayload /. Thread[variableList -> ul]};
positionPayload[q, r]
(*3. q^2+2. r*)
Dr. belisarius
  • 115,881
  • 13
  • 203
  • 453
3

You might try something like this, which makes pure functions, which means the variables used int the expression to converted only have to be clear at time expToF is called.

expToF[exp_, vars : {_Symbol ..}] :=
  With[{body = exp /. Thread[Rule[vars, Slot /@ Range @ Length[vars]]]},
    Function[body]]

Clear[x,t] f = expToF[2. x + 3. t^2, {x, t}]; f[x,t]

3. t^2 + 2. x

Not limited to two variables.

Clear[x, y, z]
g = expToF[Sin[2 x] (1 - Cos[y]) E^z, {x, y, z}];
g[a, b, c]
(E^c)(1 - Cos[b]) Sin[2 a]

It can even be used for somewhat weird things, like

Clear[x]
h = expToF[Style[x, 24, Bold, Red, "SR"], {x}]
h[1 + Sin[x]]

which produces

funny

Despite Mathematica's funny formatting of the pure function returned by expToF, the returned function, as can be seen, works perfectly well.

Update

To handle the OP's revised question, I revise my definition definition of expToF to

expToF[exp_, vars : {(_Symbol | h_Symbol[_Integer]) ..}] :=
  With[{body = exp /. Thread[Rule[vars, Slot /@ Range @ Length[vars]]]},
    Function[body]]

then

Clear[x]
positionPayload = 2. x[1] + 3. x[2]^2;
positionPayload = expToF[positionPayload, {x[1], x[2]}];
positionPayload[q, r]
2. q + 3. r^2
m_goldberg
  • 107,779
  • 16
  • 103
  • 257
2
positionPayload = 2. x + 3. t^2
(* 3. t^2 + 2. x *)

variableList = DeleteDuplicates[Variables[positionPayload]]
(* {t, x} *)

temp = positionPayload;
positionPayload =.
Evaluate[positionPayload @@ (Pattern[#, Blank[]] & /@ variableList)] := Evaluate@temp

Definition@positionPayload
(* positionPayload[t_, x_] := 3. t^2 + 2. x *)

positionPayload[q, r]
(* 3. q^2 + 2. r *)
Simon Rochester
  • 6,211
  • 1
  • 28
  • 40
2

My one shot at answering this question:

Attributes[convert] = {HoldFirst};

convert[def_Symbol?ValueQ] :=
  With[{old = def, pats = Quiet[Sequence @@ Cases[Variables @ def, s_Symbol :> s_]]},
    ClearAll[def];
    def[pats] := old;
  ]

Test:

positionPayload = 2. x + 3. t^2;

convert[positionPayload]

?? positionPayload

Global`positionPayload

positionPayload[t_, x_] := 3. t^2 + 2. x
positionPayload[5, 7]
89.

I hope it helps.

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371