Skip to main content
replaced http://mathematica.stackexchange.com/ with https://mathematica.stackexchange.com/
Source Link

I am trying to understand the following code in this answeranswer 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 does xyz[[1]] mean?

    [Added:] In the document for Compile, it is said that

    Compile[{{x1,t1,n1},…},expr] assumes that xi is a rank ni array of objects, each of a type that matches ti.

But what is 1 in {xyz,_Real,1}? If it means rank 1, then why later it is written that xyz[[1]], xyz[[2]], xyz[[3]], which suggests that xyz is an array?

  • 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? What is the input and what it is the output?

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 does xyz[[1]] mean?

    [Added:] In the document for Compile, it is said that

    Compile[{{x1,t1,n1},…},expr] assumes that xi is a rank ni array of objects, each of a type that matches ti.

But what is 1 in {xyz,_Real,1}? If it means rank 1, then why later it is written that xyz[[1]], xyz[[2]], xyz[[3]], which suggests that xyz is an array?

  • 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? What is the input and what it is the output?

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 does xyz[[1]] mean?

    [Added:] In the document for Compile, it is said that

    Compile[{{x1,t1,n1},…},expr] assumes that xi is a rank ni array of objects, each of a type that matches ti.

But what is 1 in {xyz,_Real,1}? If it means rank 1, then why later it is written that xyz[[1]], xyz[[2]], xyz[[3]], which suggests that xyz is an array?

  • 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? What is the input and what it is the output?
edited title
Link
m_goldberg
  • 108.1k
  • 16
  • 104
  • 259

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

added 29 characters in body
Source Link
user664
user664

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?

    What does {xyz,_Real,1} do and what does xyz[[1]] mean?

    [Added:] In the document for Compile, it is said that

    Compile[{{x1,t1,n1},…},expr] assumes that xi is a rank ni array of objects, each of a type that matches ti.

But what is 1 in {xyz,_Real,1}? If it means rank 1, then why later it is written that xyz[[1]], xyz[[2]], xyz[[3]], which suggests that xyz is an array?

  • 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? What is the input and what it is the output?

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?

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 does xyz[[1]] mean?

    [Added:] In the document for Compile, it is said that

    Compile[{{x1,t1,n1},…},expr] assumes that xi is a rank ni array of objects, each of a type that matches ti.

But what is 1 in {xyz,_Real,1}? If it means rank 1, then why later it is written that xyz[[1]], xyz[[2]], xyz[[3]], which suggests that xyz is an array?

  • 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? What is the input and what it is the output?
Post Undeleted by user664
Post Deleted by user664
added 59 characters in body
Source Link
user664
user664
Loading
Source Link
user664
user664
Loading