-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_PolyAnnulusAlgebra.sage
More file actions
1661 lines (1387 loc) · 55.2 KB
/
03_PolyAnnulusAlgebra.sage
File metadata and controls
1661 lines (1387 loc) · 55.2 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
from sage.rings.integer_ring import ZZ
from sage.modules.free_module_element import vector
from sage.rings.polynomial.term_order import TermOrder
from sage.matrix.constructor import matrix
from sage.structure.sage_object import SageObject
from sage.structure.element import CommutativeAlgebraElement
from sage.rings.polynomial.polydict import PolyDict
from sage.modules.free_module_element import vector
from sage.geometry.polyhedral_complex import Polyhedron
from sage.rings.polynomial.polydict import ETuple
from sage.rings.infinity import Infinity
from sage.rings.polynomial.laurent_polynomial_ring import LaurentPolynomialRing
from sage.structure.unique_representation import UniqueRepresentation
from sage.structure.parent import Parent
from sage.matrix.constructor import matrix
from sage.rings.integer_ring import ZZ
from sage.structure.category_object import normalize_names
from sage.categories.algebras import Algebras
from sage.modules.free_module_element import vector
from sage.geometry.polyhedral_complex import Polyhedron
from sage.geometry.fan import Fan
from sage.geometry.cone import Cone
from sage.rings.polynomial.polydict import ETuple
from sage.rings.infinity import Infinity
from sage.plot.plot import plot
from sage.rings.infinity import Infinity
from sage.structure.element import MonoidElement
from sage.rings.polynomial.polydict import ETuple
from sage.monoids.monoid import Monoid_class
from sage.structure.unique_representation import UniqueRepresentation
class GeneralizedOrderPolyAnnulus(SageObject):
def __init__(self, n, cones, group_order="lex", score_function="norm"):
r"""
Create a generalized monomial order for a poly-annulus polyhedral algebra
in ``n`` varaibles with optional group order and score function.
INPUT:
- ``n`` -- the number of variables
- ``group_order`` (default: ``lex``) -- the name of a group order on `\ZZ^n`, choices are: "lex"
- ``score_function`` (default: ``norm``) -- the name of a score function, choices are: "norm"
EXAMPLES::
sage: from sage.rings.polytopal.GeneralizedOrders import GeneralizedOrderPolyAnnulus
sage: cones = []
sage: cones.append(matrix(ZZ,[[1,0],[0,1]]))
sage: cones.append(matrix(ZZ,[[-1,0],[0,-1]]))
sage: cones.append(matrix(ZZ,[[-1,0],[0,1]]))
sage: cones.append(matrix(ZZ,[[1,0],[0,-1]]))
sage: GeneralizedOrderPolyAnnulus(2, cones)
Generalized monomial order for poly-annulus in 2 variables using (lex, norm)
"""
self._n = n
self._n_cones = 2**self._n
# Build cones
self._cones = cones
if group_order in ["lex"]:
self._group_order = TermOrder(name=group_order)
else:
raise ValueError("Available group order are: 'lex'")
# Set score function. Add more here.
if score_function == "norm":
self._score_function = norm_score_function
else:
raise ValueError("Available score function are: 'norm'")
# Store names
self._group_order_name = group_order
self._score_function_name = score_function
def _repr_(self):
r"""
TESTS::
sage: from sage.rings.polytopal.GeneralizedOrders import GeneralizedOrderPolyAnnulus
sage: cones = []
sage: cones.append(matrix(ZZ,[[1,0],[0,1]]))
sage: cones.append(matrix(ZZ,[[-1,0],[0,-1]]))
sage: cones.append(matrix(ZZ,[[-1,0],[0,1]]))
sage: cones.append(matrix(ZZ,[[1,0],[0,-1]]))
sage: GeneralizedOrderPolyAnnulus(2,cones)._repr_()
'Generalized monomial order for poly-annulus in 2 variables using (lex, norm)'
"""
group = self._group_order_name
function = self._score_function_name
n = str(self._n)
return (
"Generalized monomial order for poly-annulus in %s variables using (%s, %s)"
% (n, group, function)
)
def __hash__(self):
r"""
Return the hash of self. It depends on the number of variables, the group_order and the score function.
"""
return hash(self._group_order_name + self._score_function_name + str(self._n))
def n_cones(self):
r"""
Return the number of cones (which is 2 to the power of the number of variables).
EXAMPLES::
sage: from sage.rings.polytopal.GeneralizedOrders import GeneralizedOrderPolyAnnulus
sage: cones = []
sage: cones.append(matrix(ZZ,[[1,0],[0,1]]))
sage: cones.append(matrix(ZZ,[[-1,0],[0,-1]]))
sage: cones.append(matrix(ZZ,[[-1,0],[0,1]]))
sage: cones.append(matrix(ZZ,[[1,0],[0,-1]]))
sage: order = GeneralizedOrderPolyAnnulus(2, cones)
sage: order.n_cones()
4
"""
return self._n_cones
def cones(self):
r"""
Return the list of matrices containing the generators of the cones.
EXAMPLES::
sage: from sage.rings.polytopal.GeneralizedOrders import GeneralizedOrderPolyAnnulus
sage: cones = []
sage: cones.append(matrix(ZZ,[[1,0],[0,1]]))
sage: cones.append(matrix(ZZ,[[-1,0],[0,-1]]))
sage: cones.append(matrix(ZZ,[[-1,0],[0,1]]))
sage: cones.append(matrix(ZZ,[[1,0],[0,-1]]))
sage: order = GeneralizedOrderPolyAnnulus(2, cones)
sage: order.cones()
[
[1 0] [-1 0] [-1 0] [ 1 0]
[0 1], [ 0 -1], [ 0 1], [ 0 -1]
]
"""
return self._cones
def cone(self, i):
r"""
Return the matrix whose columns are the generators of the ``i``-th cone.
INPUT:
- `i` -- an integer, a cone index
EXAMPLES::
sage: from sage.rings.polytopal.GeneralizedOrders import GeneralizedOrderPolyAnnulus
sage: cones = []
sage: cones.append(matrix(ZZ,[[1,0],[0,1]]))
sage: cones.append(matrix(ZZ,[[-1,0],[0,-1]]))
sage: cones.append(matrix(ZZ,[[-1,0],[0,1]]))
sage: cones.append(matrix(ZZ,[[1,0],[0,-1]]))
sage: order = GeneralizedOrderPolyAnnulus(2, cones)
sage: order.cone(0)
[1 0]
[0 1]
"""
if i < 0 or i > self._n:
raise IndexError("cone index out of range")
return self._cones[i]
def group_order(self):
r"""
Return the underlying group order.
EXAMPLES::
sage: from sage.rings.polytopal.GeneralizedOrders import GeneralizedOrderPolyAnnulus
sage: cones = []
sage: cones.append(matrix(ZZ,[[1,0],[0,1]]))
sage: cones.append(matrix(ZZ,[[-1,0],[0,-1]]))
sage: cones.append(matrix(ZZ,[[-1,0],[0,1]]))
sage: cones.append(matrix(ZZ,[[1,0],[0,-1]]))
sage: order = GeneralizedOrderPolyAnnulus(2, cones)
sage: order.group_order()
Lexicographic term order
"""
return self._group_order
def group_order_name(self):
r"""
Return the name of the underlying group order.
EXAMPLES::
sage: from sage.rings.polytopal.GeneralizedOrders import GeneralizedOrderPolyAnnulus
sage: cones = []
sage: cones.append(matrix(ZZ,[[1,0],[0,1]]))
sage: cones.append(matrix(ZZ,[[-1,0],[0,-1]]))
sage: cones.append(matrix(ZZ,[[-1,0],[0,1]]))
sage: cones.append(matrix(ZZ,[[1,0],[0,-1]]))
sage: order = GeneralizedOrderPolyAnnulus(2, cones)
sage: order.group_order_name()
'lex'
"""
return self._group_order_name
def score_function_name(self):
r"""
Return the name of the underlying score function.
EXAMPLES::
sage: from sage.rings.polytopal.GeneralizedOrders import GeneralizedOrderPolyAnnulus
sage: cones = []
sage: cones.append(matrix(ZZ,[[1,0],[0,1]]))
sage: cones.append(matrix(ZZ,[[-1,0],[0,-1]]))
sage: cones.append(matrix(ZZ,[[-1,0],[0,1]]))
sage: cones.append(matrix(ZZ,[[1,0],[0,-1]]))
sage: order = GeneralizedOrderPolyAnnulus(2, cones)
sage: order.score_function_name()
'norm'
"""
return self._score_function_name
def score(self, t):
r"""
Compute the score of a tuple ``t``.
INPUT:
- `t` -- a tuple of integers
EXAMPLES::
sage: from sage.rings.polytopal.GeneralizedOrders import GeneralizedOrderPolyAnnulus
sage: cones = []
sage: cones.append(matrix(ZZ,[[1,0],[0,1]]))
sage: cones.append(matrix(ZZ,[[-1,0],[0,-1]]))
sage: cones.append(matrix(ZZ,[[-1,0],[0,1]]))
sage: cones.append(matrix(ZZ,[[1,0],[0,-1]]))
sage: order = GeneralizedOrderPolyAnnulus(2, cones)
sage: order.score((-2,3))
5
"""
return self._score_function(t)
def compare(self, a, b):
r"""
Return 1, 0 or -1 whether tuple ``a`` is greater than, equal or less than to tuple ``b`` respectively.
INPUT:
- `a` and `b` -- two tuples of integers
EXAMPLES::
sage: from sage.rings.polytopal.GeneralizedOrders import GeneralizedOrderPolyAnnulus
sage: cones = []
sage: cones.append(matrix(ZZ,[[1,0],[0,1]]))
sage: cones.append(matrix(ZZ,[[-1,0],[0,-1]]))
sage: cones.append(matrix(ZZ,[[-1,0],[0,1]]))
sage: cones.append(matrix(ZZ,[[1,0],[0,-1]]))
sage: order = GeneralizedOrderPolyAnnulus(2, cones)
sage: order.compare((1,2), (3,-4))
-1
"""
if a == b:
return 0
diff = self._score_function(a) - self._score_function(b)
if diff != 0:
return 1 if diff > 0 else -1
else:
return 1 if self._group_order.greater_tuple(a, b) == a else -1
def greatest_tuple(self, *L):
r"""
Return the greatest tuple in ``L``.
INPUT:
- *L -- integer tuples
EXAMPLES::
sage: from sage.rings.polytopal.GeneralizedOrders import GeneralizedOrderPolyAnnulus
sage: cones = []
sage: cones.append(matrix(ZZ,[[1,0],[0,1]]))
sage: cones.append(matrix(ZZ,[[-1,0],[0,-1]]))
sage: cones.append(matrix(ZZ,[[-1,0],[0,1]]))
sage: cones.append(matrix(ZZ,[[1,0],[0,-1]]))
sage: order = GeneralizedOrderPolyAnnulus(2, cones)
sage: order.greatest_tuple((1,-1),(2,-3),(3,-4))
(3, -4)
"""
n = len(L)
if n == 0:
raise ValueError("empty list of tuples")
if n == 1:
return L[0]
else:
a = L[0]
b = self.greatest_tuple(*L[1:])
return a if self.compare(a, b) == 1 else b
def translate_to_cone(self, i, L):
r"""
Return a tuple ``t`` such that `t + L` is contained in the ``i``-th cone.
INPUT:
- ``i`` -- an integer, a cone index
- ``L`` -- a list of integer tuples
EXAMPLES::
sage: from sage.rings.polytopal.GeneralizedOrders import GeneralizedOrder
sage: cones = []
sage: cones.append(matrix(ZZ,[[1,0],[0,1]]))
sage: cones.append(matrix(ZZ,[[-1,0],[0,-1]]))
sage: cones.append(matrix(ZZ,[[-1,0],[0,1]]))
sage: cones.append(matrix(ZZ,[[1,0],[0,-1]]))
sage: order = GeneralizedOrderPolyAnnulus(2, cones)
sage: order.translate_to_cone(2, [(1,2),(-2,-3),(1,-4)])
(-1, 4)
"""
cone_matrix = self._cones[i]
T = matrix(ZZ, [cone_matrix * vector(v) for v in L])
return tuple(cone_matrix * vector([-min(0, *c) for c in T.columns()]))
def greatest_tuple_for_cone(self, i, *L):
r"""
Return the greatest tuple of the list of tuple `L` with respect to the `i`-th cone.
This is the unique tuple `t` in `L` such that each time the greatest tuple of `s + L`
for a tuple `s` is contained in the `i`-th cone, it is equal to `s + t`.
INPUT:
- ``i`` -- an integer, a cone index
- ``L`` -- a list of integer tuples
EXAMPLES::
sage: from sage.rings.polytopal.GeneralizedOrders import GeneralizedOrder
sage: cones = []
sage: cones.append(matrix(ZZ,[[1,0],[0,1]]))
sage: cones.append(matrix(ZZ,[[-1,0],[0,-1]]))
sage: cones.append(matrix(ZZ,[[-1,0],[0,1]]))
sage: cones.append(matrix(ZZ,[[1,0],[0,-1]]))
sage: order = GeneralizedOrderPolyAnnulus(2, cones)
sage: L = [(1,2), (-2,-2), (-4,5), (5,-6)]
sage: t = order.greatest_tuple_for_cone(1, *L);t
(-2, -2)
We can check the result::
sage: s = order.translate_to_cone(1, L)
sage: sL = [tuple(vector(s) + vector(l)) for l in L]
sage: tuple(vector(s) + vector(t)) == order.greatest_tuple(*sL)
True
"""
t = vector(self.translate_to_cone(i, L))
L = [vector(l) for l in L]
return tuple(vector(self.greatest_tuple(*[tuple(t + l) for l in L])) - t)
def is_in_cone(self, i, t):
r"""
Test whether the tuple ``t`` is contained in the ``i``-th cone or not.
INPUT:
- ``i`` -- an integer, a cone index
- ``t`` -- a tuple of integers
EXAMPLES::
sage: from sage.rings.polytopal.GeneralizedOrders import GeneralizedOrder
sage: cones = []
sage: cones.append(matrix(ZZ,[[1,0],[0,1]]))
sage: cones.append(matrix(ZZ,[[-1,0],[0,-1]]))
sage: cones.append(matrix(ZZ,[[-1,0],[0,1]]))
sage: cones.append(matrix(ZZ,[[1,0],[0,-1]]))
sage: order = GeneralizedOrderPolyAnnulus(2, cones)
sage: order.is_in_cone(0, (1,2))
True
sage: order.is_in_cone(0,(-2,3))
False
"""
return all(c >= 0 for c in self.cone(i) * vector(t))
def norm_score_function(t):
return sum([abs(c) for c in t])
class PolyAnnulusAlgebraElement(CommutativeAlgebraElement):
def __init__(self, parent, x=None, prec=None):
CommutativeAlgebraElement.__init__(self, parent)
if isinstance(x, PolyAnnulusAlgebraElement):
self._poly = x._poly
if prec is None:
self._prec = x._prec
else:
self._prec = prec
elif isinstance(x, PolyAnnulusTerm):
self._poly = parent._polynomial_ring(
PolyDict({x.exponent(): parent._field(x.coefficient())})
)
self._prec = Infinity
else:
try:
self._poly = parent._polynomial_ring(x)
if prec is None:
self._prec = Infinity
else:
self._prec = prec
except TypeError:
raise
def terms(self):
r"""
Return the terms of ``self`` in a list, sorted by decreasing order.
EXAMPLES::
sage: P = Polyhedron(vertices=[(1,1), (1,-1), (-1,-1), (-1,1)])
sage: A = PolyAnnulusAlgebra(Qp(2), P, "x,y"); x,y = A.gens()
sage: f = 2*x + y + x*y + y^-1 + 1*x^-3
sage: f.terms()
[(1 + O(2^20))*x^-3,
(1 + O(2^20))*x*y,
(1 + O(2^20))*y^-1,
(1 + O(2^20))*y,
(2 + O(2^21))*x]
"""
terms = [
PolyAnnulusTerm(self.parent()._monoid_of_terms, coeff=c, exponent=e)
for e, c in self._poly.dict().items()
]
return sorted(terms, reverse=True)
def leading_term(self, i=None):
r"""
Return the leading term of this series if ``i`` is None, otherwise the
leading term for the ``i``-th vertex.
EXAMPLES::
sage: P = Polyhedron(vertices=[(1,1), (1,-1), (-1,-1), (-1,1)])
sage: A = PolyAnnulusAlgebra(Qp(2), P, "x,y"); x,y = A.gens()
sage: f = 2*x + y^-1 + x*y
sage: f.leading_term()
(1 + O(2^20))*x*y
sage: f.leading_term(2)
(1 + O(2^20))*y^-1
"""
if i is None:
return self.terms()[0]
ini = self.initial(i)._poly
le = self.parent()._order.greatest_tuple_for_cone(i, *ini.exponents())
return PolyAnnulusTerm(self.parent()._monoid_of_terms, ini[le], le)
def leading_monomial(self, i=None):
r"""
Return the leading monomial of this series if ``i`` is None, otherwise the
leading monomial for the ``i``-th vertex.
EXAMPLES::
sage: P = Polyhedron(vertices=[(1,1), (1,-1), (-1,-1), (-1,1)])
sage: A = PolyAnnulusAlgebra(Qp(2), P, "x,y"); x,y = A.gens()
sage: f = 2*x + y^-1 + x*y
sage: f.leading_monomial()
(1 + O(2^20))*x*y
sage: f.leading_monomial(2)
(1 + O(2^20))*y^-1
"""
return self.leading_term(i).monomial()
def leading_coefficient(self, i=None):
r"""
Return the leading coefficient of this series if ``i`` is None, otherwise the
leading coefficient for the ``i``-th vertex.
EXAMPLES::
sage: P = Polyhedron(vertices=[(1,1), (1,-1), (-1,-1), (-1,1)])
sage: A = PolyAnnulusAlgebra(Qp(2), P, "x,y"); x,y = A.gens()
sage: f = 2*x + y^-1 + x*y
sage: f.leading_coefficient()
1 + O(2^20)
sage: f.leading_coefficient(2)
1 + O(2^20)
"""
return self.leading_term(i).coefficient()
def leadings(self, i=None):
r"""
Return a list containing the leading coefficient, monomial and term
of this series if ``i`` is None, or th same for the ``i``-th vertex otherwise.
EXAMPLES::
sage: P = Polyhedron(vertices=[(1,1), (1,-1), (-1,-1), (-1,1)])
sage: A = PolyAnnulusAlgebra(Qp(2), P, "x,y"); x,y = A.gens()
sage: f = 2*x + y^-1 + x*y
sage: f.leadings()
(1 + O(2^20), (1 + O(2^20))*x*y, (1 + O(2^20))*x*y)
"""
lt = self.leading_term(i)
return (lt.coefficient(), lt.monomial(), lt)
def initial(self, i=None):
r"""
Return the initial part of this series if ``i`` is None, otherwise
the initial part for the ``i``-the vertex.
EXAMPLES::
sage: P = Polyhedron(vertices=[(1,1), (1,-1), (-1,-1), (-1,1)])
sage: A = PolyAnnulusAlgebra(Qp(2), P, "x,y"); x,y = A.gens()
sage: f = 2*x + y^-1 + x*y
sage: f.initial()
(1 + O(2^20))*x*y
sage: f.initial(2)
(1 + O(2^20))*y^-1
"""
terms = self.terms()
if i is None:
v = self.valuation()
else:
v = min([t.valuation(i) for t in terms])
return sum(
[self.__class__(self.parent(), t) for t in terms if t.valuation(i) == v]
)
def valuation(self, i=None):
r"""
Return the valuation of this series if ``i`` is None
(which is the minimum over the valuations at each vertex),
otherwise the valuation for the ``i``-th vertex.
EXAMPLES::
sage: P = Polyhedron(vertices=[(1,1), (1,-1), (-1,-1), (-1,1)])
sage: A = PolyAnnulusAlgebra(Qp(2), P, "x,y"); x,y = A.gens()
sage: f = 2*x + y^-1 + x*y
sage: f.valuation()
-2
"""
if self.is_zero():
return Infinity
return self.leading_term(i).valuation(i)
def _normalize(self):
r"""
Normalize this series.
EXAMPLES::
sage: P = Polyhedron(vertices=[(1,1), (1,-1), (-1,-1), (-1,1)])
sage: A = PolyAnnulusAlgebra(Qp(2), P, "x,y"); x,y = A.gens()
sage: f = 2*x + y^-1 + x*y
sage: f.add_bigoh(2) # Indirect doctest
(1 + O(2^4))*x*y + (1 + O(2^3))*y^-1 + (2 + O(2^3))*x + OO(2)
"""
if self._prec == Infinity:
return
terms = []
for t in self.terms():
exponent = t.exponent()
coeff = t.coefficient()
v = max(
[
ETuple(tuple(vertex)).dotprod(exponent)
for vertex in self.parent().vertices()
]
)
if coeff.precision_absolute() > self._prec + v:
coeff = coeff.add_bigoh(self._prec + v)
if coeff.valuation() < self._prec + v:
t._coeff = coeff
terms.append(t)
self._poly = sum([self.__class__(self.parent(), t)._poly for t in terms])
def add_bigoh(self, prec):
r"""
Return this series truncated to precision ``prec``.
EXAMPLES::
sage: P = Polyhedron(vertices=[(1,1), (1,-1), (-1,-1), (-1,1)])
sage: A = PolyAnnulusAlgebra(Qp(2), P, "x,y"); x,y = A.gens()
sage: f = 2*x + y^-1 + x*y
sage: f.add_bigoh(2) # Indirect doctest
(1 + O(2^4))*x*y + (1 + O(2^3))*y^-1 + (2 + O(2^3))*x + OO(2)
"""
return self.parent()(self, prec=prec)
def is_zero(self):
r"""
Test if this series is zero.
EXAMPLES::
sage: P = Polyhedron(vertices=[(1,1), (1,-1), (-1,-1), (-1,1)])
sage: A = PolyAnnulusAlgebra(Qp(2), P, "x,y"); x,y = A.gens()
sage: f = 2*x + y^-1 + x*y
sage: f.is_zero()
False
sage: (f-f).is_zero()
True
"""
# self._normalize()
return self._poly == 0
def __eq__(self, other):
r"""
Test equality of this series and ``other``.
EXAMPLES::
sage: P = Polyhedron(vertices=[(1,1), (1,-1), (-1,-1), (-1,1)])
sage: A = PolyAnnulusAlgebra(Qp(2), P, "x,y"); x,y = A.gens()
sage: f = 2*x + y^-1 + x*y
sage: g = 3*x*y
sage: f == g
False
sage: f == f
True
"""
diff = self - other
return diff.is_zero()
def _add_(self, other):
r"""
Return the addition of this series and ``other`.
The precision is adjusted to the min of the inputs precisions.
EXAMPLES::
sage: P = Polyhedron(vertices=[(1,1), (1,-1), (-1,-1), (-1,1)])
sage: A = PolyAnnulusAlgebra(Qp(2), P, "x,y"); x,y = A.gens()
sage: f = 2*x + y^-1 + x*y
sage: g = 3*x*y
sage: f + g
(1 + O(2^20))*y^-1 + (2 + O(2^21))*x + (2^2 + O(2^20))*x*y
"""
prec = min(self._prec, other._prec)
ans = self.__class__(self.parent(), self._poly + other._poly, prec=prec)
ans._normalize()
return ans
def _sub_(self, other):
r"""
Return the substraction of this series and ``other`.
The precision is adjusted to the min of the inputs precisions.
EXAMPLES::
sage: P = Polyhedron(vertices=[(1,1), (1,-1), (-1,-1), (-1,1)])
sage: A = PolyAnnulusAlgebra(Qp(2), P, "x,y"); x,y = A.gens()
sage: f = 2*x + y^-1 + x*y
sage: g = -3*x*y
sage: f - g
(1 + O(2^20))*y^-1 + (2 + O(2^21))*x + (2^2 + O(2^20))*x*y
"""
prec = min(self._prec, other._prec)
ans = self.__class__(self.parent(), self._poly - other._poly, prec=prec)
ans._normalize()
return ans
def _mul_(self, other):
r"""
Return the multiplication of this series and ``other`.
EXAMPLES::
sage: P = Polyhedron(vertices=[(1,1), (1,-1), (-1,-1), (-1,1)])
sage: A = PolyAnnulusAlgebra(Qp(2), P, "x,y"); x,y = A.gens()
sage: f = 2*x + y^-1 + x*y
sage: g = 3*x*y
sage: f*g
(1 + 2 + O(2^20))*x^2*y^2 + (2 + 2^2 + O(2^21))*x^2*y + (1 + 2 + O(2^20))*x
"""
a = self.valuation() + other._prec
b = other.valuation() + self._prec
prec = min(a, b)
ans = self.__class__(self.parent(), self._poly * other._poly, prec=prec)
ans._normalize()
return ans
def _div_(self, other):
r"""
Return the multiplication of this series and ``other`.
EXAMPLES::
sage: P = Polyhedron(vertices=[(1,1), (1,-1), (-1,-1), (-1,1)])
sage: A = PolyAnnulusAlgebra(Qp(2), P, "x,y"); x,y = A.gens()
sage: f = x
sage: g = x*y
sage: f/g
(1 + O(2^20))*y^-1
"""
prec = self._prec - other.valuation()
ans = self.__class__(self.parent(), self._poly / other._poly, prec=prec)
ans._normalize()
return ans
def _neg_(self):
r"""
Return the negation of this series.
EXAMPLES::
sage: P = Polyhedron(vertices=[(1,1), (1,-1), (-1,-1), (-1,1)])
sage: A = PolyAnnulusAlgebra(Qp(2), P, "x,y"); x,y = A.gens()
sage: f = -x
sage: -f
(1 + O(2^20))*x
"""
ans = self.__class__(self.parent(), -self._poly, prec=self._prec)
ans._normalize()
return ans
def _repr_(self):
r"""
Return a string representation of this series.
EXAMPLES::
sage: P = Polyhedron(vertices=[(1,1), (1,-1), (-1,-1), (-1,1)])
sage: A = PolyAnnulusAlgebra(Qp(2), P, "x,y"); x,y = A.gens()
sage: x
(1 + O(2^20))*x
"""
self._normalize()
terms = self.terms()
r = ""
for t in terms:
if t.valuation() < self._prec:
s = repr(t)
if r == "":
r += s
elif s[0] == "-":
r += " - " + s[1:]
else:
r += " + " + s
if not self._prec == Infinity:
if r:
r += " + "
r += f"OO({self._prec})"
return r
def leading_module_polyhedron(self, i=None, backend="normaliz"):
r"""
Return the polyhedron whose intger points are exactly the exponents of all
leadings terms of multiple of this series that lie in the restricted
``i``-th cone.
Is ``i`` is None, return a list with all polyhedron for all vertices.
The default backend for the polyhedron class is normaliz. It is needed
for the method least_common_multiples to retrieve integral generators.
If you only wan't to construct the polyhedron, any backend is ok.
EXAMPLES::
sage: P = Polyhedron(vertices=[(1,1), (1,-1), (-1,-1), (-1,1)])
sage: A = PolyAnnulusAlgebra(Qp(2), P, "x,y"); x,y = A.gens()
sage: f = 2*x + y^-1 + x*y
sage: f.leading_module_polyhedron(0)
A 2-dimensional polyhedron in QQ^2 defined as the convex hull of 2 vertices and 2 rays
"""
if i is None:
return [
self.leading_module_polyhedron(i, backend)
for i in range(self.parent()._nvertices)
]
vertices = self.parent()._vertices
ieqs = []
shift = vector(self.leading_monomial(i).exponent())
for j in [k for k in range(len(vertices)) if k != i]:
v = vertices[j]
a = tuple(vector(vertices[i]) - vector(v))
c = self.valuation(i) - self.valuation(j)
if j < i:
c += 1
ieqs.append((-c,) + a)
P1 = Polyhedron(ieqs=ieqs)
return Polyhedron(
rays=self.parent().polyhedron_at_vertex(i).rays(),
vertices=[list(vector(v) + shift) for v in P1.vertices()],
backend=backend,
)
def least_common_multiples(self, other, i):
r"""
Return the finite sets of monomials which are the lcms of this series and ``other``
for the ``i``-th cone.
This method require the backend normaliz.
EXAMPLES::
sage: P = Polyhedron(vertices=[(1,1), (1,-1), (-1,-1), (-1,1)])
sage: A = PolyAnnulusAlgebra(Qp(2), P, "x,y"); x,y = A.gens()
sage: f = 2*x + y^-1 + x*y
sage: g = x - y
sage: f.least_common_multiples(g,0)
[(1 + O(2^20))*x^-1*y^-1, (1 + O(2^20))*y^-2]
"""
return [
self.parent()._monoid_of_terms(1, exponent=tuple(e))
for e in self.leading_module_polyhedron(i)
.intersection(other.leading_module_polyhedron(i))
.integral_points_generators()[0]
]
def critical_pair(self, other, i, lcm):
r"""
Return the S-pair of this series and ``other`` for the ``i``-th
cone given a leading common multiple ``lcm``.
EXAMPLES::
sage: P = Polyhedron(vertices=[(1,1), (1,-1), (-1,-1), (-1,1)])
sage: A = PolyAnnulusAlgebra(Qp(2), P, "x,y"); x,y = A.gens()
sage: f = 2*x + y^-1 + x*y
sage: g = x + y^-1
sage: lcm = f.least_common_multiples(g,1)[0]
sage: f.critical_pair(g, 1, lcm)
(1 + 2 + 2^2 + 2^3 + 2^4 + 2^5 + 2^6 + 2^7 + 2^8 + 2^9 + 2^10 + 2^11 + 2^12 + 2^13 + 2^14 + 2^15 + 2^16 + 2^17 + 2^18 + 2^19 + O(2^20))*x^-1*y + (1 + O(2^20))*x^-1 + (2 + O(2^21))*y
"""
lcf, lmf, _ = self.leadings(i)
lcg, lmg, _ = other.leadings(i)
return lcg * lcm._divides(lmf) * self - lcf * lcm._divides(lmg) * other
def all_critical_pairs(self, other, i=None):
r"""
Return all S-pairs of this series and ``other`` for the ``i``-th cone,
or all S-pairs for all cones if ``i`` is None.
EXAMPLES::
sage: P = Polyhedron(vertices=[(1,1), (1,-1), (-1,-1), (-1,1)])
sage: A = PolyAnnulusAlgebra(Qp(2), P, "x,y"); x,y = A.gens()
sage: f = 2*x + y^-1 + x*y
sage: g = x + y^-1
sage: f.all_critical_pairs(g,0)
[(1 + O(2^20))*y + (1 + O(2^20)), (1 + O(2^20))*x*y^-1 + (1 + O(2^20))*x]
"""
if i is None:
return [
item
for j in range(self.parent()._nvertices)
for item in self.all_critical_pairs(other, j)
]
lcms = self.least_common_multiples(other, i)
return [self.critical_pair(other, i, lcm) for lcm in lcms]
class PolyAnnulusAlgebra(Parent, UniqueRepresentation):
r"""
Parent class for series with finite precision in a polyhedral algebra with power series converging on a poly-annulus.
EXAMPLES::
sage: P = Polyhedron(vertices=[(1,1), (1,-1), (-1,-1), (-1,1)])
sage: A = PolyAnnulusAlgebra(Qp(2), P, "x,y")
"""
Element = PolyAnnulusAlgebraElement
def __init__(
self,
field,
polyhedron,
names,
score_function="norm",
group_order="lex",
prec=10,
):
self._field = field
self.element_class = PolyAnnulusAlgebraElement
self._prec = prec
self._polyhedron = polyhedron
self._vertices = polyhedron.vertices()
self._nvertices = len(self._vertices)
self._names = normalize_names(-1, names)
self._polynomial_ring = LaurentPolynomialRing(field, names=names)
self._gens = [
self.element_class(self, g, prec=Infinity)
for g in self._polynomial_ring.gens()
]
self._ngens = len(self._gens)
self._cones = self._build_cones()
self._order = GeneralizedOrderPolyAnnulus(
self._ngens,
self._cones,
score_function=score_function,
group_order=group_order,
)
self._monoid_of_terms = PolyAnnulusTermMonoid(self)
Parent.__init__(self, names=names, category=Algebras(self._field).Commutative())
def _build_cones(self):
cones = [self.cone_at_vertex(i) for i in range(self._nvertices)]
decompo_cones = []
for cone in cones:
m = matrix.zero(ZZ, self._ngens)
for i, ray in enumerate(cone.rays()):
m.set_column(i, vector(tuple(ray)))
decompo_cones.append(m)
return decompo_cones
def generalized_order(self):
r"""
Return the term order used to break ties.
EXAMPLES::
sage: P = Polyhedron(vertices=[(1,1), (1,-1), (-1,-1), (-1,1)])
sage: A = PolyAnnulusAlgebra(Qp(2), P, "x,y")
sage: A.generalized_order()
Generalized monomial order for poly-annulus in 2 variables using (lex, norm)
"""
return self._order
def variable_names(self):
r"""
Return the variables names of this algebra.
EXAMPLES::
sage: P = Polyhedron(vertices=[(1,1), (1,-1), (-1,-1), (-1,1)])
sage: A = PolyAnnulusAlgebra(Qp(2), P, "x,y")
sage: A.variable_names()
('x', 'y')
"""
return self._names
def _coerce_map_from_(self, R):
r"""
Currently, there are no inter-algebras coercions, we only coerce from
the coefficient field or the corresponding term monoid.
EXAMPLES::
sage: P = Polyhedron(vertices=[(1,1), (1,-1), (-1,-1), (-1,1)])
sage: A = PolyAnnulusAlgebra(Qp(2), P, "x,y"); x, y = A.gens()
sage: 2*x #indirect doctest
(2 + O(2^21))*x
"""
if self._field.has_coerce_map_from(R):
return True
elif isinstance(R, PolyAnnulusTermMonoid):
return True
else:
return False
def field(self):
r"""
Return the coefficient field of this algebra (currently restricted to Qp).
EXAMPLES::
sage: P = Polyhedron(vertices=[(1,1), (1,-1), (-1,-1), (-1,1)])
sage: A = PolyAnnulusAlgebra(Qp(2), P, "x,y")
sage: A.field()
2-adic Field with capped relative precision 20
"""
return self._field
def prime(self):
r"""
Return the prime number of this algebra. This
is the prime of the coefficient field.
EXAMPLES::
sage: P = Polyhedron(vertices=[(1,1), (1,-1), (-1,-1), (-1,1)])
sage: A = PolyAnnulusAlgebra(Qp(2), P, "x,y")
sage: A.prime()
2
"""
return self._field.prime()
def gen(self, n=0):
r"""
Return the ``n``-th generator of this algebra.
EXAMPLES::
sage: P = Polyhedron(vertices=[(1,1), (1,-1), (-1,-1), (-1,1)])
sage: A = PolyAnnulusAlgebra(Qp(2), P, "x,y")
sage: A.gen(1)
(1 + O(2^20))*y
"""
if n < 0 or n >= self._ngens: