Debian Codegraph @

plugins/nsatz/ideal.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 (* Nullstellensatz with Groebner basis computation
12
13 We use a sparse representation for polynomials:
14 a monomial is an array of exponents (one for each variable)
15 with its degree in head
16 a polynomial is a sorted list of (coefficient, monomial)
17
18 *)
19
20 open Utile
21
22 exception NotInIdeal
23
24 (***********************************************************************
25 Global options
26 *)
27 let lexico = ref false
28
29 (* division of tail monomials *)
30
31 let reduire_les_queues = false
32
33 (* divide first with new polynomials *)
34
35 let nouveaux_pol_en_tete = false
36
37 type metadata = {
38 name_var : string list;
39 }
40
41 module Monomial :
42 sig
43 type t
44 val repr : t -> int array
45 val make : int array -> t
46 val deg : t -> int
47 val nvar : t -> int
48 val var_mon : int -> int -> t
49 val mult_mon : t -> t -> t
50 val compare_mon : t -> t -> int
51 val div_mon : t -> t -> t
52 val div_mon_test : t -> t -> bool
53 val ppcm_mon : t -> t -> t
54 val const_mon : int -> t
55 end =
56 struct
57 type t = int array
58 type mon = t
59 let repr m = m
60 let make m = m
61 let nvar (m : mon) = Array.length m - 1
62
63 let deg (m : mon) = m.(0)
64
65 let mult_mon (m : mon) (m' : mon) =
66 let d = nvar m in
67 let m'' = Array.make (d+1) 0 in
68 for i=0 to d do
69 m''.(i)<- (m.(i)+m'.(i));
70 done;
71 m''
72
73
74 let compare_mon (m : mon) (m' : mon) =
75 let d = nvar m in
76 if !lexico
77 then (
78 (* Comparaison de monomes avec ordre du degre lexicographique = on commence par regarder la 1ere variable*)
79 let res=ref 0 in
80 let i=ref 1 in (* 1 si lexico pur 0 si degre*)
81 while (!res=0) && (!i<=d) do
82 res:= (Int.compare m.(!i) m'.(!i));
83 i:=!i+1;
84 done;
85 !res)
86 else (
87 (* degre lexicographique inverse *)
88 match Int.compare m.(0) m'.(0) with
89 | 0 -> (* meme degre total *)
90 let res=ref 0 in
91 let i=ref d in
92 while (!res=0) && (!i>=1) do
93 res:= - (Int.compare m.(!i) m'.(!i));
94 i:=!i-1;
95 done;
96 !res
97 | x -> x)
98
99 let div_mon m m' =
100 let d = nvar m in
101 let m'' = Array.make (d+1) 0 in
102 for i=0 to d do
103 m''.(i)<- (m.(i)-m'.(i));
104 done;
105 m''
106
107 (* m' divides m *)
108 let div_mon_test m m' =
109 let d = nvar m in
110 let res=ref true in
111 let i=ref 0 in (*il faut que le degre total soit bien mis sinon, i=ref 1*)
112 while (!res) && (!i<=d) do
113 res:= (m.(!i) >= m'.(!i));
114 i:=succ !i;
115 done;
116 !res
117
118 let set_deg m =
119 let d = nvar m in
120 m.(0)<-0;
121 for i=1 to d do
122 m.(0)<- m.(i)+m.(0);
123 done;
124 m
125
126 (* lcm *)
127 let ppcm_mon m m' =
128 let d = nvar m in
129 let m'' = Array.make (d+1) 0 in
130 for i=1 to d do
131 m''.(i)<- (max m.(i) m'.(i));
132 done;
133 set_deg m''
134
135 (* returns a constant polynom ial with d variables *)
136 let const_mon d =
137 let m = Array.make (d+1) 0 in
138 let m = set_deg m in
139 m
140
141 let var_mon d i =
142 let m = Array.make (d+1) 0 in
143 m.(i) <- 1;
144 let m = set_deg m in
145 m
146
147 end
148
149 (***********************************************************************
150 Functor
151 *)
152
153 module Make (P:Polynom.S) = struct
154
155 type coef = P.t
156 let coef0 = P.of_num Q.zero
157 let coef1 = P.of_num Q.one
158 let string_of_coef c = "["^(P.to_string c)^"]"
159
160 (***********************************************************************
161 Monomials
162 array of integers, first is the degree
163 *)
164
165 open Monomial
166
167 type mon = Monomial.t
168 type deg = int
169 type poly = (coef * mon) list
170 type polynom = {
171 pol : poly;
172 num : int;
173 }
174
175 (**********************************************************************
176 Polynomials
177 list of (coefficient, monomial) decreasing order
178 *)
179
180 let repr p = p
181
182 let equal =
183 Util.List.for_all2eq
184 (fun (c1,m1) (c2,m2) -> P.equal c1 c2 && m1=m2)
185
186 let hash p =
187 let c = List.map fst p in
188 let m = List.map snd p in
189 List.fold_left (fun h p -> h * 17 + P.hash p) (Hashtbl.hash m) c
190
191 module Hashpol = Hashtbl.Make(
192 struct
193 type t = poly
194 let equal = equal
195 let hash = hash
196 end)
197
198
199 (* A pretty printer for polynomials, with Maple-like syntax. *)
200
201 let getvar lv i =
202 try (List.nth lv i)
203 with Failure _ -> (List.fold_left (fun r x -> r^" "^x) "lv= " lv)
204 ^" i="^(string_of_int i)
205
206 let string_of_pol zeroP hdP tlP coefterm monterm string_of_coef
207 dimmon string_of_exp lvar p =
208
209
210 let rec string_of_mon m coefone =
211 let s=ref [] in
212 for i=1 to (dimmon m) do
213 (match (string_of_exp m i) with
214 "0" -> ()
215 | "1" -> s:= (!s) @ [(getvar lvar (i-1))]
216 | e -> s:= (!s) @ [((getvar lvar (i-1)) ^ "^" ^ e)]);
217 done;
218 (match !s with
219 [] -> if coefone
220 then "1"
221 else ""
222 | l -> if coefone
223 then (String.concat "*" l)
224 else ( "*" ^
225 (String.concat "*" l)))
226 and string_of_term t start = let a = coefterm t and m = monterm t in
227 match (string_of_coef a) with
228 "0" -> ""
229 | "1" ->(match start with
230 true -> string_of_mon m true
231 |false -> ( "+ "^
232 (string_of_mon m true)))
233 | "-1" ->( "-" ^" "^(string_of_mon m true))
234 | c -> if (String.get c 0)='-'
235 then ( "- "^
236 (String.sub c 1
237 ((String.length c)-1))^
238 (string_of_mon m false))
239 else (match start with
240 true -> ( c^(string_of_mon m false))
241 |false -> ( "+ "^
242 c^(string_of_mon m false)))
243 and stringP p start =
244 if (zeroP p)
245 then (if start
246 then ("0")
247 else "")
248 else ((string_of_term (hdP p) start)^
249 " "^
250 (stringP (tlP p) false))
251 in
252 (stringP p true)
253
254 let stringP metadata (p : poly) =
255 string_of_pol
256 (fun p -> match p with [] -> true | _ -> false)
257 (fun p -> match p with (t::p) -> t |_ -> failwith "print_pol dans dansideal")
258 (fun p -> match p with (t::p) -> p |_ -> failwith "print_pol dans dansideal")
259 (fun (a,m) -> a)
260 (fun (a,m) -> m)
261 string_of_coef
262 (fun m -> (Array.length (Monomial.repr m))-1)
263 (fun m i -> (string_of_int ((Monomial.repr m).(i))))
264 metadata.name_var
265 p
266
267 let nsP2 = 10
268
269 let stringPcut metadata (p : poly) =
270 (*Polynomesrec.nsP1:=20;*)
271 let res =
272 if (List.length p)> nsP2
273 then (stringP metadata [List.hd p])^" + "^(string_of_int (List.length p))^" terms"
274 else stringP metadata p in
275 (*Polynomesrec.nsP1:= max_int;*)
276 res
277
278 (* Operations *)
279
280 let zeroP = []
281
282 (* returns a constant polynom ial with d variables *)
283 let polconst d c =
284 let m = const_mon d in
285 [(c,m)]
286
287 let plusP p q =
288 let rec plusP p q accu = match p, q with
289 | [], [] -> List.rev accu
290 | [], _ -> List.rev_append accu q
291 | _, [] -> List.rev_append accu p
292 | t :: p', t' :: q' ->
293 let c = compare_mon (snd t) (snd t') in
294 if c > 0 then plusP p' q (t :: accu)
295 else if c < 0 then plusP p q' (t' :: accu)
296 else
297 let c = P.plusP (fst t) (fst t') in
298 if P.equal c coef0 then plusP p' q' accu
299 else plusP p' q' ((c, (snd t)) :: accu)
300 in
301 plusP p q []
302
303 (* multiplication by (a,monomial) *)
304 let mult_t_pol a m p =
305 let map (b, m') = (P.multP a b, mult_mon m m') in
306 CList.map map p
307
308 let coef_of_int x = P.of_num (Q.of_int x)
309
310 (* variable i *)
311 let gen d i =
312 let m = var_mon d i in
313 [((coef_of_int 1),m)]
314
315 let oppP p =
316 let rec oppP p =
317 match p with
318 [] -> []
319 |(b,m')::p -> ((P.oppP b),m')::(oppP p)
320 in oppP p
321
322 (* multiplication by a coefficient *)
323 let emultP a p =
324 let rec emultP p =
325 match p with
326 [] -> []
327 |(b,m')::p -> ((P.multP a b),m')::(emultP p)
328 in emultP p
329
330 let multP p q =
331 let rec aux p accu =
332 match p with
333 [] -> accu
334 |(a,m)::p' -> aux p' (plusP (mult_t_pol a m q) accu)
335 in aux p []
336
337 let puisP p n=
338 match p with
339 [] -> []
340 |_ ->
341 if n = 0 then
342 let d = nvar (snd (List.hd p)) in
343 [coef1, const_mon d]
344 else
345 let rec puisP p n =
346 if n = 1 then p
347 else
348 let q = puisP p (n / 2) in
349 let q = multP q q in
350 if n mod 2 = 0 then q else multP p q
351 in puisP p n
352
353 (***********************************************************************
354 Division of polynomials
355 *)
356
357 type table = {
358 hmon : (mon, poly) Hashtbl.t option;
359 (* coefficients of polynomials when written with initial polynomials *)
360 coefpoldep : ((int * int), poly) Hashtbl.t;
361 mutable nallpol : int;
362 mutable allpol : polynom array;
363 (* list of initial polynomials *)
364 }
365
366 let pgcdpos a b = P.pgcdP a b
367
368 let polynom0 = { pol = []; num = 0 }
369
370 let ppol p = p.pol
371
372 let lm p = snd (List.hd (ppol p))
373
374 let new_allpol table p =
375 table.nallpol <- table.nallpol + 1;
376 if table.nallpol >= Array.length table.allpol
377 then
378 table.allpol <- Array.append table.allpol (Array.make table.nallpol polynom0);
379 let p = { pol = p; num = table.nallpol } in
380 table.allpol.(table.nallpol) <- p;
381 p
382
383 (* returns a polynomial of l whose head monomial divides m, else [] *)
384
385 let rec selectdiv m l =
386 match l with
387 [] -> polynom0
388 |q::r -> let m'= snd (List.hd (ppol q)) in
389 match (div_mon_test m m') with
390 true -> q
391 |false -> selectdiv m r
392
393 let div_pol p q a b m =
394 plusP (emultP a p) (mult_t_pol b m q)
395
396 let find_hmon table m = match table.hmon with
397 | None -> raise Not_found
398 | Some hmon -> Hashtbl.find hmon m
399
400 let add_hmon table m q =
401 match table.hmon with
402 | None -> ()
403 | Some hmon -> Hashtbl.add hmon m q
404
405 let selectdiv table m l =
406 try find_hmon table m
407 with Not_found ->
408 let q = selectdiv m l in
409 let q = ppol q in
410 match q with
411 | [] -> q
412 | _ :: _ ->
413 let () = add_hmon table m q in
414 q
415
416 let div_coef a b = P.divP a b
417
418
419 (* remainder r of the division of p by polynomials of l, returns (c,r) where c is the coefficient for pseudo-division : c p = sum_i q_i p_i + r *)
420
421 let reduce2 table p l =
422 let l = if nouveaux_pol_en_tete then List.rev l else l in
423 let rec reduce p =
424 match p with
425 [] -> (coef1,[])
426 |t::p' ->
427 let (a,m)=t in
428 let q = selectdiv table m l in
429 match q with
430 [] -> if reduire_les_queues
431 then
432 let (c,r)=(reduce p') in
433 (c,((P.multP a c,m)::r))
434 else (coef1,p)
435 |(b,m')::q' ->
436 let c=(pgcdpos a b) in
437 let a'= (div_coef b c) in
438 let b'=(P.oppP (div_coef a c)) in
439 let (e,r)=reduce (div_pol p' q' a' b'
440 (div_mon m m')) in
441 (P.multP a' e,r)
442 in let (c,r) = reduce p in
443 (c,r)
444
445 (* coef of q in p = sum_i c_i*q_i *)
446 let coefpoldep_find table p q =
447 try (Hashtbl.find table.coefpoldep (p.num,q.num))
448 with Not_found -> []
449
450 let coefpoldep_set table p q c =
451 Hashtbl.add table.coefpoldep (p.num,q.num) c
452
453 (* keeps trace in coefpoldep
454 divides without pseudodivisions *)
455
456 let reduce2_trace table p l lcp =
457 let lp = l in
458 let l = if nouveaux_pol_en_tete then List.rev l else l in
459 (* rend (lq,r), ou r = p + sum(lq) *)
460 let rec reduce p =
461 match p with
462 [] -> ([],[])
463 |t::p' ->
464 let (a,m)=t in
465 let q = selectdiv table m l in
466 match q with
467 [] ->
468 if reduire_les_queues
469 then
470 let (lq,r)=(reduce p') in
471 (lq,((a,m)::r))
472 else ([],p)
473 |(b,m')::q' ->
474 let b'=(P.oppP (div_coef a b)) in
475 let m''= div_mon m m' in
476 let p1=plusP p' (mult_t_pol b' m'' q') in
477 let (lq,r)=reduce p1 in
478 ((b',m'',q)::lq, r)
479 in let (lq,r) = reduce p in
480 (List.map2
481 (fun c0 q ->
482 let c =
483 List.fold_left
484 (fun x (a,m,s) ->
485 if equal s (ppol q)
486 then
487 plusP x (mult_t_pol a m (polconst (nvar m) (coef_of_int 1)))
488 else x)
489 c0
490 lq in
491 c)
492 lcp
493 lp,
494 r)
495
496 (***********************************************************************
497 Completion
498 *)
499
500 let spol0 ps qs=
501 let p = ppol ps in
502 let q = ppol qs in
503 let m = snd (List.hd p) in
504 let m'= snd (List.hd q) in
505 let a = fst (List.hd p) in
506 let b = fst (List.hd q) in
507 let p'= List.tl p in
508 let q'= List.tl q in
509 let c = (pgcdpos a b) in
510 let m''=(ppcm_mon m m') in
511 let m1 = div_mon m'' m in
512 let m2 = div_mon m'' m' in
513 let fsp p' q' =
514 plusP
515 (mult_t_pol
516 (div_coef b c)
517 m1 p')
518 (mult_t_pol
519 (P.oppP (div_coef a c))
520 m2 q') in
521 let sp = fsp p' q' in
522 let p0 = fsp (polconst (nvar m) (coef_of_int 1)) [] in
523 let q0 = fsp [] (polconst (nvar m) (coef_of_int 1)) in
524 (sp, p0, q0)
525
526 let etrangers p p'=
527 let m = snd (List.hd p) in
528 let m'= snd (List.hd p') in
529 let d = nvar m in
530 let res=ref true in
531 let i=ref 1 in
532 while (!res) && (!i<=d) do
533 res:= ((Monomial.repr m).(!i) = 0) || ((Monomial.repr m').(!i)=0);
534 i:=!i+1;
535 done;
536 !res
537
538 let addS x l = l @ [x] (* oblige de mettre en queue sinon le certificat deconne *)
539
540 (***********************************************************************
541 critical pairs/s-polynomials
542 *)
543
544 module CPair =
545 struct
546 type t = (int * int) * Monomial.t
547 let compare ((i1, j1), m1) ((i2, j2), m2) = compare_mon m2 m1
548 end
549
550 module Heap :
551 sig
552 type elt = (int * int) * Monomial.t
553 type t
554 val length : t -> int
555 val empty : t
556 val add : elt -> t -> t
557 val pop : t -> (elt * t) option
558 end =
559 struct
560 include Heap.Functional(CPair)
561 let length h = fold (fun _ accu -> accu + 1) h 0
562 let pop h = try Some (maximum h, remove h) with Heap.EmptyHeap -> None
563 end
564
565 let ord i j =
566 if i<j then (i,j) else (j,i)
567
568 let cpair p q accu =
569 if etrangers (ppol p) (ppol q) then accu
570 else Heap.add (ord p.num q.num, ppcm_mon (lm p) (lm q)) accu
571
572 let cpairs1 p lq accu =
573 List.fold_left (fun r q -> cpair p q r) accu lq
574
575 let rec cpairs l accu = match l with
576 | [] | [_] -> accu
577 | p :: l ->
578 cpairs l (cpairs1 p l accu)
579
580 let critere3 table ((i,j),m) lp lcp =
581 List.exists
582 (fun h ->
583 h.num <> i && h.num <> j
584 && (div_mon_test m (lm h))
585 && (h.num < j
586 || not (m = ppcm_mon
587 (lm (table.allpol.(i)))
588 (lm h)))
589 && (h.num < i
590 || not (m = ppcm_mon
591 (lm (table.allpol.(j)))
592 (lm h))))
593 lp
594
595 let infobuch p q =
596 (info (fun () -> Printf.sprintf "[%i,%i]" (List.length p) (Heap.length q)))
597
598 (* in lp new polynomials are at the end *)
599
600 type certificate =
601 { coef : coef; power : int;
602 gb_comb : poly list list; last_comb : poly list }
603
604 type current_problem = {
605 cur_poly : poly;
606 cur_coef : coef;
607 }
608
609 exception NotInIdealUpdate of current_problem
610
611 let test_dans_ideal cur_pb table metadata p lp len0 =
612 (* Invariant: [lp] is [List.tl (Array.to_list table.allpol)] *)
613 let (c,r) = reduce2 table cur_pb.cur_poly lp in
614 info (fun () -> "remainder: "^(stringPcut metadata r));
615 let cur_pb = {
616 cur_coef = P.multP cur_pb.cur_coef c;
617 cur_poly = r;
618 } in
619 match r with
620 | [] ->
621 sinfo "polynomial reduced to 0";
622 let lcp = List.map (fun q -> []) lp in
623 let c = cur_pb.cur_coef in
624 let (lcq,r) = reduce2_trace table (emultP c p) lp lcp in
625 sinfo "r ok";
626 info (fun () -> "r: "^(stringP metadata r));
627 info (fun () ->
628 let fold res cq q = plusP res (multP cq (ppol q)) in
629 let res = List.fold_left2 fold (emultP c p) lcq lp in
630 "verif sum: "^(stringP metadata res)
631 );
632 info (fun () -> "coefficient: "^(stringP metadata (polconst 1 c)));
633 let coefficient_multiplicateur = c in
634 let liste_des_coefficients_intermediaires =
635 let rec aux accu lp =
636 match lp with
637 | [] -> accu
638 | p :: lp ->
639 let elt = List.map (fun q -> coefpoldep_find table p q) lp in
640 aux (elt :: accu) lp
641 in
642 let lci = aux [] (List.rev lp) in
643 CList.skipn len0 lci
644 in
645 let liste_des_coefficients =
646 List.rev_map (fun cq -> emultP (coef_of_int (-1)) cq) lcq
647 in
648 {coef = coefficient_multiplicateur;
649 power = 1;
650 gb_comb = liste_des_coefficients_intermediaires;
651 last_comb = liste_des_coefficients}
652 | _ -> raise (NotInIdealUpdate cur_pb)
653
654 let deg_hom p =
655 match p with
656 | [] -> -1
657 | (a,m)::_ -> Monomial.deg m
658
659 let pbuchf table metadata cur_pb homogeneous (lp, lpc) p =
660 (* Invariant: [lp] is [List.tl (Array.to_list table.allpol)] *)
661 sinfo "computation of the Groebner basis";
662 let () = match table.hmon with
663 | None -> ()
664 | Some hmon -> Hashtbl.clear hmon
665 in
666 let len0 = List.length lp in
667 let rec pbuchf cur_pb (lp, lpc) =
668 infobuch lp lpc;
669 match Heap.pop lpc with
670 | None ->
671 test_dans_ideal cur_pb table metadata p lp len0
672 | Some (((i, j), m), lpc2) ->
673 if critere3 table ((i,j),m) lp lpc2
674 then (sinfo "c"; pbuchf cur_pb (lp, lpc2))
675 else
676 let (a0, p0, q0) = spol0 table.allpol.(i) table.allpol.(j) in
677 if homogeneous && a0 <>[] && deg_hom a0 > deg_hom cur_pb.cur_poly
678 then (sinfo "h"; pbuchf cur_pb (lp, lpc2))
679 else
680 (* let sa = a.sugar in*)
681 match reduce2 table a0 lp with
682 _, [] -> sinfo "0";pbuchf cur_pb (lp, lpc2)
683 | ca, _ :: _ ->
684 (* info "pair reduced\n";*)
685 let map q =
686 let r =
687 if q.num == i then p0 else if q.num == j then q0 else []
688 in
689 emultP ca r
690 in
691 let lcp = List.map map lp in
692 let (lca, a0) = reduce2_trace table (emultP ca a0) lp lcp in
693 (* info "paire re-reduced";*)
694 let a = new_allpol table a0 in
695 List.iter2 (fun c q -> coefpoldep_set table a q c) lca lp;
696 let a0 = a in
697 info (fun () -> "new polynomial: "^(stringPcut metadata (ppol a0)));
698 let nlp = addS a0 lp in
699 try test_dans_ideal cur_pb table metadata p nlp len0
700 with NotInIdealUpdate cur_pb ->
701 let newlpc = cpairs1 a0 lp lpc2 in
702 pbuchf cur_pb (nlp, newlpc)
703 in pbuchf cur_pb (lp, lpc)
704
705 let is_homogeneous p =
706 match p with
707 | [] -> true
708 | (a,m)::p1 -> let d = deg m in
709 List.for_all (fun (b,m') -> deg m' =d) p1
710
711 (* returns
712 c
713 lp = [pn;...;p1]
714 p
715 lci = [[a(n+1,n);...;a(n+1,1)];
716 [a(n+2,n+1);...;a(n+2,1)];
717 ...
718 [a(n+m,n+m-1);...;a(n+m,1)]]
719 lc = [qn+m; ... q1]
720
721 such that
722 c*p = sum qi*pi
723 where pn+k = a(n+k,n+k-1)*pn+k-1 + ... + a(n+k,1)* p1
724 *)
725
726 let in_ideal metadata d lp p =
727 let table = {
728 hmon = None;
729 coefpoldep = Hashtbl.create 51;
730 nallpol = 0;
731 allpol = Array.make 1000 polynom0;
732 } in
733 let homogeneous = List.for_all is_homogeneous (p::lp) in
734 if homogeneous then sinfo "homogeneous polynomials";
735 info (fun () -> "p: "^(stringPcut metadata p));
736 info (fun () -> "lp:\n"^(List.fold_left (fun r p -> r^(stringPcut metadata p)^"\n") "" lp));
737
738 let lp = List.map (fun c -> new_allpol table c) lp in
739 List.iter (fun p -> coefpoldep_set table p p (polconst d (coef_of_int 1))) lp;
740 let cur_pb = {
741 cur_poly = p;
742 cur_coef = coef1;
743 } in
744
745 let cert =
746 try pbuchf table metadata cur_pb homogeneous (lp, Heap.empty) p
747 with NotInIdealUpdate cur_pb ->
748 try pbuchf table metadata cur_pb homogeneous (lp, cpairs lp Heap.empty) p
749 with NotInIdealUpdate _ -> raise NotInIdeal
750 in
751 sinfo "computed";
752
753 cert
754
755 end