src/HOL/Library/Sum_Of_Squares.thy
author wenzelm
Thu Oct 15 21:08:03 2009 +0200 (2009-10-15)
changeset 32949 aa6c470a962a
parent 32645 1cc5b24f5a01
child 33041 6793b02a3409
permissions -rw-r--r--
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
eliminated aliases of Output operations;
print_cert: use warning for increased chance that the user actually sees the output;
misc tuning and simplification;
wenzelm@32949
     1
(*  Title:      HOL/Library/Sum_Of_Squares.thy
wenzelm@32949
     2
    Author:     Amine Chaieb, University of Cambridge
wenzelm@32949
     3
    Author:     Philipp Meyer, TU Muenchen
chaieb@31119
     4
*)
chaieb@31119
     5
chaieb@31119
     6
header {* A decision method for universal multivariate real arithmetic with addition, 
wenzelm@32333
     7
  multiplication and ordering using semidefinite programming *}
nipkow@32271
     8
chaieb@31119
     9
theory Sum_Of_Squares
wenzelm@32332
    10
imports Complex_Main (* "~~/src/HOL/Decision_Procs/Dense_Linear_Order" *)
wenzelm@32332
    11
uses
wenzelm@32333
    12
  ("positivstellensatz.ML")  (* duplicate use!? -- cf. Euclidian_Space.thy *)
wenzelm@32332
    13
  ("Sum_Of_Squares/sum_of_squares.ML")
Philipp@32645
    14
  ("Sum_Of_Squares/positivstellensatz_tools.ML")
wenzelm@32332
    15
  ("Sum_Of_Squares/sos_wrapper.ML")
wenzelm@32332
    16
begin
chaieb@31119
    17
wenzelm@32333
    18
text {*
wenzelm@32333
    19
  In order to use the method sos, call it with @{text "(sos
wenzelm@32333
    20
  remote_csdp)"} to use the remote solver.  Or install CSDP
wenzelm@32333
    21
  (https://projects.coin-or.org/Csdp), configure the Isabelle setting
wenzelm@32333
    22
  @{text CSDP_EXE}, and call it with @{text "(sos csdp)"}.  By
wenzelm@32333
    23
  default, sos calls @{text remote_csdp}.  This can take of the order
wenzelm@32333
    24
  of a minute for one sos call, because sos calls CSDP repeatedly.  If
wenzelm@32333
    25
  you install CSDP locally, sos calls typically takes only a few
wenzelm@32333
    26
  seconds.
Philipp@32645
    27
  sos generates a certificate which can be used to repeat the proof
Philipp@32645
    28
  without calling an external prover.
wenzelm@32333
    29
*}
wenzelm@32333
    30
wenzelm@32333
    31
text {* setup sos tactic *}
wenzelm@32332
    32
wenzelm@32332
    33
use "positivstellensatz.ML"
Philipp@32645
    34
use "Sum_Of_Squares/positivstellensatz_tools.ML"
wenzelm@32332
    35
use "Sum_Of_Squares/sum_of_squares.ML"
wenzelm@32332
    36
use "Sum_Of_Squares/sos_wrapper.ML"
wenzelm@32332
    37
wenzelm@32949
    38
setup SOS_Wrapper.setup
chaieb@31119
    39
Philipp@32645
    40
text {* Tests *}
Philipp@32645
    41
Philipp@32645
    42
lemma "(3::real) * x + 7 * a < 4 & 3 < 2 * x \<Longrightarrow> a < 0"
Philipp@32645
    43
by (sos_cert "((R<1 + (((A<1 * R<1) * (R<2 * [1]^2)) + (((A<0 * R<1) * (R<3 * [1]^2)) + ((A<=0 * R<1) * (R<14 * [1]^2))))))")
Philipp@32645
    44
Philipp@32645
    45
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)"
Philipp@32645
    46
by (sos_cert "(((A<0 * R<1) + (([~1/2*a1*b2 + ~1/2*a2*b1] * A=0) + (([~1/2*a1*a2 + 1/2*b1*b2] * A=1) + (((A<0 * R<1) * ((R<1/2 * [b2]^2) + (R<1/2 * [b1]^2))) + ((A<=0 * (A<=1 * R<1)) * ((R<1/2 * [b2]^2) + ((R<1/2 * [b1]^2) + ((R<1/2 * [a2]^2) + (R<1/2 * [a1]^2))))))))))")
Philipp@32268
    47
Philipp@32645
    48
lemma "(3::real) * x + 7 * a < 4 & 3 < 2 * x --> a < 0"
Philipp@32645
    49
by (sos_cert "((R<1 + (((A<1 * R<1) * (R<2 * [1]^2)) + (((A<0 * R<1) * (R<3 * [1]^2)) + ((A<=0 * R<1) * (R<14 * [1]^2))))))")
Philipp@32645
    50
Philipp@32645
    51
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"
Philipp@32645
    52
by (sos_cert "((R<1 + (((A<=3 * (A<=4 * R<1)) * (R<1 * [1]^2)) + (((A<=2 * (A<=7 * R<1)) * (R<1 * [1]^2)) + (((A<=1 * (A<=6 * R<1)) * (R<1 * [1]^2)) + ((A<=0 * (A<=5 * R<1)) * (R<1 * [1]^2)))))))")
chaieb@31119
    53
Philipp@32645
    54
lemma "(0::real) <= x & 0 <= y & 0 <= z & x + y + z <= 3 --> x * y + x * z + y * z >= 3 * x * y * z"
Philipp@32645
    55
by (sos_cert "(((A<0 * R<1) + (((A<0 * R<1) * (R<1/2 * [1]^2)) + (((A<=2 * R<1) * (R<1/2 * [~1*x + y]^2)) + (((A<=1 * R<1) * (R<1/2 * [~1*x + z]^2)) + (((A<=1 * (A<=2 * (A<=3 * R<1))) * (R<1/2 * [1]^2)) + (((A<=0 * R<1) * (R<1/2 * [~1*y + z]^2)) + (((A<=0 * (A<=2 * (A<=3 * R<1))) * (R<1/2 * [1]^2)) + ((A<=0 * (A<=1 * (A<=3 * R<1))) * (R<1/2 * [1]^2))))))))))")
Philipp@32645
    56
Philipp@32645
    57
lemma "((x::real)^2 + y^2 + z^2 = 1) --> (x + y + z)^2 <= 3"
Philipp@32645
    58
by (sos_cert "(((A<0 * R<1) + (([~3] * A=0) + (R<1 * ((R<2 * [~1/2*x + ~1/2*y + z]^2) + (R<3/2 * [~1*x + y]^2))))))")
chaieb@31119
    59
Philipp@32645
    60
lemma "(w^2 + x^2 + y^2 + z^2 = 1) --> (w + x + y + z)^2 <= (4::real)"
Philipp@32645
    61
by (sos_cert "(((A<0 * R<1) + (([~4] * A=0) + (R<1 * ((R<3 * [~1/3*w + ~1/3*x + ~1/3*y + z]^2) + ((R<8/3 * [~1/2*w + ~1/2*x + y]^2) + (R<2 * [~1*w + x]^2)))))))")
chaieb@31119
    62
Philipp@32645
    63
lemma "(x::real) >= 1 & y >= 1 --> x * y >= x + y - 1"
Philipp@32645
    64
by (sos_cert "(((A<0 * R<1) + ((A<=0 * (A<=1 * R<1)) * (R<1 * [1]^2))))")
Philipp@32645
    65
Philipp@32645
    66
lemma "(x::real) > 1 & y > 1 --> x * y > x + y - 1"
Philipp@32645
    67
by (sos_cert "((((A<0 * A<1) * R<1) + ((A<=0 * R<1) * (R<1 * [1]^2))))") 
chaieb@31119
    68
Philipp@32645
    69
lemma "abs(x) <= 1 --> abs(64 * x^7 - 112 * x^5 + 56 * x^3 - 7 * x) <= (1::real)"
Philipp@32645
    70
by (sos_cert "((((A<0 * R<1) + ((A<=1 * R<1) * (R<1 * [~8*x^3 + ~4*x^2 + 4*x + 1]^2)))) & ((((A<0 * A<1) * R<1) + ((A<=1 * (A<0 * R<1)) * (R<1 * [8*x^3 + ~4*x^2 + ~4*x + 1]^2)))))")
Philipp@32645
    71
Philipp@32645
    72
(* ------------------------------------------------------------------------- *)
Philipp@32645
    73
(* One component of denominator in dodecahedral example.                     *)
Philipp@32645
    74
(* ------------------------------------------------------------------------- *)
chaieb@31119
    75
Philipp@32645
    76
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)"
Philipp@32645
    77
by (sos_cert "(((A<0 * R<1) + ((R<1 * ((R<5749028157/5000000000 * [~25000/222477*x + ~25000/222477*y + ~25000/222477*z + 1]^2) + ((R<864067/1779816 * [419113/864067*x + 419113/864067*y + z]^2) + ((R<320795/864067 * [419113/1283180*x + y]^2) + (R<1702293/5132720 * [x]^2))))) + (((A<=4 * (A<=5 * R<1)) * (R<3/2 * [1]^2)) + (((A<=3 * (A<=5 * R<1)) * (R<1/2 * [1]^2)) + (((A<=2 * (A<=4 * R<1)) * (R<1 * [1]^2)) + (((A<=2 * (A<=3 * R<1)) * (R<3/2 * [1]^2)) + (((A<=1 * (A<=5 * R<1)) * (R<1/2 * [1]^2)) + (((A<=1 * (A<=3 * R<1)) * (R<1/2 * [1]^2)) + (((A<=0 * (A<=4 * R<1)) * (R<1 * [1]^2)) + (((A<=0 * (A<=2 * R<1)) * (R<1 * [1]^2)) + ((A<=0 * (A<=1 * R<1)) * (R<3/2 * [1]^2)))))))))))))")
chaieb@31119
    78
Philipp@32645
    79
(* ------------------------------------------------------------------------- *)
Philipp@32645
    80
(* Over a larger but simpler interval.                                       *)
Philipp@32645
    81
(* ------------------------------------------------------------------------- *)
chaieb@31119
    82
Philipp@32645
    83
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)"
Philipp@32645
    84
by (sos_cert "((R<1 + ((R<1 * ((R<1 * [~1/6*x + ~1/6*y + ~1/6*z + 1]^2) + ((R<1/18 * [~1/2*x + ~1/2*y + z]^2) + (R<1/24 * [~1*x + y]^2)))) + (((A<0 * R<1) * (R<1/12 * [1]^2)) + (((A<=4 * (A<=5 * R<1)) * (R<1/6 * [1]^2)) + (((A<=2 * (A<=4 * R<1)) * (R<1/6 * [1]^2)) + (((A<=2 * (A<=3 * R<1)) * (R<1/6 * [1]^2)) + (((A<=0 * (A<=4 * R<1)) * (R<1/6 * [1]^2)) + (((A<=0 * (A<=2 * R<1)) * (R<1/6 * [1]^2)) + ((A<=0 * (A<=1 * R<1)) * (R<1/6 * [1]^2)))))))))))")
chaieb@31119
    85
Philipp@32645
    86
(* ------------------------------------------------------------------------- *)
Philipp@32645
    87
(* We can do 12. I think 12 is a sharp bound; see PP's certificate.          *)
Philipp@32645
    88
(* ------------------------------------------------------------------------- *)
Philipp@32645
    89
Philipp@32645
    90
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)"
Philipp@32645
    91
by (sos_cert "(((A<0 * R<1) + (((A<=4 * R<1) * (R<2/3 * [1]^2)) + (((A<=4 * (A<=5 * R<1)) * (R<1 * [1]^2)) + (((A<=3 * (A<=4 * R<1)) * (R<1/3 * [1]^2)) + (((A<=2 * R<1) * (R<2/3 * [1]^2)) + (((A<=2 * (A<=5 * R<1)) * (R<1/3 * [1]^2)) + (((A<=2 * (A<=4 * R<1)) * (R<8/3 * [1]^2)) + (((A<=2 * (A<=3 * R<1)) * (R<1 * [1]^2)) + (((A<=1 * (A<=4 * R<1)) * (R<1/3 * [1]^2)) + (((A<=1 * (A<=2 * R<1)) * (R<1/3 * [1]^2)) + (((A<=0 * R<1) * (R<2/3 * [1]^2)) + (((A<=0 * (A<=5 * R<1)) * (R<1/3 * [1]^2)) + (((A<=0 * (A<=4 * R<1)) * (R<8/3 * [1]^2)) + (((A<=0 * (A<=3 * R<1)) * (R<1/3 * [1]^2)) + (((A<=0 * (A<=2 * R<1)) * (R<8/3 * [1]^2)) + ((A<=0 * (A<=1 * R<1)) * (R<1 * [1]^2))))))))))))))))))")
chaieb@31119
    92
Philipp@32645
    93
(* ------------------------------------------------------------------------- *)
Philipp@32645
    94
(* Inequality from sci.math (see "Leon-Sotelo, por favor").                  *)
Philipp@32645
    95
(* ------------------------------------------------------------------------- *)
wenzelm@32543
    96
Philipp@32645
    97
lemma "0 <= (x::real) & 0 <= y & (x * y = 1) --> x + y <= x^2 + y^2"
Philipp@32645
    98
by (sos_cert "(((A<0 * R<1) + (([1] * A=0) + (R<1 * ((R<1 * [~1/2*x + ~1/2*y + 1]^2) + (R<3/4 * [~1*x + y]^2))))))") 
wenzelm@32543
    99
Philipp@32645
   100
lemma "0 <= (x::real) & 0 <= y & (x * y = 1) --> x * y * (x + y) <= x^2 + y^2"
Philipp@32645
   101
by (sos_cert "(((A<0 * R<1) + (([~1*x + ~1*y + 1] * A=0) + (R<1 * ((R<1 * [~1/2*x + ~1/2*y + 1]^2) + (R<3/4 * [~1*x + y]^2))))))") 
wenzelm@32543
   102
Philipp@32645
   103
lemma "0 <= (x::real) & 0 <= y --> x * y * (x + y)^2 <= (x^2 + y^2)^2"
Philipp@32645
   104
by (sos_cert "(((A<0 * R<1) + (R<1 * ((R<1 * [~1/2*x^2 + y^2 + ~1/2*x*y]^2) + (R<3/4 * [~1*x^2 + x*y]^2)))))")
wenzelm@32543
   105
Philipp@32645
   106
lemma "(0::real) <= a & 0 <= b & 0 <= c & c * (2 * a + b)^3/ 27 <= x \<longrightarrow> c * a^2 * b <= x"
Philipp@32645
   107
by (sos_cert "(((A<0 * R<1) + (((A<=3 * R<1) * (R<1 * [1]^2)) + (((A<=1 * (A<=2 * R<1)) * (R<1/27 * [~1*a + b]^2)) + ((A<=0 * (A<=2 * R<1)) * (R<8/27 * [~1*a + b]^2))))))")
Philipp@32645
   108
 
Philipp@32645
   109
lemma "(0::real) < x --> 0 < 1 + x + x^2"
Philipp@32645
   110
by (sos_cert "((R<1 + ((R<1 * (R<1 * [x]^2)) + (((A<0 * R<1) * (R<1 * [1]^2)) + ((A<=0 * R<1) * (R<1 * [1]^2))))))")
wenzelm@32543
   111
Philipp@32645
   112
lemma "(0::real) <= x --> 0 < 1 + x + x^2"
Philipp@32645
   113
by (sos_cert "((R<1 + ((R<1 * (R<1 * [x]^2)) + (((A<=1 * R<1) * (R<1 * [1]^2)) + ((A<=0 * R<1) * (R<1 * [1]^2))))))")
chaieb@31131
   114
Philipp@32645
   115
lemma "(0::real) < 1 + x^2"
Philipp@32645
   116
by (sos_cert "((R<1 + ((R<1 * (R<1 * [x]^2)) + ((A<=0 * R<1) * (R<1 * [1]^2)))))")
Philipp@32645
   117
Philipp@32645
   118
lemma "(0::real) <= 1 + 2 * x + x^2"
Philipp@32645
   119
by (sos_cert "(((A<0 * R<1) + (R<1 * (R<1 * [x + 1]^2))))")
wenzelm@32543
   120
Philipp@32645
   121
lemma "(0::real) < 1 + abs x"
Philipp@32645
   122
by (sos_cert "((R<1 + (((A<=1 * R<1) * (R<1/2 * [1]^2)) + ((A<=0 * R<1) * (R<1/2 * [1]^2)))))")
wenzelm@32543
   123
Philipp@32645
   124
lemma "(0::real) < 1 + (1 + x)^2 * (abs x)"
Philipp@32645
   125
by (sos_cert "(((R<1 + (((A<=1 * R<1) * (R<1 * [1]^2)) + ((A<=0 * R<1) * (R<1 * [x + 1]^2))))) & ((R<1 + (((A<0 * R<1) * (R<1 * [x + 1]^2)) + ((A<=0 * R<1) * (R<1 * [1]^2))))))")
wenzelm@32543
   126
wenzelm@32543
   127
wenzelm@32543
   128
Philipp@32645
   129
lemma "abs ((1::real) + x^2) = (1::real) + x^2"
Philipp@32645
   130
by (sos_cert "(() & (((R<1 + ((R<1 * (R<1 * [x]^2)) + ((A<1 * R<1) * (R<1/2 * [1]^2))))) & ((R<1 + ((R<1 * (R<1 * [x]^2)) + ((A<0 * R<1) * (R<1 * [1]^2)))))))")
Philipp@32645
   131
lemma "(3::real) * x + 7 * a < 4 \<and> 3 < 2 * x \<longrightarrow> a < 0"
Philipp@32645
   132
by (sos_cert "((R<1 + (((A<1 * R<1) * (R<2 * [1]^2)) + (((A<0 * R<1) * (R<3 * [1]^2)) + ((A<=0 * R<1) * (R<14 * [1]^2))))))")
chaieb@31119
   133
Philipp@32645
   134
lemma "(0::real) < x --> 1 < y --> y * x <= z --> x < z"
Philipp@32645
   135
by (sos_cert "((((A<0 * A<1) * R<1) + (((A<=1 * R<1) * (R<1 * [1]^2)) + ((A<=0 * R<1) * (R<1 * [1]^2)))))")
Philipp@32645
   136
lemma "(1::real) < x --> x^2 < y --> 1 < y"
Philipp@32645
   137
by (sos_cert "((((A<0 * A<1) * R<1) + ((R<1 * ((R<1/10 * [~2*x + y + 1]^2) + (R<1/10 * [~1*x + y]^2))) + (((A<1 * R<1) * (R<1/2 * [1]^2)) + (((A<0 * R<1) * (R<1 * [x]^2)) + (((A<=0 * R<1) * ((R<1/10 * [x + 1]^2) + (R<1/10 * [x]^2))) + (((A<=0 * (A<1 * R<1)) * (R<1/5 * [1]^2)) + ((A<=0 * (A<0 * R<1)) * (R<1/5 * [1]^2)))))))))")
Philipp@32645
   138
lemma "(b::real)^2 < 4 * a * c --> ~(a * x^2 + b * x + c = 0)"
Philipp@32645
   139
by (sos_cert "(((A<0 * R<1) + (R<1 * (R<1 * [2*a*x + b]^2))))")
Philipp@32645
   140
lemma "(b::real)^2 < 4 * a * c --> ~(a * x^2 + b * x + c = 0)"
Philipp@32645
   141
by (sos_cert "(((A<0 * R<1) + (R<1 * (R<1 * [2*a*x + b]^2))))")
Philipp@32645
   142
lemma "((a::real) * x^2 + b * x + c = 0) --> b^2 >= 4 * a * c"
Philipp@32645
   143
by (sos_cert "(((A<0 * R<1) + (R<1 * (R<1 * [2*a*x + b]^2))))")
Philipp@32645
   144
lemma "(0::real) <= b & 0 <= c & 0 <= x & 0 <= y & (x^2 = c) & (y^2 = a^2 * c + b) --> a * c <= y * x"
Philipp@32645
   145
by (sos_cert "(((A<0 * (A<0 * R<1)) + (((A<=2 * (A<=3 * (A<0 * R<1))) * (R<2 * [1]^2)) + ((A<=0 * (A<=1 * R<1)) * (R<1 * [1]^2)))))")
Philipp@32645
   146
lemma "abs(x - z) <= e & abs(y - z) <= e & 0 <= u & 0 <= v & (u + v = 1) --> abs((u * x + v * y) - z) <= (e::real)"
Philipp@32645
   147
by (sos_cert "((((A<0 * R<1) + (((A<=3 * (A<=6 * R<1)) * (R<1 * [1]^2)) + ((A<=1 * (A<=5 * R<1)) * (R<1 * [1]^2))))) & ((((A<0 * A<1) * R<1) + (((A<=3 * (A<=5 * (A<0 * R<1))) * (R<1 * [1]^2)) + ((A<=1 * (A<=4 * (A<0 * R<1))) * (R<1 * [1]^2))))))")
chaieb@31119
   148
chaieb@31119
   149
Philipp@32645
   150
(* 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?*)
wenzelm@32543
   151
chaieb@31131
   152
lemma "(0::real) <= x --> (1 + x + x^2)/(1 + x^2) <= 1 + x"
Philipp@32645
   153
by (sos_cert "(((((A<0 * A<1) * R<1) + ((A<=0 * (A<0 * R<1)) * (R<1 * [x]^2)))) & ((R<1 + ((R<1 * (R<1 * [x]^2)) + ((A<0 * R<1) * (R<1 * [1]^2))))))")
chaieb@31131
   154
chaieb@31131
   155
lemma "(0::real) <= x --> 1 - x <= 1 / (1 + x + x^2)"
Philipp@32645
   156
by (sos_cert "(((R<1 + (([~4/3] * A=0) + ((R<1 * ((R<1/3 * [3/2*x + 1]^2) + (R<7/12 * [x]^2))) + ((A<=0 * R<1) * (R<1/3 * [1]^2)))))) & (((((A<0 * A<1) * R<1) + ((A<=0 * (A<0 * R<1)) * (R<1 * [x]^2)))) & ((R<1 + ((R<1 * (R<1 * [x]^2)) + (((A<0 * R<1) * (R<1 * [1]^2)) + ((A<=0 * R<1) * (R<1 * [1]^2))))))))")
chaieb@31131
   157
chaieb@31131
   158
lemma "(x::real) <= 1 / 2 --> - x - 2 * x^2 <= - x / (1 - x)"
Philipp@32645
   159
by (sos_cert "((((A<0 * A<1) * R<1) + ((A<=0 * (A<0 * R<1)) * (R<1 * [x]^2))))")
chaieb@31131
   160
Philipp@32645
   161
lemma "4*r^2 = p^2 - 4*q & r >= (0::real) & x^2 + p*x + q = 0 --> 2*(x::real) = - p + 2*r | 2*x = -p - 2*r"
Philipp@32645
   162
by (sos_cert "((((((A<0 * A<1) * R<1) + ([~4] * A=0))) & ((((A<0 * A<1) * R<1) + ([4] * A=0)))) & (((((A<0 * A<1) * R<1) + ([4] * A=0))) & ((((A<0 * A<1) * R<1) + ([~4] * A=0)))))")
chaieb@31119
   163
chaieb@31119
   164
end
Philipp@32645
   165