I was trying to execute the following code:
form[n_, m_, r_] := (
{large, small} = Sort[{n, m}, Greater]; diff = Abs[large - small];
output =
Exp[-r^2/4]*Sqrt[Factorial[small]/Factorial[large]]*(r/Sqrt[2])^
diff*LaguerreL[small, diff, r^2/2];
Return[output];
);
num = 50;
fnm[r_] := Table[form[n, m, r], {n, 0, num}, {m, 0, num}];
list = Table[Random[Complex], {i, 0, num}, {j, 0, num}];
table1[r_] :=
Table[Conjugate[list[[n1, m1]]]*fnm[r][[n1, m1]]*
list[[n2, (n2 - n1) + m1]]*fnm[r][[n2, (n2 - n1) + m1]], {n1, 1,
num + 1}, {m1, 1, num + 1}, {n2, Max[n1 - m1 + 1, 1],
Min[num + 1, n1 - m1 + num + 1]}];
sum[r_] := Total[Flatten[table1[r]]];
result = NIntegrate[sum[r], {r, 0, ∞}];
This code is extremely slow in constructing table1[r]
, and it takes more than two seconds to construct each element in table1[r]
. However, if I remove the function definition, and simply use
form[n_, m_, r_] := (
{large, small} = Sort[{n, m}, Greater]; diff = Abs[large - small];
output =
Exp[-r^2/4]*Sqrt[Factorial[small]/Factorial[large]]*(r/Sqrt[2])^
diff*LaguerreL[small, diff, r^2/2];
Return[output];
);
num = 50;
fnm = Table[form[n, m, r], {n, 0, num}, {m, 0, num}];
list = Table[Random[Complex], {i, 0, num}, {j, 0, num}];
table1 =
Table[Conjugate[list[[n1, m1]]]*fnm[[n1, m1]]*
list[[n2, (n2 - n1) + m1]]*fnm[[n2, (n2 - n1) + m1]], {n1, 1,
num + 1}, {m1, 1, num + 1}, {n2, Max[n1 - m1 + 1, 1],
Min[num + 1, n1 - m1 + num + 1]}];
sum = Total[Flatten[table1]];
result = NIntegrate[sum, {r, 0, ∞}];
the table1
variable is constructed almost instantly. So my question is:
- Why is there such a big difference by removing the functional definition?
- In general when should one use the functional definition, when should not?
By the way, I was trying to redefine fnm[r_]
as fnm[r_?NumberQ]
, but in this case the table of fnm
was not even generated, because of the NumberQ
constraint. Did I use NumberQ
incorrectly?