author | wenzelm |
Thu, 22 Oct 2009 15:20:54 +0200 | |
changeset 33069 | d284306ea923 |
parent 33063 | 4d462963a7db |
parent 33067 | a70d5baa53ee |
child 33906 | 1eebf19b773e |
permissions | -rw-r--r-- |
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32839
diff
changeset
|
1 |
(* Title: HOL/Library/Sum_Of_Squares/sum_of_squares.ML |
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32839
diff
changeset
|
2 |
Author: Amine Chaieb, University of Cambridge |
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32839
diff
changeset
|
3 |
Author: Philipp Meyer, TU Muenchen |
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
32150
diff
changeset
|
4 |
|
32839 | 5 |
A tactic for proving nonlinear inequalities. |
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
32150
diff
changeset
|
6 |
*) |
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
32150
diff
changeset
|
7 |
|
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32839
diff
changeset
|
8 |
signature SUM_OF_SQUARES = |
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
32150
diff
changeset
|
9 |
sig |
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32839
diff
changeset
|
10 |
datatype proof_method = Certificate of RealArith.pss_tree | Prover of string -> string |
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32839
diff
changeset
|
11 |
val sos_tac: (RealArith.pss_tree -> unit) -> proof_method -> Proof.context -> int -> tactic |
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32839
diff
changeset
|
12 |
val debugging: bool Unsynchronized.ref |
32332 | 13 |
exception Failure of string; |
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
32150
diff
changeset
|
14 |
end |
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
32150
diff
changeset
|
15 |
|
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32839
diff
changeset
|
16 |
structure Sum_Of_Squares: SUM_OF_SQUARES = |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
17 |
struct |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
18 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
19 |
val rat_0 = Rat.zero; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
20 |
val rat_1 = Rat.one; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
21 |
val rat_2 = Rat.two; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
22 |
val rat_10 = Rat.rat_of_int 10; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
23 |
val rat_1_2 = rat_1 // rat_2; |
33029 | 24 |
val max = Integer.max; |
25 |
val min = Integer.min; |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
26 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
27 |
val denominator_rat = Rat.quotient_of_rat #> snd #> Rat.rat_of_int; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
28 |
val numerator_rat = Rat.quotient_of_rat #> fst #> Rat.rat_of_int; |
32839 | 29 |
fun int_of_rat a = |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
30 |
case Rat.quotient_of_rat a of (i,1) => i | _ => error "int_of_rat: not an int"; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
31 |
fun lcm_rat x y = Rat.rat_of_int (Integer.lcm (int_of_rat x) (int_of_rat y)); |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
32 |
|
32839 | 33 |
fun rat_pow r i = |
34 |
let fun pow r i = |
|
35 |
if i = 0 then rat_1 else |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
36 |
let val d = pow r (i div 2) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
37 |
in d */ d */ (if i mod 2 = 0 then rat_1 else r) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
38 |
end |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
39 |
in if i < 0 then pow (Rat.inv r) (~ i) else pow r i end; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
40 |
|
32839 | 41 |
fun round_rat r = |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
42 |
let val (a,b) = Rat.quotient_of_rat (Rat.abs r) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
43 |
val d = a div b |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
44 |
val s = if r </ rat_0 then (Rat.neg o Rat.rat_of_int) else Rat.rat_of_int |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
45 |
val x2 = 2 * (a - (b * d)) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
46 |
in s (if x2 >= b then d + 1 else d) end |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
47 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
48 |
val abs_rat = Rat.abs; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
49 |
val pow2 = rat_pow rat_2; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
50 |
val pow10 = rat_pow rat_10; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
51 |
|
32740 | 52 |
val debugging = Unsynchronized.ref false; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
53 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
54 |
exception Sanity; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
55 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
56 |
exception Unsolvable; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
57 |
|
32332 | 58 |
exception Failure of string; |
59 |
||
32645
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32332
diff
changeset
|
60 |
datatype proof_method = |
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32332
diff
changeset
|
61 |
Certificate of RealArith.pss_tree |
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32332
diff
changeset
|
62 |
| Prover of (string -> string) |
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32332
diff
changeset
|
63 |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
64 |
(* Turn a rational into a decimal string with d sig digits. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
65 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
66 |
local |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
67 |
fun normalize y = |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
68 |
if abs_rat y </ (rat_1 // rat_10) then normalize (rat_10 */ y) - 1 |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
69 |
else if abs_rat y >=/ rat_1 then normalize (y // rat_10) + 1 |
32839 | 70 |
else 0 |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
71 |
in |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
72 |
fun decimalize d x = |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
73 |
if x =/ rat_0 then "0.0" else |
32839 | 74 |
let |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
75 |
val y = Rat.abs x |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
76 |
val e = normalize y |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
77 |
val z = pow10(~ e) */ y +/ rat_1 |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
78 |
val k = int_of_rat (round_rat(pow10 d */ z)) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
79 |
in (if x </ rat_0 then "-0." else "0.") ^ |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
80 |
implode(tl(explode(string_of_int k))) ^ |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
81 |
(if e = 0 then "" else "e"^string_of_int e) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
82 |
end |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
83 |
end; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
84 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
85 |
(* Iterations over numbers, and lists indexed by numbers. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
86 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
87 |
fun itern k l f a = |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
88 |
case l of |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
89 |
[] => a |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
90 |
| h::t => itern (k + 1) t f (f h k a); |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
91 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
92 |
fun iter (m,n) f a = |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
93 |
if n < m then a |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
94 |
else iter (m+1,n) f (f m a); |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
95 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
96 |
(* The main types. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
97 |
|
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
98 |
type vector = int* Rat.rat FuncUtil.Intfunc.table; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
99 |
|
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
100 |
type matrix = (int*int)*(Rat.rat FuncUtil.Intpairfunc.table); |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
101 |
|
32645
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32332
diff
changeset
|
102 |
fun iszero (k,r) = r =/ rat_0; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
103 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
104 |
fun fold_rev2 f l1 l2 b = |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
105 |
case (l1,l2) of |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
106 |
([],[]) => b |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
107 |
| (h1::t1,h2::t2) => f h1 h2 (fold_rev2 f t1 t2 b) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
108 |
| _ => error "fold_rev2"; |
32839 | 109 |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
110 |
(* Vectors. Conventionally indexed 1..n. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
111 |
|
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
112 |
fun vector_0 n = (n,FuncUtil.Intfunc.empty):vector; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
113 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
114 |
fun dim (v:vector) = fst v; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
115 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
116 |
fun vector_const c n = |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
117 |
if c =/ rat_0 then vector_0 n |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
118 |
else (n,fold_rev (fn k => FuncUtil.Intfunc.update (k,c)) (1 upto n) FuncUtil.Intfunc.empty) :vector; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
119 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
120 |
val vector_1 = vector_const rat_1; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
121 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
122 |
fun vector_cmul c (v:vector) = |
32839 | 123 |
let val n = dim v |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
124 |
in if c =/ rat_0 then vector_0 n |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
125 |
else (n,FuncUtil.Intfunc.map (fn x => c */ x) (snd v)) |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
126 |
end; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
127 |
|
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
128 |
fun vector_neg (v:vector) = (fst v,FuncUtil.Intfunc.map Rat.neg (snd v)) :vector; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
129 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
130 |
fun vector_add (v1:vector) (v2:vector) = |
32839 | 131 |
let val m = dim v1 |
132 |
val n = dim v2 |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
133 |
in if m <> n then error "vector_add: incompatible dimensions" |
32839 | 134 |
else (n,FuncUtil.Intfunc.combine (curry op +/) (fn x => x =/ rat_0) (snd v1) (snd v2)) :vector |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
135 |
end; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
136 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
137 |
fun vector_sub v1 v2 = vector_add v1 (vector_neg v2); |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
138 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
139 |
fun vector_dot (v1:vector) (v2:vector) = |
32839 | 140 |
let val m = dim v1 |
141 |
val n = dim v2 |
|
142 |
in if m <> n then error "vector_dot: incompatible dimensions" |
|
143 |
else FuncUtil.Intfunc.fold (fn (i,x) => fn a => x +/ a) |
|
32828 | 144 |
(FuncUtil.Intfunc.combine (curry op */) (fn x => x =/ rat_0) (snd v1) (snd v2)) rat_0 |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
145 |
end; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
146 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
147 |
fun vector_of_list l = |
32839 | 148 |
let val n = length l |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
149 |
in (n,fold_rev2 (curry FuncUtil.Intfunc.update) (1 upto n) l FuncUtil.Intfunc.empty) :vector |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
150 |
end; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
151 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
152 |
(* Matrices; again rows and columns indexed from 1. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
153 |
|
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
154 |
fun matrix_0 (m,n) = ((m,n),FuncUtil.Intpairfunc.empty):matrix; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
155 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
156 |
fun dimensions (m:matrix) = fst m; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
157 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
158 |
fun matrix_const c (mn as (m,n)) = |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
159 |
if m <> n then error "matrix_const: needs to be square" |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
160 |
else if c =/ rat_0 then matrix_0 mn |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
161 |
else (mn,fold_rev (fn k => FuncUtil.Intpairfunc.update ((k,k), c)) (1 upto n) FuncUtil.Intpairfunc.empty) :matrix;; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
162 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
163 |
val matrix_1 = matrix_const rat_1; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
164 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
165 |
fun matrix_cmul c (m:matrix) = |
32839 | 166 |
let val (i,j) = dimensions m |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
167 |
in if c =/ rat_0 then matrix_0 (i,j) |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
168 |
else ((i,j),FuncUtil.Intpairfunc.map (fn x => c */ x) (snd m)) |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
169 |
end; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
170 |
|
32839 | 171 |
fun matrix_neg (m:matrix) = |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
172 |
(dimensions m, FuncUtil.Intpairfunc.map Rat.neg (snd m)) :matrix; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
173 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
174 |
fun matrix_add (m1:matrix) (m2:matrix) = |
32839 | 175 |
let val d1 = dimensions m1 |
176 |
val d2 = dimensions m2 |
|
177 |
in if d1 <> d2 |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
178 |
then error "matrix_add: incompatible dimensions" |
32828 | 179 |
else (d1,FuncUtil.Intpairfunc.combine (curry op +/) (fn x => x =/ rat_0) (snd m1) (snd m2)) :matrix |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
180 |
end;; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
181 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
182 |
fun matrix_sub m1 m2 = matrix_add m1 (matrix_neg m2); |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
183 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
184 |
fun row k (m:matrix) = |
32839 | 185 |
let val (i,j) = dimensions m |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
186 |
in (j, |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
187 |
FuncUtil.Intpairfunc.fold (fn ((i,j), c) => fn a => if i = k then FuncUtil.Intfunc.update (j,c) a else a) (snd m) FuncUtil.Intfunc.empty ) : vector |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
188 |
end; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
189 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
190 |
fun column k (m:matrix) = |
32839 | 191 |
let val (i,j) = dimensions m |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
192 |
in (i, |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
193 |
FuncUtil.Intpairfunc.fold (fn ((i,j), c) => fn a => if j = k then FuncUtil.Intfunc.update (i,c) a else a) (snd m) FuncUtil.Intfunc.empty) |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
194 |
: vector |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
195 |
end; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
196 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
197 |
fun transp (m:matrix) = |
32839 | 198 |
let val (i,j) = dimensions m |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
199 |
in |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
200 |
((j,i),FuncUtil.Intpairfunc.fold (fn ((i,j), c) => fn a => FuncUtil.Intpairfunc.update ((j,i), c) a) (snd m) FuncUtil.Intpairfunc.empty) :matrix |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
201 |
end; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
202 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
203 |
fun diagonal (v:vector) = |
32839 | 204 |
let val n = dim v |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
205 |
in ((n,n),FuncUtil.Intfunc.fold (fn (i, c) => fn a => FuncUtil.Intpairfunc.update ((i,i), c) a) (snd v) FuncUtil.Intpairfunc.empty) : matrix |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
206 |
end; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
207 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
208 |
fun matrix_of_list l = |
32839 | 209 |
let val m = length l |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
210 |
in if m = 0 then matrix_0 (0,0) else |
32839 | 211 |
let val n = length (hd l) |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
212 |
in ((m,n),itern 1 l (fn v => fn i => itern 1 v (fn c => fn j => FuncUtil.Intpairfunc.update ((i,j), c))) FuncUtil.Intpairfunc.empty) |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
213 |
end |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
214 |
end; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
215 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
216 |
(* Monomials. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
217 |
|
32828 | 218 |
fun monomial_eval assig m = |
219 |
FuncUtil.Ctermfunc.fold (fn (x, k) => fn a => a */ rat_pow (FuncUtil.Ctermfunc.apply assig x) k) |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
220 |
m rat_1; |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
221 |
val monomial_1 = FuncUtil.Ctermfunc.empty; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
222 |
|
32828 | 223 |
fun monomial_var x = FuncUtil.Ctermfunc.onefunc (x, 1); |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
224 |
|
32828 | 225 |
val monomial_mul = |
33002 | 226 |
FuncUtil.Ctermfunc.combine Integer.add (K false); |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
227 |
|
32828 | 228 |
fun monomial_pow m k = |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
229 |
if k = 0 then monomial_1 |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
230 |
else FuncUtil.Ctermfunc.map (fn x => k * x) m; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
231 |
|
32828 | 232 |
fun monomial_divides m1 m2 = |
233 |
FuncUtil.Ctermfunc.fold (fn (x, k) => fn a => FuncUtil.Ctermfunc.tryapplyd m2 x 0 >= k andalso a) m1 true;; |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
234 |
|
32828 | 235 |
fun monomial_div m1 m2 = |
33002 | 236 |
let val m = FuncUtil.Ctermfunc.combine Integer.add |
32839 | 237 |
(fn x => x = 0) m1 (FuncUtil.Ctermfunc.map (fn x => ~ x) m2) |
32828 | 238 |
in if FuncUtil.Ctermfunc.fold (fn (x, k) => fn a => k >= 0 andalso a) m true then m |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
239 |
else error "monomial_div: non-divisible" |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
240 |
end; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
241 |
|
32839 | 242 |
fun monomial_degree x m = |
32828 | 243 |
FuncUtil.Ctermfunc.tryapplyd m x 0;; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
244 |
|
32828 | 245 |
fun monomial_lcm m1 m2 = |
246 |
fold_rev (fn x => FuncUtil.Ctermfunc.update (x, max (monomial_degree x m1) (monomial_degree x m2))) |
|
33042 | 247 |
(union (is_equal o FuncUtil.cterm_ord) (FuncUtil.Ctermfunc.dom m1) (FuncUtil.Ctermfunc.dom m2)) (FuncUtil.Ctermfunc.empty); |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
248 |
|
32839 | 249 |
fun monomial_multidegree m = |
32828 | 250 |
FuncUtil.Ctermfunc.fold (fn (x, k) => fn a => k + a) m 0;; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
251 |
|
32828 | 252 |
fun monomial_variables m = FuncUtil.Ctermfunc.dom m;; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
253 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
254 |
(* Polynomials. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
255 |
|
32828 | 256 |
fun eval assig p = |
257 |
FuncUtil.Monomialfunc.fold (fn (m, c) => fn a => a +/ c */ monomial_eval assig m) p rat_0; |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
258 |
|
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
259 |
val poly_0 = FuncUtil.Monomialfunc.empty; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
260 |
|
32839 | 261 |
fun poly_isconst p = |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
262 |
FuncUtil.Monomialfunc.fold (fn (m, c) => fn a => FuncUtil.Ctermfunc.is_empty m andalso a) p true; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
263 |
|
32828 | 264 |
fun poly_var x = FuncUtil.Monomialfunc.onefunc (monomial_var x,rat_1); |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
265 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
266 |
fun poly_const c = |
32828 | 267 |
if c =/ rat_0 then poly_0 else FuncUtil.Monomialfunc.onefunc(monomial_1, c); |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
268 |
|
32828 | 269 |
fun poly_cmul c p = |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
270 |
if c =/ rat_0 then poly_0 |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
271 |
else FuncUtil.Monomialfunc.map (fn x => c */ x) p; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
272 |
|
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
273 |
fun poly_neg p = FuncUtil.Monomialfunc.map Rat.neg p;; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
274 |
|
32828 | 275 |
fun poly_add p1 p2 = |
276 |
FuncUtil.Monomialfunc.combine (curry op +/) (fn x => x =/ rat_0) p1 p2; |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
277 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
278 |
fun poly_sub p1 p2 = poly_add p1 (poly_neg p2); |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
279 |
|
32828 | 280 |
fun poly_cmmul (c,m) p = |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
281 |
if c =/ rat_0 then poly_0 |
32839 | 282 |
else if FuncUtil.Ctermfunc.is_empty m |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
283 |
then FuncUtil.Monomialfunc.map (fn d => c */ d) p |
32828 | 284 |
else FuncUtil.Monomialfunc.fold (fn (m', d) => fn a => (FuncUtil.Monomialfunc.update (monomial_mul m m', c */ d) a)) p poly_0; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
285 |
|
32828 | 286 |
fun poly_mul p1 p2 = |
287 |
FuncUtil.Monomialfunc.fold (fn (m, c) => fn a => poly_add (poly_cmmul (c,m) p2) a) p1 poly_0; |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
288 |
|
32828 | 289 |
fun poly_div p1 p2 = |
32839 | 290 |
if not(poly_isconst p2) |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
291 |
then error "poly_div: non-constant" else |
32839 | 292 |
let val c = eval FuncUtil.Ctermfunc.empty p2 |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
293 |
in if c =/ rat_0 then error "poly_div: division by zero" |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
294 |
else poly_cmul (Rat.inv c) p1 |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
295 |
end; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
296 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
297 |
fun poly_square p = poly_mul p p; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
298 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
299 |
fun poly_pow p k = |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
300 |
if k = 0 then poly_const rat_1 |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
301 |
else if k = 1 then p |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
302 |
else let val q = poly_square(poly_pow p (k div 2)) in |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
303 |
if k mod 2 = 1 then poly_mul p q else q end; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
304 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
305 |
fun poly_exp p1 p2 = |
32839 | 306 |
if not(poly_isconst p2) |
307 |
then error "poly_exp: not a constant" |
|
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
308 |
else poly_pow p1 (int_of_rat (eval FuncUtil.Ctermfunc.empty p2)); |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
309 |
|
32839 | 310 |
fun degree x p = |
32828 | 311 |
FuncUtil.Monomialfunc.fold (fn (m,c) => fn a => max (monomial_degree x m) a) p 0; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
312 |
|
32828 | 313 |
fun multidegree p = |
314 |
FuncUtil.Monomialfunc.fold (fn (m, c) => fn a => max (monomial_multidegree m) a) p 0; |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
315 |
|
32828 | 316 |
fun poly_variables p = |
33042 | 317 |
sort FuncUtil.cterm_ord (FuncUtil.Monomialfunc.fold_rev (fn (m, c) => union (is_equal o FuncUtil.cterm_ord) (monomial_variables m)) p []);; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
318 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
319 |
(* Order monomials for human presentation. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
320 |
|
32828 | 321 |
val humanorder_varpow = prod_ord FuncUtil.cterm_ord (rev_order o int_ord); |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
322 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
323 |
local |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
324 |
fun ord (l1,l2) = case (l1,l2) of |
32839 | 325 |
(_,[]) => LESS |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
326 |
| ([],_) => GREATER |
32839 | 327 |
| (h1::t1,h2::t2) => |
328 |
(case humanorder_varpow (h1, h2) of |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
329 |
LESS => LESS |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
330 |
| EQUAL => ord (t1,t2) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
331 |
| GREATER => GREATER) |
32839 | 332 |
in fun humanorder_monomial m1 m2 = |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
333 |
ord (sort humanorder_varpow (FuncUtil.Ctermfunc.dest m1), |
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
334 |
sort humanorder_varpow (FuncUtil.Ctermfunc.dest m2)) |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
335 |
end; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
336 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
337 |
(* Conversions to strings. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
338 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
339 |
fun string_of_vector min_size max_size (v:vector) = |
32839 | 340 |
let val n_raw = dim v |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
341 |
in if n_raw = 0 then "[]" else |
32839 | 342 |
let |
343 |
val n = max min_size (min n_raw max_size) |
|
344 |
val xs = map (Rat.string_of_rat o (fn i => FuncUtil.Intfunc.tryapplyd (snd v) i rat_0)) (1 upto n) |
|
32830 | 345 |
in "[" ^ space_implode ", " xs ^ |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
346 |
(if n_raw > max_size then ", ...]" else "]") |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
347 |
end |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
348 |
end; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
349 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
350 |
fun string_of_matrix max_size (m:matrix) = |
32839 | 351 |
let |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
352 |
val (i_raw,j_raw) = dimensions m |
32839 | 353 |
val i = min max_size i_raw |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
354 |
val j = min max_size j_raw |
32839 | 355 |
val rstr = map (fn k => string_of_vector j j (row k m)) (1 upto i) |
32830 | 356 |
in "["^ space_implode ";\n " rstr ^ |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
357 |
(if j > max_size then "\n ...]" else "]") |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
358 |
end; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
359 |
|
32839 | 360 |
fun string_of_term t = |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
361 |
case t of |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
362 |
a$b => "("^(string_of_term a)^" "^(string_of_term b)^")" |
32839 | 363 |
| Abs x => |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
364 |
let val (xn, b) = Term.dest_abs x |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
365 |
in "(\\"^xn^"."^(string_of_term b)^")" |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
366 |
end |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
367 |
| Const(s,_) => s |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
368 |
| Free (s,_) => s |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
369 |
| Var((s,_),_) => s |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
370 |
| _ => error "string_of_term"; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
371 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
372 |
val string_of_cterm = string_of_term o term_of; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
373 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
374 |
fun string_of_varpow x k = |
32839 | 375 |
if k = 1 then string_of_cterm x |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
376 |
else string_of_cterm x^"^"^string_of_int k; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
377 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
378 |
fun string_of_monomial m = |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
379 |
if FuncUtil.Ctermfunc.is_empty m then "1" else |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
380 |
let val vps = fold_rev (fn (x,k) => fn a => string_of_varpow x k :: a) |
32839 | 381 |
(sort humanorder_varpow (FuncUtil.Ctermfunc.dest m)) [] |
32830 | 382 |
in space_implode "*" vps |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
383 |
end; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
384 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
385 |
fun string_of_cmonomial (c,m) = |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
386 |
if FuncUtil.Ctermfunc.is_empty m then Rat.string_of_rat c |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
387 |
else if c =/ rat_1 then string_of_monomial m |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
388 |
else Rat.string_of_rat c ^ "*" ^ string_of_monomial m;; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
389 |
|
32645
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32332
diff
changeset
|
390 |
fun string_of_poly p = |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
391 |
if FuncUtil.Monomialfunc.is_empty p then "<<0>>" else |
32839 | 392 |
let |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
393 |
val cms = sort (fn ((m1,_),(m2,_)) => humanorder_monomial m1 m2) (FuncUtil.Monomialfunc.dest p) |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
394 |
val s = fold (fn (m,c) => fn a => |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
395 |
if c </ rat_0 then a ^ " - " ^ string_of_cmonomial(Rat.neg c,m) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
396 |
else a ^ " + " ^ string_of_cmonomial(c,m)) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
397 |
cms "" |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
398 |
val s1 = String.substring (s, 0, 3) |
32839 | 399 |
val s2 = String.substring (s, 3, String.size s - 3) |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
400 |
in "<<" ^(if s1 = " + " then s2 else "-"^s2)^">>" |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
401 |
end; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
402 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
403 |
(* Conversion from HOL term. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
404 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
405 |
local |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
406 |
val neg_tm = @{cterm "uminus :: real => _"} |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
407 |
val add_tm = @{cterm "op + :: real => _"} |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
408 |
val sub_tm = @{cterm "op - :: real => _"} |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
409 |
val mul_tm = @{cterm "op * :: real => _"} |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
410 |
val inv_tm = @{cterm "inverse :: real => _"} |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
411 |
val div_tm = @{cterm "op / :: real => _"} |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
412 |
val pow_tm = @{cterm "op ^ :: real => _"} |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
413 |
val zero_tm = @{cterm "0:: real"} |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
414 |
val is_numeral = can (HOLogic.dest_number o term_of) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
415 |
fun is_comb t = case t of _$_ => true | _ => false |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
416 |
fun poly_of_term tm = |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
417 |
if tm aconvc zero_tm then poly_0 |
32839 | 418 |
else if RealArith.is_ratconst tm |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
419 |
then poly_const(RealArith.dest_ratconst tm) |
32839 | 420 |
else |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
421 |
(let val (lop,r) = Thm.dest_comb tm |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
422 |
in if lop aconvc neg_tm then poly_neg(poly_of_term r) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
423 |
else if lop aconvc inv_tm then |
32839 | 424 |
let val p = poly_of_term r |
425 |
in if poly_isconst p |
|
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
426 |
then poly_const(Rat.inv (eval FuncUtil.Ctermfunc.empty p)) |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
427 |
else error "poly_of_term: inverse of non-constant polyomial" |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
428 |
end |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
429 |
else (let val (opr,l) = Thm.dest_comb lop |
32839 | 430 |
in |
431 |
if opr aconvc pow_tm andalso is_numeral r |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
432 |
then poly_pow (poly_of_term l) ((snd o HOLogic.dest_number o term_of) r) |
32839 | 433 |
else if opr aconvc add_tm |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
434 |
then poly_add (poly_of_term l) (poly_of_term r) |
32839 | 435 |
else if opr aconvc sub_tm |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
436 |
then poly_sub (poly_of_term l) (poly_of_term r) |
32839 | 437 |
else if opr aconvc mul_tm |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
438 |
then poly_mul (poly_of_term l) (poly_of_term r) |
32839 | 439 |
else if opr aconvc div_tm |
440 |
then let |
|
441 |
val p = poly_of_term l |
|
442 |
val q = poly_of_term r |
|
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
443 |
in if poly_isconst q then poly_cmul (Rat.inv (eval FuncUtil.Ctermfunc.empty q)) p |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
444 |
else error "poly_of_term: division by non-constant polynomial" |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
445 |
end |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
446 |
else poly_var tm |
32839 | 447 |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
448 |
end |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
449 |
handle CTERM ("dest_comb",_) => poly_var tm) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
450 |
end |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
451 |
handle CTERM ("dest_comb",_) => poly_var tm) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
452 |
in |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
453 |
val poly_of_term = fn tm => |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
454 |
if type_of (term_of tm) = @{typ real} then poly_of_term tm |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
455 |
else error "poly_of_term: term does not have real type" |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
456 |
end; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
457 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
458 |
(* String of vector (just a list of space-separated numbers). *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
459 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
460 |
fun sdpa_of_vector (v:vector) = |
32839 | 461 |
let |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
462 |
val n = dim v |
32839 | 463 |
val strs = map (decimalize 20 o (fn i => FuncUtil.Intfunc.tryapplyd (snd v) i rat_0)) (1 upto n) |
32830 | 464 |
in space_implode " " strs ^ "\n" |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
465 |
end; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
466 |
|
32839 | 467 |
fun triple_int_ord ((a,b,c),(a',b',c')) = |
468 |
prod_ord int_ord (prod_ord int_ord int_ord) |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
469 |
((a,(b,c)),(a',(b',c'))); |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
470 |
structure Inttriplefunc = FuncFun(type key = int*int*int val ord = triple_int_ord); |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
471 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
472 |
(* String for block diagonal matrix numbered k. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
473 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
474 |
fun sdpa_of_blockdiagonal k m = |
32839 | 475 |
let |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
476 |
val pfx = string_of_int k ^" " |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
477 |
val ents = |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
478 |
Inttriplefunc.fold (fn ((b,i,j), c) => fn a => if i > j then a else ((b,i,j),c)::a) m [] |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
479 |
val entss = sort (triple_int_ord o pairself fst) ents |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
480 |
in fold_rev (fn ((b,i,j),c) => fn a => |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
481 |
pfx ^ string_of_int b ^ " " ^ string_of_int i ^ " " ^ string_of_int j ^ |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
482 |
" " ^ decimalize 20 c ^ "\n" ^ a) entss "" |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
483 |
end; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
484 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
485 |
(* String for a matrix numbered k, in SDPA sparse format. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
486 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
487 |
fun sdpa_of_matrix k (m:matrix) = |
32839 | 488 |
let |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
489 |
val pfx = string_of_int k ^ " 1 " |
32839 | 490 |
val ms = FuncUtil.Intpairfunc.fold (fn ((i,j), c) => fn a => if i > j then a else ((i,j),c)::a) (snd m) [] |
491 |
val mss = sort ((prod_ord int_ord int_ord) o pairself fst) ms |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
492 |
in fold_rev (fn ((i,j),c) => fn a => |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
493 |
pfx ^ string_of_int i ^ " " ^ string_of_int j ^ |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
494 |
" " ^ decimalize 20 c ^ "\n" ^ a) mss "" |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
495 |
end;; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
496 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
497 |
(* ------------------------------------------------------------------------- *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
498 |
(* String in SDPA sparse format for standard SDP problem: *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
499 |
(* *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
500 |
(* X = v_1 * [M_1] + ... + v_m * [M_m] - [M_0] must be PSD *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
501 |
(* Minimize obj_1 * v_1 + ... obj_m * v_m *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
502 |
(* ------------------------------------------------------------------------- *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
503 |
|
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
32150
diff
changeset
|
504 |
fun sdpa_of_problem obj mats = |
32839 | 505 |
let |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
506 |
val m = length mats - 1 |
32839 | 507 |
val (n,_) = dimensions (hd mats) |
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
32150
diff
changeset
|
508 |
in |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
509 |
string_of_int m ^ "\n" ^ |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
510 |
"1\n" ^ |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
511 |
string_of_int n ^ "\n" ^ |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
512 |
sdpa_of_vector obj ^ |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
513 |
fold_rev2 (fn k => fn m => fn a => sdpa_of_matrix (k - 1) m ^ a) (1 upto length mats) mats "" |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
514 |
end; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
515 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
516 |
fun index_char str chr pos = |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
517 |
if pos >= String.size str then ~1 |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
518 |
else if String.sub(str,pos) = chr then pos |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
519 |
else index_char str chr (pos + 1); |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
520 |
fun rat_of_quotient (a,b) = if b = 0 then rat_0 else Rat.rat_of_quotient (a,b); |
32839 | 521 |
fun rat_of_string s = |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
522 |
let val n = index_char s #"/" 0 in |
33035 | 523 |
if n = ~1 then s |> Int.fromString |> the |> Rat.rat_of_int |
32839 | 524 |
else |
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32839
diff
changeset
|
525 |
let val SOME numer = Int.fromString(String.substring(s,0,n)) |
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32839
diff
changeset
|
526 |
val SOME den = Int.fromString (String.substring(s,n+1,String.size s - n - 1)) |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
527 |
in rat_of_quotient(numer, den) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
528 |
end |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
529 |
end; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
530 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
531 |
fun isspace x = x = " " ; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
532 |
fun isnum x = x mem_string ["0","1","2","3","4","5","6","7","8","9"] |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
533 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
534 |
(* More parser basics. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
535 |
|
32828 | 536 |
val word = Scan.this_string |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
537 |
fun token s = |
32828 | 538 |
Scan.repeat ($$ " ") |-- word s --| Scan.repeat ($$ " ") |
539 |
val numeral = Scan.one isnum |
|
33067
a70d5baa53ee
use plain Scan.repeat (NB: Scan.bulk is for cascading sources -- mostly interna use);
wenzelm
parents:
33042
diff
changeset
|
540 |
val decimalint = Scan.repeat numeral >> (rat_of_string o implode) |
a70d5baa53ee
use plain Scan.repeat (NB: Scan.bulk is for cascading sources -- mostly interna use);
wenzelm
parents:
33042
diff
changeset
|
541 |
val decimalfrac = Scan.repeat numeral |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
542 |
>> (fn s => rat_of_string(implode s) // pow10 (length s)) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
543 |
val decimalsig = |
32828 | 544 |
decimalint -- Scan.option (Scan.$$ "." |-- decimalfrac) |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
545 |
>> (fn (h,NONE) => h | (h,SOME x) => h +/ x) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
546 |
fun signed prs = |
32839 | 547 |
$$ "-" |-- prs >> Rat.neg |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
548 |
|| $$ "+" |-- prs |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
549 |
|| prs; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
550 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
551 |
fun emptyin def xs = if null xs then (def,xs) else Scan.fail xs |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
552 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
553 |
val exponent = ($$ "e" || $$ "E") |-- signed decimalint; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
554 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
555 |
val decimal = signed decimalsig -- (emptyin rat_0|| exponent) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
556 |
>> (fn (h, x) => h */ pow10 (int_of_rat x)); |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
557 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
558 |
fun mkparser p s = |
32839 | 559 |
let val (x,rst) = p (explode s) |
560 |
in if null rst then x |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
561 |
else error "mkparser: unparsed input" |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
562 |
end;; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
563 |
|
32332 | 564 |
(* Parse back csdp output. *) |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
565 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
566 |
fun ignore inp = ((),[]) |
33067
a70d5baa53ee
use plain Scan.repeat (NB: Scan.bulk is for cascading sources -- mostly interna use);
wenzelm
parents:
33042
diff
changeset
|
567 |
fun csdpoutput inp = |
a70d5baa53ee
use plain Scan.repeat (NB: Scan.bulk is for cascading sources -- mostly interna use);
wenzelm
parents:
33042
diff
changeset
|
568 |
((decimal -- Scan.repeat (Scan.$$ " " |-- Scan.option decimal) >> |
a70d5baa53ee
use plain Scan.repeat (NB: Scan.bulk is for cascading sources -- mostly interna use);
wenzelm
parents:
33042
diff
changeset
|
569 |
(fn (h,to) => map_filter I ((SOME h)::to))) --| ignore >> vector_of_list) inp |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
570 |
val parse_csdpoutput = mkparser csdpoutput |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
571 |
|
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
32150
diff
changeset
|
572 |
(* Run prover on a problem in linear form. *) |
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
32150
diff
changeset
|
573 |
|
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
32150
diff
changeset
|
574 |
fun run_problem prover obj mats = |
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
32150
diff
changeset
|
575 |
parse_csdpoutput (prover (sdpa_of_problem obj mats)) |
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
32150
diff
changeset
|
576 |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
577 |
(* Try some apparently sensible scaling first. Note that this is purely to *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
578 |
(* get a cleaner translation to floating-point, and doesn't affect any of *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
579 |
(* the results, in principle. In practice it seems a lot better when there *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
580 |
(* are extreme numbers in the original problem. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
581 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
582 |
(* Version for (int*int) keys *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
583 |
local |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
584 |
fun max_rat x y = if x </ y then y else x |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
585 |
fun common_denominator fld amat acc = |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
586 |
fld (fn (m,c) => fn a => lcm_rat (denominator_rat c) a) amat acc |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
587 |
fun maximal_element fld amat acc = |
32839 | 588 |
fld (fn (m,c) => fn maxa => max_rat maxa (abs_rat c)) amat acc |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
589 |
fun float_of_rat x = let val (a,b) = Rat.quotient_of_rat x |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
590 |
in Real.fromLargeInt a / Real.fromLargeInt b end; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
591 |
in |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
592 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
593 |
fun pi_scale_then solver (obj:vector) mats = |
32839 | 594 |
let |
32828 | 595 |
val cd1 = fold_rev (common_denominator FuncUtil.Intpairfunc.fold) mats (rat_1) |
32839 | 596 |
val cd2 = common_denominator FuncUtil.Intfunc.fold (snd obj) (rat_1) |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
597 |
val mats' = map (FuncUtil.Intpairfunc.map (fn x => cd1 */ x)) mats |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
598 |
val obj' = vector_cmul cd2 obj |
32828 | 599 |
val max1 = fold_rev (maximal_element FuncUtil.Intpairfunc.fold) mats' (rat_0) |
32839 | 600 |
val max2 = maximal_element FuncUtil.Intfunc.fold (snd obj') (rat_0) |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
601 |
val scal1 = pow2 (20 - trunc(Math.ln (float_of_rat max1) / Math.ln 2.0)) |
32839 | 602 |
val scal2 = pow2 (20 - trunc(Math.ln (float_of_rat max2) / Math.ln 2.0)) |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
603 |
val mats'' = map (FuncUtil.Intpairfunc.map (fn x => x */ scal1)) mats' |
32839 | 604 |
val obj'' = vector_cmul scal2 obj' |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
605 |
in solver obj'' mats'' |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
606 |
end |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
607 |
end; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
608 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
609 |
(* Try some apparently sensible scaling first. Note that this is purely to *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
610 |
(* get a cleaner translation to floating-point, and doesn't affect any of *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
611 |
(* the results, in principle. In practice it seems a lot better when there *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
612 |
(* are extreme numbers in the original problem. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
613 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
614 |
(* Version for (int*int*int) keys *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
615 |
local |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
616 |
fun max_rat x y = if x </ y then y else x |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
617 |
fun common_denominator fld amat acc = |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
618 |
fld (fn (m,c) => fn a => lcm_rat (denominator_rat c) a) amat acc |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
619 |
fun maximal_element fld amat acc = |
32839 | 620 |
fld (fn (m,c) => fn maxa => max_rat maxa (abs_rat c)) amat acc |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
621 |
fun float_of_rat x = let val (a,b) = Rat.quotient_of_rat x |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
622 |
in Real.fromLargeInt a / Real.fromLargeInt b end; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
623 |
fun int_of_float x = (trunc x handle Overflow => 0 | Domain => 0) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
624 |
in |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
625 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
626 |
fun tri_scale_then solver (obj:vector) mats = |
32839 | 627 |
let |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
628 |
val cd1 = fold_rev (common_denominator Inttriplefunc.fold) mats (rat_1) |
32839 | 629 |
val cd2 = common_denominator FuncUtil.Intfunc.fold (snd obj) (rat_1) |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
630 |
val mats' = map (Inttriplefunc.map (fn x => cd1 */ x)) mats |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
631 |
val obj' = vector_cmul cd2 obj |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
632 |
val max1 = fold_rev (maximal_element Inttriplefunc.fold) mats' (rat_0) |
32839 | 633 |
val max2 = maximal_element FuncUtil.Intfunc.fold (snd obj') (rat_0) |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
634 |
val scal1 = pow2 (20 - int_of_float(Math.ln (float_of_rat max1) / Math.ln 2.0)) |
32839 | 635 |
val scal2 = pow2 (20 - int_of_float(Math.ln (float_of_rat max2) / Math.ln 2.0)) |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
636 |
val mats'' = map (Inttriplefunc.map (fn x => x */ scal1)) mats' |
32839 | 637 |
val obj'' = vector_cmul scal2 obj' |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
638 |
in solver obj'' mats'' |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
639 |
end |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
640 |
end; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
641 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
642 |
(* Round a vector to "nice" rationals. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
643 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
644 |
fun nice_rational n x = round_rat (n */ x) // n;; |
32839 | 645 |
fun nice_vector n ((d,v) : vector) = |
646 |
(d, FuncUtil.Intfunc.fold (fn (i,c) => fn a => |
|
647 |
let val y = nice_rational n c |
|
648 |
in if c =/ rat_0 then a |
|
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
649 |
else FuncUtil.Intfunc.update (i,y) a end) v FuncUtil.Intfunc.empty):vector |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
650 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
651 |
fun dest_ord f x = is_equal (f x); |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
652 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
653 |
(* Stuff for "equations" ((int*int*int)->num functions). *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
654 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
655 |
fun tri_equation_cmul c eq = |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
656 |
if c =/ rat_0 then Inttriplefunc.empty else Inttriplefunc.map (fn d => c */ d) eq; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
657 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
658 |
fun tri_equation_add eq1 eq2 = Inttriplefunc.combine (curry op +/) (fn x => x =/ rat_0) eq1 eq2; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
659 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
660 |
fun tri_equation_eval assig eq = |
32839 | 661 |
let fun value v = Inttriplefunc.apply assig v |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
662 |
in Inttriplefunc.fold (fn (v, c) => fn a => a +/ value v */ c) eq rat_0 |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
663 |
end; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
664 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
665 |
(* Eliminate among linear equations: return unconstrained variables and *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
666 |
(* assignments for the others in terms of them. We give one pseudo-variable *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
667 |
(* "one" that's used for a constant term. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
668 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
669 |
local |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
670 |
fun extract_first p l = case l of (* FIXME : use find_first instead *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
671 |
[] => error "extract_first" |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
672 |
| h::t => if p h then (h,t) else |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
673 |
let val (k,s) = extract_first p t in (k,h::s) end |
32839 | 674 |
fun eliminate vars dun eqs = case vars of |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
675 |
[] => if forall Inttriplefunc.is_empty eqs then dun |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
676 |
else raise Unsolvable |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
677 |
| v::vs => |
32839 | 678 |
((let |
679 |
val (eq,oeqs) = extract_first (fn e => Inttriplefunc.defined e v) eqs |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
680 |
val a = Inttriplefunc.apply eq v |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
681 |
val eq' = tri_equation_cmul ((Rat.neg rat_1) // a) (Inttriplefunc.delete_safe v eq) |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
682 |
fun elim e = |
32839 | 683 |
let val b = Inttriplefunc.tryapplyd e v rat_0 |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
684 |
in if b =/ rat_0 then e else |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
685 |
tri_equation_add e (tri_equation_cmul (Rat.neg b // a) eq) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
686 |
end |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
687 |
in eliminate vs (Inttriplefunc.update (v,eq') (Inttriplefunc.map elim dun)) (map elim oeqs) |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
688 |
end) |
32332 | 689 |
handle Failure _ => eliminate vs dun eqs) |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
690 |
in |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
691 |
fun tri_eliminate_equations one vars eqs = |
32839 | 692 |
let |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
693 |
val assig = eliminate vars Inttriplefunc.empty eqs |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
694 |
val vs = Inttriplefunc.fold (fn (x, f) => fn a => remove (dest_ord triple_int_ord) one (Inttriplefunc.dom f) @ a) assig [] |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
695 |
in (distinct (dest_ord triple_int_ord) vs, assig) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
696 |
end |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
697 |
end; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
698 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
699 |
(* Eliminate all variables, in an essentially arbitrary order. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
700 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
701 |
fun tri_eliminate_all_equations one = |
32839 | 702 |
let |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
703 |
fun choose_variable eq = |
32839 | 704 |
let val (v,_) = Inttriplefunc.choose eq |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
705 |
in if is_equal (triple_int_ord(v,one)) then |
32839 | 706 |
let val eq' = Inttriplefunc.delete_safe v eq |
707 |
in if Inttriplefunc.is_empty eq' then error "choose_variable" |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
708 |
else fst (Inttriplefunc.choose eq') |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
709 |
end |
32839 | 710 |
else v |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
711 |
end |
32839 | 712 |
fun eliminate dun eqs = case eqs of |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
713 |
[] => dun |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
714 |
| eq::oeqs => |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
715 |
if Inttriplefunc.is_empty eq then eliminate dun oeqs else |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
716 |
let val v = choose_variable eq |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
717 |
val a = Inttriplefunc.apply eq v |
32839 | 718 |
val eq' = tri_equation_cmul ((Rat.rat_of_int ~1) // a) |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
719 |
(Inttriplefunc.delete_safe v eq) |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
720 |
fun elim e = |
32839 | 721 |
let val b = Inttriplefunc.tryapplyd e v rat_0 |
722 |
in if b =/ rat_0 then e |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
723 |
else tri_equation_add e (tri_equation_cmul (Rat.neg b // a) eq) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
724 |
end |
32839 | 725 |
in eliminate (Inttriplefunc.update(v, eq') (Inttriplefunc.map elim dun)) |
726 |
(map elim oeqs) |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
727 |
end |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
728 |
in fn eqs => |
32839 | 729 |
let |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
730 |
val assig = eliminate Inttriplefunc.empty eqs |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
731 |
val vs = Inttriplefunc.fold (fn (x, f) => fn a => remove (dest_ord triple_int_ord) one (Inttriplefunc.dom f) @ a) assig [] |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
732 |
in (distinct (dest_ord triple_int_ord) vs,assig) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
733 |
end |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
734 |
end; |
32839 | 735 |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
736 |
(* Solve equations by assigning arbitrary numbers. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
737 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
738 |
fun tri_solve_equations one eqs = |
32839 | 739 |
let |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
740 |
val (vars,assigs) = tri_eliminate_all_equations one eqs |
32839 | 741 |
val vfn = fold_rev (fn v => Inttriplefunc.update(v,rat_0)) vars |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
742 |
(Inttriplefunc.onefunc(one, Rat.rat_of_int ~1)) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
743 |
val ass = |
32839 | 744 |
Inttriplefunc.combine (curry op +/) (K false) |
745 |
(Inttriplefunc.map (tri_equation_eval vfn) assigs) vfn |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
746 |
in if forall (fn e => tri_equation_eval ass e =/ rat_0) eqs |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
747 |
then Inttriplefunc.delete_safe one ass else raise Sanity |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
748 |
end; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
749 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
750 |
(* Multiply equation-parametrized poly by regular poly and add accumulator. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
751 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
752 |
fun tri_epoly_pmul p q acc = |
32828 | 753 |
FuncUtil.Monomialfunc.fold (fn (m1, c) => fn a => |
754 |
FuncUtil.Monomialfunc.fold (fn (m2,e) => fn b => |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
755 |
let val m = monomial_mul m1 m2 |
32839 | 756 |
val es = FuncUtil.Monomialfunc.tryapplyd b m Inttriplefunc.empty |
757 |
in FuncUtil.Monomialfunc.update (m,tri_equation_add (tri_equation_cmul c e) es) b |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
758 |
end) q a) p acc ; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
759 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
760 |
(* Usual operations on equation-parametrized poly. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
761 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
762 |
fun tri_epoly_cmul c l = |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
763 |
if c =/ rat_0 then Inttriplefunc.empty else Inttriplefunc.map (tri_equation_cmul c) l;; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
764 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
765 |
val tri_epoly_neg = tri_epoly_cmul (Rat.rat_of_int ~1); |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
766 |
|
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
767 |
val tri_epoly_add = Inttriplefunc.combine tri_equation_add Inttriplefunc.is_empty; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
768 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
769 |
fun tri_epoly_sub p q = tri_epoly_add p (tri_epoly_neg q);; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
770 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
771 |
(* Stuff for "equations" ((int*int)->num functions). *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
772 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
773 |
fun pi_equation_cmul c eq = |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
774 |
if c =/ rat_0 then Inttriplefunc.empty else Inttriplefunc.map (fn d => c */ d) eq; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
775 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
776 |
fun pi_equation_add eq1 eq2 = Inttriplefunc.combine (curry op +/) (fn x => x =/ rat_0) eq1 eq2; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
777 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
778 |
fun pi_equation_eval assig eq = |
32839 | 779 |
let fun value v = Inttriplefunc.apply assig v |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
780 |
in Inttriplefunc.fold (fn (v, c) => fn a => a +/ value v */ c) eq rat_0 |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
781 |
end; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
782 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
783 |
(* Eliminate among linear equations: return unconstrained variables and *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
784 |
(* assignments for the others in terms of them. We give one pseudo-variable *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
785 |
(* "one" that's used for a constant term. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
786 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
787 |
local |
32839 | 788 |
fun extract_first p l = case l of |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
789 |
[] => error "extract_first" |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
790 |
| h::t => if p h then (h,t) else |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
791 |
let val (k,s) = extract_first p t in (k,h::s) end |
32839 | 792 |
fun eliminate vars dun eqs = case vars of |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
793 |
[] => if forall Inttriplefunc.is_empty eqs then dun |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
794 |
else raise Unsolvable |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
795 |
| v::vs => |
32839 | 796 |
let |
797 |
val (eq,oeqs) = extract_first (fn e => Inttriplefunc.defined e v) eqs |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
798 |
val a = Inttriplefunc.apply eq v |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
799 |
val eq' = pi_equation_cmul ((Rat.neg rat_1) // a) (Inttriplefunc.delete_safe v eq) |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
800 |
fun elim e = |
32839 | 801 |
let val b = Inttriplefunc.tryapplyd e v rat_0 |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
802 |
in if b =/ rat_0 then e else |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
803 |
pi_equation_add e (pi_equation_cmul (Rat.neg b // a) eq) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
804 |
end |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
805 |
in eliminate vs (Inttriplefunc.update (v,eq') (Inttriplefunc.map elim dun)) (map elim oeqs) |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
806 |
end |
32332 | 807 |
handle Failure _ => eliminate vs dun eqs |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
808 |
in |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
809 |
fun pi_eliminate_equations one vars eqs = |
32839 | 810 |
let |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
811 |
val assig = eliminate vars Inttriplefunc.empty eqs |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
812 |
val vs = Inttriplefunc.fold (fn (x, f) => fn a => remove (dest_ord triple_int_ord) one (Inttriplefunc.dom f) @ a) assig [] |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
813 |
in (distinct (dest_ord triple_int_ord) vs, assig) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
814 |
end |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
815 |
end; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
816 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
817 |
(* Eliminate all variables, in an essentially arbitrary order. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
818 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
819 |
fun pi_eliminate_all_equations one = |
32839 | 820 |
let |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
821 |
fun choose_variable eq = |
32839 | 822 |
let val (v,_) = Inttriplefunc.choose eq |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
823 |
in if is_equal (triple_int_ord(v,one)) then |
32839 | 824 |
let val eq' = Inttriplefunc.delete_safe v eq |
825 |
in if Inttriplefunc.is_empty eq' then error "choose_variable" |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
826 |
else fst (Inttriplefunc.choose eq') |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
827 |
end |
32839 | 828 |
else v |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
829 |
end |
32839 | 830 |
fun eliminate dun eqs = case eqs of |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
831 |
[] => dun |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
832 |
| eq::oeqs => |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
833 |
if Inttriplefunc.is_empty eq then eliminate dun oeqs else |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
834 |
let val v = choose_variable eq |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
835 |
val a = Inttriplefunc.apply eq v |
32839 | 836 |
val eq' = pi_equation_cmul ((Rat.rat_of_int ~1) // a) |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
837 |
(Inttriplefunc.delete_safe v eq) |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
838 |
fun elim e = |
32839 | 839 |
let val b = Inttriplefunc.tryapplyd e v rat_0 |
840 |
in if b =/ rat_0 then e |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
841 |
else pi_equation_add e (pi_equation_cmul (Rat.neg b // a) eq) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
842 |
end |
32839 | 843 |
in eliminate (Inttriplefunc.update(v, eq') (Inttriplefunc.map elim dun)) |
844 |
(map elim oeqs) |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
845 |
end |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
846 |
in fn eqs => |
32839 | 847 |
let |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
848 |
val assig = eliminate Inttriplefunc.empty eqs |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
849 |
val vs = Inttriplefunc.fold (fn (x, f) => fn a => remove (dest_ord triple_int_ord) one (Inttriplefunc.dom f) @ a) assig [] |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
850 |
in (distinct (dest_ord triple_int_ord) vs,assig) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
851 |
end |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
852 |
end; |
32839 | 853 |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
854 |
(* Solve equations by assigning arbitrary numbers. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
855 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
856 |
fun pi_solve_equations one eqs = |
32839 | 857 |
let |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
858 |
val (vars,assigs) = pi_eliminate_all_equations one eqs |
32839 | 859 |
val vfn = fold_rev (fn v => Inttriplefunc.update(v,rat_0)) vars |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
860 |
(Inttriplefunc.onefunc(one, Rat.rat_of_int ~1)) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
861 |
val ass = |
32839 | 862 |
Inttriplefunc.combine (curry op +/) (K false) |
863 |
(Inttriplefunc.map (pi_equation_eval vfn) assigs) vfn |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
864 |
in if forall (fn e => pi_equation_eval ass e =/ rat_0) eqs |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
865 |
then Inttriplefunc.delete_safe one ass else raise Sanity |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
866 |
end; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
867 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
868 |
(* Multiply equation-parametrized poly by regular poly and add accumulator. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
869 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
870 |
fun pi_epoly_pmul p q acc = |
32828 | 871 |
FuncUtil.Monomialfunc.fold (fn (m1, c) => fn a => |
872 |
FuncUtil.Monomialfunc.fold (fn (m2,e) => fn b => |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
873 |
let val m = monomial_mul m1 m2 |
32839 | 874 |
val es = FuncUtil.Monomialfunc.tryapplyd b m Inttriplefunc.empty |
875 |
in FuncUtil.Monomialfunc.update (m,pi_equation_add (pi_equation_cmul c e) es) b |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
876 |
end) q a) p acc ; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
877 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
878 |
(* Usual operations on equation-parametrized poly. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
879 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
880 |
fun pi_epoly_cmul c l = |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
881 |
if c =/ rat_0 then Inttriplefunc.empty else Inttriplefunc.map (pi_equation_cmul c) l;; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
882 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
883 |
val pi_epoly_neg = pi_epoly_cmul (Rat.rat_of_int ~1); |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
884 |
|
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
885 |
val pi_epoly_add = Inttriplefunc.combine pi_equation_add Inttriplefunc.is_empty; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
886 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
887 |
fun pi_epoly_sub p q = pi_epoly_add p (pi_epoly_neg q);; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
888 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
889 |
fun allpairs f l1 l2 = fold_rev (fn x => (curry (op @)) (map (f x) l2)) l1 []; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
890 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
891 |
(* Hence produce the "relevant" monomials: those whose squares lie in the *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
892 |
(* Newton polytope of the monomials in the input. (This is enough according *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
893 |
(* to Reznik: "Extremal PSD forms with few terms", Duke Math. Journal, *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
894 |
(* vol 45, pp. 363--374, 1978. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
895 |
(* *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
896 |
(* These are ordered in sort of decreasing degree. In particular the *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
897 |
(* constant monomial is last; this gives an order in diagonalization of the *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
898 |
(* quadratic form that will tend to display constants. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
899 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
900 |
(* Diagonalize (Cholesky/LDU) the matrix corresponding to a quadratic form. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
901 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
902 |
local |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
903 |
fun diagonalize n i m = |
32839 | 904 |
if FuncUtil.Intpairfunc.is_empty (snd m) then [] |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
905 |
else |
32839 | 906 |
let val a11 = FuncUtil.Intpairfunc.tryapplyd (snd m) (i,i) rat_0 |
32332 | 907 |
in if a11 </ rat_0 then raise Failure "diagonalize: not PSD" |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
908 |
else if a11 =/ rat_0 then |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
909 |
if FuncUtil.Intfunc.is_empty (snd (row i m)) then diagonalize n (i + 1) m |
32332 | 910 |
else raise Failure "diagonalize: not PSD ___ " |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
911 |
else |
32839 | 912 |
let |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
913 |
val v = row i m |
32839 | 914 |
val v' = (fst v, FuncUtil.Intfunc.fold (fn (i, c) => fn a => |
915 |
let val y = c // a11 |
|
916 |
in if y = rat_0 then a else FuncUtil.Intfunc.update (i,y) a |
|
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
917 |
end) (snd v) FuncUtil.Intfunc.empty) |
32828 | 918 |
fun upt0 x y a = if y = rat_0 then a else FuncUtil.Intpairfunc.update (x,y) a |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
919 |
val m' = |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
920 |
((n,n), |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
921 |
iter (i+1,n) (fn j => |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
922 |
iter (i+1,n) (fn k => |
32828 | 923 |
(upt0 (j,k) (FuncUtil.Intpairfunc.tryapplyd (snd m) (j,k) rat_0 -/ FuncUtil.Intfunc.tryapplyd (snd v) j rat_0 */ FuncUtil.Intfunc.tryapplyd (snd v') k rat_0)))) |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
924 |
FuncUtil.Intpairfunc.empty) |
32839 | 925 |
in (a11,v')::diagonalize n (i + 1) m' |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
926 |
end |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
927 |
end |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
928 |
in |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
929 |
fun diag m = |
32839 | 930 |
let |
931 |
val nn = dimensions m |
|
932 |
val n = fst nn |
|
933 |
in if snd nn <> n then error "diagonalize: non-square matrix" |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
934 |
else diagonalize n 1 m |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
935 |
end |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
936 |
end; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
937 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
938 |
fun gcd_rat a b = Rat.rat_of_int (Integer.gcd (int_of_rat a) (int_of_rat b)); |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
939 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
940 |
(* Adjust a diagonalization to collect rationals at the start. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
941 |
(* FIXME : Potentially polymorphic keys, but here only: integers!! *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
942 |
local |
32828 | 943 |
fun upd0 x y a = if y =/ rat_0 then a else FuncUtil.Intfunc.update(x,y) a; |
32839 | 944 |
fun mapa f (d,v) = |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
945 |
(d, FuncUtil.Intfunc.fold (fn (i,c) => fn a => upd0 i (f c) a) v FuncUtil.Intfunc.empty) |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
946 |
fun adj (c,l) = |
32839 | 947 |
let val a = |
948 |
FuncUtil.Intfunc.fold (fn (i,c) => fn a => lcm_rat a (denominator_rat c)) |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
949 |
(snd l) rat_1 // |
32839 | 950 |
FuncUtil.Intfunc.fold (fn (i,c) => fn a => gcd_rat a (numerator_rat c)) |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
951 |
(snd l) rat_0 |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
952 |
in ((c // (a */ a)),mapa (fn x => a */ x) l) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
953 |
end |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
954 |
in |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
955 |
fun deration d = if null d then (rat_0,d) else |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
956 |
let val d' = map adj d |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
957 |
val a = fold (lcm_rat o denominator_rat o fst) d' rat_1 // |
32839 | 958 |
fold (gcd_rat o numerator_rat o fst) d' rat_0 |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
959 |
in ((rat_1 // a),map (fn (c,l) => (a */ c,l)) d') |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
960 |
end |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
961 |
end; |
32839 | 962 |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
963 |
(* Enumeration of monomials with given multidegree bound. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
964 |
|
32839 | 965 |
fun enumerate_monomials d vars = |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
966 |
if d < 0 then [] |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
967 |
else if d = 0 then [FuncUtil.Ctermfunc.empty] |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
968 |
else if null vars then [monomial_1] else |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
969 |
let val alts = |
33063 | 970 |
map_range (fn k => let val oths = enumerate_monomials (d - k) (tl vars) |
971 |
in map (fn ks => if k = 0 then ks else FuncUtil.Ctermfunc.update (hd vars, k) ks) oths end) (d + 1) |
|
32830 | 972 |
in flat alts |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
973 |
end; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
974 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
975 |
(* Enumerate products of distinct input polys with degree <= d. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
976 |
(* We ignore any constant input polynomials. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
977 |
(* Give the output polynomial and a record of how it was derived. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
978 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
979 |
fun enumerate_products d pols = |
32839 | 980 |
if d = 0 then [(poly_const rat_1,RealArith.Rational_lt rat_1)] |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
981 |
else if d < 0 then [] else |
32839 | 982 |
case pols of |
32828 | 983 |
[] => [(poly_const rat_1,RealArith.Rational_lt rat_1)] |
32839 | 984 |
| (p,b)::ps => |
985 |
let val e = multidegree p |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
986 |
in if e = 0 then enumerate_products d ps else |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
987 |
enumerate_products d ps @ |
32828 | 988 |
map (fn (q,c) => (poly_mul p q,RealArith.Product(b,c))) |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
989 |
(enumerate_products (d - e) ps) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
990 |
end |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
991 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
992 |
(* Convert regular polynomial. Note that we treat (0,0,0) as -1. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
993 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
994 |
fun epoly_of_poly p = |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
995 |
FuncUtil.Monomialfunc.fold (fn (m,c) => fn a => FuncUtil.Monomialfunc.update (m, Inttriplefunc.onefunc ((0,0,0), Rat.neg c)) a) p FuncUtil.Monomialfunc.empty; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
996 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
997 |
(* String for block diagonal matrix numbered k. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
998 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
999 |
fun sdpa_of_blockdiagonal k m = |
32839 | 1000 |
let |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1001 |
val pfx = string_of_int k ^" " |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1002 |
val ents = |
32839 | 1003 |
Inttriplefunc.fold |
1004 |
(fn ((b,i,j),c) => fn a => if i > j then a else ((b,i,j),c)::a) |
|
1005 |
m [] |
|
1006 |
val entss = sort (triple_int_ord o pairself fst) ents |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1007 |
in fold_rev (fn ((b,i,j),c) => fn a => |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1008 |
pfx ^ string_of_int b ^ " " ^ string_of_int i ^ " " ^ string_of_int j ^ |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1009 |
" " ^ decimalize 20 c ^ "\n" ^ a) entss "" |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1010 |
end; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1011 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1012 |
(* SDPA for problem using block diagonal (i.e. multiple SDPs) *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1013 |
|
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
32150
diff
changeset
|
1014 |
fun sdpa_of_blockproblem nblocks blocksizes obj mats = |
32839 | 1015 |
let val m = length mats - 1 |
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
32150
diff
changeset
|
1016 |
in |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1017 |
string_of_int m ^ "\n" ^ |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1018 |
string_of_int nblocks ^ "\n" ^ |
32830 | 1019 |
(space_implode " " (map string_of_int blocksizes)) ^ |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1020 |
"\n" ^ |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1021 |
sdpa_of_vector obj ^ |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1022 |
fold_rev2 (fn k => fn m => fn a => sdpa_of_blockdiagonal (k - 1) m ^ a) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1023 |
(1 upto length mats) mats "" |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1024 |
end; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1025 |
|
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
32150
diff
changeset
|
1026 |
(* Run prover on a problem in block diagonal form. *) |
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
32150
diff
changeset
|
1027 |
|
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
32150
diff
changeset
|
1028 |
fun run_blockproblem prover nblocks blocksizes obj mats= |
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
32150
diff
changeset
|
1029 |
parse_csdpoutput (prover (sdpa_of_blockproblem nblocks blocksizes obj mats)) |
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
32150
diff
changeset
|
1030 |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1031 |
(* 3D versions of matrix operations to consider blocks separately. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1032 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1033 |
val bmatrix_add = Inttriplefunc.combine (curry op +/) (fn x => x =/ rat_0); |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1034 |
fun bmatrix_cmul c bm = |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
1035 |
if c =/ rat_0 then Inttriplefunc.empty |
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
1036 |
else Inttriplefunc.map (fn x => c */ x) bm; |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1037 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1038 |
val bmatrix_neg = bmatrix_cmul (Rat.rat_of_int ~1); |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1039 |
fun bmatrix_sub m1 m2 = bmatrix_add m1 (bmatrix_neg m2);; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1040 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1041 |
(* Smash a block matrix into components. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1042 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1043 |
fun blocks blocksizes bm = |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1044 |
map (fn (bs,b0) => |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1045 |
let val m = Inttriplefunc.fold |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
1046 |
(fn ((b,i,j),c) => fn a => if b = b0 then FuncUtil.Intpairfunc.update ((i,j),c) a else a) bm FuncUtil.Intpairfunc.empty |
32839 | 1047 |
val d = FuncUtil.Intpairfunc.fold (fn ((i,j),c) => fn a => max a (max i j)) m 0 |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1048 |
in (((bs,bs),m):matrix) end) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1049 |
(blocksizes ~~ (1 upto length blocksizes));; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1050 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1051 |
(* FIXME : Get rid of this !!!*) |
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
32150
diff
changeset
|
1052 |
local |
32332 | 1053 |
fun tryfind_with msg f [] = raise Failure msg |
1054 |
| tryfind_with msg f (x::xs) = (f x handle Failure s => tryfind_with s f xs); |
|
32839 | 1055 |
in |
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
32150
diff
changeset
|
1056 |
fun tryfind f = tryfind_with "tryfind" f |
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
32150
diff
changeset
|
1057 |
end |
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
32150
diff
changeset
|
1058 |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1059 |
(* Positiv- and Nullstellensatz. Flag "linf" forces a linear representation. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1060 |
|
32839 | 1061 |
|
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
32150
diff
changeset
|
1062 |
fun real_positivnullstellensatz_general prover linf d eqs leqs pol = |
32839 | 1063 |
let |
33042 | 1064 |
val vars = fold_rev (union (op aconvc) o poly_variables) |
1065 |
(pol :: eqs @ map fst leqs) [] |
|
32839 | 1066 |
val monoid = if linf then |
32828 | 1067 |
(poly_const rat_1,RealArith.Rational_lt rat_1):: |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1068 |
(filter (fn (p,c) => multidegree p <= d) leqs) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1069 |
else enumerate_products d leqs |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1070 |
val nblocks = length monoid |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1071 |
fun mk_idmultiplier k p = |
32839 | 1072 |
let |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1073 |
val e = d - multidegree p |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1074 |
val mons = enumerate_monomials e vars |
32839 | 1075 |
val nons = mons ~~ (1 upto length mons) |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1076 |
in (mons, |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
1077 |
fold_rev (fn (m,n) => FuncUtil.Monomialfunc.update(m,Inttriplefunc.onefunc((~k,~n,n),rat_1))) nons FuncUtil.Monomialfunc.empty) |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1078 |
end |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1079 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1080 |
fun mk_sqmultiplier k (p,c) = |
32839 | 1081 |
let |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1082 |
val e = (d - multidegree p) div 2 |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1083 |
val mons = enumerate_monomials e vars |
32839 | 1084 |
val nons = mons ~~ (1 upto length mons) |
1085 |
in (mons, |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1086 |
fold_rev (fn (m1,n1) => |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1087 |
fold_rev (fn (m2,n2) => fn a => |
32839 | 1088 |
let val m = monomial_mul m1 m2 |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1089 |
in if n1 > n2 then a else |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1090 |
let val c = if n1 = n2 then rat_1 else rat_2 |
32839 | 1091 |
val e = FuncUtil.Monomialfunc.tryapplyd a m Inttriplefunc.empty |
32828 | 1092 |
in FuncUtil.Monomialfunc.update(m, tri_equation_add (Inttriplefunc.onefunc((k,n1,n2), c)) e) a |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1093 |
end |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1094 |
end) nons) |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
1095 |
nons FuncUtil.Monomialfunc.empty) |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1096 |
end |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1097 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1098 |
val (sqmonlist,sqs) = split_list (map2 mk_sqmultiplier (1 upto length monoid) monoid) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1099 |
val (idmonlist,ids) = split_list(map2 mk_idmultiplier (1 upto length eqs) eqs) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1100 |
val blocksizes = map length sqmonlist |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1101 |
val bigsum = |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1102 |
fold_rev2 (fn p => fn q => fn a => tri_epoly_pmul p q a) eqs ids |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1103 |
(fold_rev2 (fn (p,c) => fn s => fn a => tri_epoly_pmul p s a) monoid sqs |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1104 |
(epoly_of_poly(poly_neg pol))) |
32828 | 1105 |
val eqns = FuncUtil.Monomialfunc.fold (fn (m,e) => fn a => e::a) bigsum [] |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1106 |
val (pvs,assig) = tri_eliminate_all_equations (0,0,0) eqns |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1107 |
val qvars = (0,0,0)::pvs |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1108 |
val allassig = fold_rev (fn v => Inttriplefunc.update(v,(Inttriplefunc.onefunc(v,rat_1)))) pvs assig |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1109 |
fun mk_matrix v = |
32839 | 1110 |
Inttriplefunc.fold (fn ((b,i,j), ass) => fn m => |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1111 |
if b < 0 then m else |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1112 |
let val c = Inttriplefunc.tryapplyd ass v rat_0 |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1113 |
in if c = rat_0 then m else |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1114 |
Inttriplefunc.update ((b,j,i), c) (Inttriplefunc.update ((b,i,j), c) m) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1115 |
end) |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
1116 |
allassig Inttriplefunc.empty |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1117 |
val diagents = Inttriplefunc.fold |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1118 |
(fn ((b,i,j), e) => fn a => if b > 0 andalso i = j then tri_equation_add e a else a) |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
1119 |
allassig Inttriplefunc.empty |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1120 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1121 |
val mats = map mk_matrix qvars |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1122 |
val obj = (length pvs, |
32828 | 1123 |
itern 1 pvs (fn v => fn i => FuncUtil.Intfunc.updatep iszero (i,Inttriplefunc.tryapplyd diagents v rat_0)) |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
1124 |
FuncUtil.Intfunc.empty) |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1125 |
val raw_vec = if null pvs then vector_0 0 |
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
32150
diff
changeset
|
1126 |
else tri_scale_then (run_blockproblem prover nblocks blocksizes) obj mats |
32828 | 1127 |
fun int_element (d,v) i = FuncUtil.Intfunc.tryapplyd v i rat_0 |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1128 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1129 |
fun find_rounding d = |
32839 | 1130 |
let |
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32839
diff
changeset
|
1131 |
val _ = |
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32839
diff
changeset
|
1132 |
if !debugging |
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32839
diff
changeset
|
1133 |
then writeln ("Trying rounding with limit "^Rat.string_of_rat d ^ "\n") |
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32839
diff
changeset
|
1134 |
else () |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1135 |
val vec = nice_vector d raw_vec |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1136 |
val blockmat = iter (1,dim vec) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1137 |
(fn i => fn a => bmatrix_add (bmatrix_cmul (int_element vec i) (nth mats i)) a) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1138 |
(bmatrix_neg (nth mats 0)) |
32839 | 1139 |
val allmats = blocks blocksizes blockmat |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1140 |
in (vec,map diag allmats) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1141 |
end |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1142 |
val (vec,ratdias) = |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1143 |
if null pvs then find_rounding rat_1 |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1144 |
else tryfind find_rounding (map Rat.rat_of_int (1 upto 31) @ |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1145 |
map pow2 (5 upto 66)) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1146 |
val newassigs = |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1147 |
fold_rev (fn k => Inttriplefunc.update (nth pvs (k - 1), int_element vec k)) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1148 |
(1 upto dim vec) (Inttriplefunc.onefunc ((0,0,0), Rat.rat_of_int ~1)) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1149 |
val finalassigs = |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1150 |
Inttriplefunc.fold (fn (v,e) => fn a => Inttriplefunc.update(v, tri_equation_eval newassigs e) a) allassig newassigs |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1151 |
fun poly_of_epoly p = |
32828 | 1152 |
FuncUtil.Monomialfunc.fold (fn (v,e) => fn a => FuncUtil.Monomialfunc.updatep iszero (v,tri_equation_eval finalassigs e) a) |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
1153 |
p FuncUtil.Monomialfunc.empty |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1154 |
fun mk_sos mons = |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1155 |
let fun mk_sq (c,m) = |
32828 | 1156 |
(c,fold_rev (fn k=> fn a => FuncUtil.Monomialfunc.updatep iszero (nth mons (k - 1), int_element m k) a) |
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
1157 |
(1 upto length mons) FuncUtil.Monomialfunc.empty) |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1158 |
in map mk_sq |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1159 |
end |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1160 |
val sqs = map2 mk_sos sqmonlist ratdias |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1161 |
val cfs = map poly_of_epoly ids |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1162 |
val msq = filter (fn (a,b) => not (null b)) (map2 pair monoid sqs) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1163 |
fun eval_sq sqs = fold_rev (fn (c,q) => poly_add (poly_cmul c (poly_mul q q))) sqs poly_0 |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1164 |
val sanity = |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1165 |
fold_rev (fn ((p,c),s) => poly_add (poly_mul p (eval_sq s))) msq |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1166 |
(fold_rev2 (fn p => fn q => poly_add (poly_mul p q)) cfs eqs |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1167 |
(poly_neg pol)) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1168 |
|
32829
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents:
32828
diff
changeset
|
1169 |
in if not(FuncUtil.Monomialfunc.is_empty sanity) then raise Sanity else |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1170 |
(cfs,map (fn (a,b) => (snd a,b)) msq) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1171 |
end |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1172 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1173 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1174 |
(* Iterative deepening. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1175 |
|
32839 | 1176 |
fun deepen f n = |
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32839
diff
changeset
|
1177 |
(writeln ("Searching with depth limit " ^ string_of_int n); |
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32839
diff
changeset
|
1178 |
(f n handle Failure s => (writeln ("failed with message: " ^ s); deepen f (n + 1)))); |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1179 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1180 |
|
32645
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32332
diff
changeset
|
1181 |
(* Map back polynomials and their composites to a positivstellensatz. *) |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1182 |
|
32828 | 1183 |
fun cterm_of_sqterm (c,p) = RealArith.Product(RealArith.Rational_lt c,RealArith.Square p); |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1184 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1185 |
fun cterm_of_sos (pr,sqs) = if null sqs then pr |
32830 | 1186 |
else RealArith.Product(pr,foldr1 RealArith.Sum (map cterm_of_sqterm sqs)); |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1187 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1188 |
(* Interface to HOL. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1189 |
local |
32828 | 1190 |
open Conv |
1191 |
val concl = Thm.dest_arg o cprop_of |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1192 |
fun simple_cterm_ord t u = TermOrd.fast_term_ord (term_of t, term_of u) = LESS |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1193 |
in |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1194 |
(* FIXME: Replace tryfind by get_first !! *) |
32645
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32332
diff
changeset
|
1195 |
fun real_nonlinear_prover proof_method ctxt = |
32839 | 1196 |
let |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1197 |
val {add,mul,neg,pow,sub,main} = Normalizer.semiring_normalizers_ord_wrapper ctxt |
33035 | 1198 |
(the (NormalizerData.match ctxt @{cterm "(0::real) + 1"})) |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1199 |
simple_cterm_ord |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1200 |
val (real_poly_add_conv,real_poly_mul_conv,real_poly_neg_conv, |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1201 |
real_poly_pow_conv,real_poly_sub_conv,real_poly_conv) = (add,mul,neg,pow,sub,main) |
32839 | 1202 |
fun mainf cert_choice translator (eqs,les,lts) = |
1203 |
let |
|
32828 | 1204 |
val eq0 = map (poly_of_term o Thm.dest_arg1 o concl) eqs |
1205 |
val le0 = map (poly_of_term o Thm.dest_arg o concl) les |
|
1206 |
val lt0 = map (poly_of_term o Thm.dest_arg o concl) lts |
|
33063 | 1207 |
val eqp0 = map_index (fn (i, t) => (t,RealArith.Axiom_eq i)) eq0 |
1208 |
val lep0 = map_index (fn (i, t) => (t,RealArith.Axiom_le i)) le0 |
|
1209 |
val ltp0 = map_index (fn (i, t) => (t,RealArith.Axiom_lt i)) lt0 |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1210 |
val (keq,eq) = List.partition (fn (p,_) => multidegree p = 0) eqp0 |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1211 |
val (klep,lep) = List.partition (fn (p,_) => multidegree p = 0) lep0 |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1212 |
val (kltp,ltp) = List.partition (fn (p,_) => multidegree p = 0) ltp0 |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1213 |
fun trivial_axiom (p,ax) = |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1214 |
case ax of |
32839 | 1215 |
RealArith.Axiom_eq n => if eval FuncUtil.Ctermfunc.empty p <>/ Rat.zero then nth eqs n |
32332 | 1216 |
else raise Failure "trivial_axiom: Not a trivial axiom" |
32839 | 1217 |
| RealArith.Axiom_le n => if eval FuncUtil.Ctermfunc.empty p </ Rat.zero then nth les n |
32332 | 1218 |
else raise Failure "trivial_axiom: Not a trivial axiom" |
32839 | 1219 |
| RealArith.Axiom_lt n => if eval FuncUtil.Ctermfunc.empty p <=/ Rat.zero then nth lts n |
32332 | 1220 |
else raise Failure "trivial_axiom: Not a trivial axiom" |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1221 |
| _ => error "trivial_axiom: Not a trivial axiom" |
32839 | 1222 |
in |
32645
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32332
diff
changeset
|
1223 |
(let val th = tryfind trivial_axiom (keq @ klep @ kltp) |
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32332
diff
changeset
|
1224 |
in |
32828 | 1225 |
(fconv_rule (arg_conv (arg1_conv real_poly_conv) then_conv field_comp_conv) th, RealArith.Trivial) |
32645
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32332
diff
changeset
|
1226 |
end) |
32839 | 1227 |
handle Failure _ => |
32645
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32332
diff
changeset
|
1228 |
(let val proof = |
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32332
diff
changeset
|
1229 |
(case proof_method of Certificate certs => |
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32332
diff
changeset
|
1230 |
(* choose certificate *) |
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32332
diff
changeset
|
1231 |
let |
32828 | 1232 |
fun chose_cert [] (RealArith.Cert c) = c |
1233 |
| chose_cert (RealArith.Left::s) (RealArith.Branch (l, _)) = chose_cert s l |
|
1234 |
| chose_cert (RealArith.Right::s) (RealArith.Branch (_, r)) = chose_cert s r |
|
32645
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32332
diff
changeset
|
1235 |
| chose_cert _ _ = error "certificate tree in invalid form" |
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32332
diff
changeset
|
1236 |
in |
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32332
diff
changeset
|
1237 |
chose_cert cert_choice certs |
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32332
diff
changeset
|
1238 |
end |
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32332
diff
changeset
|
1239 |
| Prover prover => |
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32332
diff
changeset
|
1240 |
(* call prover *) |
32839 | 1241 |
let |
32645
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32332
diff
changeset
|
1242 |
val pol = fold_rev poly_mul (map fst ltp) (poly_const Rat.one) |
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32332
diff
changeset
|
1243 |
val leq = lep @ ltp |
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32332
diff
changeset
|
1244 |
fun tryall d = |
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32332
diff
changeset
|
1245 |
let val e = multidegree pol |
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32332
diff
changeset
|
1246 |
val k = if e = 0 then 0 else d div e |
32839 | 1247 |
val eq' = map fst eq |
32645
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32332
diff
changeset
|
1248 |
in tryfind (fn i => (d,i,real_positivnullstellensatz_general prover false d eq' leq |
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32332
diff
changeset
|
1249 |
(poly_neg(poly_pow pol i)))) |
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32332
diff
changeset
|
1250 |
(0 upto k) |
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32332
diff
changeset
|
1251 |
end |
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32332
diff
changeset
|
1252 |
val (d,i,(cert_ideal,cert_cone)) = deepen tryall 0 |
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32332
diff
changeset
|
1253 |
val proofs_ideal = |
32828 | 1254 |
map2 (fn q => fn (p,ax) => RealArith.Eqmul(q,ax)) cert_ideal eq |
32645
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32332
diff
changeset
|
1255 |
val proofs_cone = map cterm_of_sos cert_cone |
32828 | 1256 |
val proof_ne = if null ltp then RealArith.Rational_lt Rat.one else |
32839 | 1257 |
let val p = foldr1 RealArith.Product (map snd ltp) |
32828 | 1258 |
in funpow i (fn q => RealArith.Product(p,q)) (RealArith.Rational_lt Rat.one) |
32645
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32332
diff
changeset
|
1259 |
end |
32839 | 1260 |
in |
1261 |
foldr1 RealArith.Sum (proof_ne :: proofs_ideal @ proofs_cone) |
|
32645
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32332
diff
changeset
|
1262 |
end) |
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32332
diff
changeset
|
1263 |
in |
32828 | 1264 |
(translator (eqs,les,lts) proof, RealArith.Cert proof) |
32645
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32332
diff
changeset
|
1265 |
end) |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1266 |
end |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1267 |
in mainf end |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1268 |
end |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1269 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1270 |
fun C f x y = f y x; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1271 |
(* FIXME : This is very bad!!!*) |
32839 | 1272 |
fun subst_conv eqs t = |
1273 |
let |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1274 |
val t' = fold (Thm.cabs o Thm.lhs_of) eqs t |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1275 |
in Conv.fconv_rule (Thm.beta_conversion true) (fold (C combination) eqs (reflexive t')) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1276 |
end |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1277 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1278 |
(* A wrapper that tries to substitute away variables first. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1279 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1280 |
local |
32828 | 1281 |
open Conv |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1282 |
fun simple_cterm_ord t u = TermOrd.fast_term_ord (term_of t, term_of u) = LESS |
32828 | 1283 |
val concl = Thm.dest_arg o cprop_of |
32839 | 1284 |
val shuffle1 = |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1285 |
fconv_rule (rewr_conv @{lemma "(a + x == y) == (x == y - (a::real))" by (atomize (full)) (simp add: ring_simps) }) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1286 |
val shuffle2 = |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1287 |
fconv_rule (rewr_conv @{lemma "(x + a == y) == (x == y - (a::real))" by (atomize (full)) (simp add: ring_simps)}) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1288 |
fun substitutable_monomial fvs tm = case term_of tm of |
32839 | 1289 |
Free(_,@{typ real}) => if not (member (op aconvc) fvs tm) then (Rat.one,tm) |
32332 | 1290 |
else raise Failure "substitutable_monomial" |
32839 | 1291 |
| @{term "op * :: real => _"}$c$(t as Free _ ) => |
32828 | 1292 |
if RealArith.is_ratconst (Thm.dest_arg1 tm) andalso not (member (op aconvc) fvs (Thm.dest_arg tm)) |
1293 |
then (RealArith.dest_ratconst (Thm.dest_arg1 tm),Thm.dest_arg tm) else raise Failure "substitutable_monomial" |
|
32839 | 1294 |
| @{term "op + :: real => _"}$s$t => |
32828 | 1295 |
(substitutable_monomial (Thm.add_cterm_frees (Thm.dest_arg tm) fvs) (Thm.dest_arg1 tm) |
1296 |
handle Failure _ => substitutable_monomial (Thm.add_cterm_frees (Thm.dest_arg1 tm) fvs) (Thm.dest_arg tm)) |
|
32332 | 1297 |
| _ => raise Failure "substitutable_monomial" |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1298 |
|
32839 | 1299 |
fun isolate_variable v th = |
32828 | 1300 |
let val w = Thm.dest_arg1 (cprop_of th) |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1301 |
in if v aconvc w then th |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1302 |
else case term_of w of |
32839 | 1303 |
@{term "op + :: real => _"}$s$t => |
1304 |
if Thm.dest_arg1 w aconvc v then shuffle2 th |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1305 |
else isolate_variable v (shuffle1 th) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1306 |
| _ => error "isolate variable : This should not happen?" |
32839 | 1307 |
end |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1308 |
in |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1309 |
|
32268
d50f0cb67578
Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
32150
diff
changeset
|
1310 |
fun real_nonlinear_subst_prover prover ctxt = |
32839 | 1311 |
let |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1312 |
val {add,mul,neg,pow,sub,main} = Normalizer.semiring_normalizers_ord_wrapper ctxt |
33035 | 1313 |
(the (NormalizerData.match ctxt @{cterm "(0::real) + 1"})) |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1314 |
simple_cterm_ord |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1315 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1316 |
val (real_poly_add_conv,real_poly_mul_conv,real_poly_neg_conv, |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1317 |
real_poly_pow_conv,real_poly_sub_conv,real_poly_conv) = (add,mul,neg,pow,sub,main) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1318 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1319 |
fun make_substitution th = |
32839 | 1320 |
let |
32828 | 1321 |
val (c,v) = substitutable_monomial [] (Thm.dest_arg1(concl th)) |
1322 |
val th1 = Drule.arg_cong_rule (Thm.capply @{cterm "op * :: real => _"} (RealArith.cterm_of_rat (Rat.inv c))) (mk_meta_eq th) |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1323 |
val th2 = fconv_rule (binop_conv real_poly_mul_conv) th1 |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1324 |
in fconv_rule (arg_conv real_poly_conv) (isolate_variable v th2) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1325 |
end |
32839 | 1326 |
fun oprconv cv ct = |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1327 |
let val g = Thm.dest_fun2 ct |
32839 | 1328 |
in if g aconvc @{cterm "op <= :: real => _"} |
1329 |
orelse g aconvc @{cterm "op < :: real => _"} |
|
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1330 |
then arg_conv cv ct else arg1_conv cv ct |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1331 |
end |
32645
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32332
diff
changeset
|
1332 |
fun mainf cert_choice translator = |
32839 | 1333 |
let |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1334 |
fun substfirst(eqs,les,lts) = |
32839 | 1335 |
((let |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1336 |
val eth = tryfind make_substitution eqs |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1337 |
val modify = fconv_rule (arg_conv (oprconv(subst_conv [eth] then_conv real_poly_conv))) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1338 |
in substfirst |
32839 | 1339 |
(filter_out (fn t => (Thm.dest_arg1 o Thm.dest_arg o cprop_of) t |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1340 |
aconvc @{cterm "0::real"}) (map modify eqs), |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1341 |
map modify les,map modify lts) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1342 |
end) |
32645
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32332
diff
changeset
|
1343 |
handle Failure _ => real_nonlinear_prover prover ctxt cert_choice translator (rev eqs, rev les, rev lts)) |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1344 |
in substfirst |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1345 |
end |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1346 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1347 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1348 |
in mainf |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1349 |
end |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1350 |
|
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1351 |
(* Overall function. *) |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1352 |
|
32645
1cc5b24f5a01
sos method generates and uses proof certificates
Philipp Meyer
parents:
32332
diff
changeset
|
1353 |
fun real_sos prover ctxt = |
32828 | 1354 |
RealArith.gen_prover_real_arith ctxt (real_nonlinear_subst_prover prover ctxt) |
31119
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1355 |
end; |
2532bb2d65c7
A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff
changeset
|
1356 |
|
32839 | 1357 |
val known_sos_constants = |
1358 |
[@{term "op ==>"}, @{term "Trueprop"}, |
|
1359 |
@{term "op -->"}, @{term "op &"}, @{term "op |"}, |
|
1360 |
@{term "Not"}, @{term "op = :: bool => _"}, |
|
1361 |
@{term "All :: (real => _) => _"}, @{term "Ex :: (real => _) => _"}, |
|
1362 |
@{term "op = :: real => _"}, @{term "op < :: real => _"}, |
|
1363 |
@{term "op <= :: real => _"}, |
|
1364 |
@{term "op + :: real => _"}, @{term "op - :: real => _"}, |
|
1365 |
@{term "op * :: real => _"}, @{term "uminus :: real => _"}, |
|
31512 | 1366 |
@{term "op / :: real => _"}, @{term "inverse :: real => _"}, |
32839 | 1367 |
@{term "op ^ :: real => _"}, @{term "abs :: real => _"}, |
31512 | 1368 |
@{term "min :: real => _"}, @{term "max :: real => _"}, |
1369 |
@{term "0::real"}, @{term "1::real"}, @{term "number_of :: int => real"}, |
|
1370 |
@{term "number_of :: int => nat"}, |
|
32839 | 1371 |
@{term "Int.Bit0"}, @{term "Int.Bit1"}, |
31512 | 1372 |
@{term "Int.Pls"}, @{term "Int.Min"}]; |
1373 |
||
32839 | 1374 |
fun check_sos kcts ct = |
31512 | 1375 |
let |
1376 |
val t = term_of ct |
|
32839 | 1377 |
val _ = if not (null (Term.add_tfrees t []) |
1378 |
andalso null (Term.add_tvars t [])) |
|
31512 | 1379 |
then error "SOS: not sos. Additional type varables" else () |
1380 |
val fs = Term.add_frees t [] |
|
32839 | 1381 |
val _ = if exists (fn ((_,T)) => not (T = @{typ "real"})) fs |
31512 | 1382 |
then error "SOS: not sos. Variables with type not real" else () |
1383 |
val vs = Term.add_vars t [] |
|
32839 | 1384 |
val _ = if exists (fn ((_,T)) => not (T = @{typ "real"})) vs |
31512 | 1385 |
then error "SOS: not sos. Variables with type not real" else () |
1386 |
val ukcs = subtract (fn (t,p) => Const p aconv t) kcts (Term.add_consts t []) |
|
32839 | 1387 |
val _ = if null ukcs then () |
31512 | 1388 |
else error ("SOSO: Unknown constants in Subgoal:" ^ commas (map fst ukcs)) |
1389 |
in () end |
|
1390 |
||
32838
d9dfd30af9ae
core_sos_tac: SUBPROOF body operates on subgoal 1;
wenzelm
parents:
32831
diff
changeset
|
1391 |
fun core_sos_tac print_cert prover = SUBPROOF (fn {concl, context, ...} => |
32831 | 1392 |
let |
32838
d9dfd30af9ae
core_sos_tac: SUBPROOF body operates on subgoal 1;
wenzelm
parents:
32831
diff
changeset
|
1393 |
val _ = check_sos known_sos_constants concl |
d9dfd30af9ae
core_sos_tac: SUBPROOF body operates on subgoal 1;
wenzelm
parents:
32831
diff
changeset
|
1394 |
val (ths, certificates) = real_sos prover context (Thm.dest_arg concl) |
d9dfd30af9ae
core_sos_tac: SUBPROOF body operates on subgoal 1;
wenzelm
parents:
32831
diff
changeset
|
1395 |
val _ = print_cert certificates |
d9dfd30af9ae
core_sos_tac: SUBPROOF body operates on subgoal 1;
wenzelm
parents:
32831
diff
changeset
|
1396 |
in rtac ths 1 end) |
31131 | 1397 |
|
1398 |
fun default_SOME f NONE v = SOME v |
|
1399 |
| default_SOME f (SOME v) _ = SOME v; |
|
1400 |
||
1401 |
fun lift_SOME f NONE a = f a |
|
1402 |
| lift_SOME f (SOME a) _ = SOME a; |
|
1403 |
||
1404 |
||
1405 |
local |
|
1406 |
val is_numeral = can (HOLogic.dest_number o term_of) |
|
1407 |
in |
|
1408 |
fun get_denom b ct = case term_of ct of |
|
32839 | 1409 |
@{term "op / :: real => _"} $ _ $ _ => |
31131 | 1410 |
if is_numeral (Thm.dest_arg ct) then get_denom b (Thm.dest_arg1 ct) |
1411 |
else default_SOME (get_denom b) (get_denom b (Thm.dest_arg ct)) (Thm.dest_arg ct, b) |
|
1412 |
| @{term "op < :: real => _"} $ _ $ _ => lift_SOME (get_denom true) (get_denom true (Thm.dest_arg ct)) (Thm.dest_arg1 ct) |
|
1413 |
| @{term "op <= :: real => _"} $ _ $ _ => lift_SOME (get_denom true) (get_denom true (Thm.dest_arg ct)) (Thm.dest_arg1 ct) |
|
1414 |
| _ $ _ => lift_SOME (get_denom b) (get_denom b (Thm.dest_fun ct)) (Thm.dest_arg ct) |
|
1415 |
| _ => NONE |
|
1416 |
end; |
|
1417 |
||
32839 | 1418 |
fun elim_one_denom_tac ctxt = |
1419 |
CSUBGOAL (fn (P,i) => |
|
1420 |
case get_denom false P of |
|
31131 | 1421 |
NONE => no_tac |
32839 | 1422 |
| SOME (d,ord) => |
1423 |
let |
|
1424 |
val ss = simpset_of ctxt addsimps @{thms field_simps} |
|
31131 | 1425 |
addsimps [@{thm nonzero_power_divide}, @{thm power_divide}] |
32839 | 1426 |
val th = instantiate' [] [SOME d, SOME (Thm.dest_arg P)] |
31131 | 1427 |
(if ord then @{lemma "(d=0 --> P) & (d>0 --> P) & (d<(0::real) --> P) ==> P" by auto} |
1428 |
else @{lemma "(d=0 --> P) & (d ~= (0::real) --> P) ==> P" by blast}) |
|
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32839
diff
changeset
|
1429 |
in rtac th i THEN Simplifier.asm_full_simp_tac ss i end); |
31131 | 1430 |
|
1431 |
fun elim_denom_tac ctxt i = REPEAT (elim_one_denom_tac ctxt i); |
|
1432 |
||
32949
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32839
diff
changeset
|
1433 |
fun sos_tac print_cert prover ctxt = |
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32839
diff
changeset
|
1434 |
ObjectLogic.full_atomize_tac THEN' |
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32839
diff
changeset
|
1435 |
elim_denom_tac ctxt THEN' |
aa6c470a962a
eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents:
32839
diff
changeset
|
1436 |
core_sos_tac print_cert prover ctxt; |
31131 | 1437 |
|
31512 | 1438 |
end; |