-
Notifications
You must be signed in to change notification settings - Fork 0
/
RngUPol.mag
1360 lines (1256 loc) · 46.2 KB
/
RngUPol.mag
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
// This file is part of ExactpAdics
// Copyright (C) 2018 Christopher Doris
//
// ExactpAdics is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ExactpAdics is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with ExactpAdics. If not, see <http://www.gnu.org/licenses/>.
///# Univariate polynomials
///toc
import "Utils.mag": Z, Q, TRIM_PR, CAP_PR, SHIFTARG, SHIFTSLOPE, WVAL, WZERO, OO;
import "Promotion.mag": do_binop;
declare type RngUPol_FldPadExact[RngUPolElt_FldPadExact]: StrPadExact;
declare attributes RngUPol_FldPadExact
: base_ring // the base ring
, varname // name of the variable
// cache
, generator // Generator(*), *.1, Name(*,1)
, zero // Zero(*)
, one // One(*)
;
declare type RngUPolElt_FldPadExact: PadExactElt;
declare attributes RngUPolElt_FldPadExact
// cache
: degree // Degree(*)
, coefficient // i -> Coefficient(*, i)
, coefficients // Coefficients(*)
, is_eisenstein // IsEisenstein(*)
, is_inertial // IsInertial(*)
, negation // -*
, derivative // i -> Derivative(*, i)
, discriminant // Discriminant(*)
, fulldegree_epoch
, min_fulldegree_epoch
, npfactorization
;
///## Creation of rings
intrinsic PolynomialRing(F :: FldPadExact) -> RngUPol_FldPadExact
{The univariate polynomial ring over F.}
if not assigned F`polynomial_ring then
R := New(RngUPol_FldPadExact);
R`base_ring := F;
R`dependencies := [* F *];
R`get_approximation := func<n, xds | PolynomialRing(xds[1])>;
Init(R);
F`polynomial_ring := R;
end if;
return F`polynomial_ring;
end intrinsic;
///hide
intrinsic Print(R :: RngUPol_FldPadExact, lvl :: MonStgElt)
{Print.}
case lvl:
when "Magma":
printf "PolynomialRing(%m)", BaseRing(R);
else
printf "Unvariate polynomial ring";
if assigned R`varname then
printf " in %o", R`varname;
end if;
printf " over %O", BaseRing(R), lvl;
end case;
end intrinsic;
///hide
intrinsic Print(f :: RngUPolElt_FldPadExact, lvl :: MonStgElt)
{Print.}
case lvl:
when "Maximal":
printf "%o", BestApproximation(f);
else
if f`id eq Generator(Parent(f))`id then
printf "%o", BestApproximation(f);
else
printf "%o", CAP_PR(BestApproximation(f), 1);
end if;
end case;
end intrinsic;
///hide
intrinsic InterpolateEpochs(f :: RngUPolElt_FldPadExact, n1 :: RngIntElt, n2 :: RngIntElt, xf2 :: RngUPolElt[FldPad]) -> []
{Interpolates between the given epochs.}
return [* EpochApproximation(Parent(f), n) ! xf2 : n in [n1+1..n2-1] *];
end intrinsic;
///hide
intrinsic Name(R :: RngUPol_FldPadExact, i :: RngIntElt) -> RngUPolElt_FldPadExact
{The ith generator of R.}
require i eq 1: "i must be 1";
return Generator(R);
end intrinsic;
///hide
intrinsic '.'(R :: RngUPol_FldPadExact, i :: RngIntElt) -> RngUPolElt_FldPadExact
{"}
require i eq 1: "i must be 1";
return Generator(R);
end intrinsic;
///hide
intrinsic NumberOfNames(R :: RngUPol_FldPadExact) -> RngIntElt
{The number of generators of R.}
return 1;
end intrinsic;
///hide
intrinsic AssignNames(~R :: RngUPol_FldPadExact, names :: [MonStgElt])
{Assigns names to the generators of R.}
require #names le 1: "names must have length at most 1";
if #names eq 0 then
if assigned R`varname then
delete R`varname;
end if;
else
R`varname := names[1];
end if;
for i in [1..#R`approximations] do
AssignNames(~R`approximations[i], R);
end for;
end intrinsic;
///hide
intrinsic AssignNames(~xR :: RngUPol[FldPad], R :: RngUPol_FldPadExact)
{Assigns the names of xR from R.}
if assigned R`varname then
AssignNames(~xR, [R`varname]);
end if;
end intrinsic;
///hide
intrinsic SetApproximationHook(R :: RngUPol_FldPadExact, n :: RngIntElt, ~xR :: RngUPol[FldPad])
{Called by SetApproximation.}
AssignNames(~xR, R);
end intrinsic;
///hide
intrinsic ExistsCoveringStructure(R :: RngUPol_FldPadExact, S :: RngUPol_FldPadExact) -> BoolElt, .
{True if there is a ring containing both R and S.}
if R eq S then
return true, R;
end if;
ok, F := ExistsCoveringStructure(BaseRing(R), BaseRing(S));
if ok then
return true, case<F | BaseRing(R): R, BaseRing(S): S, default: PolynomialRing(F)>;
else
return false, _;
end if;
end intrinsic;
///## Ring basics
intrinsic BaseRing(R :: RngUPol_FldPadExact) -> FldPadExact
{The base ring of R.}
return R`base_ring;
end intrinsic;
///hide
intrinsic BaseRing(f :: RngUPolElt_FldPadExact) -> FldPadExact
{The base ring of f.}
return BaseRing(Parent(f));
end intrinsic;
///## Creation of polynomials
///### Special values
intrinsic Generator(R :: RngUPol_FldPadExact) -> RngUPolElt_FldPadExact
{The generator of R.}
if not assigned R`generator then
x := New(RngUPolElt_FldPadExact);
x`parent := R;
x`dependencies := [* R *];
x`get_approximation := func<n, xR | xR[1].1>;
Init(x);
R`generator := x;
end if;
return R`generator;
end intrinsic;
/// Zero and one.
intrinsic Zero(R :: RngUPol_FldPadExact) -> RngUPolElt_FldPadExact
{The zero of R.}
if not assigned R`zero then
h := New(RngUPolElt_FldPadExact);
h`parent := R;
h`dependencies := [* R *];
h`get_approximation := func<n, xR | xR[1] ! 0>;
Init(h);
R`zero := h;
end if;
return R`zero;
end intrinsic;
///hide
intrinsic One(R :: RngUPol_FldPadExact) -> RngUPolElt_FldPadExact
{The one of R.}
if not assigned R`one then
h := New(RngUPolElt_FldPadExact);
h`parent := R;
h`dependencies := [* R *];
h`get_approximation := func<n, xR | xR[1] ! 1>;
Init(h);
R`one := h;
end if;
return R`one;
end intrinsic;
///### From coefficients
intrinsic Polynomial(cs :: [FldPadExactElt]) -> RngUPolElt_FldPadExact
{The polynomial with coefficients cs.}
return PolynomialRing(Universe(cs)) ! cs;
end intrinsic;
intrinsic Polynomial(cs :: ModTupFldElt_FldPadExact) -> RngUPolElt_FldPadExact
{"}
return PolynomialRing(BaseField(cs)) ! cs;
end intrinsic;
intrinsic Polynomial(F :: FldPadExact, cs :: []) -> RngUPolElt_FldPadExact
{The polynomial over F with coefficients cs.}
ok, cs2 := CanChangeUniverse(cs, F);
require ok: "coefficients not coercible to F";
return PolynomialRing(F) ! cs;
end intrinsic;
///### Coercion
///
/// We can coerce the following to a polynomial in `R`:
/// - A polynomial in `R`
/// - A polynomial whose coefficients are coercible to the base ring of `R`
/// - A sequence or vector of anything coercible to the base ring of `R`
/// - Anything coercible to the base ring of `R`
intrinsic IsCoercible(R :: RngUPol_FldPadExact, X) -> BoolElt, .
{True if X is coercible to R.}
ok, x := IsCoercible(BaseRing(R), X);
if ok then
return true, R ! x;
end if;
return false, "wrong type";
end intrinsic;
///hide
intrinsic IsCoercible(R :: RngUPol_FldPadExact, X :: RngUPolElt_FldPadExact) -> BoolElt, .
{"}
if Parent(X) eq R then
return true, X;
else
return IsCoercible(R, Coefficients(X));
end if;
end intrinsic;
///hide
intrinsic IsCoercible(R :: RngUPol_FldPadExact, X :: FldPadExactElt) -> BoolElt, .
{"}
ok, x := IsCoercible(BaseRing(R), X);
if ok then
h := New(RngUPolElt_FldPadExact);
h`parent := R;
h`dependencies := [* R, x *];
h`get_approximation := func<n,xds | xds[1] ! xds[2]>;
Init(h);
return true, h;
end if;
return false, "not coercible to base ring: " cat Sprint(x);
end intrinsic;
///hide
intrinsic IsCoercible(R :: RngUPol_FldPadExact, X :: []) -> BoolElt, .
{"}
ok, cs := CanChangeUniverse(X, BaseRing(R));
if not ok then
return false, "coefficients not coercible to base ring";
end if;
f := New(RngUPolElt_FldPadExact);
f`parent := R;
f`dependencies := [* R *] cat [* c : c in cs *];
f`get_approximation := func<n, xds | xds[1] ! [c : c in xds[2..#xds]]>;
f`coefficient := cs; // this seems like a good optimization, but one would probably expect that the coefficients depend on f, and this breaks that expectation; e.g. calling IsEisenstein(f) will not increase the precision of f, so it can remain weakly non-Eisenstein
Init(f);
return true, f;
end intrinsic;
///hide
intrinsic IsCoercible(R :: RngUPol_FldPadExact, X :: ModTupFldElt) -> BoolElt, .
{"}
return IsCoercible(R, Eltseq(X));
end intrinsic;
///hide
intrinsic IsCoercible(R :: RngUPol_FldPadExact, X :: ModTupFldElt_FldPadExact) -> BoolElt, .
{"}
// try to change X to a vector over the base field of R
if BaseRing(R) eq BaseField(X) then
X2 := X;
else
ok, X2 := IsCoercible(VectorSpace(BaseRing(R), Degree(X)), X);
if not ok then
return IsCoercible(R, Eltseq(X));
end if;
end if;
f := New(RngUPolElt_FldPadExact);
f`parent := R;
f`dependencies := [* R, X2 *];
f`get_approximation := func<n, xds | xds[1] ! Eltseq(xds[2])>;
Init(f);
return true, f;
end intrinsic;
///hide
intrinsic IsCoercible(R :: RngUPol_FldPadExact, X :: RngUPolElt[FldPad]) -> BoolElt, .
{"}
if IsCoercible(BaseRing(R), BaseRing(X)!1) then
f := New(RngUPolElt_FldPadExact);
f`parent := R;
f`dependencies := [* R *];
f`get_approximation := func<n, xds | xds[1] ! X>;
Init(f);
IncreaseAbsolutePrecision(f, ExactpAdics_APr(X));
return true, f;
else
return false, "not castable to an approximation of R";
end if;
end intrinsic;
///hide
intrinsic IsCoercible(R :: RngUPol_FldPadExact, X :: RngUPolElt) -> BoolElt, .
{"}
return IsCoercible(R, Coefficients(X));
end intrinsic;
intrinsic ChangeRing(f :: RngUPolElt_FldPadExact, F :: FldPadExact) -> RngUPolElt_FldPadExact
{Changes the base ring of f to F.}
if BaseRing(f) eq F then
return f;
else
return PolynomialRing(F) ! f;
end if;
end intrinsic;
intrinsic ChangeRing(f :: RngUPolElt, F :: FldPadExact) -> RngUPolElt_FldPadExact
{"}
return PolynomialRing(F) ! f;
end intrinsic;
///## Polynomial basics
///### Degree
intrinsic Degree(f :: RngUPolElt_FldPadExact) -> RngIntElt
{The degree of f.}
if not assigned f`degree then
ok, n := IsDefinitelyFullDegree(f);
require ok: "weak leading coefficient of f is weakly zero";
f`degree := WeakDegree(f);
end if;
return f`degree;
end intrinsic;
///### Coefficients
intrinsic Coefficient(f :: RngUPolElt_FldPadExact, i :: RngIntElt) -> RngIntElt
{The ith coefficient of f.}
require i ge 0: "i must not be negative";
R := Parent(f);
F := BaseRing(R);
if i gt WeakDegree(f) then
return Zero(BaseRing(f));
end if;
if not assigned f`coefficient then
f`coefficient := [F|];
end if;
if not IsDefined(f`coefficient, i+1) then
c := New(FldPadExactElt);
c`parent := F;
c`dependencies := [* f *];
c`get_approximation := func<n, xf | Coefficient(xf[1], i)>;
Init(c);
f`coefficient[i+1] := c;
end if;
return f`coefficient[i+1];
end intrinsic;
intrinsic Coefficients(f :: RngUPolElt_FldPadExact) -> []
{The coefficients of f.}
if not assigned f`coefficients then
f`coefficients := [BaseRing(f)| Coefficient(f,i) : i in [0..Degree(f)]];
end if;
return f`coefficients;
end intrinsic;
intrinsic LeadingCoefficient(f :: RngUPolElt_FldPadExact) -> FldPadExactElt
{The leading coefficient of f.}
return Coefficient(f, Degree(f));
end intrinsic;
///### Arithmetic
/// Negate, add, subtract, multiply, divide by scalar, power, sum, product.
intrinsic '-'(f :: RngUPolElt_FldPadExact) -> RngUPolElt_FldPadExact
{Negation.}
if not assigned f`negation then
g := New(RngUPolElt_FldPadExact);
g`parent := Parent(f);
g`dependencies := [* f *];
g`get_approximation := func<n,xf | -xf[1]>;
Init(g);
g`negation := f;
f`negation := g;
end if;
return f`negation;
end intrinsic;
///ditto
intrinsic '+'(f :: RngUPolElt_FldPadExact, g :: RngUPolElt_FldPadExact) -> RngUPolElt_FldPadExact
{Add.}
ok, R := ExistsCoveringStructure(Parent(f), Parent(g));
require ok: "not coercible to a common ring";
return &+[R|f,g];
end intrinsic;
///hide
intrinsic '+'(x :: RngUPolElt_FldPadExact, y) -> .
{"}
return do_binop('+', x, y);
end intrinsic;
///hide
intrinsic '+'(x, y :: RngUPolElt_FldPadExact) -> .
{"}
return do_binop('+', x, y);
end intrinsic;
///ditto
intrinsic '-'(f :: RngUPolElt_FldPadExact, g :: RngUPolElt_FldPadExact) -> RngUPolElt_FldPadExact
{Subtract.}
ok, R := ExistsCoveringStructure(Parent(f), Parent(g));
require ok: "not coercible to a common ring";
h := New(RngUPolElt_FldPadExact);
h`parent := R;
h`dependencies := [* R!f, R!g *];
h`get_approximation := func<n,xds | xds[1]-xds[2]>;
Init(h);
return h;
end intrinsic;
///hide
intrinsic '-'(x :: RngUPolElt_FldPadExact, y) -> .
{"}
return do_binop('-', x, y);
end intrinsic;
///hide
intrinsic '-'(x, y :: RngUPolElt_FldPadExact) -> .
{"}
return do_binop('-', x, y);
end intrinsic;
///ditto
intrinsic '*'(f :: RngUPolElt_FldPadExact, g :: RngUPolElt_FldPadExact) -> RngUPolElt_FldPadExact
{Multiply.}
ok, R := ExistsCoveringStructure(Parent(f), Parent(g));
require ok: "not coercible to a common ring";
return &*[R|f,g];
end intrinsic;
///hide
intrinsic '*'(x :: RngUPolElt_FldPadExact, y) -> .
{"}
return do_binop('*', x, y);
end intrinsic;
///hide
intrinsic '*'(x, y :: RngUPolElt_FldPadExact) -> .
{"}
return do_binop('*', x, y);
end intrinsic;
///ditto
///param Safe:=false When true, this may be used as an intermediate variable in [`WithDependencies`]({{site.baseurl}}/internals#WithDependencies) with the `Fast` option.
intrinsic '/'(f :: RngUPolElt_FldPadExact, x :: FldPadExactElt : Safe:=false) -> RngUPolElt_FldPadExact
{Divide by a scalar.}
ok, F := ExistsCoveringStructure(BaseRing(f), Parent(x));
require ok: "not coercible to a common field";
R := PolynomialRing(F);
f2 := R ! f;
x2 := F ! x;
g := New(RngUPolElt_FldPadExact);
if Safe then
ok, n := IsDefinitelyNonzero(x);
require ok: "x is weakly zero";
g`min_epoch := n;
else
EnsureAllApproximationsNonzero(x2);
end if;
g`parent := R;
g`dependencies := [* f2, x2 *];
g`get_approximation := func<n, xds | xds[1] / xds[2]>;
Init(g);
return g;
end intrinsic;
///hide
intrinsic '/'(f :: RngUPolElt_FldPadExact, x) -> .
{"}
ok, F := ExistsCoveringStructure(BaseRing(f), Parent(x));
require ok: "not coercible to a common ring";
return (PolynomialRing(F) ! f) / (F ! x);
end intrinsic;
///hide
intrinsic '/'(f :: RngUPolElt, x :: FldPadExactElt) -> .
{"}
ok, F := ExistsCoveringStructure(BaseRing(f), Parent(x));
require ok: "not coercible to a common ring";
return (PolynomialRing(F) ! x) / (F ! x);
end intrinsic;
///ditto
intrinsic '^'(f :: RngUPolElt_FldPadExact, m :: RngIntElt) -> RngUPolElt_FldPadExact
{Power.}
require m ge 0: "m must not be negative";
R := Parent(f);
if m eq 0 then
return One(R);
end if;
h := New(RngUPolElt_FldPadExact);
h`parent := R;
h`dependencies := [* f *];
h`get_approximation := func<n,xf | xf[1]^m>;
Init(h);
return h;
end intrinsic;
///ditto
intrinsic '&+'(fs :: [RngUPolElt_FldPadExact]) -> RngUPolElt_FldPadExact
{Sum.}
R := Universe(fs);
case #fs:
when 0:
return Zero(R);
when 1:
return fs[1];
else
h := New(RngUPolElt_FldPadExact);
h`parent := R;
h`dependencies := [* f : f in fs *];
h`get_approximation := func<n,xfs|&+[xf : xf in xfs]>;
Init(h);
return h;
end case;
end intrinsic;
///ditto
intrinsic '&*'(fs :: [RngUPolElt_FldPadExact]) -> RngUPolElt_FldPadExact
{Product.}
R := Universe(fs);
case #fs:
when 0:
return One(R);
when 1:
return fs[1];
else
h := New(RngUPolElt_FldPadExact);
h`parent := R;
h`dependencies := [* f : f in fs *];
h`get_approximation := func<n,xfs | &*[xf : xf in xfs]>;
Init(h);
return h;
end case;
end intrinsic;
/// Division and remainder.
///param Safe:=false When true, this may be used as an intermediate variable in [`WithDependencies`]({{site.baseurl}}/internals#WithDependencies) with the `Fast` option.
intrinsic 'div'(f :: RngUPolElt_FldPadExact, g :: RngUPolElt_FldPadExact : Safe:=false) -> RngUPolElt_FldPadExact
{Exact divide.}
ok, R := ExistsCoveringStructure(Parent(f), Parent(g));
require ok: "not coercible to a common ring";
f2 := R ! f;
g2 := R ! g;
h := New(RngUPolElt_FldPadExact);
if Safe then
ok, n := IsDefinitelyFullDegree(g2);
require ok: "leading coefficient of g weakly zero";
h`min_epoch := n;
else
EnsureAllApproximationsFullDegree(g2);
end if;
h`parent := R;
h`dependencies := [*f2,g2*];
h`get_approximation := func<n,xds | xds[1] div xds[2]>;
Init(h);
return h;
end intrinsic;
///hide
intrinsic 'div'(x :: RngUPolElt_FldPadExact, y) -> .
{"}
return do_binop('div', x, y);
end intrinsic;
///hide
intrinsic 'div'(x, y :: RngUPolElt_FldPadExact) -> .
{"}
return do_binop('div', x, y);
end intrinsic;
///ditto
intrinsic 'mod'(f :: RngUPolElt_FldPadExact, g :: RngUPolElt_FldPadExact : Safe:=false) -> RngUPolElt_FldPadExact
{Remainder.}
ok, R := ExistsCoveringStructure(Parent(f), Parent(g));
require ok: "not coercible to a common ring";
f2 := R ! f;
g2 := R ! g;
h := New(RngUPolElt_FldPadExact);
if Safe then
ok, n := IsDefinitelyFullDegree(g2);
require ok: "leading coefficient of g is weakly zero";
h`min_epoch := n;
else
EnsureAllApproximationsFullDegree(g2);
end if;
h`parent := R;
h`dependencies := [*f2,g2*];
h`get_approximation := func<n,xds | xds[1] mod xds[2]>;
Init(h);
return h;
end intrinsic;
///hide
intrinsic 'mod'(x :: RngUPolElt_FldPadExact, y) -> .
{"}
return do_binop('mod', x, y);
end intrinsic;
///hide
intrinsic 'mod'(x, y :: RngUPolElt_FldPadExact) -> .
{"}
return do_binop('mod', x, y);
end intrinsic;
///### Derivative
/// The mth or first derivative of f.
intrinsic Derivative(f :: RngUPolElt_FldPadExact, m :: RngIntElt) -> RngUPolElt_FldPadExact
{The mth derivative of f.}
require m ge 0: "m must be non-negative";
R := Parent(f);
if m eq 0 then
return f;
elif m gt WeakDegree(f) then
return Zero(R);
end if;
if not assigned f`derivative then
f`derivative := [R|];
end if;
if not IsDefined(f`derivative, m) then
h := New(RngUPolElt_FldPadExact);
h`parent := R;
h`dependencies := [* f *];
h`get_approximation := func<n,xf | Derivative(xf[1],m)>;
Init(h);
f`derivative[m] := h;
end if;
return f`derivative[m];
end intrinsic;
intrinsic Derivative(f :: RngUPolElt_FldPadExact) -> RngUPolElt_FldPadExact
{The derivative of f.}
return Derivative(f, 1);
end intrinsic;
///### Evaluate
intrinsic Evaluate(f :: RngUPolElt_FldPadExact, x :: FldPadExactElt) -> FldPadExact
{Evaluates `f(x)`.}
ok, K := ExistsCoveringStructure(BaseRing(f), Parent(x));
require ok: "f and x must be defined over a common field";
f := ChangeRing(f, K);
x := K ! x;
y := New(FldPadExactElt);
y`parent := K;
y`dependencies := [* f, x *];
y`get_approximation := func<n, xds | Evaluate(xds[1], xds[2])>;
Init(y);
return y;
end intrinsic;
///hide
intrinsic Evaluate(f :: RngUPolElt_FldPadExact, x) -> .
{"}
ok, K := ExistsCoveringStructure(BaseRing(f), Parent(x));
require ok: "f and x must be coercible to a common ring";
return Evaluate(PolynomialRing(K) ! f, K ! x);
end intrinsic;
///hide
intrinsic Evaluate(f :: RngUPolElt, x :: FldPadExactElt) -> .
{"}
ok, K := ExistsCoveringStructure(BaseRing(f), Parent(x));
require ok: "f and x must be coercible to a common ring";
return Evaluate(PolynomialRing(K) ! f, K ! x);
end intrinsic;
intrinsic Evaluate(f :: RngUPolElt_FldPadExact, g :: RngUPolElt_FldPadExact) -> RngUPolElt_FldPadExact
{Evaluates `f(g)`.}
ok, R := ExistsCoveringStructure(Parent(f), Parent(g));
require ok: "f and g must be coercible to a common ring";
f := R ! f;
g := R ! g;
h := New(RngUPolElt_FldPadExact);
h`parent := R;
h`dependencies := [* f, g *];
h`get_approximation := func<n, xds | Evaluate(xds[1], xds[2])>;
Init(h);
return h;
end intrinsic;
///### Special forms
intrinsic IsEisenstein(f :: RngUPolElt_FldPadExact) -> BoolElt
{True if f is Eisenstein. That is, the leading coefficient has valuation 0, the constant coefficient has valuation 1, and the other coefficients have valuation at least 1. Eisenstein polynomials define totally ramified extensions.}
if not assigned f`is_eisenstein then
d := Degree(f);
f`is_eisenstein := d ge 1
and ValuationEq(Coefficient(f,0), 1)
and ValuationEq(Coefficient(f,d), 0)
and forall{i : i in [1..d-1] | ValuationGe(Coefficient(f,i), 1)};
end if;
return f`is_eisenstein;
end intrinsic;
intrinsic IsInertial(f :: RngUPolElt_FldPadExact) -> BoolElt
{True if f is inertial. That is, it is integral, the leading coefficient has valuation 0, and it is irreducible over the residue class field. Inertial polynomials define unramified extensions.}
if not assigned f`is_inertial then
d := Degree(f);
f`is_inertial := d ge 1
and ValuationEq(Coefficient(f, d), 0)
and forall{i : i in [0..d-1] | ValuationGe(Coefficient(f, i), 0)}
and (IsIrreducible(Polynomial([FF|c@q : c in Coefficients(f)])) where FF,q:=ResidueClassField(BaseRing(f)));
end if;
return f`is_inertial;
end intrinsic;
///## Discriminant and resultant
///param Safe:=false When true, this may be used as an intermediate variable in [`WithDependencies`]({{site.baseurl}}/internals#WithDependencies) with the `Fast` option.
intrinsic Discriminant(f :: RngUPolElt_FldPadExact : Safe:=false) -> FldPadExact
{The discriminant of f.}
if Safe or not assigned f`discriminant then
D := New(FldPadExactElt);
if Safe then
ok, n := IsDefinitelyFullDegree(f);
require ok: "leading coefficient of f is weakly zero";
D`min_epoch := n;
else
EnsureAllApproximationsFullDegree(f);
end if;
D`parent := BaseRing(f);
D`dependencies := [* f *];
D`get_approximation := func<n, xf | Discriminant(xf[1])>;
Init(D);
if Safe then
// no cacheing the Safe version
return D;
else
f`discriminant := D;
end if;
end if;
return f`discriminant;
end intrinsic;
///param Safe:=false When true, this may be used as an intermediate variable in [`WithDependencies`]({{site.baseurl}}/internals#WithDependencies) with the `Fast` option.
intrinsic Resultant(f :: RngUPolElt_FldPadExact, g :: RngUPolElt_FldPadExact : Safe:=false) -> FldPadExact
{The resultant of f and g.}
ok, R := ExistsCoveringStructure(Parent(f), Parent(g));
require ok: "f and g must be coercible to a common ring";
f2 := R ! f;
g2 := R ! g;
r := New(FldPadExactElt);
if Safe then
ok, n1 := IsDefinitelyFullDegree(f2);
require ok: "leading coefficient of f is weakly zero";
ok, n2 := IsDefinitelyFullDegree(g2);
require ok: "leading coefficient of g is weakly zero";
r`min_epoch := Max(n1, n2);
else
EnsureAllApproximationsFullDegree(f2);
EnsureAllApproximationsFullDegree(g2);
end if;
r`parent := BaseRing(R);
r`dependencies := [* f2, g2 *];
r`get_approximation := func<n,xds | Resultant(xds[1], xds[2])>;
Init(r);
return r;
end intrinsic;
///## Newton polygon
/// The Newton polygon of f.
///
/// If some of the coefficients of f are weakly zero, it can sometimes be quicker to find just part of the polygon. The Support parameter controls how much of the Newton polygon is required, and is one of:
/// - A single integer or rational, which must be in the support.
/// - A sequence or tuple of two integers or rationals, which specify an interval which must be in the support.
/// - A function accepting a Newton polygon and returning true if its support is valid.
///
///param Support Specifies a lower bound on the support of the returned polygon.
intrinsic NewtonPolygon(f :: RngUPolElt_FldPadExact : Support:=false) -> NwtnPgon
{The Newton polygon of f.}
// parse the Support
case Type(Support):
when BoolElt:
deg := Degree(f);
valid := func<np | #vs gt 0 and vs[1][1] le 0 and vs[#vs][1] ge deg where vs:=Vertices(np)>;
when RngIntElt, FldRatElt:
valid := func<np | #vs ne 0 and vs[1][1] le Support and vs[#vs][1] ge Support where vs:=Vertices(np)>;
when SeqEnum, Tup:
require #Support eq 2 and forall{x : x in Support | Type(x) in {RngIntElt, FldRatElt}}: "Support invalid";
valid := func<np | #vs ne 0 and vs[1][1] le Support[1] and vs[#vs][1] ge Support[2] where vs:=Vertices(np)>;
when UserProgram:
valid := Support;
else
require false: "Support invalid";
end case;
for epoch in [1..99999] do
xf := EpochApproximation(f, epoch);
// the lower weak polygons
wlp := NewtonPolygon([<i, v> : i in [0..Degree(xf)] | v lt OO where v:=Valuation(c) where c:=Coefficient(xf,i)] : Faces:="Lower");
// the vertices
lvs := ChangeUniverse(Vertices(wlp), car<Z, Z>);
// find runs of vertices
run := [];
for v in lvs do
if IsWeaklyZero(Coefficient(xf, v[1])) then
// weak vertex, ends the run
if #run ne 0 then
np := NewtonPolygon(run : Faces:="Lower");
if valid(np) then
return np;
end if;
run := [];
end if;
else
// strong vertex, add to the run
Append(~run, v);
end if;
end for;
// check the final run
if #run ne 0 then
np := NewtonPolygon(run : Faces:="Lower");
if valid(np) then
return np;
end if;
end if;
end for;
end intrinsic;
///## Hensel lifting
/// True if `x` is Hensel-liftable to a root of `f`. If so, also returns the root.
///
/// This uses a generalized statement of Hensel's lemma which does not require the inputs to be integral, namely:
///
/// **Hensel's lemma.** *If $f(x) \in K[x]$ and $x \in K$ such that $x$ is closer to one root of $f$ than any other, then iterating $x \mapsto x - f(x)/f'(x)$ converges to that root.*
intrinsic IsHenselLiftable(f :: RngUPolElt_FldPadExact, x :: FldPadExactElt) -> BoolElt, FldPadExactElt
{True if x is sufficiently close to a root of f to be Hensel-liftable. If so, also returns the root.}
K := Parent(x);
R := PolynomialRing(K);
ok, f2 := IsCoercible(R, f);
require ok: "f must be coercible to the same field as x";
// trivial case, no roots possible
if WeakDegree(f2) le 0 then
return false, _;
end if;
// check the hensel condition
// we take an approximation of f and x, compute g(X):=f(x+X), and see if its Newton polygon has a vertex at 1
// we do this by computing the Newton polygon of the weak valuations of g, and seeing if this has a vertex at 1 and the coefficient is not weakly zero
for epoch in [1..99999] do
xf := EpochApproximation(f2, epoch);
xx := EpochApproximation(x, epoch);
xR := Parent(xf);
xK := BaseRing(xR);
assert Parent(xx) eq xK;
xg := Evaluate(xf, xR.1 + xx);
np := NewtonPolygon([<i, v> : i in [0..Degree(xg)] | v lt OO where v:=WVAL(Coefficient(xg, i))] : Faces:="Lower");
vs := ChangeUniverse(Vertices(np), car<Z,Z>);
// these asserts may not hold; it is possible that g has some exactly zero coefficients
// TODO: deal with this case
assert vs[1][1] eq 0;
assert vs[2][1] ge 1;
// if there is a vertex at 1...
if vs[2][1] eq 1 then
// ... and it is a real vertex ...
if not WZERO(Coefficient(xg, 1)) then
// then we have a root
val0 := vs[1][2];
val1 := vs[2][2];
break epoch;
else
// else, we don't know
continue epoch;
end if;
end if;
// if there is a face from 0 to n, n>1, then it is not hensel liftable
assert vs[2][1] gt 1;
x0,y0:=Explode(vs[1]);
x1,y1:=Explode(vs[2]);
s := (y1-y0)/(x1-x0);
if not WZERO(Coefficient(xg, 0)) and exists{i : i in [2..x1] | (not WZERO(c)) and WVAL(c) eq y0+s*(i-x0) where c:=Coefficient(xg, i)} then
return false, _;
end if;
end for;
// hooray! now find a lift
slope := val0 - val1;
r := New(FldPadExactElt);
r`parent := K;
r`dependencies := [* f2 *];
r`internal_data := BestApproximation(x);
function start(xf)
xR := Parent(xf);
xK := BaseRing(xR);
xr := IsWeaklyZero(xr0) select xK!0 else ChangePrecision(xr0, Precision(xK)) where xr0:=xK ! r`internal_data;
xg := SHIFTARG(xf, xr);
xh := SHIFTSLOPE(xg, slope : Offset:=-val0);
pr := Z ! Min([AbsolutePrecision(c) : c in Coefficients(xh)]);
return pr, xr, xK, xh;
end function;
r`min_epoch := FirstEpochWithApproximation(f2, func<xf | start(xf) ge 1>);
r`get_approximation := function (n, xds)
pr, xr, xK, xh := start(xds[1]);
assert pr ge 1;
assert Valuation(Coefficient(xh, 0)) ge 0;
assert Valuation(Coefficient(xh, 1)) eq 0;
assert forall{i : i in [2..Degree(xh)] | Valuation(Coefficient(xh, i)) gt 0};
Q := quo<Integers(xK) | ShiftValuation(xK!1, pr)>;
qh := ChangeRing(xh, Q);
qr := Q ! 0;
dqh := Derivative(qh);
while true do
qhr := Evaluate(qh, qr);
dqhr := Evaluate(dqh, qr);
s := Valuation(qhr);
assert Valuation(dqhr) eq 0;
if s eq pr then
xx := ShiftValuation(xK ! qr, slope) + xr;
locr := r;
locr`internal_data := xx;
return xx;
else
qr2 := qr - qhr div dqhr;
assert Valuation(Evaluate(qh, qr2)) ge Min(pr, 2*s);
qr := qr2;
end if;
end while;
end function;
Init(r);
return true, r;
end intrinsic;
/// True if `g` is Hensel-liftable to a factor of `f`. If so, also returns the factor (with the same leading coefficient as `g`) and its cofactor.
///
/// **Future.** Optionally choose Slope, fShift and gShift automagically.
///
///param Slope:=0 A rational number, deslope the polynomials by this amount before applying Hensel's lemma; usually Slope will be a slope of the Newton polygon of `g`.
///param fShift:=0 A rational number, subtract this from the valuation of `f` after applying `Slope`.
///param gShift:=0 A rational number, subtract this from the valuation of `g` after applying `Slope`.
intrinsic IsHenselLiftable(f :: RngUPolElt_FldPadExact, g :: RngUPolElt_FldPadExact : Slope:=0, fShift:=0, gShift:=0) -> BoolElt, RngUPolElt_FldPadExact, RngUPolElt_FldPadExact
{True if g is Hensel-liftable to a factor of f, and the factor and cofactor.}
// cast to a common ring
R := Parent(g);
ok, f := IsCoercible(R, f);
require ok: "f must be coercible to the same ring as g";
// check parameters
ok, Slope := IsCoercible(Q, Slope);
require ok: "Slope must be a rational number";
ok, fShift := IsCoercible(Q, fShift);
require ok: "fShift must be a rational number";
ok, gShift := IsCoercible(Q, gShift);
require ok: "gShift must be a rational number";
// check degrees
df := WeakDegree(f);
dg := Degree(g);
if dg lt 0 then
// g=0 is a factor of nothing