# How to correctly reassign variables {c11 -> p11, c12 -> p12} using a Table or some other similar structure?

Why is the value of the function Pxx[1,2] is a number but the function Pxxx[1,2] is an expression? Isn't this construction TT in the function Pxxx[p11_, p12_] the same thing as its explicit entry {c11 -> p11, c12 -> p12} in the function Pxx[p11_, p12_]? What is the correct way to write the function Pxxx[p11_, p12_] so that Pxxx[1,2] outputs the number?

ClearAll["Global*"]
imax = 1; jmax = 2;
Psi[r_, z_, i_, j_] := Exp[-z^2]*Exp[-r^2];
c[i_, j_] := Symbol["c" <> ToString[i] <> ToString[j]]
p[i_, j_] := Symbol["p" <> ToString[i] <> ToString[j]]
VB[r_, z_] = -(1/(2*Sqrt[r^2 + z^2]))*Exp[-Sqrt[r^2 + z^2]*1/2];
Px[i1_, j1_, i2_, j2_] :=
NIntegrate[
Psi[r, z, i2, j2]*VB[r, z]*Psi[r, z, i1, j1]*r, {r,
0, \[Infinity]}, {z, -Infinity, Infinity}];
Px1 = 2 Pi Sum[
c[i1, j1] c[i2, j2] Px[i1, j1, i2, j2], {i1, 1, imax}, {i2, 1,
imax}, {j1, 1, jmax}, {j2, 1, jmax}];

Pxx[p11_, p12_] := Module[{}, Px1 /. {c11 -> p11, c12 -> p12}]
Pxx[1, 2]
(*-10.469192355392455*)
TT = Flatten[Table[c[i, j] -> p[i, j], {i, 1, imax}, {j, 1, jmax}]]
{c11 -> p11, c12 -> p12}
Pxxx[p11_, p12_] := Module[{}, Px1 /. TT]
Pxxx[1, 2]
(*2 (-0.1851359681711769 p11^2 - 0.3702719363423538 p11 p12 -
0.1851359681711769 p12^2) \[Pi]*)


Pxxx[1, 2]


The evaluator looks for a rule to apply, and it finds one:

HoldPattern[Pxxx[p11_, p12_]] :> Module[{}, Px1 /. TT]


This is how the definition is stored in DownValues. How does it apply this rule? It looks at the argument patterns on the left-hand side and figures out where argument values should be inserted on the right-hand side. There are no p11 or p12 on the right-hand side. So, it's done with replacements (since there were none to do) and goes ahead with the next step, which is to evaluate the right-hand side. In doing that, it will eventually expand Px1 and TT, but by that time, it won't see p11 and p12 as placeholders to be filled with argument values, but just plain old symbols that need to be evaluated. Since it doesn't find any own-values for them, they will just remain as-is in the result.

#### Note

I really, really, wish you would tell us what your actual problem is. The code that you are sharing here is far more complicated than it needs to be. You're doing things that look bizarre. For example:

Psi[r_, z_, i_, j_] := Exp[-z^2]*Exp[-r^2]


You declare i and j as arguments, but they aren't used on the right-hand side. That's a huge red flag that tells me that you don't really understand how to define functions in Mathematica. Consequently, i2 and j2 in your definition of Px are unused.

You go to an inordinate amount of trouble to create argument names that include a sort of indexing scheme, but they're just normal arguments that get replaced during evaluation. Why waste all of that effort? You're tying yourself in knots trying to do things that I'm sure are very easy to do.

• Thanks for the explanation! But I can't realize, how can this procedure be done? May 26, 2023 at 16:20
• I'm sorry for such unoptimized code, but this code is not original. I forgot to remove the variables i and j` May 26, 2023 at 16:20
• I will write the original part of the code and what I would like to receive, if you could show how to make these things more simple, I would be very glad May 26, 2023 at 17:35
• I posted part of the original code and the question here: mathematica.stackexchange.com/questions/285763/…. I would be glad for your help May 26, 2023 at 18:38