-
Notifications
You must be signed in to change notification settings - Fork 1
/
grobner.ml
422 lines (328 loc) · 15.4 KB
/
grobner.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
(* ========================================================================= *)
(* Grobner basis algorithm. *)
(* *)
(* Copyright (c) 2003-2007, John Harrison. (See "LICENSE.txt" for details.) *)
(* ========================================================================= *)
(* ------------------------------------------------------------------------- *)
(* Operations on monomials. *)
(* ------------------------------------------------------------------------- *)
let mmul (c1,m1) (c2,m2) = (c1*/c2,map2 (+) m1 m2);;
let mdiv =
let index_sub n1 n2 = if n1 < n2 then failwith "mdiv" else n1-n2 in
fun (c1,m1) (c2,m2) -> (c1//c2,map2 index_sub m1 m2);;
let mlcm (c1,m1) (c2,m2) = (Int 1,map2 max m1 m2);;
(* ------------------------------------------------------------------------- *)
(* Monomial ordering. *)
(* ------------------------------------------------------------------------- *)
let morder_lt m1 m2 =
let n1 = itlist (+) m1 0 and n2 = itlist (+) m2 0 in
n1 < n2 || n1 = n2 && lexord(>) m1 m2;;
(* ------------------------------------------------------------------------- *)
(* Arithmetic on canonical multivariate polynomials. *)
(* ------------------------------------------------------------------------- *)
let mpoly_mmul cm pol = map (mmul cm) pol;;
let mpoly_neg = map (fun (c,m) -> (minus_num c,m));;
let mpoly_const vars c =
if c =/ Int 0 then [] else [c,map (fun k -> 0) vars];;
let mpoly_var vars x =
[Int 1,map (fun y -> if y = x then 1 else 0) vars];;
let rec mpoly_add l1 l2 =
match (l1,l2) with
([],l2) -> l2
| (l1,[]) -> l1
| ((c1,m1)::o1,(c2,m2)::o2) ->
if m1 = m2 then
let c = c1+/c2 and rest = mpoly_add o1 o2 in
if c =/ Int 0 then rest else (c,m1)::rest
else if morder_lt m2 m1 then (c1,m1)::(mpoly_add o1 l2)
else (c2,m2)::(mpoly_add l1 o2);;
let mpoly_sub l1 l2 = mpoly_add l1 (mpoly_neg l2);;
let rec mpoly_mul l1 l2 =
match l1 with
[] -> []
| (h1::t1) -> mpoly_add (mpoly_mmul h1 l2) (mpoly_mul t1 l2);;
let mpoly_pow vars l n =
funpow n (mpoly_mul l) (mpoly_const vars (Int 1));;
let mpoly_inv p =
match p with
[(c,m)] when forall (fun i -> i = 0) m -> [(Int 1 // c),m]
| _ -> failwith "mpoly_inv: non-constant polynomial";;
let mpoly_div p q = mpoly_mul p (mpoly_inv q);;
(* ------------------------------------------------------------------------- *)
(* Convert formula into canonical form. *)
(* ------------------------------------------------------------------------- *)
let rec mpolynate vars tm =
match tm with
Var x -> mpoly_var vars x
| Fn("-",[t]) -> mpoly_neg (mpolynate vars t)
| Fn("+",[s;t]) -> mpoly_add (mpolynate vars s) (mpolynate vars t)
| Fn("-",[s;t]) -> mpoly_sub (mpolynate vars s) (mpolynate vars t)
| Fn("*",[s;t]) -> mpoly_mul (mpolynate vars s) (mpolynate vars t)
| Fn("/",[s;t]) -> mpoly_div (mpolynate vars s) (mpolynate vars t)
| Fn("^",[t;Fn(n,[])]) ->
mpoly_pow vars (mpolynate vars t) (int_of_string n)
| _ -> mpoly_const vars (dest_numeral tm);;
let mpolyatom vars fm =
match fm with
Atom(R("=",[s;t])) -> mpolynate vars (Fn("-",[s;t]))
| _ -> failwith "mpolyatom: not an equation";;
(* ------------------------------------------------------------------------- *)
(* Reduce monomial cm by polynomial pol, returning replacement for cm. *)
(* ------------------------------------------------------------------------- *)
let reduce1 cm pol =
match pol with
[] -> failwith "reduce1"
| hm::cms -> let c,m = mdiv cm hm in mpoly_mmul (minus_num c,m) cms;;
(* ------------------------------------------------------------------------- *)
(* Try this for all polynomials in a basis. *)
(* ------------------------------------------------------------------------- *)
let reduceb cm pols = tryfind (reduce1 cm) pols;;
(* ------------------------------------------------------------------------- *)
(* Reduction of a polynomial (always picking largest monomial possible). *)
(* ------------------------------------------------------------------------- *)
let rec reduce pols pol =
match pol with
[] -> []
| cm::ptl -> try reduce pols (mpoly_add (reduceb cm pols) ptl)
with Failure _ -> cm::(reduce pols ptl);;
(* ------------------------------------------------------------------------- *)
(* Compute S-polynomial of two polynomials. *)
(* ------------------------------------------------------------------------- *)
let spoly pol1 pol2 =
match (pol1,pol2) with
([],p) -> []
| (p,[]) -> []
| (m1::ptl1,m2::ptl2) ->
let m = mlcm m1 m2 in
mpoly_sub (mpoly_mmul (mdiv m m1) ptl1)
(mpoly_mmul (mdiv m m2) ptl2);;
(* ------------------------------------------------------------------------- *)
(* Grobner basis algorithm. *)
(* ------------------------------------------------------------------------- *)
let rec grobner basis pairs =
print_string(string_of_int(length basis)^" basis elements and "^
string_of_int(length pairs)^" pairs");
print_newline();
match pairs with
[] -> basis
| (p1,p2)::opairs ->
let sp = reduce basis (spoly p1 p2) in
if sp = [] then grobner basis opairs
else if forall (forall ((=) 0) ** snd) sp then [sp] else
let newcps = map (fun p -> p,sp) basis in
grobner (sp::basis) (opairs @ newcps);;
(* ------------------------------------------------------------------------- *)
(* Overall function. *)
(* ------------------------------------------------------------------------- *)
let groebner basis = grobner basis (distinctpairs basis);;
(* ------------------------------------------------------------------------- *)
(* Use the Rabinowitsch trick to eliminate inequations. *)
(* That is, replace p =/= 0 by exists v. 1 - v * p = 0 *)
(* ------------------------------------------------------------------------- *)
let rabinowitsch vars v p =
mpoly_sub (mpoly_const vars (Int 1))
(mpoly_mul (mpoly_var vars v) p);;
(* ------------------------------------------------------------------------- *)
(* Universal complex number decision procedure based on Grobner bases. *)
(* ------------------------------------------------------------------------- *)
let grobner_trivial fms =
let vars0 = itlist (union ** fv) fms []
and eqs,neqs = partition positive fms in
let rvs = map (fun n -> variant ("_"^string_of_int n) vars0)
(1--length neqs) in
let vars = vars0 @ rvs in
let poleqs = map (mpolyatom vars) eqs
and polneqs = map (mpolyatom vars ** negate) neqs in
let pols = poleqs @ map2 (rabinowitsch vars) rvs polneqs in
reduce (groebner pols) (mpoly_const vars (Int 1)) = [];;
let grobner_decide fm =
let fm1 = specialize(prenex(nnf(simplify fm))) in
forall grobner_trivial (simpdnf(nnf(Not fm1)));;
(* ------------------------------------------------------------------------- *)
(* Examples. *)
(* ------------------------------------------------------------------------- *)
START_INTERACTIVE;;
grobner_decide
<<a^2 = 2 /\ x^2 + a*x + 1 = 0 ==> x^4 + 1 = 0>>;;
grobner_decide
<<a^2 = 2 /\ x^2 + a*x + 1 = 0 ==> x^4 + 2 = 0>>;;
grobner_decide
<<(a * x^2 + b * x + c = 0) /\
(a * y^2 + b * y + c = 0) /\
~(x = y)
==> (a * x * y = c) /\ (a * (x + y) + b = 0)>>;;
(* ------------------------------------------------------------------------- *)
(* Compare with earlier procedure. *)
(* ------------------------------------------------------------------------- *)
let fm =
<<(a * x^2 + b * x + c = 0) /\
(a * y^2 + b * y + c = 0) /\
~(x = y)
==> (a * x * y = c) /\ (a * (x + y) + b = 0)>> in
time complex_qelim (generalize fm),time grobner_decide fm;;
(* ------------------------------------------------------------------------- *)
(* More tests. *)
(* ------------------------------------------------------------------------- *)
time grobner_decide <<a^2 = 2 /\ x^2 + a*x + 1 = 0 ==> x^4 + 1 = 0>>;;
time grobner_decide <<a^2 = 2 /\ x^2 + a*x + 1 = 0 ==> x^4 + 2 = 0>>;;
time grobner_decide <<(a * x^2 + b * x + c = 0) /\
(a * y^2 + b * y + c = 0) /\
~(x = y)
==> (a * x * y = c) /\ (a * (x + y) + b = 0)>>;;
time grobner_decide
<<(y_1 = 2 * y_3) /\
(y_2 = 2 * y_4) /\
(y_1 * y_3 = y_2 * y_4)
==> (y_1^2 = y_2^2)>>;;
time grobner_decide
<<(x1 = u3) /\
(x1 * (u2 - u1) = x2 * u3) /\
(x4 * (x2 - u1) = x1 * (x3 - u1)) /\
(x3 * u3 = x4 * u2) /\
~(u1 = 0) /\
~(u3 = 0)
==> (x3^2 + x4^2 = (u2 - x3)^2 + (u3 - x4)^2)>>;;
time grobner_decide
<<(u1 * x1 - u1 * u3 = 0) /\
(u3 * x2 - (u2 - u1) * x1 = 0) /\
(x1 * x4 - (x2 - u1) * x3 - u1 * x1 = 0) /\
(u3 * x4 - u2 * x3 = 0) /\
~(u1 = 0) /\
~(u3 = 0)
==> (2 * u2 * x4 + 2 * u3 * x3 - u3^2 - u2^2 = 0)>>;;
(*** Checking resultants (in one direction) ***)
time grobner_decide
<<a * x^2 + b * x + c = 0 /\ 2 * a * x + b = 0
==> 4*a^2*c-b^2*a = 0>>;;
time grobner_decide
<<a * x^2 + b * x + c = 0 /\ d * x + e = 0
==> d^2*c-e*d*b+a*e^2 = 0>>;;
time grobner_decide
<<a * x^2 + b * x + c = 0 /\ d * x^2 + e * x + f = 0
==> d^2*c^2-2*d*c*a*f+a^2*f^2-e*d*b*c-e*b*a*f+a*e^2*c+f*d*b^2 = 0>>;;
(****** Seems a bit too lengthy?
time grobner_decide
<<a * x^3 + b * x^2 + c * x + d = 0 /\ e * x^2 + f * x + g = 0
==>
e^3*d^2+3*e*d*g*a*f-2*e^2*d*g*b-g^2*a*f*b+g^2*e*b^2-f*e^2*c*d+f^2*c*g*a-f*e*c*
g*b+f^2*e*b*d-f^3*a*d+g*e^2*c^2-2*e*c*a*g^2+a^2*g^3 = 0>>;;
********)
(********** Works correctly, but it's lengthy
time grobner_decide
<< (x1 - x0)^2 + (y1 - y0)^2 =
(x2 - x0)^2 + (y2 - y0)^2 /\
(x2 - x0)^2 + (y2 - y0)^2 =
(x3 - x0)^2 + (y3 - y0)^2 /\
(x1 - x0')^2 + (y1 - y0')^2 =
(x2 - x0')^2 + (y2 - y0')^2 /\
(x2 - x0')^2 + (y2 - y0')^2 =
(x3 - x0')^2 + (y3 - y0')^2
==> x0 = x0' /\ y0 = y0'>>;;
**** Corrected with non-isotropy conditions; even lengthier
time grobner_decide
<<(x1 - x0)^2 + (y1 - y0)^2 =
(x2 - x0)^2 + (y2 - y0)^2 /\
(x2 - x0)^2 + (y2 - y0)^2 =
(x3 - x0)^2 + (y3 - y0)^2 /\
(x1 - x0')^2 + (y1 - y0')^2 =
(x2 - x0')^2 + (y2 - y0')^2 /\
(x2 - x0')^2 + (y2 - y0')^2 =
(x3 - x0')^2 + (y3 - y0')^2 /\
~((x1 - x0)^2 + (y1 - y0)^2 = 0) /\
~((x1 - x0')^2 + (y1 - y0')^2 = 0)
==> x0 = x0' /\ y0 = y0'>>;;
*** Maybe this is more efficient? (No?)
time grobner_decide
<<(x1 - x0)^2 + (y1 - y0)^2 = d /\
(x2 - x0)^2 + (y2 - y0)^2 = d /\
(x3 - x0)^2 + (y3 - y0)^2 = d /\
(x1 - x0')^2 + (y1 - y0')^2 = e /\
(x2 - x0')^2 + (y2 - y0')^2 = e /\
(x3 - x0')^2 + (y3 - y0')^2 = e /\
~(d = 0) /\ ~(e = 0)
==> x0 = x0' /\ y0 = y0'>>;;
***********)
(* ------------------------------------------------------------------------- *)
(* Inversion of homographic function (from Gosper's CF notes). *)
(* ------------------------------------------------------------------------- *)
time grobner_decide
<<y * (c * x + d) = a * x + b ==> x * (c * y - a) = b - d * y>>;;
(* ------------------------------------------------------------------------- *)
(* Manual "sums of squares" for 0 <= a /\ a <= b ==> a^3 <= b^3. *)
(* ------------------------------------------------------------------------- *)
time complex_qelim
<<forall a b c d e.
a = c^2 /\ b = a + d^2 /\ (b^3 - a^3) * e^2 + 1 = 0
==> (a * d * e)^2 + (c^2 * d * e)^2 + (c * d^2 * e)^2 + (b * d * e)^2 + 1 =
0>>;;
time grobner_decide
<<a = c^2 /\ b = a + d^2 /\ (b^3 - a^3) * e^2 + 1 = 0
==> (a * d * e)^2 + (c^2 * d * e)^2 + (c * d^2 * e)^2 + (b * d * e)^2 + 1 =
0>>;;
(* ------------------------------------------------------------------------- *)
(* Special case of a = 1, i.e. 1 <= b ==> 1 <= b^3 *)
(* ------------------------------------------------------------------------- *)
time complex_qelim
<<forall b d e.
b = 1 + d^2 /\ (b^3 - 1) * e^2 + 1 = 0
==> 2 * (d * e)^2 + (d^2 * e)^2 + (b * d * e)^2 + 1 = 0>>;;
time grobner_decide
<<b = 1 + d^2 /\ (b^3 - 1) * e^2 + 1 = 0
==> 2 * (d * e)^2 + (d^2 * e)^2 + (b * d * e)^2 + 1 = 0>>;;
(* ------------------------------------------------------------------------- *)
(* Converse, 0 <= a /\ a^3 <= b^3 ==> a <= b *)
(* *)
(* This derives b <= 0, but not a full solution. *)
(* ------------------------------------------------------------------------- *)
time grobner_decide
<<a = c^2 /\ b^3 = a^3 + d^2 /\ (b - a) * e^2 + 1 = 0
==> c^2 * b + a^2 + b^2 + (e * d)^2 = 0>>;;
(* ------------------------------------------------------------------------- *)
(* Here are further steps towards a solution, step-by-step. *)
(* ------------------------------------------------------------------------- *)
time grobner_decide
<<a = c^2 /\ b^3 = a^3 + d^2 /\ (b - a) * e^2 + 1 = 0
==> c^2 * b = -(a^2 + b^2 + (e * d)^2)>>;;
time grobner_decide
<<a = c^2 /\ b^3 = a^3 + d^2 /\ (b - a) * e^2 + 1 = 0
==> c^6 * b^3 = -(a^2 + b^2 + (e * d)^2)^3>>;;
time grobner_decide
<<a = c^2 /\ b^3 = a^3 + d^2 /\ (b - a) * e^2 + 1 = 0
==> c^6 * (c^6 + d^2) + (a^2 + b^2 + (e * d)^2)^3 = 0>>;;
(* ------------------------------------------------------------------------- *)
(* A simpler one is ~(x < y /\ y < x), i.e. x < y ==> x <= y. *)
(* *)
(* Yet even this isn't completed! *)
(* ------------------------------------------------------------------------- *)
time grobner_decide
<<(y - x) * s^2 = 1 /\ (x - y) * t^2 = 1 ==> s^2 + t^2 = 0>>;;
(* ------------------------------------------------------------------------- *)
(* Inspired by Cardano's formula for a cubic. This actually works worse than *)
(* with naive quantifier elimination (of course it's false...) *)
(* ------------------------------------------------------------------------- *)
(******
time grobner_decide
<<t - u = n /\ 27 * t * u = m^3 /\
ct^3 = t /\ cu^3 = u /\
x = ct - cu
==> x^3 + m * x = n>>;;
***********)
END_INTERACTIVE;;
(* ------------------------------------------------------------------------- *)
(* For looking at things it's nice to map back to normal term. *)
(* ------------------------------------------------------------------------- *)
(*****
let term_of_varpow vars (x,k) =
if k = 1 then Var x else Fn("^",[Var x; mk_numeral(Int k)]);;
let term_of_varpows vars lis =
let tms = filter (fun (a,b) -> b <> 0) (zip vars lis) in
end_itlist (fun s t -> Fn("*",[s;t])) (map (term_of_varpow vars) tms);;
let term_of_monomial vars (c,m) =
if forall (fun x -> x = 0) m then mk_numeral c
else if c =/ Int 1 then term_of_varpows vars m
else Fn("*",[mk_numeral c; term_of_varpows vars m]);;
let term_of_poly vars pol =
end_itlist (fun s t -> Fn("+",[s;t])) (map (term_of_monomial vars) pol);;
let grobner_basis vars pols =
map (term_of_poly vars) (groebner (map (mpolyatom vars) pols));;
*****)