(* Compute the polar coordinates of a point given in *)
(* Cartesian coordinates.                            *)
fromCartesian2Polar[ q_ ] := 
Module [
{ x , y , r , t , res },
(
x = q[[ 1 ]] ;
y = q[[ 2 ]] ;

r = Sqrt[ x x + y y ] ;

If [ r > 0 ,
If [ x < 0  ,
If [ y ≥ 0 ,
t =   Pi - ArcCos[ Abs[ x ] / r ] ,
t = -Pi + ArcCos[ Abs[ x ] / r ]
],
If [ y ≥ 0 ,
t =   ArcSin[              y / r ] ,
t =-ArcSin[ Abs[ y ] / r ] 
]
] ,
(
r = 0 ;
t = 0 
)
] ;

res = { r , t } ;
res
) 
] ;

(* Compute the Cartesian coordinates of a point given *)
(* in Polar coordinates.                              *)
fromPolar2Cartesian[ q_ ] := 
Module [
{ x , y , r , t , res },
(
r = q[[ 1 ]] ;
t = q[[ 2 ]] ;

x = r Cos[ t ] ;
y = r Sin[ t ] ;

res = { x , y } ;
res
) 
] ;

(* Map a point from a p-domain to the canonical domain. *)
fromPDomain2Canonical[ q_ , nu_ ] := 
Module [
{ r , t , s ,b , res },
(
r = q[[ 1 ]] ;
t = q[[ 2 ]] ;

s = r^{nu / 6} ;
b =( nu / 6 ) t ;

res = Append[ s , b ] ;
res
) 
] ;

(* Map a point from the canonical domain to a p-domain. *)
fromCanonical2PDomain[ q_ , nu_  ] := 
Module [
{ r , t , s , b , res },
(
r = q[[ 1 ]] ;
t = q[[ 2 ]] ;

s = r^{6 / nu} ;
b = ( 6 / nu ) t ;

res = Append[  s , b ] ;
res
) 
] ;

(* The h map - the double reflection *)
hFunc[ x_ , y_ ] :=
Module [
{ res  } ,
(
res = { 1 - x , -y } ;
res
)
] ;

(* Map a point from a p-domain to another p-domain. *)
fromPDomain2PDomain[ p_ , nu_ , i_ , nv_ , j_ ] := 
Module [
{ r , t , s , b , x , y , res },
(
(* Compute the polar coordinates of the given point. *)
{ r , t } = fromCartesian2Polar[ p ] ;

(* Apply a rotation of (2Pi / nu) degrees to the point. *) 
 t =  t - ( ( 2Pi i )/ nu ) ;

(* Map the point to the canonical domain *)
{ r , t } = fromPDomain2Canonical[ { r , t } , nu ] ;

(* Compute the Cartesian coordinates of the point. *)
{ x , y } = fromPolar2Cartesian[ { r , t } ] ;

(* Apply a rotation of 180 degrees to ( x , y ). *)
{ x , y } = hFunc[ x , y ] ;

(* Compute the polar coordinates of the given point. *)
{ s , b } = fromCartesian2Polar[ { x , y } ] ;

(* Map the point to the target p-domain. *)
{ s , b } = fromCanonical2PDomain[ { s , b } , nv ] ;

(* Apply a rotation of (2Pi / nv) degrees to the point. *)
 b = b + ( ( 2 Pi j ) / nv ) ;

(* Compute the Cartesian coordinates of the point. *)
{ x , y } = fromPolar2Cartesian[ { s , b } ] ;

res = { x , y } ;
res
) 
] ;

(* Testing the cocycle condition - Assume that w precedes v in *)
(* a counterclockwise traversal of lk( u , K ).                *)

(* Transition function from Omega_{uv} to Omega_{vu} *)
phiVU[ x_ , y_ ] :=
Module[
{ u , v , res } ,
(
{ u , v } = fromPDomain2PDomain[ { x , y } , 5 , 1 , 6 , 0 ] ;

res = { u , v } ;
res
)
] ;

(* Transition function from Omega_{uw} to Omega_{wu} *)
phiWU[ x_ , y_ ] :=
Module[
{ u , v , res } ,
(
{ u , v } = fromPDomain2PDomain[ { x , y } , 5 , 0 , 7 , 1 ] ;

res = { u , v } ;
res
)
] ;

(* Transition function from Omega_{vu} to Omega_{uv} *)
phiUV[ x_ , y_ ] :=
Module[
{ u , v , res } ,
(
{ u , v } = fromPDomain2PDomain[ { x , y } , 6 , 0 , 5 , 1 ] ;

res = { u  , v } ;
res
)
] ;

(* Transition function from Omega_{vw} to Omega_{wv} *)
phiWV[ x_ , y_ ] :=
Module[
{ u , v , res } ,
(
{ u , v } = fromPDomain2PDomain[ { x , y } , 6 , 1 , 7 , 0 ] ;

res = { u  , v } ;
res
)
] ;

(* Transition function from Omega_{wu} to Omega_{uw} *)
phiUW[ x_ , y_ ] :=
Module[
{ u , v , res } ,
(
{ u , v } = fromPDomain2PDomain[ { x , y } , 7 , 1 , 5 , 0 ] ;

res = { u  , v } ;
res
)
] ;

(* Transition function from Omega_{wv} to Omega_{vw} *)
phiVW[ x_ , y_ ] :=
Module[
{ u , v , res } ,
(
{ u , v } = fromPDomain2PDomain[ { x , y } , 7 , 0 , 6 , 1 ] ;

res = { u  , v } ;
res
)
] ;

(* Testing the cocycle condition. *)

{ x1 , y1 } = phiVU[ 0.5 , 0.5 ]

{ x2 , y2 } = phiWU[ 0.5 , 0.5 ]

{ x3 , y3 } = phiVW[ x2 , y2 ]

