# Evaluating functions out of expressions stored in list

I have a method that takes an integer $n$ and returns a list of variables vars = {x1,...,xn} and down the line a list of expressions in $x_{1},\cdots,x_{n}$ is created: $$\text{exps} = \lbrace \text{expression } 1,\cdots, \text{expression } n \rbrace.$$ Now, I want to make a new list consisting of each expression evaluated at some point in $n$-tuple. In other words, for some point a = {a1,...,an}, I would like $$\text{final} = \lbrace \text{expression } 1 |_{a}, \cdots, \text{expression } n|_{a} \rbrace.$$ I have tried

 final = {};
For[i = 1, i <= Length[vars], i++,
f[vars[[i]]_] := exps[[i]];
AppendTo[final, f[ai]];
]
final


but I don't think Mathematica likes me using f[vars[[i]]_] and returns an error stating that "Tag Function... is Protected." My idea seems not very robust at best. How would I go about interpreting this list of expressions as a list of functions, evaluating at my desired point, and creating a new list with the evaluated expressions?

• can you post an example of what, say, expression1 in exps looks like? – kglr Jun 6 '18 at 6:53
• maybe exps /. Thread[vars -> a]? – kglr Jun 6 '18 at 7:01
• Through[{e1, e2, e3} @@ {a1, a2, a3, a4}]? – Kuba Jun 6 '18 at 7:10
• @kglr An example of expressions1 would be something like x1+2(x2+x3). I don't have Mathematica handy right now, so I'll give the suggestions a try tomorrow. Thanks all! – Dan Jun 6 '18 at 7:19

vars = Array[Symbol["x" <> ToString @ #] &, 5];
aa = Array[Symbol["a" <> ToString @ #] &, 5];

SeedRandom[1];
exps = vars.RandomInteger[9, {5, 5}];
exps  // TeXForm


$\scriptsize\{\text{x1}+\text{x3}+\text{x4},4 \text{x1}+8 \text{x2}+8 \text{x3}+3 \text{x4}+2 \text{x5},6 \text{x2}+5 \text{x3}+2 \text{x4}+6 \text{x5},7 \text{x1}+\text{x3}+\text{x4}+4 \text{x5},4 \text{x2}+\text{x3}+6 \text{x4}+5 \text{x5}\}$

You can use ReplaceAll to replace vars in exps with aa :

newexps = exps /. Thread[vars -> aa];
newexps  // TeXForm


$\scriptsize\{\text{a1}+\text{a3}+\text{a4},4 \text{a1}+8 \text{a2}+8 \text{a3}+3 \text{a4}+2 \text{a5},6 \text{a2}+5 \text{a3}+2 \text{a4}+6 \text{a5},7 \text{a1}+\text{a3}+\text{a4}+4 \text{a5},4 \text{a2}+\text{a3}+6 \text{a4}+5 \text{a5}\}$

Alternatively, you can make exps into a function of vars and use it on aa:

ClearAll[foo]
foo[Pattern[#, _] & /@ vars] := Evaluate[exps]
foo @ aa // TeXForm


$\scriptsize\{\text{a1}+\text{a3}+\text{a4},4 \text{a1}+8 \text{a2}+8 \text{a3}+3 \text{a4}+2 \text{a5},6 \text{a2}+5 \text{a3}+2 \text{a4}+6 \text{a5},7 \text{a1}+\text{a3}+\text{a4}+4 \text{a5},4 \text{a2}+\text{a3}+6 \text{a4}+5 \text{a5}\}$

Or, make exps into a pure function bar that can be Applyed to aa:

bar = Function[Evaluate[exps /. Thread[vars -> Slot /@ Range[Length@vars]]]];
bar @@ aa // TeXForm


same result as above

• Yet again you provided a great, comprehensive answer! Thanks, works well! – Dan Jun 6 '18 at 23:08
• @Dan, my pleasure. Thank you for the accept. – kglr Jun 6 '18 at 23:17