(* ========================================================================== *)
(* FLYSPECK - BOOK FORMALIZATION                                              *)
(*                                                                            *)
(* Definitions: (General definitions file)         *)
(* Chapter: General                                                     *)
(* Author: Thomas C. Hales                                                    *)
(* Date: 2010-02-09                                                           *)
(* ========================================================================== *)


(* needs "Multivariate/flyspeck.ml";; *)


module Sphere = struct

prioritize_real();;

let ineq = define
 `(!c. ineq [] c <=> c)
    /\ (!a x b xs c. ineq (CONS (a,x,b) xs) c <=> a <= x /\ x <= b ==> ineq xs c)`;;
let all_forall bod = let mk_forall = mk_binder "!" in itlist (curry mk_forall) (sort (<) (frees bod)) bod;;
let periodic = new_definition `periodic (f:num->A) n = (!i. (f (i+n) = f (i:num)))`;;
(* symmetric difference *)
let SDIFF = new_definition `SDIFF X Y = (X DIFF Y) UNION (Y DIFF X)`;;
(* With the following definition, we should have atn2(x,y) = pi - Arg(complex(-- x, y)), including in the degenerate cases. In this sense, it is the "right" definition. *)
let atn2 = new_definition(`atn2(x,y) =
    if ( abs y < x ) then atn(y / x) else
    (if (&0 < y) then ((pi / &2) - atn(x / y)) else
    (if (y < &0) then (-- (pi/ &2) - atn (x / y)) else (  pi )))`);;
(* ------------------------------------------------------------------ *) (* f = \x. a x^2 + b x + c, extract a b c *)
let abc_of_quadratic = 
new_definition `abc_of_quadratic f = 
  let c = f (&0) in
  let  p = f (&1) in
  let n = f (-- &1) in
  ((p + n)/(&2) - c, (p -n)/(&2), c)` ;;
let quadratic_root_plus = 
   new_definition `quadratic_root_plus (a, b, c) =
      ( -- b + sqrt(b pow 2 - &4 * a * c))/ (&2 * a)`;;
(* ------------------------------------------------------------------ *)
let sqrt8 = new_definition (`sqrt8 = sqrt (&8) `);;
let sqrt2 = new_definition (`sqrt2 = sqrt (&2) `);;
let sqrt3 = new_definition `sqrt3 = sqrt(&3)`;;
let pi_rt18 = new_definition(`pi_rt18= pi/(sqrt (&18))`);;
(* ------------------------------------------------------------------ *) (* This polynomial is essentially the Cayley-Menger determinant. *) (* ------------------------------------------------------------------ *)
let delta_x = new_definition (`delta_x x1 x2 x3 x4 x5 x6 =
        x1*x4*(--x1 + x2 + x3 -x4 + x5 + x6) +
        x2*x5*(x1 - x2 + x3 + x4 -x5 + x6) +
        x3*x6*(x1 + x2 - x3 + x4 + x5 - x6)
        -x2*x3*x4 - x1*x3*x5 - x1*x2*x6 -x4*x5*x6`);;
let delta_y = new_definition `delta_y y1 y2 y3 y4 y5 y6 =
    delta_x (y1*y1) (y2*y2) (y3*y3) (y4*y4) (y5*y5) (y6*y6)`;;
let edge_flat =  new_definition`edge_flat y1 y2 y3 y5 y6 = 
 sqrt(quadratic_root_plus (abc_of_quadratic (
   \x4.  -- delta_x (y1*y1) (y2*y2)  (y3*y3)  x4 (y5*y5)  (y6*y6))))`;;
let edge_flat2_x = new_definition `edge_flat2_x x1 x2 x3 x4 x5 x6 =
  (edge_flat (sqrt x1) (sqrt x2) (sqrt x3) (sqrt x5) (sqrt x6)) pow 2`;;
(* x4 dummy *)
let edge_flat_x =  new_definition`edge_flat_x x1 x2 x3 (x4:real) x5 x6 = 
 edge_flat (sqrt x1) (sqrt x2) (sqrt x3) (sqrt x5) (sqrt x6)`;;
(* ------------------------------------------------------------------ *) (* The partial derivative of delta_x with respect to x4. *) (* ------------------------------------------------------------------ *)
let delta_x4= new_definition(`delta_x4 x1 x2 x3 x4 x5 x6
        =  -- x2* x3 -  x1* x4 + x2* x5
        + x3* x6 -  x5* x6 + x1* (-- x1 +  x2 +  x3 -  x4 +  x5 +  x6)`);;
let delta_x6 = new_definition(`delta_x6 x1 x2 x3 x4 x5 x6
        = -- x1 * x2 - x3*x6 + x1 * x4
        + x2* x5 - x4* x5 + x3*(-- x3 + x1 + x2 - x6 + x4 + x5)`);;
(* ------------------------------------------------------------------ *) (* Circumradius . *) (* ------------------------------------------------------------------ *)
let ups_x = new_definition(`ups_x x1 x2 x6 =
    --x1*x1 - x2*x2 - x6*x6
    + &2 *x1*x6 + &2 *x1*x2 + &2 *x2*x6`);;
let eta_x = new_definition(`eta_x x1 x2 x3 =
        (sqrt ((x1*x2*x3)/(ups_x x1 x2 x3)))
        `);;
let eta_y = new_definition(`eta_y y1 y2 y3 =
                let x1 = y1*y1 in
                let x2 = y2*y2 in
                let x3 = y3*y3 in
                eta_x x1 x2 x3`);;
let rho_x = new_definition(`rho_x x1 x2 x3 x4 x5 x6 =
        --x1*x1*x4*x4 - x2*x2*x5*x5 - x3*x3*x6*x6 +
        (&2)*x1*x2*x4*x5 + (&2)*x1*x3*x4*x6 + (&2)*x2*x3*x5*x6`);;
let chi_x = new_definition(`chi_x x1 x2 x3 x4 x5 x6
        = -- (x1*x4*x4) + x1*x4*x5 + x2*x4*x5 -  x2*x5*x5
        + x1*x4*x6 + x3*x4*x6 +
   x2*x5*x6 + x3*x5*x6 -  (&2) * x4*x5*x6 -  x3*x6*x6`);;
(* ------------------------------------------------------------------ *) (* The formula for the dihedral angle of a simplex. *) (* The variables xi are the squares of the lengths of the edges. *) (* The angle is computed along the first edge (x1). *) (* ------------------------------------------------------------------ *)
let dih_x = new_definition(`dih_x x1 x2 x3 x4 x5 x6 =
       let d_x4 = delta_x4 x1 x2 x3 x4 x5 x6 in
       let d = delta_x x1 x2 x3 x4 x5 x6 in
       pi/ (&2) +  atn2( (sqrt ((&4) * x1 * d)),--  d_x4)`);;
let dih_y = new_definition(`dih_y y1 y2 y3 y4 y5 y6 =
       let (x1,x2,x3,x4,x5,x6)= (y1*y1,y2*y2,y3*y3,y4*y4,y5*y5,y6*y6) in
       dih_x x1 x2 x3 x4 x5 x6`);;
let dih2_y = new_definition(`dih2_y y1 y2 y3 y4 y5 y6 =
        dih_y y2 y1 y3 y5 y4 y6`);;
let dih3_y = new_definition(`dih3_y y1 y2 y3 y4 y5 y6 =
        dih_y y3 y1 y2 y6 y4 y5`);;
let dih2_x = new_definition(`dih2_x x1 x2 x3 x4 x5 x6 =
        dih_x x2 x1 x3 x5 x4 x6`);;
let dih3_x = new_definition(`dih3_x x1 x2 x3 x4 x5 x6 =
        dih_x x3 x1 x2 x6 x4 x5`);;
(* ------------------------------------------------------------------ *) (* Harriot-Euler formula for the area of a spherical triangle *) (* in terms of the angles: area = alpha+beta+gamma - pi *) (* ------------------------------------------------------------------ *)
let sol_x = new_definition(`sol_x x1 x2 x3 x4 x5 x6 =
        (dih_x x1 x2 x3 x4 x5 x6) +
        (dih_x x2 x3 x1 x5 x6 x4) +  (dih_x x3 x1 x2 x6 x4 x5) -  pi`);;
let sol_y = new_definition(`sol_y y1 y2 y3 y4 y5 y6 =
        (dih_y y1 y2 y3 y4 y5 y6) +
        (dih_y y2 y3 y1 y5 y6 y4) +  (dih_y y3 y1 y2 y6 y4 y5) -  pi`);;
(* ------------------------------------------------------------------ *) (* squander functions -2009 version *) (* ------------------------------------------------------------------ *)
let interp = new_definition `interp x1 y1 x2 y2 x = y1 + (x - x1) * (y2- y1)/(x2-x1)`;;
let const1 = new_definition `const1 = sol_y (&2) (&2) (&2) (&2) (&2) (&2) / pi`;;
let ly = new_definition `ly y = interp (&2) (&1) (#2.52) (&0) y`;;
let rho = new_definition `rho y = &1 + const1 - const1* ly y`;;
let h0 = new_definition `h0 = #1.26`;;
let sol0 = new_definition `sol0 = &3 * acs (&1 / &3)  - pi`;;
let rho_fun = new_definition `rho_fun y =  &1 + (inv (&2 * h0 - &2)) * (inv pi) * sol0 * (y - &2)`;;
let rhazim = new_definition `rhazim y1 y2 y3 y4 y5 y6 = rho y1 * dih_y y1 y2 y3 y4 y5 y6`;;
let lnazim = new_definition `lnazim y1 y2 y3 y4 y5 y6 = ly y1 * dih_y y1 y2 y3 y4 y5 y6`;;
let taum = new_definition 
  `taum y1 y2 y3 y4 y5 y6 = sol_y y1 y2 y3 y4 y5 y6 * (&1 + const1) - 
  const1 * (lnazim y1 y2 y3 y4 y5 y6 + lnazim y2 y3 y1 y5 y6 y4 + lnazim y3 y1 y2 y6 y4 y5)`;;
let tauV = new_definition 
  `tauV (v1:real^3) v2 v3 = taum (norm v1) (norm v2) (norm v3) (dist(v2,v3)) (dist(v1,v3)) (dist(v1,v2))`;;
let node2_y = new_definition `node2_y f y1 y2 y3 y4 y5 y6 = f y2 y3 y1 y5 y6 y4`;;
let node3_y = new_definition `node3_y f y1 y2 y3 y4 y5 y6 = f y3 y1 y2 y6 y4 y5`;;
let rhazim2 = new_definition `rhazim2 = node2_y rhazim`;;
let rhazim3 = new_definition `rhazim3 = node3_y rhazim`;;
let dih4_y = define `dih4_y y1 y2 y3 y4 y5 y6 = dih_y y4 y2 y6 y1 y5 y3`;;
let dih5_y = define `dih5_y y1 y2 y3 y4 y5 y6 = dih_y y5 y1 y6 y2 y4 y3`;;
let dih6_y = define `dih6_y y1 y2 y3 y4 y5 y6 = dih_y y6 y1 y5 y3 y4 y2`;;
let rhazim4 = define `rhazim4 y1 y2 y3 y4 y5 y6 = rho y4 * dih4_y y1 y2 y3 y4 y5 y6`;;
let rhazim5 = define `rhazim5 y1 y2 y3 y4 y5 y6 = rho y5 * dih5_y y1 y2 y3 y4 y5 y6 `;;
let rhazim6 = define `rhazim6 y1 y2 y3 y4 y5 y6 = rho y6 * dih6_y y1 y2 y3 y4 y5 y6`;;
(* two tetrahedra, shared edges y2 y3 y4 *)
let tauq = new_definition `tauq y1 y2 y3 y4 y5 y6 y7 y8 y9 = taum y1 y2 y3 y4 y5 y6 + taum y7 y2 y3 y4 y8 y9`;;
(* ------------------------------------------------------------------ *) (* The Cayley-Menger formula for the volume of a simplex *) (* The variables xi are the squares of the lengths of the edges. *) (* ------------------------------------------------------------------ *)
let vol_x = new_definition(`vol_x x1 x2 x3 x4 x5 x6 =
        (sqrt (delta_x x1 x2 x3 x4 x5 x6))/ (&12)`);;
(* ------------------------------------------------------------------ *) (* Some lower dimensional funcions and Rogers simplices. *) (* ------------------------------------------------------------------ *)
let arclength = new_definition(`arclength a b c =
        pi/(&2) + (atn2( (sqrt (ups_x (a*a) (b*b) (c*c))),(c*c - a*a  -b*b)))`);;
let volR = new_definition(`volR a b c =
        (sqrt (a*a*(b*b-a*a)*(c*c-b*b)))/(&6)`);;
let solR = new_definition(`solR a b c =
        (&2)*atn2( sqrt(((c+b)*(b+a))), sqrt ((c-b)*(b-a)))`);;
let dihR = new_definition(`dihR a b c =
        atn2( sqrt(b*b-a*a),sqrt (c*c-b*b))`);;
let rad2_x = new_definition(`rad2_x x1 x2 x3 x4 x5 x6 =
        (rho_x x1 x2 x3 x4 x5 x6)/((delta_x x1 x2 x3 x4 x5 x6)*(&4))`);;
(* aff is deprecated *)
let aff = new_definition `aff = ( hull ) affine`;;
let lin_combo = new_definition `lin_combo V f = vsum V (\v. f v % (v:real^N))`;;
let affsign = new_definition `affsign sgn s t (v:real^A) = (?f.
  (v = lin_combo (s UNION t) f) /\ (!w. t w ==> sgn (f w)) /\ (sum (s UNION t) f = &1))`;;
let sgn_gt = new_definition `sgn_gt = (\t. (&0 < t))`;;
let sgn_ge = new_definition `sgn_ge = (\t. (&0 <= t))`;;
let sgn_lt = new_definition `sgn_lt = (\t. (t < &0))`;;
let sgn_le = new_definition `sgn_le = (\t. (t <= &0))`;;
let cone = new_definition `cone v S:real^A->bool = affsign sgn_ge {v} S`;;
let cone0 = new_definition `cone0 v S:real^A->bool = affsign sgn_gt {v} S`;;
let conv0 = new_definition `conv0 S:real^A->bool = affsign sgn_gt {} S`;;
let aff_gt_def = new_definition `aff_gt = affsign sgn_gt`;;
let aff_ge_def = new_definition `aff_ge = affsign sgn_ge`;;
let aff_lt_def = new_definition `aff_lt = affsign sgn_lt`;;
let aff_le_def = new_definition `aff_le = affsign sgn_le`;;
let voronoi_open = new_definition 
  `voronoi_open S (v:real^N) = { x | !w. ((S w) /\ ~(w=v)) ==> (dist( x, v) < dist( x, w)) }`;;
let voronoi_closed = new_definition 
  `voronoi_closed S (v:real^N) = { x | !w. S w ==> (dist( x, v) <= dist( x, w)) }`;;
let VORONOI_SET = new_definition 
  `voronoi_set V W = INTERS { voronoi_closed V (v:real^3) |  v IN W}`;;
let VORONOI_LIST  =
    new_definition `voronoi_list V wl = voronoi_set (V:real^3 -> bool) (set_of_list  wl)`;;
let VORONOI_NONDG =
  new_definition `voronoi_nondg V ul =
  ((LENGTH ul < 5) /\ (set_of_list ul SUBSET V) /\
     (aff_dim (voronoi_list V ul) + &(LENGTH ul) = &4))`;;
let INITIAL_SUBLIST = new_definition 
  `initial_sublist (xl:(A)list) zl = (?yl. zl = APPEND xl yl)`;;
let BARV = new_definition `barV V k ul = ((LENGTH ul = k+1) /\
                        (!vl.  (initial_sublist vl ul /\  0 < LENGTH vl) ==> voronoi_nondg V vl))`;;
let TRUNCATE_SIMPLEX = new_definition
    `truncate_simplex j (ul:(A) list) =
      @vl. ( LENGTH vl = j+1 /\ initial_sublist vl ul)`;;
let OMEGA_LIST_N = define `(omega_list_n V ul 0 = HD ul) /\
  (omega_list_n V ul (SUC i) =
   closest_point (voronoi_list V (truncate_simplex (SUC i) ul)) (omega_list_n V ul i))`;;
let OMEGA_LIST = new_definition
  `!V ul. omega_list V ul = omega_list_n V ul ((LENGTH ul) - 1)`;;
let ROGERS = new_definition `!V ul.  rogers V ul =
  convex hull (IMAGE (omega_list_n V ul) {  j |    j  < LENGTH ul })`;;
(* AFFINE GEOMETRY *)
let line = new_definition `line x = (?(v:real^N) w. ~(v  =w) /\ (x = affine hull {v,w}))`;;
let plane = new_definition 
  `plane x = (?(u:real^N) v w. ~(collinear {u,v,w}) /\ (x = affine hull {u,v,w}))`;;
let closed_half_plane = new_definition 
  `closed_half_plane x = (?(u:real^N) v w. ~(collinear {u,v,w}) /\ (x = aff_ge {u,v} {w}))`;;
let open_half_plane = new_definition 
  `open_half_plane x = (?(u:real^N) v w. ~(collinear {u,v,w}) /\ (x = aff_gt {u,v} {w}))`;;
let closed_half_space = new_definition 
  `closed_half_space x = (?u v w w'. ~(coplanar {u,v,w,w'}) /\ (x = aff_ge {u,v,w} {w'}))`;;
let open_half_space = new_definition 
  `open_half_space x = (?u v w w'. ~(coplanar {u,v,w,w'}) /\ (x = aff_gt {u,v,w} {w'}))`;;
let bis = new_definition `bis (u:real^N) v = {x | dist(x,u) = dist(x,v)}`;;
let bis_le = new_definition `bis_le (u:real^N) v = {x | dist(x,u) <= dist(x,v) }`;;
let bis_lt = new_definition `bis_lt (u:real^N) v = {x | dist(x,u) < dist(x,v) }`;;
let BIS_SYM = 
prove(`!p (q:real^N). bis p q = bis q p`,
REWRITE_TAC[bis] THEN SET_TAC[]);;
let circumcenter = new_definition `circumcenter S = @v. ( (affine hull S) v /\ (?c. !w. (S w) ==> (c = dist(v,w))))`;;
let radV = new_definition `radV S = @c. !w. (S w) ==> (c = dist(circumcenter S,w))`;;
let orientation = new_definition `orientation S v sgn = affsign sgn (S DIFF {v}) {v} (circumcenter S)`;;
let arcV = new_definition `arcV u v w = acs (( (v - u) dot (w - u))/((norm (v-u)) * (norm (w-u))))`;;
let dihV = new_definition  `dihV w0 w1 w2 w3 =
     let va = w2 - w0 in
     let vb = w3 - w0 in
     let vc = w1 - w0 in
     let vap = ( vc dot vc) % va - ( va dot vc) % vc in
     let vbp = ( vc dot vc) % vb - ( vb dot vc) % vc in
       arcV (vec 0) vap vbp`;;
(* conventional ordering on variables *)
let ylist = new_definition `ylist w0 w1 w2 w3 =
      ((dist (w0, w1)),(dist( w0, w2)),(dist( w0, w3)),(dist( w2, w3)),(dist( w1, w3)),(dist( w1, w2)))`;;
let xlist = new_definition `xlist w0 w1 w2 w3 =
    let (y1,y2,y3,y4,y5,y6) = ylist w0 w1 w2 w3 in
    (y1 pow 2, y2 pow 2, y3 pow 2, y4 pow 2, y5 pow 2, y6 pow 2)`;;
let euler_p = new_definition `euler_p v0 v1 v2 v3 =
    (let (y1,y2,y3,y4,y5,y6) = ylist v0 v1 v2 v3 in
     let w1 = v1 - v0 in
     let w2 = v2 - v0 in
     let w3 = v3 - v0 in
    y1*y2*y3 + y1*( w2 dot w3) + y2*( w3 dot w1) + y3*( w1 dot w2))`;;
let orthonormal = new_definition `orthonormal e1 e2 e3 =
     (( e1 dot e1 = &1) /\ (e2 dot e2 = &1) /\ ( e3 dot e3 = &1) /\
     ( e1 dot e2 = &0) /\ ( e1 dot e3 = &0) /\ ( e2 dot e3 = &0) /\
     (&0 <  (e1 cross e2) dot e3))`;;
let cyclic_set = new_definition `cyclic_set W v w =
     (~(v=w) /\ (FINITE W) /\ (!p q h. W p /\ W q /\ (p = q + h % (v - w)) ==> (p=q)) /\
        (W INTER (affine hull {v,w}) = EMPTY))`;;
(* projection to plane orth to e *)
let projection = new_definition `projection e x =
   x - ((x dot e)/(e dot e)) % e`;;
let azim_cycle = new_definition `azim_cycle W v w p =
   if (W SUBSET {p}) then p else
    (@u. ~(u=p) /\ W u /\
      (!q.  ~(q=p) /\ W q ==>
          (azim v w p u < azim v w p q) \/
          ((azim v w p u = azim v w p q) /\
             (norm (projection (w-v) (u-v)) <= norm (projection (w-v) (q-v))))))`;;
let packing = new_definition `packing (S:real^3 -> bool) = (!u v. S u /\ S v /\ ~(u = v) ==> (&2 <= dist( u, v)))`;;
let packing_lt = 
prove(`packing (V:real^3 -> bool) = (!u:real^3 v:real^3. (u IN V) /\ (v IN V) /\ (dist( u, v) < &2) ==> (u = v))`,
REWRITE_TAC[packing;IN;REAL_ARITH `x<y <=> ~(y<= x)`] THEN MESON_TAC[]);;
let saturated =new_definition `saturated S= (!x. ?(y:real^N). y IN S /\ dist (x,y)< &2)`;;
let c_cone = new_definition `c_cone (v,w:real^3, r:real)={x:real^3 | ((x-v) dot w = norm (x-v)* norm w* r)}`;;
let circular_cone =new_definition
  `circular_cone (V:real^3-> bool)=
   (? (v,w:real^3)(r:real). ~(w = vec 0) /\ V = c_cone (v,w,r))`;;
let null_equiv = new_definition `null_equiv (s,t :real^3->bool)=(? (B:real^3-> bool). NULLSET B /\
((s DIFF t) UNION (t DIFF s)) SUBSET B)`;;
let radial = new_definition `radial r (x:real^A) C <=> (C SUBSET ball (x,r)) /\ (!u. (x+u) IN C ==> (!t.(t> &0) /\ (t* norm u < r)==>(x+ t % u) IN C))`;;
let eventually_radial = new_definition `eventually_radial x C <=> (?r. (r> &0) /\ radial r x (C INTER ball (x, r)))`;;
let rconesgn = new_definition `rconesgn sgn v w h = {x:real^A | sgn ((x-v) dot (w-v)) (dist(x,v)*dist(w,v)*h)}`;;
let rcone_ge = new_definition `rcone_ge = rconesgn ( >= )`;;
let rcone_gt = new_definition `rcone_gt = rconesgn ( > )`;;
let rcone_lt = new_definition `rcone_lt = rconesgn ( < )`;;
let rcone_eq = new_definition `rcone_eq = rconesgn ( = )`;;
let scale = new_definition `scale (t:real^3) (u:real^3) = vector[t$1 * u$1; t$2 * u$2; t$3 * u$3]`;;
let vol_solid_triangle = new_definition `vol_solid_triangle v0 v1 v2 v3 r =
   let a123 = dihV v0 v1 v2 v3 in
   let a231 = dihV v0 v2 v3 v1 in
   let a312 = dihV v0 v3 v1 v2 in
     (a123 + a231 + a312 - pi)*(r pow 3)/(&3)`;;
let vol_frustt_wedge = new_definition `vol_frustt_wedge v0 v1 v2 v3 h a =
       (azim v0 v1 v2 v3)*(h pow 3)*(&1/(a*a) - &1)/(&6)`;;
(* volume of intersection of conic cap and wedge *)
let vol_conic_cap_wedge = new_definition `vol_conic_cap_wedge v0 v1 v2 v3 r c =
       (azim v0 v1 v2 v3)*(&1 - c)*(r pow 3)/(&3)`;;
let vol_conv = new_definition `vol_conv v1 v2 v3 v4 =
   let x12 = dist(v1,v2) pow 2 in
   let x13 = dist(v1,v3) pow 2 in
   let x14 = dist(v1,v4) pow 2 in
   let x23 = dist(v2,v3) pow 2 in
   let x24 = dist(v2,v4) pow 2 in
   let x34 = dist(v3,v4) pow 2 in
   sqrt(delta_x x12 x13 x14 x34 x24 x23)/(&12)`;;
let vol_rect = new_definition `vol_rect a b =
   if (a$1 < b$1) /\ (a$2 < b$2) /\ (a$3 < b$3) then (b$3-a$3)*(b$2-a$2)*(b$1-a$1) else &0`;;
let vol_ball_wedge = new_definition `vol_ball_wedge v0 v1 v2 v3 r =
   (azim v0 v1 v2 v3)*(&2)*(r pow 3)/(&3)`;;
let ortho0 = new_definition `ortho0 x v1 v2 v3 = conv0 {x,x+v1,x+v1+v2,x+v1+v2+v3}`;;
let make_point = new_definition `make_point v1 v2 v3 w r1 r2 r3 = @v. (aff_ge {v1,v2,v3} {w} (v:real^3)) /\ (r1 = dist(v1,v)) /\ (r2 = dist(v2,v)) /\ (r3 = dist(v3,v))`;;
let abc_param = new_definition `abc_param v0 v1 v2 c =
    let a = (&1/(&2)) * dist(v0,v1) in
    let b = radV {v0,v1,v2} in
     (a,b,c)`;;
let res = new_definition `!f:A->A s:A->bool x:A. res f s x = if x IN s then f x else x`;;
let regular_spherical_polygon_area = new_definition
  `regular_spherical_polygon_area ca k =
    &2 * pi - &2 * k * asn (ca * sin (pi /k))`;;
(* corrected 2010-06-06. *)
let tau0 = new_definition `tau0 = &4 * pi - &20 * sol0`;;
let mm1 = new_definition `mm1 = sol0 * sqrt(&8)/tau0`;;
let mm2 = new_definition `mm2 = (&6 * sol0 - pi) * sqrt(&2) /(&6 * tau0)`;;
let hplus = new_definition `hplus = #1.3254`;;
let h0cut = new_definition `h0cut y = if (y <= &2 * h0) then &1 else &0`;;
let marchal_quartic = new_definition `marchal_quartic h = 
    (sqrt(&2)-h)*(h- hplus )*(&9*(h pow 2) - &17*h + &3)/
      ((sqrt(&2) - &1)* &5 *(hplus - &1))`;;
let lmfun = new_definition`lmfun h = if (h<=h0) then (h0 - h)/(h0 - &1) else &0`;;
let lfun = new_definition `lfun h =  (h0 - h)/(h0 - &1)`;;
let flat_term = new_definition `flat_term y = sol0 * (y - &2 * h0)/(&2 * h0 - &2)`;;
let hminus = new_definition `hminus = @x. #1.2 <= x /\ x < #1.3 /\ marchal_quartic x = lmfun x`;;
let cstab=new_definition ` cstab= #3.01`;;
(* --- *)
let y_of_x = new_definition `y_of_x fx y1 y2 y3 y4 y5 y6 = 
    fx (y1*y1) (y2*y2) (y3*y3) (y4*y4) (y5*y5) (y6*y6)`;;
let rad2_y = new_definition `rad2_y = y_of_x rad2_x`;;
let delta4_y = new_definition `delta4_y = y_of_x delta_x4`;;
(* real and fake 4-cell volumes *)
let vol_y = new_definition `vol_y = y_of_x vol_x`;;
let vol4f = new_definition `vol4f y1 y2 y3 y4 y5 y6 f = 
   (&2 * mm1 / pi) * 
               (sol_y y1 y2 y3 y4 y5 y6 +
		  sol_y y1 y5 y6 y4 y2 y3 +
		  sol_y y4 y5 y3 y1 y2 y6 +
		  sol_y y4 y2 y6 y1 y5 y3)
	       - (&8 * mm2/pi) *
	       (f(y1/ &2)* dih_y y1 y2 y3 y4 y5 y6 +
		  f(y2/ &2)* dih_y y2 y3 y1 y5 y6 y4 +
		  f(y3/ &2)* dih_y y3 y1 y2 y6 y4 y5 +
		  f(y4/ &2)* dih_y y4 y3 y5 y1 y6 y2 +
		  f(y5/ &2)* dih_y y5 y1 y6 y2 y4 y3 +
		  f(y6/ &2)* dih_y y6 y1 y5 y3 y4 y2)`;;
let gamma4f = new_definition `gamma4f y1 y2 y3 y4 y5 y6 f =
    vol_y y1 y2 y3 y4 y5 y6 - vol4f  y1 y2 y3 y4 y5 y6 f`;;
let gamma4fgcy = new_definition `gamma4fgcy y1 y2 y3 y4 y5 y6 f =
   gamma4f y1 y2 y3 y4 y5 y6 f`;;
(* real and fake 3-cell volumes *)
let vol3r = new_definition `vol3r y1 y2 y3 r = vol_y r r r y1 y2 y3`;;
let vol3f = new_definition `vol3f y1 y2 y3 r f = (&2 * mm1 / pi) * 
        (sol_y y1 y2 r r r y3 +
	   sol_y y2 y3 r r r y1 +
	   sol_y y3 y1 r r r y2)
    - (&8 * mm2/pi) *
       (f(y1/ &2)* dih_y y1 y2 r r r y3 +
	  f(y2/ &2)* dih_y y2 y3 r r r y1 +
	  f(y3/ &2)* dih_y y3 y1 r r r y2)`;;
let gamma3f = new_definition `gamma3f y1 y2 y3 r f = vol3r y1 y2 y3 r - vol3f y1 y2 y3 r f`;;
(* error XX. vol2r missing factor of (y/2). *)
let vol2r = new_definition `vol2r y r = &2 * pi * (r*r - (y / (&2)) pow 2)/(&3)`;;
let vol2f = new_definition  `vol2f y r f =  (&2 * mm1 / pi) *  &2 *pi* (&1- y/ (r * &2))
    - (&8 * mm2/pi) * &2 * pi * f (y/ (&2)) `;;
(* For nonlinear inequalities *)
let norm2hh = new_definition `norm2hh y1 y2 y3 y4 y5 y6 = 
    (y1- hminus - hplus) pow 2 + (y2 - &2) pow 2 + (y3 - &2) pow 2 + (y4 - &2) pow 2 
     + (y5 - &2) pow 2 + (y6 - &2) pow 2`;;
let bump = new_definition `!h. bump h = #0.005*(&1 - ((h- h0) pow 2)/((hplus - h0) pow 2))`;;
let critical_edge_y = new_definition `critical_edge_y y = ((&2*hminus <= y) /\ (y <= &2 *hplus))`;;
let beta_bumpA_y = new_definition `beta_bumpA_y y1 y2 y3 y4 y5 y6 =  
         (if critical_edge_y y1 then &1 else &0) *
         (if y2 < &2 * hminus then &1 else &0) *
         (if y3 < &2 * hminus then &1 else &0) *
         (if critical_edge_y y4 then &1 else &0) *
         (if  y5 < &2 * hminus  then &1 else &0) *
         (if  y6 < &2 * hminus  then &1 else &0) *
         (bump (y1 / &2) - bump (y4 / &2))`;;
let beta_bump_force_y = new_definition `beta_bump_force_y y1 y2 y3 y4 y5 y6 =
    (bump (y1/ &2) - bump (y4 / &2))`;;
let wtcount3_y = new_definition `wtcount3_y y1 y2 y3  = 
  (if critical_edge_y y1 then 1 else 0) +
  (if critical_edge_y y2 then 1 else 0) +
  (if critical_edge_y y3 then 1 else 0) `;;
let wtcount6_y = new_definition 
 `wtcount6_y y1 y2 y3 y4 y5 y6 = wtcount3_y y1 y2 y3 + wtcount3_y y4 y5 y6`;;
(* machine_eps is a hack for numerical procedures. It is irrelevant for formal proofs. *)
let machine_eps = new_definition `machine_eps = &0`;;
(* nonlinear inequalities *)
let a_spine5 = new_definition `a_spine5 = #0.0560305`;;
let b_spine5 = new_definition `b_spine5 = -- #0.0445813`;;
let beta_bump_lb = new_definition `beta_bump_lb = -- #0.005`;;
let gamma23f = new_definition `gamma23f y1 y2 y3 y4 y5 y6 w1 w2 r f =
      (gamma3f y1 y2 y6 r f / &w1 + gamma3f y1 y3 y5 r f / &w2
      + (dih_y y1 y2 y3 y4 y5 y6 - dih_y y1 y2 r r r y6 - dih_y y1 y3 r r r y5) * (vol2r y1 r - vol2f y1 r f)/(&2 * pi)) `;;
let gamma23f_126_03 = new_definition `gamma23f_126_03 y1 y2 y3 y4 y5 y6 w1 r f =
      (gamma3f y1 y2 y6 r f / &w1 
      + (dih_y y1 y2 y3 y4 y5 y6 - dih_y y1 y2 r r r y6 - #0.03) * (vol2r y1 r - vol2f y1 r f)/(&2 * pi)) `;;
let gamma23f_red_03 = new_definition `gamma23f_red_03 y1 y2 y3 y4 y5 y6 r f =
       (dih_y y1 y2 y3 y4 y5 y6 - &2 * #0.03) * (vol2r y1 r - vol2f y1 r f)/(&2 * pi)`;;
let pathL = new_definition `pathL (a,b) = (a,(a+b)/ &2)`;;
let pathR = new_definition `pathR (a,b) = ((a+b)/ &2,b)`;;
let rotate2 = new_definition `rotate2 f x1 x2 x3 x4 x5 x6 = 
  f x2 x3 x1 x5 x6 x4`;;
let rotate3 = new_definition `rotate3 f x1 x2 x3 x4 x5 x6 = 
  f x3 x1 x2 x6 x4 x5`;;
let rotate4 = new_definition `rotate4 f x1 x2 x3 x4 x5 x6 = 
  f x4 x2 x6 x1 x5 x3`;;
let rotate5 = new_definition `rotate5 f x1 x2 x3 x4 x5 x6 = 
  f x5 x3 x4 x2 x6 x1`;;
let rotate6 = new_definition `rotate6 f x1 x2 x3 x4 x5 x6 = 
  f x6 x1 x5 x3 x4 x2`;;
let rad2_y = new_definition `rad2_y = y_of_x rad2_x`;;
let norm2hh_x = new_definition `norm2hh_x  x1 x2 x3 x4 x5 x6 = 
  norm2hh (sqrt x1) (sqrt x2) (sqrt x3) (sqrt x4) (sqrt x5) (sqrt x6)`;;
let rhazim_x = new_definition `rhazim_x x1 x2 x3 x4 x5 x6 = 
  rhazim (sqrt x1) (sqrt x2) (sqrt x3) (sqrt x4) (sqrt x5) (sqrt x6)`;;
let rhazim2_x = new_definition `rhazim2_x x1 x2 x3 x4 x5 x6 = 
  rhazim2 (sqrt x1) (sqrt x2) (sqrt x3) (sqrt x4) (sqrt x5) (sqrt x6)`;;
let rhazim3_x = new_definition `rhazim3_x x1 x2 x3 x4 x5 x6 = 
  rhazim3 (sqrt x1) (sqrt x2) (sqrt x3) (sqrt x4) (sqrt x5) (sqrt x6)`;;
let dih4_x = new_definition `dih4_x x1 x2 x3 x4 x5 x6 = 
  dih4_y (sqrt x1) (sqrt x2) (sqrt x3) (sqrt x4) (sqrt x5) (sqrt x6)`;;
let dih5_x = new_definition `dih5_x x1 x2 x3 x4 x5 x6 = 
  dih5_y (sqrt x1) (sqrt x2) (sqrt x3) (sqrt x4) (sqrt x5) (sqrt x6)`;;
let dih6_x = new_definition `dih6_x x1 x2 x3 x4 x5 x6 = 
  dih6_y (sqrt x1) (sqrt x2) (sqrt x3) (sqrt x4) (sqrt x5) (sqrt x6)`;;
let gcy = new_definition `gcy y = &4 * mm1/pi - (&8*mm2/pi) * lmfun(y/ &2)`;;
let gchi = new_definition `gchi y = (&4*mm1/pi) - (&504*(mm2/pi))/(&13) + (&200*y*(mm2/pi))/(&13) `;;
let gchi1_x = new_definition `gchi1_x x1 x2 x3 x4 x5 x6 = gchi (sqrt x1) * dih_x x1 x2 x3 x4 x5 x6`;;
let gchi2_x = new_definition `gchi2_x x1 x2 x3 x4 x5 x6 = gchi (sqrt x2) * dih2_x x1 x2 x3 x4 x5 x6`;;
let gchi3_x = new_definition `gchi3_x x1 x2 x3 x4 x5 x6 = gchi (sqrt x3) * dih3_x x1 x2 x3 x4 x5 x6`;;
let gchi4_x = new_definition `gchi4_x x1 x2 x3 x4 x5 x6 = gchi (sqrt x4) * dih4_x x1 x2 x3 x4 x5 x6`;;
let gchi5_x = new_definition `gchi5_x x1 x2 x3 x4 x5 x6 = gchi (sqrt x5) * dih5_x x1 x2 x3 x4 x5 x6`;;
let gchi6_x = new_definition `gchi6_x x1 x2 x3 x4 x5 x6 = gchi (sqrt x6) * dih6_x x1 x2 x3 x4 x5 x6`;;
let ldih_x = new_definition `ldih_x x1 x2 x3 x4 x5 x6 =
   lfun(sqrt(x1) / &2) * dih_x x1 x2 x3 x4 x5 x6`;;
let ldih2_x = new_definition `ldih2_x x1 x2 x3 x4 x5 x6 =
   lfun(sqrt(x2) / &2) * dih2_x x1 x2 x3 x4 x5 x6`;;
let ldih3_x = new_definition `ldih3_x x1 x2 x3 x4 x5 x6 =
   lfun(sqrt(x3) / &2) * dih3_x x1 x2 x3 x4 x5 x6`;;
let ldih6_x = new_definition `ldih6_x x1 x2 x3 x4 x5 x6 =
   lfun(sqrt(x6) / &2) * dih6_x x1 x2 x3 x4 x5 x6`;;
(* modified arctangent, analytic continuation of ArcTan[Sqrt[x]]/Sqrt[x] *)
let matan = new_definition `matan x = 
  if (x = &0) then &1
  else if (x > &0) then atn (sqrt x) / (sqrt x) 
  else  (log ((&1 + sqrt( -- x))/(&1 - sqrt( -- x)))) / (&2 * sqrt (-- x))`;;
(* compare "Euler_main_theorem.EULER_ANGLE_SUM_rescal" *)
let sol_euler_x =  new_definition `sol_euler_x x1 x2 x3 x4 x5 x6 = 
  (let a = sqrt(x1*x2*x3) + sqrt( x1)*(x2 + x3 - x4)/ &2 + 
     sqrt(x2)*(x1 + x3 - x5)/ &2 + sqrt(x3)*(x1 + x2 - x6)/ &2 in
  &2 * atn2( &2 * a, sqrt (delta_x x1 x2 x3 x4 x5 x6)))`;;
let taum_y1 = new_definition 
 `taum_y1 a b y1 (y2:real) (y3:real) (y4:real) (y5:real) (y6:real) = 
  taum (&2) (&2) (&2) a b y1`;;
let taum_y2 = new_definition 
 `taum_y2 a b (y1:real) (y2:real) (y3:real) (y4:real) (y5:real) (y6:real) = 
  taum (&2) (&2) (&2) a b y2`;;
let taum_y1_y2 = new_definition 
 `taum_y1_y2 a (y1:real) (y2:real) (y3:real) (y4:real) (y5:real) (y6:real) = 
  taum (&2) (&2) (&2) a y1 y2`;;
let taum_x1 = new_definition `taum_x1 a b x1 x2 x3 x4 x5 x6 = 
  taum_y1 a b (sqrt x1) (sqrt x2) (sqrt x3) (sqrt x4) (sqrt x5) (sqrt x6)`;;
let taum_x2 = new_definition `taum_x2 a b x1 x2 x3 x4 x5 x6 = 
  taum_y2 a b (sqrt x1) (sqrt x2) (sqrt x3) (sqrt x4) (sqrt x5) (sqrt x6)`;;
let taum_x1_x2 = new_definition `taum_x1_x2 a x1 x2 x3 x4 x5 x6 = 
  taum_y1_y2 a (sqrt x1) (sqrt x2) (sqrt x3) (sqrt x4) (sqrt x5) (sqrt x6)`;;
let arclength_y1 = new_definition 
 `arclength_y1 a b 
  (y1:real) (y2:real) (y3:real) (y4:real) (y5:real) (y6:real) =
  arclength y1 a b`;;
let arclength_y2 = new_definition 
 `arclength_y2 a b 
  (y1:real) (y2:real) (y3:real) (y4:real) (y5:real) (y6:real) =
  arclength y2 a b`;;
let arclength_x1 = new_definition 
 `arclength_x1 a b x1 x2 x3 x4 x5 x6 = 
  arclength_y1 a b (sqrt x1) (sqrt x2) (sqrt x3) (sqrt x4) (sqrt x5) (sqrt x6)`;;
let arclength_x2 = new_definition 
 `arclength_x2 a b x1 x2 x3 x4 x5 x6 = 
  arclength_y2 a b (sqrt x1) (sqrt x2) (sqrt x3) (sqrt x4) (sqrt x5) (sqrt x6)`;;
let arc_hhn = new_definition `arc_hhn = 
  arclength (&2 * h0) (&2 * h0) (&2)`;;
let asn797k = new_definition `asn797k k x2 x3 x4 x5 x6 = 
  k * asn (cos (#0.797) * sin (pi / k))`;;
let asnFnhk = new_definition `asnFnhk h k x3 x4 x5 x6 = 
  k * asn (( h * sqrt3 / #4.0 + sqrt(&1 - (h/ &2) pow 2)/ &2) * sin (pi/ k))`;;
let lfun_y1 = new_definition `lfun_y1 (y1:real) (y2:real) (y3:real) 
  (y4:real) (y5:real) (y6:real) =  lfun y1`;;
let acs_sqrt_x1_d4 = new_definition `acs_sqrt_x1_d4 (x1:real) (x2:real) (x3:real) (x4:real) (x5:real) (x6:real) = 
  acs (sqrt(x1)/ &4)`;;
let acs_sqrt_x2_d4 = new_definition `acs_sqrt_x2_d4 (x1:real) (x2:real) (x3:real) (x4:real) (x5:real) (x6:real) = 
  acs (sqrt(x2) / &4)`;;
let arclength_x_123 = new_definition `arclength_x_123  (x1:real) (x2:real) (x3:real) (x4:real) (x5:real) (x6:real) = arclength (sqrt x1) (sqrt x2) (sqrt x3)`;;
(* tame hypermap *)
let tame_table_d = new_definition
  `tame_table_d r s = if (r + 2*s > 3) 
  then #0.103 * (&2 - &s) + #0.2759 * (&r + &2* &s - &4)
  else #0.0`;;
(* preferred term over d2_tame *)
let eta2_126 = new_definition `eta2_126 x1 (x2:real) (x3:real) (x4:real) (x5:real) x6 = 
  (eta_y (sqrt x1) (sqrt x2) (sqrt x6)) pow 2`;;
let eta2_135 = new_definition `eta2_135 x1 (x2:real) (x3:real) (x4:real) (x5:real) (x6:real) = 
  (eta_y (sqrt x1) (sqrt x3) (sqrt x5)) pow 2`;;
let eta2_456 = new_definition `eta2_456 (x1:real) (x2:real) (x3:real) (x4:real) (x5:real) (x6:real) = 
  (eta_y (sqrt x4) (sqrt x5) (sqrt x6)) pow 2`;;
let num1 = new_definition `num1 e1 e2 e3 a2 b2 c2 =
   -- &4*((a2 pow 2) *e1 + &8*(b2 - c2)*(e2 - e3) - 
  a2*(&16*e1 + ( b2 - &8 )*e2 + (c2 - &8)*e3))`;;
let flat_term_x = new_definition `flat_term_x x = flat_term (sqrt x)`;;
let taum_x = new_definition `taum_x x1 x2 x3 x4 x5 x6 = 
    rhazim_x x1 x2 x3 x4 x5 x6 + rhazim2_x x1 x2 x3 x4 x5 x6 + 
     rhazim3_x x1  x2 x3 x4 x5 x6 - (&1 + const1)* pi`;;
let eulerA_x = new_definition `eulerA_x x1 x2 x3 x4 x5 x6 =
  sqrt(x1) * sqrt(x2) * sqrt(x3) + sqrt(x1) * (x2 + x3 - x4) / &2 + sqrt(x2) * (x1 + x3 - x5) / &2 +
    sqrt(x3) * (x1 + x2 - x6) / &2`;;
let delta4_squared_x = new_definition 
  `delta4_squared_x x1 x2 x3 x4 x5 x6 = (delta_x4 x1 x2 x3 x4 x5 x6) pow 2`;;
let delta4_squared_y = new_definition `delta4_squared_y = y_of_x delta4_squared_x`;;
let x1_delta_x = new_definition `x1_delta_x x1 x2 x3 x4 x5 x6 = x1 * delta_x x1 x2 x3 x4 x5 x6`;;
let x1_delta_y = new_definition `x1_delta_y = y_of_x x1_delta_x`;;
let delta_126_x = new_definition 
  `delta_126_x (x3s:real) (x4s:real) (x5s:real) (x1:real) (x2:real) (x3:real) (x4:real) (x5:real) (x6:real) = 
     delta_x x1 x2 x3s x4s x5s x6`;;
let delta_234_x = new_definition 
  `delta_234_x (x1s:real) (x5s:real) (x6s:real) (x1:real) (x2:real) (x3:real) (x4:real) (x5:real) (x6:real) = 
     delta_x x1s x2 x3 x4 x5s x6s`;;
let delta_135_x = new_definition 
  `delta_135_x (x2s:real) (x4s:real) (x6s:real) (x1:real) (x2:real) (x3:real) (x4:real) (x5:real) (x6:real) = 
     delta_x x1 x2s x3 x4s x5 x6s`;;
(* Added May 22, 2011 *)
let delta_sub1_x = new_definition 
  `delta_sub1_x (x1s:real) (x1:real) (x2:real) (x3:real) (x4:real) (x5:real) (x6:real) = 
     delta_x x1s x2 x3 x4 x5 x6`;;
let taum_sub1_x = new_definition `taum_sub1_x x1s (x1:real) x2 x3 x4 x5 x6 = 
      taum_x x1s x2 x3 x4 x5 x6`;;
let taum_sub246_x = new_definition 
 `taum_sub246_x x2s x4s x6s (x1:real) (x2:real) x3 (x4:real) x5 (x6:real) = 
      taum_x x1 x2s x3 x4s x5 x6s`;;
let taum_sub345_x = new_definition 
 `taum_sub345_x x3s x4s x5s (x1:real) (x2:real) (x3:real) (x4:real) (x5:real) (x6:real) = 
      taum_x x1 x2 x3s x4s x5s x6`;;
end;;