// This function computes a lower bound for the height difference
// h - hcan on genus 2 hyperelliptic curves
// Input, a degree 5 or degree 6 polynomial irreducible,
// defined over the rationals.
HeightDifferenceLowerBound:=function(f);
// The equations for the Kummer surface are obtained from a 
// degree 6 model, so in the degree 5 case we need to change coords.
if Degree(f) eq 5 then;
     a0,a1,a2,a3,a4,a5:=Explode(Coefficients(f));
     fsix:=x*(a0*x^5+a1*x^4+a2*x^3+a3*x^2+a4*x+a5);  // Polynomial obtained
                                      // from the change of vars X=1/x
     f0,f1,f2,f3,f4,f5,f6:=Explode(Coefficients(fsix));
else
    f0,f1,f2,f3,f4,f5,f6:=Explode(Coefficients(f));
end if;

Qk<k1,k2,k3,k4>:=PolynomialRing(Rationals(),4);

// The following are quartics definig multiplication by 2 on the
// Kummer surface. 
// They are adapted for MAGMA from the
// file available by anon. ftp from ftp.liv.ac.uk as
//     ~ftp/pub/genus2/kummer/duplication
// These are Flynn's equations.
delta1 :=
4*k1*k4^3
+
4*k4^2*(k1^2*f2-k2*k3*f5-3*k3^2*f6)
+
4*k4*(-4*k1^3*f0*f4+k1^3*f1*f3-8*k1^2*k2*f0*f5-2*k1^2*k3*f1*f5-12*k1*k2^2*
f0*f6-k1*k2^2*f1*f5-6*k1*k2*k3*f1*f6-2*k1*k2*k3*f2*f5-4*k1*k3^2*f2*f6-k1*k3^
2*f3*f5-2*k2^3*f1*f6-4*k2^2*k3*f2*f6-6*k2*k3^2*f3*f6-8*k3^3*f4*f6+2*k3^3*
f5^2)
+
4*k3^4*f4*f5^2+4*k1^4*f1^2*f4+4*k1^4*f0*f3^2-16*k3^4*f2*f6^2+4*k2^4*f0
*f5^2-16*k3^4*f4^2*f6-16*k2*k3^3*f3*f4*f6-16*k1^4*f0^2*f6-16*k1^4*f0*f2*
f4-4*k1^2*k2*k3*f1*f3*f5+4*k2^3*k3*f1*f5^2+4*k2*k3^3*f3*f5^2+4*k2^2*k3^2
*f2*f5^2-16*k1^3*k2*f0*f1*f6-32*k1^3*k2*f0*f2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1*f6^2-32*k1*k2*k3^
2*f0*f2*f5*f6+64*k1*k2*k3^2*f0*f3*f4*f6+16*k1*k2*k3^2*f1^2*f5*f6+8*k1*k2*k3
^2*f1*f3^2*f6-64*k1*k3^3*f0*f2*f6^2+16*k1*k3^3*f0*f3*f5*f6+32*k1*k3^3*f1
^2*f6^2+16*k1*k3^3*f1*f3*f4*f6-4*k1*k3^3*f1*f3*f5^2+16*k2^4*f0*f2*f4*f6-4
*k2^4*f0*f2*f5^2-4*k2^4*f1^2*f4*f6+32*k2^3*k3*f0*f1*f6^2+32*k2^3*k3*f0*
f3*f4*f6-8*k2^3*k3*f0*f3*f5^2+32*k2^2*k3^2*f0*f2*f6^2-24*k2^2*k3^2*f0*f3
*f5*f6+64*k2^2*k3^2*f0*f4^2*f6-16*k2^2*k3^2*f0*f4*f5^2+16*k2^2*k3^2*f1
^2*f6^2+8*k2^2*k3^2*f1*f3*f4*f6-2*k2^2*k3^2*f1*f3*f5^2-32*k2*k3^3*f0*f3
*f6^2+32*k2*k3^3*f0*f4*f5*f6-8*k2*k3^3*f0*f5^3+32*k2*k3^3*f1*f2*f6^2-16*
k2*k3^3*f1*f3*f5*f6+32*k2*k3^3*f1*f4^2*f6-8*k2*k3^3*f1*f4*f5^2-4*k3^4*f0*
f5^2*f6+8*k3^4*f1*f4*f5*f6-8*k3^4*f2*f3*f5*f6+16*k3^4*f2*f4^2*f6-4*k3^4*
  f2*f4*f5^2-4*k3^4*f3^2*f4*f6;

c2:=Log(Max( [Max([AbsoluteValue(i) : i in Coefficients(j)]) : j in [ delta1, delta2, delta3, delta4 ] ]));
// According to Flynn and Smart, a lower bound for the difference
// h - hcan is given by -c2/3.
return -c2/3;
end function;


