-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathmpoly.v
More file actions
5314 lines (4327 loc) · 187 KB
/
mpoly.v
File metadata and controls
5314 lines (4327 loc) · 187 KB
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(* --------------------------------------------------------------------
* (c) Copyright 2014--2015 IMDEA Software Institute.
*
* You may distribute this file under the terms of the CeCILL-B license
* -------------------------------------------------------------------- *)
(* -------------------------------------------------------------------------- *)
(* This file provides a library for multivariate polynomials over ring *)
(* structures; it also provides an extended theory for polynomials *)
(* whose coefficients range over commutative rings and integral domains. *)
(* *)
(* 'X_{1..n} == the type of monomials in n variables. m : 'X_{1..n} *)
(* acts as a function from 'I_n to nat, returning the *)
(* power of the i-th variable in m. Notations related *)
(* to 'X_{1..n} lies in the multi_scope scope, *)
(* delimited by %MM *)
(* [multinom E i | i < n] *)
(* == the monomial in n variables whose i-th power is E(i) *)
(* mdeg m == the degree of the monomial m; i.e. *)
(* mdeg m = \sum_(i < n) (m i) *)
(* 'X_{1..n < k} == the finite type of monomials in n variables with *)
(* degree bounded by k. *)
(* (m1 <= m2)%MM == the point-wise partial order over monomials, i.e. *)
(* (m1 <= m2)%MM <=> forall i, m1 i <= m2 i *)
(* (m1 <= m2)%O == the total cpo (equipped with a cpoType) over *)
(* monomials. This is the degrevlex monomial ordering. *)
(* 0, 'U_i, m1 + m2, == 'X_{1..n} is equipped with a semi-group structure, *)
(* m1 - m2, m *+ n, ... all operations being point-wise. The substraction *)
(* is truncated when (m1 <= m2)%MM does not hold. *)
(* mlcm m1 m2 == the monomial that is the least common multiple *)
(* {mpoly R[n]} == the type of multivariate polynomials in n variables *)
(* and with coefficients of type R represented as *)
(* {free 'X_{1..n} / R}, i.e. as a formal sum over *)
(* 'X_{1..n} and with coefficients in R. *)
(* [mpoly D] == the multivariate polynomial constructed from a free *)
(* sum in {freeg 'X_{1..n} / R} *)
(* 0, 1, - p, p + q, == the usual ring operations: {mpoly R} has a canonical *)
(* p * q, p ^+ n, ... ringType structure, which is commutative / integral *)
(* when R is commutative / integral, respectively. *)
(* {ipoly R[n]} == the type obtained by iterating the univariate *)
(* polynomial type, with R as base ring. *)
(* {ipoly R[n]}^p == copy of {ipoly R[n]} with a ring canonical structure *)
(* mpcast Emn p == the {mpoly R[m]} p cast as a {mpoly R[n]} *)
(* using Emn : m = n *)
(* mwiden p == the canonical injection (ring morphism) from *)
(* {mpoly R[n]} to {mpoly R[n.+1]} *)
(* mrshift m p == the injection (ring morphism) from {mpoly R[n]} *)
(* to {mpoly R[m+n]} that maps each 'X_i to 'X_(i+m) *)
(* mpolyC c, c%:MP == the constant multivariate polynomial c *)
(* 'X_i == the variable i, for i : 'I_n *)
(* 'X_[m] == the monomial m as a multivariate polynomial *)
(* msupp p == the support of p, i.e. the m s.t. p@_m != 0 *)
(* p@_m == the coefficient of 'X_[m] in p. *)
(* msize p == 1 + the degree of p, or 0 if p = 0. *)
(* mlead p == the leading monomial of p; this is the maximum *)
(* monomial of p for the degrevlex monimial ordering. *)
(* mlead p defaults to 0%MM when p is 0. *)
(* mlast p == the smallest non-zero monomial of p for the *)
(* degrevlex monimial ordering. *)
(* mlast p defaults to 0%MM when p is 0. *)
(* mleadc p == the coefficient of the highest monomial in p; *)
(* this is a notation for p@_(mlead p). *)
(* p \is a mpolyOver S <=> the coefficients of p satisfy S; S should have a *)
(* key that should be (at least) an addrPred. *)
(* p.@[x] == the evaluation of a polynomial p at a point x, where *)
(* v is a n.-tuple R s.t. 'X_i evaluates to (tnth v i) *)
(* p^`M() == formal derivative of p w.r.t the i-th variable *)
(* p^`M(n, i) == formal n-derivative of p w.r.t the i-th variable *)
(* p^`M[m] == formal parallel (m i)-derivative of p w.r.t the *)
(* i-th variable, i ranging in {0..n.-1}. *)
(* p \mPo lq == multivariate polynomial composition, where lq is a *)
(* (n.-tuple {mpoly R[k]}) s.t. 'X_i is substituted by *)
(* (tnth lq i). *)
(* map_mpoly f p == the image of the polynomial by the function f (which *)
(* is usually a ring morphism). *)
(* p \is symmetric == p is a symmetric polynomial. *)
(* 's_(n, k) == the k-th elementary symmetric polynomial with *)
(* n indeterminates. We prove the fundamental lemma of *)
(* symmetric polynomials. *)
(* p \is d.-homog == p is a homogeneous polynomial of degree d. *)
(* -------------------------------------------------------------------------- *)
(* -------------------------------------------------------------------- *)
From Corelib Require Import Setoid.
From HB Require Import structures.
From mathcomp Require Import ssreflect ssrfun ssrbool eqtype ssrnat seq path.
From mathcomp Require Import choice fintype tuple finfun bigop finset binomial.
From mathcomp Require Import order fingroup perm ssralg zmodp poly ssrint.
From mathcomp Require Import matrix vector.
From mathcomp Require Import bigenough.
Require Import ssrcomplements freeg.
Set Implicit Arguments.
Unset Strict Implicit.
Unset Printing Implicit Defensive.
Import Order.Theory GRing.Theory BigEnough.
Local Open Scope ring_scope.
Declare Scope mpoly_scope.
Declare Scope multi_scope.
Delimit Scope mpoly_scope with MP.
Delimit Scope multi_scope with MM.
Local Notation simpm := Monoid.simpm.
Local Infix "@@" := (allpairs pair) (at level 60, right associativity).
Local Notation widen := (widen_ord (leqnSn _)).
Import Order.DefaultProdLexiOrder.
Import Order.DefaultSeqLexiOrder.
Import Order.DefaultTupleLexiOrder.
(* -------------------------------------------------------------------- *)
Reserved Notation "''X_{1..' n '}'" (n at level 2).
Reserved Notation "''X_{1..' n < b '}'" (n, b at level 2).
Reserved Notation "''X_{1..' n < b1 , b2 '}'" (b1, b2 at level 2).
Reserved Notation "[ 'multinom' s ]" (format "[ 'multinom' s ]").
Reserved Notation "[ 'multinom' 'of' s ]" (format "[ 'multinom' 'of' s ]").
Reserved Notation "[ 'multinom' F | i < n ]"
(i at level 0,
format "[ '[hv' 'multinom' F '/' | i < n ] ']'").
Reserved Notation "'U_(' n )" (format "'U_(' n )").
Reserved Notation "{ 'mpoly' T [ n ] }"
(T at level 2, format "{ 'mpoly' T [ n ] }").
Reserved Notation "[ 'mpoly' D ]" (format "[ 'mpoly' D ]").
Reserved Notation "{ 'ipoly' T [ n ] }"
(T at level 2, format "{ 'ipoly' T [ n ] }").
Reserved Notation "{ 'ipoly' T [ n ] }^p"
(T at level 2, format "{ 'ipoly' T [ n ] }^p").
Reserved Notation "''X_' i"
(at level 8, i at level 2, format "''X_' i").
Reserved Notation "''X_[' i ]" (format "''X_[' i ]").
Reserved Notation "''X_[' R , i ]" (format "''X_[' R , i ]").
Reserved Notation "c %:MP" (format "c %:MP").
Reserved Notation "c %:MP_[ n ]" (format "c %:MP_[ n ]").
Reserved Notation "c %:IP" (format "c %:IP").
Reserved Notation "s @_ i"
(at level 3, i at level 2, left associativity, format "s @_ i").
Reserved Notation "e .@[ x ]" (format "e .@[ x ]").
Reserved Notation "e .@[< x >]" (format "e .@[< x >]").
Reserved Notation "p \mPo q" (at level 50).
Reserved Notation "x ^[ f ]" (format "x ^[ f ]").
Reserved Notation "x ^[ f , g ]" (format "x ^[ f , g ]").
Reserved Notation "p ^`M ( m )"
(at level 8, format "p ^`M ( m )").
Reserved Notation "p ^`M ( m , n )"
(at level 8, format "p ^`M ( m , n )").
Reserved Notation "p ^`M [ m ]"
(at level 8, format "p ^`M [ m ]").
Reserved Notation "''s_' k"
(at level 8, k at level 2, format "''s_' k").
Reserved Notation "''s_(' n , k )" (format "''s_(' n , k )").
Reserved Notation "''s_(' K , n , k )" (format "''s_(' K , n , k )").
Reserved Notation "+%MM".
Reserved Notation "-%MM".
(* -------------------------------------------------------------------- *)
Section MultinomDef.
Context (n : nat).
Record multinom : predArgType := Multinom { multinom_val :> n.-tuple nat }.
HB.instance Definition _ := [isNew for multinom_val].
Definition fun_of_multinom M (i : 'I_n) := tnth (multinom_val M) i.
Coercion fun_of_multinom : multinom >-> Funclass.
Lemma multinomE M : Multinom M =1 tnth M.
Proof. by []. Qed.
End MultinomDef.
Notation "[ 'multinom' s ]" := (@Multinom _ s) : form_scope.
Notation "[ 'multinom' 'of' s ]" := [multinom [tuple of s]] : form_scope.
Notation "[ 'multinom' E | i < n ]" :=
[multinom [tuple E%N | i < n]] : form_scope.
Notation "[ 'multinom' E | i < n ]" :=
[multinom [tuple E%N : nat | i < n]] (only parsing) : form_scope.
(* -------------------------------------------------------------------- *)
Notation "''X_{1..' n '}'" := (multinom n) : type_scope.
HB.instance Definition _ n := [Countable of 'X_{1..n} by <:].
Bind Scope multi_scope with multinom.
(* -------------------------------------------------------------------- *)
Definition lem n (m1 m2 : 'X_{1..n}) :=
[forall i, m1%MM i <= m2%MM i].
Definition ltm n (m1 m2 : 'X_{1..n}) :=
(m1 != m2) && (lem m1 m2).
(* -------------------------------------------------------------------- *)
Section MultinomTheory.
Context {n : nat}.
Implicit Types (m : 'X_{1..n}).
Lemma mnm_tnth m j : m j = tnth m j.
Proof. by []. Qed.
Lemma mnm_nth x0 m j : m j = nth x0 m j.
Proof. by rewrite mnm_tnth (tnth_nth x0). Qed.
Lemma mnmE E j : [multinom E i | i < n] j = E j.
Proof. by rewrite multinomE tnth_mktuple. Qed.
Lemma mnm_valK t : [multinom t] = t :> n.-tuple nat.
Proof. by []. Qed.
Lemma mnmP m1 m2 : (m1 = m2) <-> (m1 =1 m2).
Proof.
case: m1 m2 => [m1] [m2] /=; split => [->//|h].
by apply/val_inj/eq_from_tnth => i; rewrite -!multinomE.
Qed.
End MultinomTheory.
(* -------------------------------------------------------------------- *)
Section MultinomMonoid.
Context {n : nat}.
Implicit Types (m : 'X_{1..n}).
Definition mnm0 := [multinom 0 | _ < n].
Definition mnm1 (c : 'I_n) := [multinom c == i | i < n].
Definition mnm_add m1 m2 := [multinom m1 i + m2 i | i < n].
Definition mnm_sub m1 m2 := [multinom m1 i - m2 i | i < n].
Definition mnm_muln m i := nosimpl iterop _ i mnm_add m mnm0.
Local Notation "0" := mnm0 : multi_scope.
Local Notation "'U_(' n )" := (mnm1 n) : multi_scope.
Local Notation "m1 + m2" := (mnm_add m1 m2) : multi_scope.
Local Notation "m1 - m2" := (mnm_sub m1 m2) : multi_scope.
Local Notation "x *+ n" := (mnm_muln x n) : multi_scope.
Local Notation "+%MM" := (@mnm_add) : function_scope.
Local Notation "-%MM" := (@mnm_sub) : function_scope.
Local Notation "m1 <= m2" := (lem m1 m2) : multi_scope.
Local Notation "m1 < m2" := (ltm m1 m2) : multi_scope.
Lemma mnm0E i : 0%MM i = 0%N. Proof. exact/mnmE. Qed.
Lemma mnmDE i m1 m2 : (m1 + m2)%MM i = (m1 i + m2 i)%N. Proof. exact/mnmE. Qed.
Lemma mnmBE i m1 m2 : (m1 - m2)%MM i = (m1 i - m2 i)%N. Proof. exact/mnmE. Qed.
Lemma mnm_sumE (I : Type) i (r : seq I) P F :
(\big[+%MM/0%MM]_(x <- r | P x) (F x)) i = (\sum_(x <- r | P x) (F x i))%N.
Proof. by apply/(big_morph (fun m => m i)) => [x y|]; rewrite mnmE. Qed.
(*-------------------------------------------------------------------- *)
Lemma mnm_lepP {m1 m2} : reflect (forall i, m1 i <= m2 i) (m1 <= m2)%MM.
Proof. exact: (iffP forallP). Qed.
Lemma lepm_refl m : (m <= m)%MM. Proof. exact/mnm_lepP. Qed.
Lemma lepm_trans m3 m1 m2 : (m1 <= m2 -> m2 <= m3 -> m1 <= m3)%MM.
Proof.
move=> h1 h2; apply/mnm_lepP => i.
exact: leq_trans (mnm_lepP h1 i) (mnm_lepP h2 i).
Qed.
Lemma addmC : commutative +%MM.
Proof. by move=> m1 m2; apply/mnmP=> i; rewrite !mnmE addnC. Qed.
Lemma addmA : associative +%MM.
Proof. by move=> m1 m2 m3; apply/mnmP=> i; rewrite !mnmE addnA. Qed.
Lemma add0m : left_id 0%MM +%MM.
Proof. by move=> m; apply/mnmP=> i; rewrite !mnmE add0n. Qed.
Lemma addm0 : right_id 0%MM +%MM.
Proof. by move=> m; rewrite addmC add0m. Qed.
HB.instance Definition _ := Monoid.isComLaw.Build 'X_{1..n} 0%MM +%MM
addmA addmC add0m.
Lemma subm0 m : (m - 0)%MM = m.
Proof. by apply/mnmP=> i; rewrite !mnmE subn0. Qed.
Lemma sub0m m : (0 - m = 0)%MM.
Proof. by apply/mnmP=> i; rewrite !mnmE sub0n. Qed.
Lemma addmK m : cancel (+%MM^~ m) (-%MM^~ m).
Proof. by move=> m' /=; apply/mnmP=> i; rewrite !mnmE addnK. Qed.
Lemma addIm : left_injective +%MM.
Proof. by move=> ? ? ? /(can_inj (@addmK _)). Qed.
Lemma addmI : right_injective +%MM.
Proof. by move=> m ? ?; rewrite ![(m + _)%MM]addmC => /addIm. Qed.
Lemma eqm_add2l m n1 n2 : (m + n1 == m + n2)%MM = (n1 == n2).
Proof. exact/inj_eq/addmI. Qed.
Lemma eqm_add2r m n1 n2 : (n1 + m == n2 + m)%MM = (n1 == n2).
Proof. exact: (inj_eq (@addIm _)). Qed.
Lemma submK m m' : (m <= m')%MM -> (m' - m + m = m')%MM.
Proof. by move/mnm_lepP=> h; apply/mnmP=> i; rewrite !mnmE subnK. Qed.
Lemma addmBA m1 m2 m3 :
(m3 <= m2)%MM -> (m1 + (m2 - m3))%MM = (m1 + m2 - m3)%MM.
Proof. by move/mnm_lepP=> h; apply/mnmP=> i; rewrite !mnmE addnBA. Qed.
Lemma submDA m1 m2 m3 : (m1 - m2 - m3)%MM = (m1 - (m2 + m3))%MM.
Proof. by apply/mnmP=> i; rewrite !mnmE subnDA. Qed.
Lemma submBA m1 m2 m3 : (m3 <= m2)%MM -> (m1 - (m2 - m3) = m1 + m3 - m2)%MM.
Proof. by move/mnm_lepP=> h; apply/mnmP=> i; rewrite !mnmE subnBA. Qed.
Lemma lem_subr m1 m2 : (m1 - m2 <= m1)%MM.
Proof. by apply/mnm_lepP=> i; rewrite !mnmE leq_subr. Qed.
Lemma lem_addr m1 m2 : (m1 <= m1 + m2)%MM.
Proof. by apply/mnm_lepP=> i; rewrite mnmDE leq_addr. Qed.
Lemma lem_addl m1 m2 : (m2 <= m1 + m2)%MM.
Proof. by apply/mnm_lepP=> i; rewrite mnmDE leq_addl. Qed.
(* -------------------------------------------------------------------- *)
Lemma mulm0n m : (m *+ 0 = 0)%MM.
Proof. by []. Qed.
Lemma mulm1n m : (m *+ 1 = m)%MM.
Proof. by []. Qed.
Lemma mulmS m i : (m *+ i.+1 = m + m *+ i)%MM.
Proof. by rewrite /mnm_muln !Monoid.iteropE iterS. Qed.
Lemma mulmSr m i : (m *+ i.+1 = m *+ i + m)%MM.
Proof. by rewrite mulmS addmC. Qed.
Lemma mulmnE m k i : ((m *+ k) i)%MM = (m i * k)%N.
Proof.
elim: k => [|k ih]; first by rewrite muln0 mulm0n !mnmE.
by rewrite mulmS mulnS mnmDE ih.
Qed.
Lemma mnm1E i j : U_(i)%MM j = (i == j). Proof. exact/mnmE. Qed.
Lemma lep1mP i m : (U_(i) <= m)%MM = (m i != 0%N).
Proof.
apply/mnm_lepP/idP=> [/(_ i)|]; rewrite -lt0n; first by rewrite mnm1E eqxx.
by move=> lt0_mi j; rewrite mnm1E; case: eqP=> // <-.
Qed.
End MultinomMonoid.
(* -------------------------------------------------------------------- *)
Notation "+%MM" := (@mnm_add _).
Notation "-%MM" := (@mnm_sub _).
Notation "0" := (@mnm0 _) : multi_scope.
Notation "'U_(' n )" := (mnm1 n) : multi_scope.
Notation "m1 + m2" := (mnm_add m1 m2) : multi_scope.
Notation "m1 - m2" := (mnm_sub m1 m2) : multi_scope.
Notation "x *+ n" := (mnm_muln x n) : multi_scope.
Notation "m1 <= m2" := (lem m1 m2) : multi_scope.
Notation "m1 < m2" := (ltm m1 m2) : multi_scope.
Notation "\sum_ ( i <- r | P ) F" :=
(\big[+%MM/0%MM]_(i <- r | P%B) F%MM) : multi_scope.
Notation "\sum_ ( i <- r ) F" :=
(\big[+%MM/0%MM]_(i <- r) F%MM) : multi_scope.
Notation "\sum_ ( m <= i < n | P ) F" :=
(\big[+%MM/0%MM]_(m <= i < n | P%B) F%MM) : multi_scope.
Notation "\sum_ ( m <= i < n ) F" :=
(\big[+%MM/0%MM]_(m <= i < n) F%MM) : multi_scope.
Notation "\sum_ ( i | P ) F" :=
(\big[+%MM/0%MM]_(i | P%B) F%MM) : multi_scope.
Notation "\sum_ i F" :=
(\big[+%MM/0%MM]_i F%MM) : multi_scope.
Notation "\sum_ ( i : t | P ) F" :=
(\big[+%MM/0%MM]_(i : t | P%B) F%MM) (only parsing) : multi_scope.
Notation "\sum_ ( i : t ) F" :=
(\big[+%MM/0%MM]_(i : t) F%MM) (only parsing) : multi_scope.
Notation "\sum_ ( i < n | P ) F" :=
(\big[+%MM/0%MM]_(i < n | P%B) F%MM) : multi_scope.
Notation "\sum_ ( i < n ) F" :=
(\big[+%MM/0%MM]_(i < n) F%MM) : multi_scope.
Notation "\sum_ ( i 'in' A | P ) F" :=
(\big[+%MM/0%MM]_(i in A | P%B) F%MM) : multi_scope.
Notation "\sum_ ( i 'in' A ) F" :=
(\big[+%MM/0%MM]_(i in A) F%MM) : multi_scope.
(* -------------------------------------------------------------------- *)
Lemma multinomUE_id n (m : 'X_{1..n}) : m = (\sum_i U_(i) *+ m i)%MM.
Proof.
apply/mnmP=> i; rewrite mnm_sumE (bigD1 i) //=.
rewrite big1; first by rewrite addn0 mulmnE mnm1E eqxx mul1n.
by move=> j ne_ji; rewrite mulmnE mnm1E (negbTE ne_ji).
Qed.
Lemma multinomUE n (s : 'S_n) (m : 'X_{1..n}) :
m = (\sum_i U_(s i) *+ m (s i))%MM.
Proof.
rewrite (reindex s^-1)%g //=; last first.
by exists s=> i _; rewrite (permK, permKV).
by rewrite [LHS]multinomUE_id; apply/eq_bigr => i _; rewrite permKV.
Qed.
(* -------------------------------------------------------------------- *)
Section MultinomDeg.
Context {n : nat}.
Implicit Types (m : 'X_{1..n}).
Definition mdeg m := (\sum_(i <- m) i)%N.
Lemma mdegE m : mdeg m = (\sum_i (m i))%N.
Proof. exact: big_tuple. Qed.
Lemma mdeg0 : mdeg 0%MM = 0%N.
Proof. by rewrite mdegE big1 // => i; rewrite mnmE. Qed.
Lemma mdeg1 i : mdeg U_(i) = 1%N.
Proof.
rewrite mdegE (bigD1 i) //= big1 => [|j]; first by rewrite mnmE eqxx addn0.
by rewrite mnmE eq_sym => /negbTE ->.
Qed.
Lemma mdegD m1 m2 : mdeg (m1 + m2) = (mdeg m1 + mdeg m2)%N.
Proof. by rewrite !mdegE -big_split; apply/eq_bigr => i _; rewrite mnmE. Qed.
Lemma mdegB m1 m2 : mdeg (m1 - m2) <= mdeg m1.
Proof. by rewrite !mdegE; apply/leq_sum => i _; rewrite mnmE leq_subr. Qed.
Lemma mdegMn m k : mdeg (m *+ k) = (mdeg m * k)%N.
Proof. by rewrite !mdegE big_distrl; apply/eq_bigr => i _; rewrite mulmnE. Qed.
Lemma mdeg_sum (I : Type) (r : seq I) P F :
mdeg (\sum_(x <- r | P x) F x) = (\sum_(x <- r | P x) mdeg (F x))%N.
Proof. exact/big_morph/mdeg0/mdegD. Qed.
Lemma mdeg_eq0 m : (mdeg m == 0%N) = (m == 0%MM).
Proof.
apply/idP/eqP=> [h|->]; last by rewrite mdeg0.
apply/mnmP=> i; move: h; rewrite mdegE mnm0E.
by rewrite (bigD1 i) //= addn_eq0 => /andP[/eqP-> _].
Qed.
Lemma mnmD_eq0 m1 m2 : (m1 + m2 == 0)%MM = (m1 == 0%MM) && (m2 == 0%MM).
Proof. by rewrite -!mdeg_eq0 mdegD addn_eq0. Qed.
Lemma mnm1_eq0 i : (U_(i) == 0 :> 'X_{1..n})%MM = false.
Proof. by rewrite -mdeg_eq0 mdeg1. Qed.
Lemma eq_mnm1 (i j : 'I_n) : (U_(i)%MM == U_(j)%MM) = (i == j).
Proof.
by apply/eqP/eqP => [/mnmP /(_ j)|->//]; rewrite !mnm1E eqxx; case: eqP.
Qed.
Lemma mdeg_eq1 m : (mdeg m == 1%N) = [exists i : 'I_n, m == U_(i)%MM].
Proof.
apply/eqP/idP=> [|/existsP[i /eqP ->]]; last by rewrite mdeg1.
rewrite [m]multinomUE_id => Hmdeg.
have: [exists i, m i != 0%N].
rewrite -negb_forall; apply/contra_eqN: Hmdeg => /forallP Hm0.
by rewrite big1 ?mdeg0 //= => i _; rewrite (eqP (Hm0 i)).
case/existsP => i Hi; apply/existsP; exists i; move: Hmdeg.
rewrite (bigD1 i) //= mdegD mdegMn mdeg1 mul1n.
case: (m i) Hi => [|[|]] //= _ [] /eqP; rewrite mdeg_eq0 => /eqP ->.
by rewrite mulm1n addm0.
Qed.
Lemma mdeg1P m : reflect (exists i, m == U_(i)%MM) (mdeg m == 1%N).
Proof. by rewrite mdeg_eq1; apply/existsP. Qed.
End MultinomDeg.
(* -------------------------------------------------------------------- *)
Section MultinomOrder.
Context {n : nat}.
Implicit Types (m : 'X_{1..n}).
Definition mnmc_le m1 m2 := (mdeg m1 :: m1 <= mdeg m2 :: m2)%O.
Definition mnmc_lt m1 m2 := (mdeg m1 :: m1 < mdeg m2 :: m2)%O.
Local Lemma lemc_refl : reflexive mnmc_le.
Proof. by move=> m; apply/le_refl. Qed.
Local Lemma lemc_anti : antisymmetric mnmc_le.
Proof. by move=> m1 m2 /le_anti [_] /val_inj/val_inj. Qed.
Local Lemma lemc_trans : transitive mnmc_le.
Proof. by move=> m2 m1 m3; apply/le_trans. Qed.
Lemma lemc_total : total mnmc_le.
Proof. by move=> m1 m2; apply/le_total. Qed.
Local Lemma ltmc_def m1 m2 : mnmc_lt m1 m2 = (m2 != m1) && mnmc_le m1 m2.
Proof.
apply/esym; rewrite andbC /mnmc_lt /mnmc_le lt_def lexi_cons eqseq_cons.
by case: ltgtP; rewrite //= 1?andbC //; apply/contra_ltN => /eqP ->.
Qed.
HB.instance Definition _ := Order.isPOrder.Build Order.default_display 'X_{1..n}
ltmc_def lemc_refl lemc_anti lemc_trans.
Lemma leEmnm m1 m2 : (m1 <= m2)%O = (mdeg m1 :: val m1 <= mdeg m2 :: val m2)%O.
Proof. by []. Qed.
Lemma ltEmnm m m' : (m < m')%O = (mdeg m :: m < mdeg m' :: m')%O.
Proof. by []. Qed.
HB.instance Definition _ :=
Order.POrder_isTotal.Build Order.default_display 'X_{1..n} lemc_total.
Lemma le0m m : (0%MM <= m)%O.
Proof.
rewrite leEmnm; have [/eqP|] := eqVneq (mdeg m) 0%N.
by rewrite mdeg_eq0 => /eqP->; rewrite lexx.
by rewrite -lt0n mdeg0 lexi_cons leEnat; case: ltngtP.
Qed.
HB.instance Definition _ :=
Order.hasBottom.Build Order.default_display 'X_{1..n} le0m.
Lemma ltmcP m1 m2 : mdeg m1 = mdeg m2 -> reflect
(exists2 i : 'I_n, forall (j : 'I_n), j < i -> m1 j = m2 j & m1 i < m2 i)
(m1 < m2)%O.
Proof.
by move=> eq_mdeg; rewrite ltEmnm eq_mdeg eqhead_ltxiE; apply: ltxi_tuplePlt.
Qed.
Lemma lemc_mdeg m1 m2 : (m1 <= m2)%O -> mdeg m1 <= mdeg m2.
Proof. by rewrite leEmnm lexi_cons leEnat; case: ltngtP. Qed.
Lemma lt_mdeg_ltmc m1 m2 : mdeg m1 < mdeg m2 -> (m1 < m2)%O.
Proof. by rewrite ltEmnm ltxi_cons leEnat; case: ltngtP. Qed.
Lemma mdeg_max m1 m2 : mdeg (m1 `|` m2)%O = maxn (mdeg m1) (mdeg m2).
Proof.
have [/lemc_mdeg|Hgt] := leP; first by case: ltngtP.
by apply/esym/maxn_idPl; apply/contra_lt_leq: Hgt => /lt_mdeg_ltmc /ltW.
Qed.
(* FIXME: introduce \max_ to replace \join_ ? This would require bOrderType. *)
Lemma mdeg_bigmax (r : seq 'X_{1..n}) :
mdeg (\join_(m <- r) m)%O = \max_(m <- r) mdeg m.
Proof.
elim: r => [|m r ih]; first by rewrite !big_nil mdeg0.
by rewrite !big_cons mdeg_max ih.
Qed.
Lemma ltmc_add2r m m1 m2 : ((m + m1)%MM < (m + m2)%MM)%O = (m1 < m2)%O.
Proof.
case: (ltngtP (mdeg m1) (mdeg m2)) => [lt|lt|].
+ by rewrite !lt_mdeg_ltmc // !mdegD ltn_add2l.
+ rewrite !ltNge !le_eqVlt !lt_mdeg_ltmc ?orbT //.
by rewrite !mdegD ltn_add2l.
move=> eq; have eqD: mdeg (m + m1) = mdeg (m + m2).
by rewrite !mdegD (rwP eqP) eqn_add2l eq.
apply/ltmcP/ltmcP => // {eq eqD} -[i eq lt]; exists i.
+ by move=> j /eq /eqP; rewrite !mnmDE (rwP eqP) eqn_add2l.
+ by move: lt; rewrite !mnmDE ltn_add2l.
+ by move=> j /eq /eqP; rewrite !mnmDE (rwP eqP) eqn_add2l.
+ by rewrite !mnmDE ltn_add2l.
Qed.
Lemma ltmc_add2l m1 m2 m : ((m1 + m)%MM < (m2 + m)%MM)%O = (m1 < m2)%O.
Proof. by rewrite ![(_+m)%MM]addmC ltmc_add2r. Qed.
Lemma lemc_add2r m m1 m2 : ((m + m1)%MM <= (m + m2)%MM)%O = (m1 <= m2)%O.
Proof. by rewrite !le_eqVlt eqm_add2l ltmc_add2r. Qed.
Lemma lemc_add2l m1 m2 m : ((m1 + m)%MM <= (m2 + m)%MM)%O = (m1 <= m2)%O.
Proof. by rewrite ![(_+m)%MM]addmC lemc_add2r. Qed.
Lemma lemc_addr m1 m2 : (m1 <= (m1 + m2)%MM)%O.
Proof. by rewrite -{1}[m1]addm0 lemc_add2r le0x. Qed.
Lemma lemc_addl m1 m2 : (m2 <= (m1 + m2)%MM)%O.
Proof. by rewrite addmC lemc_addr. Qed.
Lemma lemc_lt_add m1 m2 n1 n2 :
(m1 <= n1 -> m2 < n2 -> (m1 + m2)%MM < (n1 + n2)%MM)%O.
Proof.
move=> le lt; apply/(le_lt_trans (y := (n1 + m2)%MM)).
by rewrite lemc_add2l. by rewrite ltmc_add2r.
Qed.
Lemma ltmc_le_add m1 m2 n1 n2 :
(m1 < n1 -> m2 <= n2 -> (m1 + m2)%MM < (n1 + n2)%MM)%O.
Proof.
move=> lt le; apply/(lt_le_trans (y := (n1 + m2)%MM)).
by rewrite ltmc_add2l. by rewrite lemc_add2r.
Qed.
Lemma ltm_add m1 m2 n1 n2 :
(m1 < n1 -> m2 < n2 -> (m1 + m2)%MM < (n1 + n2)%MM)%O.
Proof. by move=> lt1 /ltW /(ltmc_le_add lt1). Qed.
Lemma lem_add m1 m2 n1 n2 :
(m1 <= n1 -> m2 <= n2 -> (m1 + m2)%MM <= (n1 + n2)%MM)%O.
Proof.
move=> le1 le2; apply/(le_trans (y := (m1 + n2)%MM)).
by rewrite lemc_add2r. by rewrite lemc_add2l.
Qed.
Lemma lem_leo m1 m2 : (m1 <= m2)%MM -> (m1 <= m2)%O.
Proof. by move=> ml; rewrite -(submK ml) -{1}[m1]add0m lem_add // le0x. Qed.
(* -------------------------------------------------------------------- *)
Section WF.
Context (P : 'X_{1..n} -> Type).
Lemma ltmwf :
(forall m1, (forall m2, (m2 < m1)%O -> P m2) -> P m1) -> forall m, P m.
Proof.
pose tof m := [tuple of mdeg m :: m].
move=> ih m; move: {2}(tof _) (erefl (tof m))=> t.
elim/(@ltxwf _ nat): t m=> //=; last first.
move=> t wih m Em; apply/ih=> m' lt_m'm.
by apply/(wih (tof m')); rewrite // -Em.
move=> Q {}ih x; elim: x {-2}x (leqnn x).
move=> x; rewrite leqn0=> /eqP->; apply/ih.
by move=> y; rewrite ltEnat/= ltn0.
move=> k wih l le_l_Sk; apply/ih=> y; rewrite ltEnat => lt_yl.
by apply/wih; have := leq_trans lt_yl le_l_Sk; rewrite ltnS.
Qed.
End WF.
Lemma ltom_wf : @well_founded 'X_{1..n} <%O.
Proof. by apply: ltmwf=> m1 IH; apply: Acc_intro => m2 /IH. Qed.
End MultinomOrder.
(* -------------------------------------------------------------------- *)
Section DegBoundMultinom.
Context (n bound : nat).
Record bmultinom := BMultinom { bmnm :> 'X_{1..n}; _ : mdeg bmnm < bound }.
HB.instance Definition _ := [isSub for bmnm].
HB.instance Definition _ := [Countable of bmultinom by <:].
Lemma bmeqP (m1 m2 : bmultinom) : (m1 == m2) = (m1 == m2 :> 'X_{1..n}).
Proof. by []. Qed.
Lemma bmdeg (m : bmultinom) : mdeg m < bound.
Proof. by case: m. Qed.
Lemma bm0_proof : mdeg (0%MM : 'X_{1..n}) < bound.+1.
Proof. by rewrite mdeg0. Qed.
End DegBoundMultinom.
Definition bm0 n b := BMultinom (bm0_proof n b).
Arguments bm0 {n b}.
Notation "''X_{1..' n < b '}'" := (bmultinom n b) : type_scope.
Notation "''X_{1..' n < b1 , b2 '}'" :=
('X_{1..n < b1} * 'X_{1..n < b2})%type : type_scope.
(* -------------------------------------------------------------------- *)
Section FinDegBound.
Context (n b : nat).
Definition bmnm_enum : seq 'X_{1..n < b} :=
let project (x : n.-tuple 'I_b) := [multinom of map val x] in
pmap insub [seq (project x) | x <- enum {: n.-tuple 'I_b }].
Lemma bmnm_enumP : Finite.axiom bmnm_enum.
Proof.
case=> m lt_dm_b /=; rewrite count_uniq_mem; last first.
rewrite (pmap_uniq (@insubK _ _ _)) 1?map_inj_uniq ?enum_uniq //.
by move=> t1 t2 [] /(inj_map val_inj) /val_inj ->.
apply/eqP; rewrite eqb1 mem_pmap_sub /=; apply/mapP.
case: b m lt_dm_b=> // b' [m] /= lt_dm_Sb; exists [tuple of map inord m].
by rewrite mem_enum.
apply/mnmP=> i; rewrite !multinomE !tnth_map inordK //.
move: lt_dm_Sb; rewrite mdegE (bigD1 i) //= multinomE.
by move=> /(leq_trans _) ->//; rewrite ltnS leq_addr.
Qed.
HB.instance Definition _ := isFinite.Build 'X_{1..n < b} bmnm_enumP.
End FinDegBound.
Section Mlcm.
Context (n : nat).
Implicit Types (m : 'X_{1..n}).
Definition mlcm m1 m2 := [multinom maxn (m1 i) (m2 i) | i < n].
Lemma mlcmC : commutative mlcm.
Proof.
by move=> m1 m2; apply/mnmP=> i; rewrite /mlcm /= !mnmE maxnC.
Qed.
Lemma mlc0m : left_id 0%MM mlcm.
Proof. by move=> m; apply/mnmP=> i; rewrite /mlcm /= !mnmE max0n. Qed.
Lemma mlcm0 : right_id 0%MM mlcm.
Proof. by move=> m; rewrite mlcmC mlc0m. Qed.
Lemma mlcmE m1 m2 : mlcm m1 m2 = (m1 + (m2 - m1))%MM.
Proof. by apply/mnmP=> i; rewrite /mlcm /= !mnmE maxnE. Qed.
Lemma lem_mlcm m m1 m2 : (mlcm m1 m2 <= m)%MM = (m1 <= m)%MM && (m2 <= m)%MM.
Proof.
apply/forallP/andP => [H|[/forallP H1 /forallP H2] i]; first split.
- by apply/forallP=> i; apply: leq_trans (H i); rewrite mnmE leq_maxl.
- by apply/forallP=> i; apply: leq_trans (H i); rewrite mnmE leq_maxr.
by rewrite mnmE geq_max H1 H2.
Qed.
Lemma lem_mlcml m1 m2 : (m1 <= mlcm m1 m2)%MM.
Proof. by apply/forallP=> i; rewrite /mlcm /= !mnmE leq_maxl. Qed.
Lemma lem_mlcmr m1 m2 : (m2 <= mlcm m1 m2)%MM.
Proof. by apply/forallP=> i; rewrite /mlcm /= !mnmE leq_maxr. Qed.
End Mlcm.
(* -------------------------------------------------------------------- *)
Section MPolyDef.
Context (n : nat) (R : ringType).
Inductive mpoly := MPoly of {freeg 'X_{1..n} / R}.
Coercion mpoly_val p := let: MPoly D := p in D.
HB.instance Definition _ := [isNew for mpoly_val].
HB.instance Definition _ := [Choice of mpoly by <:].
End MPolyDef.
Bind Scope ring_scope with mpoly.
Notation "{ 'mpoly' T [ n ] }" := (mpoly n T).
Notation "[ 'mpoly' D ]" := (@MPoly _ _ D).
(* -------------------------------------------------------------------- *)
Section MPolyTheory.
Context (n : nat) (R : ringType).
Implicit Types (p q r : {mpoly R[n]}) (D : {freeg 'X_{1..n} / R}).
Lemma mpoly_valK D : [mpoly D] = D :> {freeg _ / _}.
Proof. by []. Qed.
Lemma mpoly_eqP p q : (p = q) <-> (p = q :> {freeg _ / _}).
Proof.
split=> [->//|]; case: p q => [p] [q].
by rewrite !mpoly_valK=> ->.
Qed.
Definition mpolyC (c : R) : {mpoly R[n]} := [mpoly << c *g 0%MM >>].
Local Notation "c %:MP" := (mpolyC c) : ring_scope.
Lemma mpolyC_eq (c1 c2 : R) : (c1%:MP == c2%:MP) = (c1 == c2).
Proof.
apply/eqP/eqP=> [|->//] /eqP /freeg_eqP /(_ 0%MM).
by rewrite !coeffU eqxx !mulr1.
Qed.
Definition mcoeff (m : 'X_{1..n}) p : R := coeff m p.
Lemma mcoeff_MPoly D m : mcoeff m (MPoly D) = coeff m D.
Proof. by []. Qed.
Local Notation "p @_ i" := (mcoeff i p) : ring_scope.
Lemma mcoeffC c m : c%:MP@_m = c * (m == 0%MM)%:R.
Proof. by rewrite mcoeff_MPoly coeffU eq_sym. Qed.
Lemma mpolyCK : cancel mpolyC (mcoeff 0%MM).
Proof. by move=> c; rewrite mcoeffC eqxx mulr1. Qed.
Definition msupp p : seq 'X_{1..n} := nosimpl (dom p).
Lemma msuppE p : msupp p = dom p :> seq _.
Proof. by []. Qed.
Lemma msupp_uniq p : uniq (msupp p).
Proof. by rewrite msuppE uniq_dom. Qed.
Lemma mcoeff_msupp p m : (m \in msupp p) = (p@_m != 0).
Proof. by rewrite msuppE /mcoeff mem_dom. Qed.
Lemma memN_msupp_eq0 p m : m \notin msupp p -> p@_m = 0.
Proof. by rewrite !msuppE /mcoeff => /coeff_outdom. Qed.
Lemma mcoeff_eq0 p m : (p@_m == 0) = (m \notin msupp p).
Proof. by rewrite msuppE mem_dom /mcoeff negbK. Qed.
Lemma msupp0 : msupp 0%:MP = [::].
Proof. by rewrite msuppE /= freegU0 dom0. Qed.
Lemma msupp1 : msupp 1%:MP = [:: 0%MM].
Proof. by rewrite msuppE /= domU1. Qed.
Lemma msuppC (c : R) :
msupp c%:MP = if c == 0 then [::] else [:: 0%MM].
Proof. by have [->|nz_c] := eqVneq; [rewrite msupp0 | rewrite msuppE domU]. Qed.
Lemma mpolyP p q : (forall m, mcoeff m p = mcoeff m q) <-> (p = q).
Proof. by split=> [|->] // h; apply/mpoly_eqP/eqP/freeg_eqP/h. Qed.
Lemma freeg_mpoly p: p = [mpoly \sum_(m <- msupp p) << p@_m *g m >>].
Proof. by case: p=> p; apply/mpoly_eqP; rewrite /= -{1}[p]freeg_sumE. Qed.
End MPolyTheory.
Notation "c %:MP" := (mpolyC _ c) : ring_scope.
Notation "c %:MP_[ n ]" := (mpolyC n c) : ring_scope.
Notation "p @_ i" := (mcoeff i p) : ring_scope.
#[global] Hint Resolve msupp_uniq : core.
(* -------------------------------------------------------------------- *)
Section NVar0.
Context (n : nat) (R : ringType).
Implicit Types (p q r : {mpoly R[n]}).
Lemma nvar0_mnmE : @all_equal_to 'X_{1..0} 0%MM.
Proof. by move=> mon; apply/mnmP; case. Qed.
Lemma nvar0_mpolyC (p : {mpoly R[0]}): p = (p@_0%MM)%:MP.
Proof. by apply/mpolyP=> m; rewrite mcoeffC nvar0_mnmE eqxx mulr1. Qed.
Lemma nvar0_mpolyC_eq p : n = 0%N -> p = (p@_0%MM)%:MP.
Proof. by move=> z_p; move:p; rewrite z_p; apply/nvar0_mpolyC. Qed.
End NVar0.
(* -------------------------------------------------------------------- *)
Section MPolyZMod.
Context (n : nat) (R : ringType).
Implicit Types (p q r : {mpoly R[n]}).
Definition mpoly_opp p := [mpoly - mpoly_val p].
Definition mpoly_add p q := [mpoly mpoly_val p + mpoly_val q].
Lemma add_mpoly0 : left_id 0%:MP mpoly_add.
Proof. by move=> p; apply/mpoly_eqP; rewrite !mpoly_valK freegU0 add0r. Qed.
Lemma add_mpolyN : left_inverse 0%:MP mpoly_opp mpoly_add.
Proof. by move=> p; apply/mpoly_eqP; rewrite !mpoly_valK freegU0 addrC subrr. Qed.
Lemma add_mpolyC : commutative mpoly_add.
Proof. by move=> p q; apply/mpoly_eqP; rewrite !mpoly_valK addrC. Qed.
Lemma add_mpolyA : associative mpoly_add.
Proof. by move=> p q r; apply/mpoly_eqP; rewrite !mpoly_valK addrA. Qed.
HB.instance Definition _ := GRing.isZmodule.Build (mpoly n R)
add_mpolyA add_mpolyC add_mpoly0 add_mpolyN.
HB.instance Definition _ := GRing.Zmodule.on {mpoly R[n]}.
Definition mpoly_scale c p := [mpoly c *: mpoly_val p].
Local Notation "c *:M p" := (mpoly_scale c p) (at level 40, left associativity).
Lemma scale_mpolyA c1 c2 p : c1 *:M (c2 *:M p) = (c1 * c2) *:M p.
Proof. by apply/mpoly_eqP; rewrite !mpoly_valK scalerA. Qed.
Lemma scale_mpoly1m p : 1 *:M p = p.
Proof. by apply/mpoly_eqP; rewrite !mpoly_valK scale1r. Qed.
Lemma scale_mpolyDr c p1 p2 : c *:M (p1 + p2) = c *:M p1 + c *:M p2.
Proof. by apply/mpoly_eqP; rewrite !mpoly_valK scalerDr. Qed.
Lemma scale_mpolyDl p c1 c2 : (c1 + c2) *:M p = c1 *:M p + c2 *:M p.
Proof. by apply/mpoly_eqP; rewrite !mpoly_valK scalerDl. Qed.
HB.instance Definition _ := GRing.Zmodule_isLmodule.Build R (mpoly n R)
scale_mpolyA scale_mpoly1m scale_mpolyDr scale_mpolyDl.
HB.instance Definition _ := GRing.Lmodule.on {mpoly R[n]}.
Local Notation mcoeff := (@mcoeff n R).
Lemma mcoeff_is_additive m : additive (mcoeff m).
Proof. by move=> p q /=; rewrite /mcoeff raddfB. Qed.
HB.instance Definition _ m := GRing.isAdditive.Build {mpoly R[n]} R (mcoeff m)
(mcoeff_is_additive m).
Lemma mcoeff0 m : mcoeff m 0 = 0 . Proof. exact: raddf0. Qed.
Lemma mcoeffN m : {morph mcoeff m: x / - x} . Proof. exact: raddfN. Qed.
Lemma mcoeffD m : {morph mcoeff m: x y / x + y}. Proof. exact: raddfD. Qed.
Lemma mcoeffB m : {morph mcoeff m: x y / x - y}. Proof. exact: raddfB. Qed.
Lemma mcoeffMn m k : {morph mcoeff m: x / x *+ k} . Proof. exact: raddfMn. Qed.
Lemma mcoeffMNn m k : {morph mcoeff m: x / x *- k} . Proof. exact: raddfMNn. Qed.
Lemma mcoeffZ c p m : mcoeff m (c *: p) = c * (mcoeff m p).
Proof. by rewrite /mcoeff coeffZ. Qed.
HB.instance Definition _ m :=
GRing.isScalable.Build R {mpoly R[n]} R *%R (mcoeff m)
(fun c => (mcoeffZ c)^~ m).
Local Notation mpolyC := (@mpolyC n R).
Lemma mpolyC_is_additive : additive mpolyC.
Proof. by move=> p q; apply/mpoly_eqP; rewrite /= freegUB. Qed.
HB.instance Definition _ := GRing.isAdditive.Build R {mpoly R[n]} mpolyC
mpolyC_is_additive.
Lemma mpolyC0 : mpolyC 0 = 0 . Proof. exact: raddf0. Qed.
Lemma mpolyCN : {morph mpolyC: x / - x} . Proof. exact: raddfN. Qed.
Lemma mpolyCD : {morph mpolyC: x y / x + y}. Proof. exact: raddfD. Qed.
Lemma mpolyCB : {morph mpolyC: x y / x - y}. Proof. exact: raddfB. Qed.
Lemma mpolyCMn k : {morph mpolyC: x / x *+ k} . Proof. exact: raddfMn. Qed.
Lemma mpolyCMNn k : {morph mpolyC: x / x *- k} . Proof. exact: raddfMNn. Qed.
Lemma msupp_eq0 p : (msupp p == [::]) = (p == 0).
Proof.
case: p=> p /=; rewrite msuppE /GRing.zero /= /mpolyC.
by rewrite dom_eq0 freegU0 /=.
Qed.
Lemma msuppnil0 p : msupp p = [::] -> p = 0.
Proof. by move/eqP; rewrite msupp_eq0 => /eqP. Qed.
Lemma mpolyC_eq0 c : (c%:MP == 0 :> {mpoly R[n]}) = (c == 0).
Proof.
rewrite eqE /=; apply/idP/eqP=> [/freeg_eqP/(_ 0%MM)|->//].
by rewrite !coeffU eqxx !mulr1.
Qed.
End MPolyZMod.
(* -------------------------------------------------------------------- *)
HB.mixin Record isMeasure (n : nat) (mf : 'X_{1..n} -> nat) := {
mf0 : mf 0%MM = 0%N;
mfD : {morph mf : m1 m2 / (m1 + m2)%MM >-> (m1 + m2)%N};
}.
#[short(type="measure")]
HB.structure Definition Measure (n : nat) := {mf of isMeasure n mf}.
#[deprecated(since="multinomials 2.2.0", note="Use Measure.clone instead.")]
Notation "[ 'measure' 'of' f ]" := (Measure.clone _ f _)
(at level 0, only parsing) : form_scope.
(* -------------------------------------------------------------------- *)
#[hnf] HB.instance Definition _ n := isMeasure.Build n mdeg mdeg0 mdegD.
(* -------------------------------------------------------------------- *)
Section MMeasure.
Context (n : nat) (R : ringType) (mf : measure n).
Implicit Types (m : 'X_{1..n}) (p q : {mpoly R[n]}).
Lemma mfE m : mf m = (\sum_(i < n) (m i) * mf U_(i)%MM)%N.
Proof.
rewrite {1}(multinomUE_id m) (big_morph mf mfD mf0); apply/eq_bigr => i _.
elim: (m i) => [// | d ih] /=; first by rewrite mul0n mulm0n mf0.
by rewrite mulmS mulSn mfD ih.
Qed.
Definition mmeasure p := (\max_(m <- msupp p) (mf m).+1)%N.
Lemma mmeasureE p : mmeasure p = (\max_(m <- msupp p) (mf m).+1)%N.
Proof. by []. Qed.
Lemma mmeasure0 : mmeasure 0 = 0%N.
Proof. by rewrite /mmeasure msupp0 big_nil. Qed.
Lemma mmeasure_mnm_lt p m : m \in msupp p -> mf m < mmeasure p.
Proof. by move=> m_in_p; rewrite /mmeasure (bigD1_seq m) //= leq_max leqnn. Qed.
Lemma mmeasure_mnm_ge p m : mmeasure p <= mf m -> m \notin msupp p.
Proof. by apply/contra_leqN => /mmeasure_mnm_lt. Qed.
End MMeasure.
(* -------------------------------------------------------------------- *)
Section MSuppZMod.
Context (n : nat) (R : ringType).
Implicit Types (p q r : {mpoly R[n]}) (D : {freeg 'X_{1..n} / R}).
Lemma msuppN p : perm_eq (msupp (-p)) (msupp p).
Proof. by apply/domN_perm_eq. Qed.
Lemma msuppD_le p q : {subset msupp (p + q) <= msupp p ++ msupp q}.
Proof. by move=> x /domD_subset. Qed.
Lemma msuppB_le p q : {subset msupp (p - q) <= msupp p ++ msupp q}.
Proof. by move=> x /msuppD_le; rewrite !mem_cat (perm_mem (msuppN _)). Qed.
Lemma msuppD (p1 p2 : {mpoly R[n]}) :
[predI (msupp p1) & (msupp p2)] =1 xpred0
-> perm_eq (msupp (p1 + p2)) (msupp p1 ++ msupp p2).
Proof. by apply/domD_perm_eq. Qed.