Is it possible to put an If
statement as below within a compile (see below)? I received a warning about SetDelayed
.
eef = Compile[{{μ, _Real}, {NNN, _Integer}},
Module[{NN = NNN},
kd[m_, n_] := If[m == n, 1, 0];
cosfun[m_, n_] :=
If[m == n,
0,
(1 - Cos[(m + n) π])/( (m + n) π)
+ ((Cos[(m - n) π] - 1)/( (m - n) π))
];
mm = Table[kd[m, n] (MM - B2 k k - B1 (m Pi/L)^2),
{m, 1, NN}, {n, 1, NN}];
kz = Table[cosfun[m, n] (-I A2 m Pi/L), {m, 1, NN}, {n, 1, NN}];
kxM = Table[kd[m, n] A1 k, {m, 1, NN}, {n, 1, NN}];
μM = Table[kd[m, n] μ, {m, 1, NN}, {n, 1, NN}];
HH = ArrayFlatten[{
{μM + mm, 0 mm, kz, kxM},
{0 mm, μM + mm, kxM, -kz},
{kz, kxM, μM - mm, 0 mm},
{kxM, -kz, 0 mm, μM - mm}
}];
ees = Table[Eigenvalues[HH], {k, -.1, .1, .01}]
] (* End Module *)
] (* End Compile *)
where A1
, A2
, B1
, B2
, and MM
are global variables.
Answer
Compile
can't handle SetDelayed
.
In your specific case, you might be able to avoid the need for SetDelayed
altogether through the use of Boole
.
kd[m_, n_] :=If[m == n, 1, 0]
can be replaced by Boole[n==m]
. (or as FJRA pointed out in comments, by KroneckerDelta
.)
cosfun[m_, n_] :=
If[m == n, 0, (1 - Cos[(m + n) π])/((m + n) π) +
((Cos[(m - n) π] - 1)/((m - n) π))]
can be replaced by
Boole[m == n] (1 - Cos[(m + n) π])/((m + n) π) +
((Cos[(m - n) π] - 1)/((m - n) π)) (-I A2 m Pi/L)
Giving
eenew = Compile[{{μ, _Real}, {NNN, _Integer}},
Module[{NN = NNN}, (* do you even need this placeholder for the input? *)
mm = Table[Boole[m == n] (MM - B2 k k - B1 (m Pi/L)^2), {m, 1, NN}, {n, 1, NN}];
kz = Table[Boole[m != n] (1 - Cos[(m + n) π])/((m + n) π) +
((Cos[(m - n) π] - 1)/((m - n) π)) (-I A2 m Pi/L),
{m, 1, NN}, {n, 1, NN}];
kxM = Table[Boole[m == n] A1 k, {m, 1, NN}, {n, 1, NN}];
μM = Table[Boole[m == n] μ, {m, 1, NN}, {n, 1, NN}];
HH = ArrayFlatten[{{μM + mm, 0 mm, kz, kxM},
{0 mm, μM + mm, kxM, -kz}, {kz, kxM, μM - mm,0 mm},
{kxM, -kz, 0 mm, μM - mm}}];
ees = Table[Eigenvalues[HH], {k, -.1, .1, .01}]]]
What is k
for in this last line? There is no way for the iterator to matter for HH
.
This doesn't give the error involving SetDelayed
anymore.
Comments
Post a Comment