I'm trying to compile what I think is a pretty simple function that does matrix multilplications in a loop (see below). But Mathematica issues the message:
Could not complete external evaluation at instruction 1; proceeding with uncompiled evaluation
Could you please help.
Refl[λ_, d1_, d2_, n1_, n2_, Np_] :=
{
M = {{0.5, 0.5}, {0.5, -0.5}};
Do[
{
M =
M.{{Cos[2 Pi*n1*d1/λ], - ((I Sin[2 Pi*n1*d1/λ])/n1)},
{-I n1*Sin[2 Pi*n1*d1/λ], Cos[2 Pi*n1*d1/λ]}};
M =
M.{{Cos[2 Pi*n2*d2/λ], -((I Sin[2 Pi*n2*d2/λ])/n2)},
{-I n2* Sin[2 Pi*n2*d2/λ], Cos[2 Pi*n2*d2/λ]}}
}, {i, 1, Np}];
M = M.{{1.0, 1.0}, {1.0, -1.0}};
Return[Abs[M[[2]][[1]]/M[[1]][[1]]]^2]
}
CompiledrRefl =
Compile[
{{λ, _Real}, {d1, _Real}, {d2, _Real}, {n1, _Complex},
{n2, _Complex}, {Np, _Integer}},
Refl[λ, d1, d2, n1, n2, Np]]
Compile
is an advanced topic. Are you sure you really need it or could this be a case of premature optimization $\endgroup${
,}
in Mathematica - they don't do the same thing as in C for example, so your definition ofRefl[]
should probably be wrapped in a module or block - see this documentation and this one: The Four Kinds of Bracketing in the Wolfram Language. $\endgroup$