

(*GENERAL DERIVATION FILE:
The main routines are baseMatrix and fiberMatrix.
Their arguments are the vectors RR0,RR1,RR2,RR3
describing the image of the standard square [-1,1]^2
under the subdivision rule.  These routines then
get the matrices describing the skew product/*)


(*This is the unit square*)
QQ0={-1,-1,1};
QQ1={+1,-1,1};
QQ2={+1,+1,1};
QQ3={-1,+1,1};


(*This is a projective transformation that takes
the quad with vectors V0,V1,V2,V3 to the quad with
vertices
W0={-1,-1,1},
W1={+1,-1,1},
W2={+1,+1,1},
W3={-1,+1,1}*)


squareMap[V0_,V1_,V2_,V3_]:=(
  aa=Transpose[{V0,V1,V2}];

  bb=Transpose[{
    {-1,-1,1},
    {1,-1,1},
    {1,1,1}
  }];
  cc=LinearSolve[aa,V3];
  M=bb.DiagonalMatrix[{1/cc[[1]],-1/cc[[2]],1/cc[[3]]}].Inverse[aa];
  Simplify[M])

squareMapInverse[V0_,V1_,V2_,V3_]:=Inverse[squareMap[V0,V1,V2,V3]];

(*Here is the normalized quad with the invariants
alpha=(a,b) and tau=(t1,t2)*)

normalizedQuad[a_,b_,t1_,t2_]:=(
P0={(a-1)/2,0};
P1=(b-1)/2 {t1,t2};
P2={(a+1)/2,0};
P3=(b+1)/2 {t1,t2};
PP0=Append[P0,1];
PP1=Append[P1,1];
PP2=Append[P2,1];
PP3=Append[P3,1];
{PP0,PP1,PP2,PP3})


rawFiberMap[RR0_,RR1_,RR2_,RR3_]:=(
{PP0,PP1,PP2,PP3}=normalizedQuad[a,b,t1,t2];
mm=squareMap[PP0,PP1,PP2,PP3];
QQ0={-1,-1,1};
QQ1={+1,-1,1};
QQ2={+1,+1,1};
QQ3={-1,+1,1};
nn=Inverse[mm];

SS0=nn.RR0;
SS1=nn.RR1;
SS2=nn.RR2;
SS3=nn.RR3;

S0=Take[SS0,2]/SS0[[3]];
S1=Take[SS1,2]/SS1[[3]];
S2=Take[SS2,2]/SS2[[3]];
S3=Take[SS3,2]/SS3[[3]];

Z20=S2-S0;
Z31=S3-S1;

z20=Z20[[1]]+I Z20[[2]];
z31=Z31[[1]]+I Z31[[2]];
tauPrime=Simplify[z31/z20])



rawBaseMap[RR0_,RR1_,RR2_,RR3_]:=(
{PP0,PP1,PP2,PP3}=normalizedQuad[a,b,t1,t2];
mm=squareMap[PP0,PP1,PP2,PP3];
QQ0={-1,-1,1};
QQ1={+1,-1,1};
QQ2={+1,+1,1};
QQ3={-1,+1,1};
nn=Inverse[mm];

SS0=nn.RR0;
SS1=nn.RR1;
SS2=nn.RR2;
SS3=nn.RR3;

S0=Take[SS0,2]/SS0[[3]];
S1=Take[SS1,2]/SS1[[3]];
S2=Take[SS2,2]/SS2[[3]];
S3=Take[SS3,2]/SS3[[3]];

A0=Det[{S3-S0,S1-S0}];
A1=Det[{S0-S1,S2-S1}];
A2=Det[{S1-S2,S3-S2}];
A3=Det[{S2-S3,S0-S3}];

AA=A0+A1+A2+A3;
B0=2 A0/AA;
B1=2 A1/AA;
B2=2 A2/AA;
B3=2 A3/AA;

alphaPrime=Factor[{B2-B0,B3-B1}])



baseMatrix[RR0_,RR1_,RR2_,RR3_]:=(
alphaPrime=rawBaseMap[RR0,RR1,RR2,RR3];
den=Denominator[alphaPrime[[1]]];
aa1=Expand[den alphaPrime];
aa2=Append[aa1,den];

mat=Table[
 {Coefficient[aa2[[i]],a],
  Coefficient[aa2[[i]],b],
  aa2[[i]]/.{a->0,b->0}},
 {i,1,3}];

Factor[mat])


fiberMatrix[RR0_,RR1_,RR2_,RR3_]:=(

tauPrime=rawFiberMap[RR0,RR1,RR2,RR3];
tt=Together[tauPrime/.{t1->z,t2->0}];
num=Expand[Numerator[tt]];
den=Expand[Denominator[tt]];
hmat={
 {Coefficient[num,z],num/.z->0},
 {Coefficient[den,z],den/.z->0}
};
Factor[hmat])