@lericr have written under several of my questions that the way I try to modernize the code could be made much simpler. Below is the original part of the code. The remaining parts have similar designs, so if what I would like to get can be simplified, then this can be applied to other parts.
The goal I would like to achieve is to have the function EER[a1_, b1_, qa_, qb_, c11_, c12_, c13_,..., c21_, c22_, c23_,...]
that depends on an arbitrary number of arguments of this type cij
. The number of variables cij
depends on the imax
and jmax
.
The function EER
is the sum of the Kxx
and Pxx
each of which depends on variables a1, b1, qa, qb, c11, c12, c13,..., c21, c22, c23,...
. I managed to present the function Kxx
in such a way that it is flexible in relation to the number of variables (that is, when changing imax
and jmax
, a specific value of this function can be easily obtained by substituting the numerical values of all variables). But I can't do the same procedure with the function Pxx
.
Once again, briefly goal: to make the function Pxx[a1_, b1_, qa_, qb_, c11_, c12_, c13_,..., c21_, c22_, c23_,...]
universal in relation to the number of variables, and also, if possible, to reasonably simplify the code.
The code is written for imax=1
and jmax=2
. If you change them to imax=2
and jmax=3
for example, you can see that the function Kxx
is flexible and you can immediately get its values (in:Kxx[1.22, 0.44, 1.40, 1.81, 1, 2, 1, 3, 2, 1]
out: 281.248
), but this is not done with the function Pxx
(in:Pxx[1.22, 0.44, 1.40, 1.81, 1, 2, 1, 3, 2, 1]
out: Pxx[1.22, 0.44, 1.4, 1.81, 1, 2, 1, 3, 2, 1]
).
ClearAll["Global`*"]
imax = 1; jmax = 2;
Psi[r_, z_, i_, j_] := Exp[-b[j]*z^2]*Exp[-a[i]*r^2];
(*variables cij*)
c[i_, j_] := Symbol["c" <> ToString[i] <> ToString[j]]
var = Join[{a1, b1, qa, qb},
Flatten[Table[c[i, j], {i, 1, imax}, {j, 1, jmax}]]];
var1 = Pattern[#, Blank[]] & /@ var;
(*a[i] and b[i] are geometric progressions*)
a[1] = a1;
b[1] = b1;
Do[a[i] = a[i - 1] qa, {i, 2, imax}];
Do[b[j] = b[j - 1] qb, {j, 2, jmax}];
Kk = FullSimplify[
Psi[r, z, i2, j2] *
Laplacian[Psi[r, z, i1, j1] , {r, \[Theta], z}, "Cylindrical"]];
Kk1 = Integrate[Kk r, {r, 0, \[Infinity]}, {z, -Infinity, Infinity},
Assumptions -> {a[i1] > 0, b[i1] > 0, a[i2] > 0, b[i2] > 0,
a[j1] > 0, b[j1] > 0, a[j2] > 0, b[j2] > 0}];
Kx = -1/2 2 Pi Sum[
c[i1, j1] c[i2, j2] Kk1, {i1, 1, imax}, {i2, 1, imax}, {j1, 1,
jmax}, {j2, 1, jmax}];
Kxx[Sequence @@ var1] = Kx;
VB1[r_, z_] := -(1/(2*Sqrt[r^2 + z^2]))*Exp[-Sqrt[r^2 + z^2]*1/2];
Px1[a10_, b10_, qa0_, qb0_, i1_, j1_, i2_, j2_] :=
Block[{a1 = a10, b1 = b10, qa = qa0, qb = qb0},
NIntegrate[
Psi[r, z, i2, j2]*VB1[r, z]*Psi[r, z, i1, j1]*r, {r,
0, \[Infinity]}, {z, -Infinity, Infinity}]];
Pxx[a10_, b10_, qa0_, qb0_, c111_, c121_] :=
Block[{c11 = c111, c12 = c121},
2 Pi Sum[
c[i1, j1] c[i2, j2] Px1[a10, b10, qa0, qb0, i1, j1, i2, j2], {i1,
1, imax}, {i2, 1, imax}, {j1, 1, jmax}, {j2, 1, jmax}]];
With[{var = var},
EER[Sequence @@ var1] :=
N[Kxx[Sequence @@ var] + Pxx[Sequence @@ var]]];
(*imax=1, jmax=2*)
(*Kxx[1.22,0.44,1.40,1.81,1,2]*)
(*27.549720475849007` *)
(*Pxx[1.22,0.44,1.40,1.81,1,2]*)
(*-10.388782043150181` *)
(*imax=2, jmax=3*)
(*Kxx[1.22,0.44,1.40,1.81,1,2,1,3,2,1]*)
(*281.2480800909348` *)
(*Pxx[1.22,0.44,1.40,1.81,1,2,1,3,2,1]*)
(*Pxx[1.22`,0.44`,1.4`,1.81`,1,2,1,3,2,1]*)
(*imax=1, jmax=2*)
(*EER[1.22,0.44,1.40,1.81,1,2]*)
(*17.160938432698828` *)
(*imax=2, jmax=3*)
(*EER[1.22,0.44,1.40,1.81,1,2,1,3,2,1]*)
(*EER[1.22`,0.44`,1.4`,1.81`,1,2,1,3,2,1]*)
myFun[varList_List]
and then process the list items in the body of the function? $\endgroup$