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}]];
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 doesxyz[[1]]
mean?[Added:] In the document for
Compile
, it is said thatCompile[{{x1,t1,n1},…},expr]
assumes thatxi
is a rankni
array of objects, each of a type that matchesti
.But what is
1
in{xyz,_Real,1}
? If it means rank 1, then why later it is written thatxyz[[1]]
,xyz[[2]]
,xyz[[3]]
, which suggests thatxyz
is an array?How do
Compile
andModule
work together to give the definition of a function?- What is the formula for the function that this code is really defining? What is the input and what it is the output?
[[
and press F1 (or Command-Shift-F) to see what it is (it's array indexing). The formula is exactly as written there, just substitute inr
andtheta
from the first part of theModule
.xyz
in this case is a three-element real array representing a 3D vector with x, y, z components. $\endgroup$:-)
). $\endgroup$Module
does the same thing insideCompile
as it does outsideCompile
. $\endgroup$