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]];

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?

  • $\begingroup$ can you post an example of what, say, expression1 in exps looks like? $\endgroup$ – kglr Jun 6 '18 at 6:53
  • $\begingroup$ maybe exps /. Thread[vars -> a]? $\endgroup$ – kglr Jun 6 '18 at 7:01
  • $\begingroup$ Through[{e1, e2, e3} @@ {a1, a2, a3, a4}]? $\endgroup$ – Kuba Jun 6 '18 at 7:10
  • $\begingroup$ @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! $\endgroup$ – Dan Jun 6 '18 at 7:19
vars = Array[Symbol["x" <> ToString @ #] &, 5];
aa = Array[Symbol["a" <> ToString @ #] &, 5];

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:

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

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

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.