3

So I have this rather complicated looking expression

{-0.192308 (0.004 - 2.5 α00^2 - 1.25 α01^2 - 
    1.25 α02^2 - 1.25 β01^2 - 1.25 β02^2), 
 0.0000815811 (19030.2 α00 - 2420.83 α00^3 - 
    3631.25 α00 α01^2 - 
    1815.62 α01^2 α02 - 
    3631.25 α00 α02^2 - 3042.11 α11 β01 - 
    3042.11 α21 β01 - 3631.25 α00 β01^2 + 
    1815.62 α02 β01^2 - 6084.22 α12 β02 - 
    6084.22 α22 β02 - 
    3631.25 α01 β01 β02 - 
    3631.25 α00 β02^2 + 3042.11 α01 β11 + 
    6084.22 α02 β12 + 3042.11 α01 β21 + 
    6084.22 α02 β22 + 16286. α10 ω1^2 + 
    36254.4 α20 ω1^2), 
 0.0000815811 (-3933.52 α00 - 897.3 α00^3 - 
    1345.95 α00 α01^2 - 
    672.975 α01^2 α02 - 
    1345.95 α00 α02^2 - 1127.58 α11 β01 - 
    1127.58 α21 β01 - 1345.95 α00 β01^2 + 
    672.975 α02 β01^2 - 2255.16 α12 β02 - 
    2255.16 α22 β02 - 
    1345.95 α01 β01 β02 - 
    1345.95 α00 β02^2 + 1127.58 α01 β11 + 
    2255.16 α02 β12 + 1127.58 α01 β21 + 
    2255.16 α02 β22 + 1493.11 α10 ω1^2 + 
    123758. α20 ω1^2), 
 0.384615 (2.5 α00 α01 + 1.25 α01 α02 + 
    2.51327 β01 + (π β01)/2 + 
    1.25 β01 β02 - π β11 - π β21), \
-0.0000815811 (-19030.2 α01 + 7262.5 α00^2 α01 + 
    1815.62 α01^3 + 7262.5 α00 α01 α02 + 
    3631.25 α01 α02^2 + 30244.7 α11 + 
    6084.22 α12 β01 + 6084.22 α22 β01 + 
    1815.62 α01 β01^2 + 3042.11 α11 β02 + 
    3042.11 α21 β02 + 
    7262.5 α00 β01 β02 + 
    3631.25 α01 β02^2 - 6084.22 α00 β11 - 
    3042.11 α02 β11 - 6084.22 α01 β12 - 
    6084.22 α00 β21 - 3042.11 α02 β21 - 
    6084.22 α01 β22 - 16286. α11 ω1^2 - 
    36254.4 α21 ω1^2), -0.0000815811 (3933.52 \
α01 + 2691.9 α00^2 α01 + 672.975 α01^3 + 
    2691.9 α00 α01 α02 + 
    1345.95 α01 α02^2 + 30244.7 α21 + 
    2255.16 α12 β01 + 2255.16 α22 β01 + 
    672.975 α01 β01^2 + 1127.58 α11 β02 + 
    1127.58 α21 β02 + 
    2691.9 α00 β01 β02 + 
    1345.95 α01 β02^2 - 2255.16 α00 β11 - 
    1127.58 α02 β11 - 2255.16 α01 β12 - 
    2255.16 α00 β21 - 1127.58 α02 β21 - 
    2255.16 α01 β22 - 1493.11 α11 ω1^2 - 
    123758. α21 ω1^2), 
 0.192308 (1.25 α01^2 + 5. α00 α02 - 
    1.25 β01^2 + 10.0531 β02 + 2 π β02 - 
    4 π β12 - 
    4 π β22), -0.0000815811 (3631.25 α00 \
α01^2 - 19030.2 α02 + 7262.5 α00^2 α02 + 
    3631.25 α01^2 α02 + 1815.62 α02^3 + 
    120979. α12 - 3042.11 α11 β01 - 
    3042.11 α21 β01 - 3631.25 α00 β01^2 + 
    3631.25 α02 β01^2 + 1815.62 α02 β02^2 - 
    3042.11 α01 β11 - 12168.4 α00 β12 - 
    3042.11 α01 β21 - 12168.4 α00 β22 - 
    16286. α12 ω1^2 - 
    36254.4 α22 ω1^2), -0.0000815811 (1345.95 \
α00 α01^2 + 3933.52 α02 + 
    2691.9 α00^2 α02 + 
    1345.95 α01^2 α02 + 672.975 α02^3 + 
    120979. α22 - 1127.58 α11 β01 - 
    1127.58 α21 β01 - 1345.95 α00 β01^2 + 
    1345.95 α02 β01^2 + 672.975 α02 β02^2 - 
    1127.58 α01 β11 - 4510.32 α00 β12 - 
    1127.58 α01 β21 - 4510.32 α00 β22 - 
    1493.11 α12 ω1^2 - 
    123758. α22 ω1^2), -0.384615 (2.51327 α01 + \
(π α01)/2 - π α11 - π α21 - 
    2.5 α00 β01 + 1.25 α02 β01 - 
    1.25 α01 β02), -0.0000815811 (6084.22 α00 \
α11 - 3042.11 α02 α11 + 
    6084.22 α01 α12 + 6084.22 α00 α21 - 
    3042.11 α02 α21 + 6084.22 α01 α22 - 
    19030.2 β01 + 7262.5 α00^2 β01 + 
    1815.62 α01^2 β01 - 
    7262.5 α00 α02 β01 + 
    3631.25 α02^2 β01 + 1815.62 β01^3 + 
    7262.5 α00 α01 β02 + 
    3631.25 β01 β02^2 + 30244.7 β11 - 
    3042.11 β02 β11 + 6084.22 β01 β12 - 
    3042.11 β02 β21 + 6084.22 β01 β22 - 
    16286. β11 ω1^2 - 
    36254.4 β21 ω1^2), -0.0000815811 (2255.16 α00 \
α11 - 1127.58 α02 α11 + 
    2255.16 α01 α12 + 2255.16 α00 α21 - 
    1127.58 α02 α21 + 2255.16 α01 α22 + 
    3933.52 β01 + 2691.9 α00^2 β01 + 
    672.975 α01^2 β01 - 
    2691.9 α00 α02 β01 + 
    1345.95 α02^2 β01 + 672.975 β01^3 + 
    2691.9 α00 α01 β02 + 
    1345.95 β01 β02^2 - 1127.58 β02 β11 + 
    2255.16 β01 β12 + 30244.7 β21 - 
    1127.58 β02 β21 + 2255.16 β01 β22 - 
    1493.11 β11 ω1^2 - 
    123758. β21 ω1^2), -0.384615 (8.16814 α02 - 
    2 π α12 - 2 π α22 - 
    1.25 α01 β01 - 
    2.5 α00 β02), -0.0000815811 (3042.11 α01 \
α11 + 12168.4 α00 α12 + 
    3042.11 α01 α21 + 12168.4 α00 α22 + 
    7262.5 α00 α01 β01 - 19030.2 β02 + 
    7262.5 α00^2 β02 + 3631.25 α01^2 β02 + 
    1815.62 α02^2 β02 + 3631.25 β01^2 β02 + 
    1815.62 β02^3 - 3042.11 β01 β11 + 
    120979. β12 - 3042.11 β01 β21 - 
    16286. β12 ω1^2 - 
    36254.4 β22 ω1^2), -0.0000815811 (1127.58 α01 \
α11 + 4510.32 α00 α12 + 
    1127.58 α01 α21 + 4510.32 α00 α22 + 
    2691.9 α00 α01 β01 + 3933.52 β02 + 
    2691.9 α00^2 β02 + 1345.95 α01^2 β02 + 
    672.975 α02^2 β02 + 1345.95 β01^2 β02 + 
    672.975 β02^3 - 1127.58 β01 β11 - 
    1127.58 β01 β21 + 120979. β22 - 
    1493.11 β12 ω1^2 - 
    123758. β22 ω1^2), α00 + α01 + \
α02 + α10 + α11 + α12 + α20 + \
α21 + α22}

I would like to to turn into a multi variable function of these variables.

{ω0, α00, α01, α02, α10, \
α11, α12, α20, α21, α22, \
β01, β02, β11, β12, β21, β22}

I know you can just do something like

f[ω0_, α00_, α01_, α02_, α10_, α11_, α12_, α20_, α21_, α22_, β01_, β02_,β11_, β12_, β21_, β22_] := ...

I was hoping to find a way where I don't specifically write out each variable, since the number of variables is going to vary with different iterations of the program.

xentity1x
  • 261
  • 1
  • 7
  • Related/duplicate: http://mathematica.stackexchange.com/questions/10067/convert-an-expression-to-a-function – Michael E2 May 22 '15 at 00:11

5 Answers5

6

It was actually a lot easier than I thought

f = Function @@ {vars, expr}
xentity1x
  • 261
  • 1
  • 7
6

If you have a list of variables stored in vars in the desired order and expr is the function's formula, then

f = Function @@ {vars, expr}

will define a function for you. If you're content with the variables being in their sorted order and the expression expr is polynomial-ish (test Variables[expr] first), then

f = Function @@ {Variables[expr], expr}

will define a function for you.


If for some reason your variables have assigned values that interfere with the above, then one can use

f = Thread[
    Union @ Cases[Hold[expr] /. OwnValues[expr],
      v_Symbol /; Context[v] === "Global`" :> Hold[v],
      Infinity], 
    Hold] /. 
   Hold[v_] :> Block[v, Function @@ {Variables[expr], expr}];

(There may be a more elegant workaround, but that's what I can come with.)

Michael E2
  • 235,386
  • 17
  • 334
  • 747
1
f[ω0_, α00_, α01_, α02_, α10_, α11_, α12_, α20_, α21_, α22_, β01_, β02_,β11_, β12_, β21_, β22_] := ... 

where ... is the list in your OP.

N.B. you're missing ω1 as an argument, I assume that's intentional...

ciao
  • 25,774
  • 2
  • 58
  • 139
  • Thanks. Do you know of a way to do it if the the number of variables is unknown? The number of variables is going to be a parameter of the program I'm running. – xentity1x May 21 '15 at 23:12
  • @xentity1x Is ω1 always a parameter (an omitted variable)? – Michael E2 May 21 '15 at 23:15
  • Yes it is. Sorry forgot to put in a numerical value for that one. – xentity1x May 21 '15 at 23:19
  • @xentity1x Forgive me, to me "yes it is" means ω1 is to remain symbol, but "forgot to put in a numerical value" suggests it was supposed to be a number. The two interpretations contradict each other. Which is correct? -- Also, if the number of variables is unknown, how do you know what order to put them in (as arguments for the function)? – Michael E2 May 21 '15 at 23:22
  • ω1 is supposed to be a number. The alphas and the betas are ordered by their indices, so I want to put them in the order determined by that. Beginning to think this might not be possible... – xentity1x May 21 '15 at 23:26
  • @xentity1x: Probably easier, based on your last comment, to consider using indexed variables (e.g. blah[n]), and use a replace with a rule-set that has the variable(s) for which you want to insert values. – ciao May 21 '15 at 23:56
1

Supposing that you want a DownValues definition rather than a Function you could use:

(* with your expression assigned to expr *)

(x \[Function] x_) /@ Variables[expr];

f[Sequence @@ %] = expr;
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
0

Why not to use a list for your arguments? Something like:

f1[αvars_, βvars_] := {αvars[[1]] + ..., ..., βvars[[5]] ... }

Or, as I can see you have double indicies for your variables. Can you formulate your equations as matrix manipulation? This can significantly simplify the task if possible.

Bichoy
  • 1,203
  • 6
  • 15