1
$\begingroup$

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)$?

$\endgroup$
3
  • $\begingroup$ Something like: D[Expon, x1] /. {x1 -> 1, x2 -> 2, y1 -> 3, y2 -> 4} $\endgroup$ Commented Oct 16, 2020 at 8:56
  • $\begingroup$ 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. $\endgroup$ Commented Oct 16, 2020 at 9:11
  • 1
    $\begingroup$ You can easily automate this: ... /. Thread[ variables -> values] where variables is a list of variables and values a list of values. $\endgroup$ Commented Oct 16, 2020 at 9:46

1 Answer 1

1
$\begingroup$

From your question, we have

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.

$\endgroup$
1
  • $\begingroup$ It works; thank you for the answer! $\endgroup$ Commented Oct 18, 2020 at 10:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.