I have a function that behaves itself, but I want to make it much faster so I tried to put it into a compilable form After much fiddling I can't get it to work. Here's one of my failed attempts:
DiracPoint = Compile[{{angle, _Complex}},
Module[{c1, c2, c3, Phi, cond1, cond2, cond3, sols, sol, theta,
phi},
theta = Re[angle];
phi = Im[angle];
{c1, c2, c3} =
1 - 3 Sin[theta]^2 Cos[phi - 2 Pi (# - 1)/3]^2 & /@ {1, 2, 3};
If[0 <= ((c2 + c3)^2 - c1^2)/(4 c2 c3) <= 1,
Phi = Sqrt[((c2 + c3)^2 - c1^2)/(4 c2 c3)];
cond1 = (c1 Cos[3 y/2] + (c2 + c3) Cos[Sqrt[3] x/2] == 0);
cond2 = (-c1 Sin[3 y/2] + (-c2 + c3) Sin[Sqrt[3] x/2] == 0);
cond3 = (Sin[Sqrt[3] x/2] == Phi);
sols = Solve[cond1 && cond2 && cond3, {x, y}];
{x, y} /. Refine[sols, {C[1] == 1, C[2] == 1}][[1]]
, {$MaxMachineNumber, $MaxMachineNumber}
]
]
];
Why doesn't this work? I either get errors to do with incompatibility of rank sizes but I'm pretty confident the If statement is returning two objects of the same type and size. If I change {MaxM...,MaxM..} to just MaxM... I lose that error and get the error
"CompiledFunction::cfse: "Compiled expression y should be a !(\"machine-size real number\").""
If I then include x and y into my Module beginning declarations I get the error
Compile::initvar: The variable y has not been initialized or has been initialized to Null.
What is the proper way to initialise the variables x and y, or is my problem something else?
Here is how I apply it:
res = 4;
AngleArray =
Transpose[
Table[theta + I phi, {theta, 0, Pi, Pi/res}, {phi, 0, 2 Pi,
Pi/res}]];
start = AbsoluteTime[];
array = Map[DiracPoint, AngleArray, {2}];
end = AbsoluteTime[] - start
Compileisn't the catholicon for speed, the core part of your code is just not compilable. GenerallyCompileonly speed up low level arithmetic calculation, see the link @george2079 gave for more details. – xzczd Mar 05 '15 at 03:26Solve, but you might want to implement Newton's Method to solve your nonlinear system of equations. This iterative method can be compiled, no problem there. But then again it might be even faster or more accurate to useNSolve. – Wizard Mar 07 '15 at 17:57