There is a weird behavior of Compile
that I do not understand. Consider the following example:
(*initial parameters*)
lL = 64.;
pi = 3.14;
tab = Flatten[Table[{kx, ky},
{kx, 0., pi, pi/lL}, {ky, 0., pi, pi/lL}], 1];
enn = Compile[{{kx, _Real}, {ky, _Real}}, Cos[kx] Cos[ky],
CompilationTarget -> "C"];
First approach
fF = Compile[{{En, _Real}}, 1./(1. + Exp[500. En]),
CompilationTarget -> "C"];
sumT = Compile[{{tab, _Real, 2}},
Total[Map[fF[enn[#[[1]], #[[2]]]] &, tab, 1]],
CompilationTarget -> "C"];
sumT[tab] // AbsoluteTiming
{2.389137, 131584.5}
Second approach
ffd = Compile[{{kx, _Real}, {ky, _Real}},
1./(1. + Exp[500. enn[kx, ky]]), {{enn, _Real}},
CompilationTarget -> "C"];
sumt = Compile[{{tab, _Real, 2}},
Total[Map[ffd[#[[1]], #[[2]]] &, tab]],
CompilationTarget -> "C"];
sumt[tab] // AbsoluteTiming
{0.061003, 131584.5}
The first approach is much slower and returns the following error:
"Argument {{0.,0.},(...),<<263119>>}
at position 1 should be a "machine-size real number"
If the argument of fF
is declared as _Real
, shouldn't it work as well?
I've tried to add {{enn,_Real}}
as a declaration of subexpression in sumT, but it didn't change a thing.
Somehow, in the problem I try to tackle, I'd prefer to use the first approach (if it would work:-).