# Substitution of expressions in a symbolic expression

I define tables of symbolic variables in the following form (for convenience)

X = Table[Symbol["x" <> ToString[i]], {i, 1, num}];
Y = Table[Symbol["y" <> ToString[j]], {j, 1, num}];


And after that, in cycles, I create some expressions. For example, here is one of them

Expon := Exp[ - ((X[[1]] * Y[[1]]) / 4) ];
For[i = 2, i <= num, i++,
Expon = Expon * Exp[ - ((X[[i]] * Y[[i]]) / 4)] ]


After that, I want to act by some differential operator on my symbolic expression (let's call it $$\Psi$$) and substitute in the final expression some tables of numbers X1 and Y1 (here they are not symbolic, but filled by real numbers). I tried to use ReplaceAll ./ command, but it didn't work. Could you tell me please, how can I substitute two or more tables of real numbers in symbolic expression? Long story short, how to calculate something like $$\Psi(X1, Y1)$$?

• Something like: D[Expon, x1] /. {x1 -> 1, x2 -> 2, y1 -> 3, y2 -> 4} – Daniel Huber Oct 16 '20 at 8:56
• Thank you for your answer. But the problem is that I have ~ 200 variables (100 x_i and 100 y_i), and I'd like to do substitution in the cycle. Doing that manually would be crazy. – MightyPower Oct 16 '20 at 9:11
• You can easily automate this: ... /. Thread[ variables -> values] where variables is a list of variables and values a list of values. – Daniel Huber Oct 16 '20 at 9:46

num = 3

X = Table[Symbol["x" <> ToString[i]], {i, 1, num}];
Y = Table[Symbol["y" <> ToString[j]], {j, 1, num}];


and we have two lists of values of the X's and Y's,

{xvals, yvals} = RandomReal[{-10, 10}, {2, num}];


We can use Thread to create our replacement rules like this

rules = Join[Thread[X -> xvals], Thread[Y -> yvals]];


We can apply the rules to any expressions, e.g.

ψlist = Flatten@Outer[ψ, X, Y];

ψlist /. rules

(*  {ψ[-0.1739, 4.43855], ψ[-0.1739, 1.32993],  ψ[-0.1739, 3.49117],
ψ[4.42524, 4.43855], ψ[4.42524, 1.32993],  ψ[4.42524, 3.49117],
ψ[-4.26432,4.43855], ψ[-4.26432, 1.32993], ψ[-4.26432, 3.49117]}  *)


We can also use rules = Thread /@ {X -> xvals, Y -> yvals} // Flatten, which may be easier to read.

• It works; thank you for the answer! – MightyPower Oct 18 '20 at 10:35