src/HOL/Library/Sum_Of_Squares.thy
changeset 31119 2532bb2d65c7
child 31131 d9752181691a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Library/Sum_Of_Squares.thy	Tue May 12 17:32:49 2009 +0100
     1.3 @@ -0,0 +1,114 @@
     1.4 +(* Title:      Library/Sum_Of_Squares
     1.5 +   Author:     Amine Chaieb, University of Cambridge
     1.6 +*)
     1.7 +
     1.8 +header {* A decision method for universal multivariate real arithmetic with addition, 
     1.9 +          multiplication and ordering using semidefinite programming*}
    1.10 +theory Sum_Of_Squares
    1.11 +  imports Complex_Main (* "~~/src/HOL/Decision_Procs/Dense_Linear_Order" *)
    1.12 +  uses "positivstellensatz.ML" "sum_of_squares.ML"
    1.13 +  begin
    1.14 +
    1.15 +method_setup sos = {* 
    1.16 +let 
    1.17 + fun strip_all ct = 
    1.18 +  case term_of ct of 
    1.19 +   Const("all",_) $ Abs (xn,xT,p) => 
    1.20 +    let val (a,(v,t')) = (apsnd (Thm.dest_abs (SOME xn)) o Thm.dest_comb) ct
    1.21 +    in apfst (cons v) (strip_all t')
    1.22 +    end
    1.23 + | _ => ([],ct)
    1.24 +
    1.25 + fun core_sos_conv ctxt t = Drule.arg_cong_rule @{cterm Trueprop} (Sos.real_sos ctxt (Thm.dest_arg t) RS @{thm Eq_TrueI})
    1.26 + fun core_sos_tac ctxt = CSUBGOAL (fn (ct, i) => 
    1.27 +   let val (avs, p) = strip_all ct
    1.28 +       val th = standard (fold_rev forall_intr avs (Sos.real_sos ctxt (Thm.dest_arg p)))
    1.29 +   in rtac th i end) (* CONVERSION o core_sos_conv *)
    1.30 +in Scan.succeed (SIMPLE_METHOD' o core_sos_tac)
    1.31 +end
    1.32 +
    1.33 +*} "Prove universal problems over the reals using sums of squares"
    1.34 +
    1.35 +text{* Tests -- commented since they work only when csdp is installed *}
    1.36 +
    1.37 +(*
    1.38 +lemma "(3::real) * x + 7 * a < 4 & 3 < 2 * x --> a < 0" by sos
    1.39 +
    1.40 +lemma "a1 >= 0 & a2 >= 0 \<and> (a1 * a1 + a2 * a2 = b1 * b1 + b2 * b2 + 2) \<and> (a1 * b1 + a2 * b2 = 0) --> a1 * a2 - b1 * b2 >= (0::real)" by sos
    1.41 +
    1.42 +lemma "(3::real) * x + 7 * a < 4 & 3 < 2 * x --> a < 0" by sos
    1.43 +
    1.44 +lemma "(0::real) <= x & x <= 1 & 0 <= y & y <= 1  --> x^2 + y^2 < 1 |(x - 1)^2 + y^2 < 1 | x^2 + (y - 1)^2 < 1 | (x - 1)^2 + (y - 1)^2 < 1" by sos
    1.45 +
    1.46 +lemma "(0::real) <= x & 0 <= y & 0 <= z & x + y + z <= 3 --> x * y + x * z + y * z >= 3 * x * y * z" by sos
    1.47 +
    1.48 +lemma "((x::real)^2 + y^2 + z^2 = 1) --> (x + y + z)^2 <= 3" by sos
    1.49 +
    1.50 +lemma "(w^2 + x^2 + y^2 + z^2 = 1) --> (w + x + y + z)^2 <= (4::real)" by sos
    1.51 +
    1.52 +lemma "(x::real) >= 1 & y >= 1 --> x * y >= x + y - 1" by sos
    1.53 +
    1.54 +lemma "(x::real) > 1 & y > 1 --> x * y > x + y - 1" by sos; 
    1.55 +
    1.56 +lemma "abs(x) <= 1 --> abs(64 * x^7 - 112 * x^5 + 56 * x^3 - 7 * x) <= (1::real)" by sos  
    1.57 +*)
    1.58 +(* ------------------------------------------------------------------------- *)
    1.59 +(* One component of denominator in dodecahedral example.                     *)
    1.60 +(* ------------------------------------------------------------------------- *)
    1.61 +(*
    1.62 +lemma "2 <= x & x <= 125841 / 50000 & 2 <= y & y <= 125841 / 50000 & 2 <= z & z <= 125841 / 50000 --> 2 * (x * z + x * y + y * z) - (x * x + y * y + z * z) >= (0::real)" by sos;
    1.63 +*)
    1.64 +(* ------------------------------------------------------------------------- *)
    1.65 +(* Over a larger but simpler interval.                                       *)
    1.66 +(* ------------------------------------------------------------------------- *)
    1.67 +(*
    1.68 +lemma "(2::real) <= x & x <= 4 & 2 <= y & y <= 4 & 2 <= z & z <= 4 --> 0 <= 2 * (x * z + x * y + y * z) - (x * x + y * y + z * z)" by sos
    1.69 +*)
    1.70 +(* ------------------------------------------------------------------------- *)
    1.71 +(* We can do 12. I think 12 is a sharp bound; see PP's certificate.          *)
    1.72 +(* ------------------------------------------------------------------------- *)
    1.73 +(*
    1.74 +lemma "2 <= (x::real) & x <= 4 & 2 <= y & y <= 4 & 2 <= z & z <= 4 --> 12 <= 2 * (x * z + x * y + y * z) - (x * x + y * y + z * z)" by sos
    1.75 +*)
    1.76 +
    1.77 +(* ------------------------------------------------------------------------- *)
    1.78 +(* Inequality from sci.math (see "Leon-Sotelo, por favor").                  *)
    1.79 +(* ------------------------------------------------------------------------- *)
    1.80 +(*
    1.81 +lemma "0 <= (x::real) & 0 <= y & (x * y = 1) --> x + y <= x^2 + y^2" by sos 
    1.82 +
    1.83 +lemma "0 <= (x::real) & 0 <= y & (x * y = 1) --> x * y * (x + y) <= x^2 + y^2" by sos 
    1.84 +
    1.85 +lemma "0 <= (x::real) & 0 <= y --> x * y * (x + y)^2 <= (x^2 + y^2)^2" by sos
    1.86 +
    1.87 +lemma "(0::real) <= a & 0 <= b & 0 <= c & c * (2 * a + b)^3/ 27 <= x \<longrightarrow> c * a^2 * b <= x" by sos
    1.88 + 
    1.89 +lemma "(0::real) < x --> 0 < 1 + x + x^2" by sos
    1.90 +
    1.91 +lemma "(0::real) <= x --> 0 < 1 + x + x^2" by sos
    1.92 +
    1.93 +lemma "(0::real) < 1 + x^2" by sos
    1.94 +
    1.95 +lemma "(0::real) <= 1 + 2 * x + x^2" by sos
    1.96 +
    1.97 +lemma "(0::real) < 1 + abs x" by sos
    1.98 +
    1.99 +lemma "(0::real) < 1 + (1 + x)^2 * (abs x)" by sos
   1.100 +
   1.101 +
   1.102 +
   1.103 +lemma "abs ((1::real) + x^2) = (1::real) + x^2" by sos
   1.104 +lemma "(3::real) * x + 7 * a < 4 \<and> 3 < 2 * x \<longrightarrow> a < 0" by sos
   1.105 +
   1.106 +lemma "(0::real) < x --> 1 < y --> y * x <= z --> x < z" by sos
   1.107 +lemma "(1::real) < x --> x^2 < y --> 1 < y" by sos
   1.108 +lemma "(b::real)^2 < 4 * a * c --> ~(a * x^2 + b * x + c = 0)" by sos
   1.109 +lemma "(b::real)^2 < 4 * a * c --> ~(a * x^2 + b * x + c = 0)" by sos
   1.110 +lemma "((a::real) * x^2 + b * x + c = 0) --> b^2 >= 4 * a * c" by sos
   1.111 +lemma "(0::real) <= b & 0 <= c & 0 <= x & 0 <= y & (x^2 = c) & (y^2 = a^2 * c + b) --> a * c <= y * x" by sos
   1.112 +lemma "abs(x - z) <= e & abs(y - z) <= e & 0 <= u & 0 <= v & (u + v = 1) --> abs((u * x + v * y) - z) <= (e::real)" by sos
   1.113 +*)
   1.114 +(*
   1.115 +lemma "((x::real) - y - 2 * x^4 = 0) & 0 <= x & x <= 2 & 0 <= y & y <= 3 --> y^2 - 7 * y - 12 * x + 17 >= 0" by sos *) (* Too hard?*)
   1.116 +
   1.117 +end