Skip to main content
2 of 5
added 59 characters in body
user avatar
user avatar

How do `Compile` and `Module` work in a code?

I am trying to understand the following code in this answer regarding Möbius transformation:

(* Projection from the sphere to the plane *)
stereo = Compile[{{xyz, _Real, 1}, {XYZ, _Real, 1}}, Module[{
     r = Sqrt[(xyz[[1]] - XYZ[[1]])^2 + (xyz[[2]] - XYZ[[2]])^2],
     theta = ArcTan[(xyz[[1]] - XYZ[[1]]), (xyz[[2]] - XYZ[[2]])]},
    {(r (1 + xyz[[3]]))/(1 - XYZ[[3]] + xyz[[3]]) Cos[theta + Pi] + xyz[[1]],
     (r (1 + xyz[[3]]))/(1 - XYZ[[3]] + xyz[[3]]) Sin[theta + Pi] + xyz[[2]], 0}]];

(* Projection from the plane to the sphere *)
stereoInv = Compile[{{pq, _Real, 1}, {xyz, _Real, 1}},
   {2 pq[[1]], 2 pq[[2]], 
    pq[[1]]^2 + pq[[2]]^2 - 1}/(pq[[1]]^2 + pq[[2]]^2 + 1) + xyz];

I have read the documents for the functions Compile and Module, but still cannot figure out how the code works.

Could anyone elaborate the following?

  • What does {xyz,_Real,1} do and what does xyz[[1]] mean?
  • How do Compile and Module work together to give the definition of a function?
  • What is the formula for the function that this code is really defining?
user664