Debian Codegraph @

plugins/nsatz/polynom.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 (* Recursive polynomials: R[x1]...[xn]. *)
12 open Util
13 open Utile
14
15 (* 1. Coefficients: R *)
16
17 module type Coef = sig
18 type t
19 val equal : t -> t -> bool
20 val lt : t -> t -> bool
21 val le : t -> t -> bool
22 val abs : t -> t
23 val plus : t -> t -> t
24 val mult : t -> t -> t
25 val sub : t -> t -> t
26 val opp : t -> t
27 val div : t -> t -> t
28 val modulo : t -> t -> t
29 val puis : t -> int -> t
30 val pgcd : t -> t -> t
31
32 val hash : t -> int
33 val of_num : Q.t -> t
34 val to_string : t -> string
35 end
36
37 module type S = sig
38 type coef
39 type variable = int
40 type t = Pint of coef | Prec of variable * t array
41
42 val of_num : Q.t -> t
43 val x : variable -> t
44 val monome : variable -> int -> t
45 val is_constantP : t -> bool
46 val is_zero : t -> bool
47
48 val max_var_pol : t -> variable
49 val max_var_pol2 : t -> variable
50 val max_var : t array -> variable
51 val equal : t -> t -> bool
52 val norm : t -> t
53 val deg : variable -> t -> int
54 val deg_total : t -> int
55 val copyP : t -> t
56 val coef : variable -> int -> t -> t
57
58 val plusP : t -> t -> t
59 val content : t -> coef
60 val div_int : t -> coef -> t
61 val vire_contenu : t -> t
62 val vars : t -> variable list
63 val int_of_Pint : t -> coef
64 val multx : int -> variable -> t -> t
65 val multP : t -> t -> t
66 val deriv : variable -> t -> t
67 val oppP : t -> t
68 val moinsP : t -> t -> t
69 val puisP : t -> int -> t
70 val ( @@ ) : t -> t -> t
71 val ( -- ) : t -> t -> t
72 val ( ^^ ) : t -> int -> t
73 val coefDom : variable -> t -> t
74 val coefConst : variable -> t -> t
75 val remP : variable -> t -> t
76 val coef_int_tete : t -> coef
77 val normc : t -> t
78 val coef_constant : t -> coef
79 val univ : bool ref
80 val string_of_var : int -> string
81 val nsP : int ref
82 val to_string : t -> string
83 val printP : t -> unit
84 val print_tpoly : t array -> unit
85 val print_lpoly : t list -> unit
86 val quo_rem_pol : t -> t -> variable -> t * t
87 val div_pol : t -> t -> variable -> t
88 val divP : t -> t -> t
89 val div_pol_rat : t -> t -> bool
90 val pseudo_div : t -> t -> variable -> t * t * int * t
91 val pgcdP : t -> t -> t
92 val pgcd_pol : t -> t -> variable -> t
93 val content_pol : t -> variable -> t
94 val pgcd_coef_pol : t -> t -> variable -> t
95 val pgcd_pol_rec : t -> t -> variable -> t
96 val gcd_sub_res : t -> t -> variable -> t
97 val gcd_sub_res_rec : t -> t -> t -> t -> int -> variable -> t
98 val lazard_power : t -> t -> int -> variable -> t
99 val hash : t -> int
100 module Hashpol : Hashtbl.S with type key=t
101 end
102
103 (***********************************************************************
104 2. Type of polynomials, operations.
105 *)
106 module Make (C:Coef) = struct
107
108 type coef = C.t
109 let coef_of_int i = C.of_num (Q.of_int i)
110 let coef0 = coef_of_int 0
111 let coef1 = coef_of_int 1
112
113 type variable = int
114
115 type t =
116 Pint of coef (* constant polynomial *)
117 | Prec of variable * (t array) (* coefficients, increasing degree *)
118
119 (* by default, operations work with normalized polynomials:
120 - variables are positive integers
121 - coefficients of a polynomial in x only use variables < x
122 - no zero coefficient at beginning
123 - no Prec(x,a) where a is constant in x
124 *)
125
126 (* constant polynomials *)
127 let of_num x = Pint (C.of_num x)
128 let cf0 = of_num Q.zero
129 let cf1 = of_num Q.one
130
131 (* nth variable *)
132 let x n = Prec (n,[|cf0;cf1|])
133
134 (* create v^n *)
135 let monome v n =
136 match n with
137 0->Pint coef1;
138 |_->let tmp = Array.make (n+1) (Pint coef0) in
139 tmp.(n)<-(Pint coef1);
140 Prec (v, tmp)
141
142 let is_constantP = function
143 Pint _ -> true
144 | Prec _ -> false
145
146 let int_of_Pint = function
147 Pint x -> x
148 | _ -> failwith "non"
149
150 let is_zero p =
151 match p with Pint n -> if C.equal n coef0 then true else false |_-> false
152
153 let max_var_pol p =
154 match p with
155 Pint _ -> 0
156 |Prec(x,_) -> x
157
158 (* p not normalized *)
159 let rec max_var_pol2 p =
160 match p with
161 Pint _ -> 0
162 |Prec(v,c)-> Array.fold_right (fun q m -> max (max_var_pol2 q) m) c v
163
164 let max_var l = Array.fold_right (fun p m -> max (max_var_pol2 p) m) l 0
165
166 (* equality between polynomials *)
167
168 let rec equal p q =
169 match (p,q) with
170 (Pint a,Pint b) -> C.equal a b
171 |(Prec(x,p1),Prec(y,q1)) -> (Int.equal x y) && Array.for_all2 equal p1 q1
172 | (_,_) -> false
173
174 (* normalize polynomial: remove head zeros, coefficients are normalized
175 if constant, returns the coefficient
176 *)
177
178 let norm p = match p with
179 Pint _ -> p
180 |Prec (x,a)->
181 let d = (Array.length a -1) in
182 let n = ref d in
183 while !n>0 && (equal a.(!n) (Pint coef0)) do
184 n:=!n-1;
185 done;
186 if !n<0 then Pint coef0
187 else if Int.equal !n 0 then a.(0)
188 else if Int.equal !n d then p
189 else (let b=Array.make (!n+1) (Pint coef0) in
190 for i=0 to !n do b.(i)<-a.(i);done;
191 Prec(x,b))
192
193
194 (* degree in v, v >= max var of p *)
195 let deg v p =
196 match p with
197 Prec(x,p1) when Int.equal x v -> Array.length p1 -1
198 |_ -> 0
199
200
201 (* total degree *)
202 let rec deg_total p =
203 match p with
204 Prec (x,p1) -> let d = ref 0 in
205 Array.iteri (fun i q -> d:= (max !d (i+(deg_total q)))) p1;
206 !d
207 |_ -> 0
208
209 let rec copyP p =
210 match p with
211 Pint i -> Pint i
212 |Prec(x,q) -> Prec(x,Array.map copyP q)
213
214 (* coefficient of degree i in v, v >= max var of p *)
215 let coef v i p =
216 match p with
217 Prec (x,p1) when Int.equal x v -> if i<(Array.length p1) then p1.(i) else Pint coef0
218 |_ -> if Int.equal i 0 then p else Pint coef0
219
220 (* addition *)
221
222 let rec plusP p q =
223 let res =
224 (match (p,q) with
225 (Pint a,Pint b) -> Pint (C.plus a b)
226 |(Pint a, Prec (y,q1)) -> let q2=Array.map copyP q1 in
227 q2.(0)<- plusP p q1.(0);
228 Prec (y,q2)
229 |(Prec (x,p1),Pint b) -> let p2=Array.map copyP p1 in
230 p2.(0)<- plusP p1.(0) q;
231 Prec (x,p2)
232 |(Prec (x,p1),Prec (y,q1)) ->
233 if x<y then (let q2=Array.map copyP q1 in
234 q2.(0)<- plusP p q1.(0);
235 Prec (y,q2))
236 else if x>y then (let p2=Array.map copyP p1 in
237 p2.(0)<- plusP p1.(0) q;
238 Prec (x,p2))
239 else
240 (let n=max (deg x p) (deg x q) in
241 let r=Array.make (n+1) (Pint coef0) in
242 for i=0 to n do
243 r.(i)<- plusP (coef x i p) (coef x i q);
244 done;
245 Prec(x,r)))
246 in norm res
247
248
249 (* content, positive integer *)
250 let rec content p =
251 match p with
252 Pint a -> C.abs a
253 | Prec (x ,p1) ->
254 Array.fold_left C.pgcd coef0 (Array.map content p1)
255
256 let rec div_int p a=
257 match p with
258 Pint b -> Pint (C.div b a)
259 | Prec(x,p1) -> Prec(x,Array.map (fun x -> div_int x a) p1)
260
261 let vire_contenu p =
262 let c = content p in
263 if C.equal c coef0 then p else div_int p c
264
265 (* sorted list of variables of a polynomial *)
266
267 let rec vars=function
268 Pint _->[]
269 | Prec (x,l)->(List.flatten ([x]::(List.map vars (Array.to_list l))))
270
271
272 (* multiply p by v^n, v >= max_var p *)
273 let multx n v p =
274 match p with
275 Prec (x,p1) when Int.equal x v -> let p2= Array.make ((Array.length p1)+n) (Pint coef0) in
276 for i=0 to (Array.length p1)-1 do
277 p2.(i+n)<-p1.(i);
278 done;
279 Prec (x,p2)
280 |_ -> if equal p (Pint coef0) then (Pint coef0)
281 else (let p2=Array.make (n+1) (Pint coef0) in
282 p2.(n)<-p;
283 Prec (v,p2))
284
285 (* product *)
286 let rec multP p q =
287 match (p,q) with
288 (Pint a,Pint b) -> Pint (C.mult a b)
289 |(Pint a, Prec (y,q1)) ->
290 if C.equal a coef0 then Pint coef0
291 else let q2 = Array.map (fun z-> multP p z) q1 in
292 Prec (y,q2)
293
294 |(Prec (x,p1), Pint b) ->
295 if C.equal b coef0 then Pint coef0
296 else let p2 = Array.map (fun z-> multP z q) p1 in
297 Prec (x,p2)
298 |(Prec (x,p1), Prec(y,q1)) ->
299 if x<y
300 then (let q2 = Array.map (fun z-> multP p z) q1 in
301 Prec (y,q2))
302 else if x>y
303 then (let p2 = Array.map (fun z-> multP z q) p1 in
304 Prec (x,p2))
305 else Array.fold_left plusP (Pint coef0)
306 (Array.mapi (fun i z-> (multx i x (multP z q))) p1)
307
308
309
310 (* derive p with variable v, v >= max_var p *)
311 let deriv v p =
312 match p with
313 Pint a -> Pint coef0
314 | Prec(x,p1) when Int.equal x v ->
315 let d = Array.length p1 -1 in
316 if Int.equal d 1 then p1.(1)
317 else
318 (let p2 = Array.make d (Pint coef0) in
319 for i=0 to d-1 do
320 p2.(i)<- multP (Pint (coef_of_int (i+1))) p1.(i+1);
321 done;
322 Prec (x,p2))
323 | Prec(x,p1)-> Pint coef0
324
325
326 (* opposite *)
327 let rec oppP p =
328 match p with
329 Pint a -> Pint (C.opp a)
330 |Prec(x,p1) -> Prec(x,Array.map oppP p1)
331
332 let moinsP p q=plusP p (oppP q)
333
334 let rec puisP p n = match n with
335 0 -> cf1
336 |_ -> (multP p (puisP p (n-1)))
337
338
339 (* infix notations *)
340 (*let (++) a b = plusP a b
341 *)
342 let (@@) a b = multP a b
343
344 let (--) a b = moinsP a b
345
346 let (^^) a b = puisP a b
347
348
349 (* leading coefficient in v, v>= max_var p *)
350
351 let coefDom v p= coef v (deg v p) p
352
353 let coefConst v p = coef v 0 p
354
355 (* tail of a polynomial *)
356 let remP v p =
357 moinsP p (multP (coefDom v p) (puisP (x v) (deg v p)))
358
359
360 (* first integer coefficient of p *)
361 let rec coef_int_tete p =
362 let v = max_var_pol p in
363 if v>0
364 then coef_int_tete (coefDom v p)
365 else (match p with | Pint a -> a |_ -> assert false)
366
367
368 (* divide by the content and make the head int coef positive *)
369 let normc p =
370 let p = vire_contenu p in
371 let a = coef_int_tete p in
372 if C.le coef0 a then p else oppP p
373
374
375 (* constant coef of normalized polynomial *)
376 let rec coef_constant p =
377 match p with
378 Pint a->a
379 |Prec(_,q)->coef_constant q.(0)
380
381
382 (***********************************************************************
383 3. Printing polynomials.
384 *)
385
386 (* if univ = false, we use x,y,z,a,b,c,d... as variables, else x1,x2,...
387 *)
388 let univ=ref true
389
390 let string_of_var x=
391 if !univ then
392 "u"^(string_of_int x)
393 else
394 if x<=3 then String.make 1 (Char.chr(x+(Char.code 'w')))
395 else String.make 1 (Char.chr(x-4+(Char.code 'a')))
396
397 let nsP = ref 0
398
399 let rec string_of_Pcut p =
400 if (!nsP)<=0
401 then "..."
402 else
403 match p with
404 |Pint a-> nsP:=(!nsP)-1;
405 if C.le coef0 a
406 then C.to_string a
407 else "("^(C.to_string a)^")"
408 |Prec (x,t)->
409 let v=string_of_var x
410 and s=ref ""
411 and sp=ref "" in
412 let st0 = string_of_Pcut t.(0) in
413 if not (String.equal st0 "0")
414 then s:=st0;
415 let fin = ref false in
416 for i=(Array.length t)-1 downto 1 do
417 if (!nsP)<0
418 then (sp:="...";
419 if not (!fin) then s:=(!s)^"+"^(!sp);
420 fin:=true)
421 else (
422 let si=string_of_Pcut t.(i) in
423 sp:="";
424 if Int.equal i 1
425 then (
426 if not (String.equal si "0")
427 then (nsP:=(!nsP)-1;
428 if String.equal si "1"
429 then sp:=v
430 else
431 (if (String.contains si '+')
432 then sp:="("^si^")*"^v
433 else sp:=si^"*"^v)))
434 else (
435 if not (String.equal si "0")
436 then (nsP:=(!nsP)-1;
437 if String.equal si "1"
438 then sp:=v^"^"^(string_of_int i)
439 else (if (String.contains si '+')
440 then sp:="("^si^")*"^v^"^"^(string_of_int i)
441 else sp:=si^"*"^v^"^"^(string_of_int i))));
442 if not (String.is_empty !sp) && not (!fin)
443 then (nsP:=(!nsP)-1;
444 if String.is_empty !s
445 then s:=!sp
446 else s:=(!s)^"+"^(!sp)));
447 done;
448 if String.is_empty !s then (nsP:=(!nsP)-1;
449 (s:="0"));
450 !s
451
452 let to_string p =
453 nsP:=20;
454 string_of_Pcut p
455
456 let printP p = Format.printf "@[%s@]" (to_string p)
457
458 let print_tpoly lp =
459 let s = ref "\n{ " in
460 Array.iter (fun p -> s:=(!s)^(to_string p)^"\n") lp;
461 prt0 ((!s)^"}")
462
463 let print_lpoly lp = print_tpoly (Array.of_list lp)
464
465 (***********************************************************************
466 4. Exact division of polynomials.
467 *)
468
469 (* return (s,r) s.t. p = s*q+r *)
470 let rec quo_rem_pol p q x =
471 if Int.equal x 0
472 then (match (p,q) with
473 |(Pint a, Pint b) ->
474 if C.equal (C.modulo a b) coef0
475 then (Pint (C.div a b), cf0)
476 else failwith "div_pol1"
477 |_ -> assert false)
478 else
479 let m = deg x q in
480 let b = coefDom x q in
481 let q1 = remP x q in (* q = b*x^m+q1 *)
482 let r = ref p in
483 let s = ref cf0 in
484 let continue =ref true in
485 while (!continue) && (not (equal !r cf0)) do
486 let n = deg x !r in
487 if n<m
488 then continue:=false
489 else (
490 let a = coefDom x !r in
491 let p1 = remP x !r in (* r = a*x^n+p1 *)
492 let c = div_pol a b (x-1) in (* a = c*b *)
493 let s1 = c @@ ((monome x (n-m))) in
494 s:= plusP (!s) s1;
495 r:= p1 -- (s1 @@ q1);
496 )
497 done;
498 (!s,!r)
499
500 (* returns quotient p/q if q divides p, else fails *)
501 and div_pol p q x =
502 let (s,r) = quo_rem_pol p q x in
503 if equal r cf0
504 then s
505 else failwith ("div_pol:\n"
506 ^"p:"^(to_string p)^"\n"
507 ^"q:"^(to_string q)^"\n"
508 ^"r:"^(to_string r)^"\n"
509 ^"x:"^(string_of_int x)^"\n"
510 )
511 let divP p q=
512 let x = max (max_var_pol p) (max_var_pol q) in
513 div_pol p q x
514
515 let div_pol_rat p q=
516 let x = max (max_var_pol p) (max_var_pol q) in
517 try
518 let r = puisP (Pint(coef_int_tete q)) (1+(deg x p)-(deg x q)) in
519 let _ = div_pol (multP p r) q x in
520 true
521 with Failure _ -> false
522
523 (***********************************************************************
524 5. Pseudo-division and gcd with subresultants.
525 *)
526
527 (* pseudo division :
528 q = c*x^m+q1
529 returns (r,c,d,s) s.t. c^d*p = s*q + r.
530 *)
531
532 let pseudo_div p q x =
533 match q with
534 Pint _ -> (cf0, q,1, p)
535 | Prec (v,q1) when not (Int.equal x v) -> (cf0, q,1, p)
536 | Prec (v,q1) ->
537 (
538 (* pr "pseudo_division: c^d*p = s*q + r";*)
539 let delta = ref 0 in
540 let r = ref p in
541 let c = coefDom x q in
542 let q1 = remP x q in
543 let d' = deg x q in
544 let s = ref cf0 in
545 while (deg x !r)>=(deg x q) do
546 let d = deg x !r in
547 let a = coefDom x !r in
548 let r1=remP x !r in
549 let u = a @@ ((monome x (d-d'))) in
550 r:=(c @@ r1) -- (u @@ q1);
551 s:=plusP (c @@ (!s)) u;
552 delta := (!delta) + 1;
553 done;
554 (*
555 pr ("deg d: "^(string_of_int (!delta))^", deg c: "^(string_of_int (deg_total c)));
556 pr ("deg r:"^(string_of_int (deg_total !r)));
557 *)
558 (!r,c,!delta, !s)
559 )
560
561 (* gcd with subresultants *)
562
563 let rec pgcdP p q =
564 let x = max (max_var_pol p) (max_var_pol q) in
565 pgcd_pol p q x
566
567 and pgcd_pol p q x =
568 pgcd_pol_rec p q x
569
570 and content_pol p x =
571 match p with
572 Prec(v,p1) when Int.equal v x ->
573 Array.fold_left (fun a b -> pgcd_pol_rec a b (x-1)) cf0 p1
574 | _ -> p
575
576 and pgcd_coef_pol c p x =
577 match p with
578 Prec(v,p1) when Int.equal x v ->
579 Array.fold_left (fun a b -> pgcd_pol_rec a b (x-1)) c p1
580 |_ -> pgcd_pol_rec c p (x-1)
581
582 and pgcd_pol_rec p q x =
583 match (p,q) with
584 (Pint a,Pint b) -> Pint (C.pgcd (C.abs a) (C.abs b))
585 |_ ->
586 if equal p cf0
587 then q
588 else if equal q cf0
589 then p
590 else if Int.equal (deg x q) 0
591 then pgcd_coef_pol q p x
592 else if Int.equal (deg x p) 0
593 then pgcd_coef_pol p q x
594 else (
595 let a = content_pol p x in
596 let b = content_pol q x in
597 let c = pgcd_pol_rec a b (x-1) in
598 pr (string_of_int x);
599 let p1 = div_pol p c x in
600 let q1 = div_pol q c x in
601 let r = gcd_sub_res p1 q1 x in
602 let cr = content_pol r x in
603 let res = c @@ (div_pol r cr x) in
604 res
605 )
606
607 (* Sub-résultants:
608
609 ai*Ai = Qi*Ai+1 + bi*Ai+2
610
611 deg Ai+2 < deg Ai+1
612
613 Ai = ci*X^ni + ...
614 di = ni - ni+1
615
616 ai = (- ci+1)^(di + 1)
617 b1 = 1
618 bi = ci*si^di si i>1
619
620 s1 = 1
621 si+1 = ((ci+1)^di*si)/si^di
622
623 *)
624 and gcd_sub_res p q x =
625 if equal q cf0
626 then p
627 else
628 let d = deg x p in
629 let d' = deg x q in
630 if d<d'
631 then gcd_sub_res q p x
632 else
633 let delta = d-d' in
634 let c' = coefDom x q in
635 let r = snd (quo_rem_pol (((oppP c')^^(delta+1))@@p) (oppP q) x) in
636 gcd_sub_res_rec q r (c'^^delta) c' d' x
637
638 and gcd_sub_res_rec p q s c d x =
639 if equal q cf0
640 then p
641 else (
642 let d' = deg x q in
643 let c' = coefDom x q in
644 let delta = d-d' in
645 let r = snd (quo_rem_pol (((oppP c')^^(delta+1))@@p) (oppP q) x) in
646 let s'= lazard_power c' s delta x in
647 gcd_sub_res_rec q (div_pol r (c @@ (s^^delta)) x) s' c' d' x
648 )
649
650 and lazard_power c s d x =
651 let res = ref c in
652 for _i = 1 to d - 1 do
653 res:= div_pol ((!res)@@c) s x;
654 done;
655 !res
656
657 (* memoizations *)
658
659 let rec hash = function
660 Pint a -> (C.hash a)
661 | Prec (v,p) ->
662 Array.fold_right (fun q h -> h + hash q) p 0
663
664 module Hashpol = Hashtbl.Make(
665 struct
666 type poly = t
667 type t = poly
668 let equal = equal
669 let hash = hash
670 end)
671
672 end