
(* This file contains the code which computes the hessian
   of the energy function, as in Chapter 6. *)


(*The Cross Ratio*)
CR[a_,b_,c_,d_]:=(a-b) (c-d)/(a-c)/(b-d);

(*A better version of reduction mod n*)
MOD[k_,n_]:=If[Mod[k,n]==0,n,Mod[k,n]]


(*The functions for the energy*)

LogSingleCrossRatio[P_,j_]:=(
l=Length[P];
p1=P[[MOD[j+0,l]]];
p2=P[[MOD[j+1,l]]];
p3=P[[MOD[j+2,l]]];
p4=P[[MOD[j+3,l]]];
Log[CR[p1,p2,p3,p4]])

LogConformalEnergy[P_]:=Sum[LogSingleCrossRatio[P,j],{j,1,Length[P]}];

F[X_]:=(XX=Table[Exp[I X[[j]]],{j,1,Length[X]}];
        LogConformalEnergy[XX])


(*This function checks that the entries we put in the
  Hessian are correct, up to sign. The sign is switched
  because X13 is negative. This is how we computed these
  entries.  Note that X3, which is the derivative of
  the expression with respect to a3, only involves the variables
  a1,a2,a3,a4,a5. The other variables are extraneous,
  but we include them so that the user can see that
  firsthand that no other variables occur.*)

ComputeHessian[]:=(
Clear[a0,a1,a2,a3,a4,a5,a6,a7];
X=F[{a0,a1,a2,a3,a4,a5,a6,a7}];
X3=Simplify[D[X,a3]];
H13=Simplify[D[X3,a1]];
H23=Simplify[D[X3,a2]];
H33=Simplify[D[X3,a3]];
H43=Simplify[D[X3,a4]];
H53=Simplify[D[X3,a5]];
{a1,a2,a3,a4,a5}={0,s,2 s,3 s,4 s};
LIST=Simplify[{H13/H13,H23/H13,H33/H13,H43/H13,H53/H13}];
Expand[LIST])


(*Here is the Hessian*)

Hess[s_,n_]:=(
ROW1={{-1,2+2 Cos[s],-2 - 4 Cos[s],2+2 Cos[s],-1},Table[0,{n-5}]};
ROW1=Flatten[ROW1];
Table[RotateRight[ROW1,-3+j],{j,1,n}])

(*Here is the positive eigenvector.*)
Eig[s_,n_]:=Table[Cos[j s],{j,1,n}]

(*A sample test*)

test=Eig[2 Pi/7,7].Hess[2 Pi/7+.1,7].Eig[2 Pi/7,7]//N

