This repository was archived by the owner on Aug 1, 2018. It is now read-only.
forked from MCLF/mac_lane
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvaluation_space.py
More file actions
1632 lines (1271 loc) · 59.3 KB
/
valuation_space.py
File metadata and controls
1632 lines (1271 loc) · 59.3 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
# -*- coding: utf-8 -*-
r"""
Spaces of valuations
This module provides spaces of exponential pseudo-valuations on integral
domains. It currently, only provides support for such valuations if they are
discrete, i.e., their image is a discrete additive subgroup of the rational
numbers extended by `\infty`.
EXAMPLES::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: pAdicValuation(QQ, 2).parent()
Discrete pseudo-valuations on Rational Field
AUTHORS:
- Julian Rüth (2016-10-14): initial version
"""
#*****************************************************************************
# Copyright (C) 2016 Julian Rüth <julian.rueth@fsfe.org>
#
# Distributed under the terms of the GNU General Public License (GPL)
# as published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
# http://www.gnu.org/licenses/
#*****************************************************************************
from sage.categories.homset import Homset
from sage.misc.lazy_attribute import lazy_attribute
from sage.misc.abstract_method import abstract_method
from sage.structure.unique_representation import UniqueRepresentation
from sage.misc.cachefunc import cached_method
class DiscretePseudoValuationSpace(UniqueRepresentation, Homset):
r"""
The space of discrete pseudo-valuations on ``domain``.
EXAMPLES::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: H = DiscretePseudoValuationSpace(QQ)
sage: pAdicValuation(QQ, 2) in H
True
.. NOTE::
We do not distinguish between the space of discrete valuations and the
space of discrete pseudo-valuations. This is entirely for practical
reasons: We would like to model the fact that every discrete valuation
is also a discrete pseudo-valuation. At first, it seems to be
sufficient to make sure that the ``in`` operator works which can
essentially be achieved by overriding ``_element_constructor_`` of
the space of discrete pseudo-valuations to accept discrete valuations
by just returning them. Currently, however, if one does not change the
parent of an element in ``_element_constructor_`` to ``self``, then
one can not register that conversion as a coercion. Consequently, the
operators ``<=`` and ``>=`` can not be made to work between discrete
valuations and discrete pseudo-valuations on the same domain (because
the implementation only calls ``_richcmp`` if both operands have the
same parent.) Of course, we could override ``__ge__`` and ``__le__``
but then we would likely run into other surprises.
So in the end, we went for a single homspace for all discrete
valuations (pseudo or not) as this makes the implementation much
easier.
.. TODO::
The comparison problem might be fixed by :trac:`22029` or similar.
TESTS::
sage: TestSuite(H).run() # long time
"""
def __init__(self, domain):
r"""
TESTS::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: isinstance(pAdicValuation(QQ, 2).parent(), DiscretePseudoValuationSpace)
True
"""
from value_group import DiscreteValuationCodomain
# A valuation is a map from an additive semigroup to an additive semigroup, however, it
# does not preserve that structure. It is therefore only a morphism in the category of sets.
from sage.categories.all import Sets
UniqueRepresentation.__init__(self)
Homset.__init__(self, domain, DiscreteValuationCodomain(), category = Sets())
from sage.categories.domains import Domains
if domain not in Domains():
raise ValueError("domain must be an integral domain")
@lazy_attribute
def _abstract_element_class(self):
r"""
Return an abstract base class for all valuations in this space.
This is used to extend every valuation with a number of generic methods
that are independent of implementation details.
Usually, extensions of this kind would be done by implementing an
appropriate class ``MorphismMethods`` in the category of this homset.
However, there is no category whose arrows are the valuations, so we
need to move this magic down to the level of the actual homset.
EXAMPLES::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: isinstance(pAdicValuation(QQ, 2), DiscretePseudoValuationSpace.ElementMethods) # indirect doctest
True
"""
class_name = "%s._abstract_element_class"%self.__class__.__name__
from sage.structure.dynamic_class import dynamic_class
return dynamic_class(class_name, (super(DiscretePseudoValuationSpace,self)._abstract_element_class, self.__class__.ElementMethods))
def _get_action_(self, S, op, self_on_left):
r"""
Return the ``op`` action of ``S`` on elements in this space.
EXAMPLES::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: v = pAdicValuation(QQ, 2)
sage: from operator import mul
sage: v.parent().get_action(ZZ, mul) # indirect doctest
"""
from operator import mul, div
from sage.rings.all import QQ, InfinityRing, ZZ
if op == mul and not self_on_left and (S is InfinityRing or S is QQ or S is ZZ):
return ScaleAction(S, self)
if op == div and self_on_left and (S is InfinityRing or S is QQ or S is ZZ):
return InverseScaleAction(self, S)
return None
def _an_element_(self):
r"""
Return a trivial valuation in this space.
EXAMPLES::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: DiscretePseudoValuationSpace(QQ).an_element() # indirect doctest
Trivial pseudo-valuation on Rational Field
"""
from trivial_valuation import TrivialPseudoValuation
return TrivialPseudoValuation(self.domain())
def _repr_(self):
r"""
Return a printable representation of this space.
EXAMPLES::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: DiscretePseudoValuationSpace(QQ) # indirect doctest
Discrete pseudo-valuations on Rational Field
"""
return "Discrete pseudo-valuations on %r"%(self.domain(),)
def __contains__(self, x):
r"""
Return whether ``x`` is a valuation in this space.
EXAMPLES:
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: H = DiscretePseudoValuationSpace(QQ)
sage: H.an_element() in H
True
Elements of spaces which embed into this spaces are correctly handled::
sage: pAdicValuation(QQ, 2) in H
True
"""
# override the logic from Homset with the original implementation for Parent
# which entirely relies on a proper implementation of
# _element_constructor_ and coercion maps
from sage.structure.parent import Parent
return Parent.__contains__(self, x)
def __call__(self, x):
r"""
Create an element in this space from ``x``.
EXAMPLES:
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: H = DiscretePseudoValuationSpace(QQ)
sage: H(pAdicValuation(QQ, 2))
2-adic valuation
"""
# override the logic from Homset with the original implementation for Parent
# which entirely relies on a proper implementation of
# _element_constructor_ and coercion maps
from sage.structure.parent import Parent
return Parent.__call__(self, x)
def _element_constructor_(self, x):
r"""
Create an element in this space from ``x``,
EXAMPLES:
We try to convert valuations defined on different domains by changing
their base ring::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: Z = DiscretePseudoValuationSpace(ZZ)
sage: Q = DiscretePseudoValuationSpace(QQ)
sage: v = pAdicValuation(ZZ, 2)
sage: v in Q
False
sage: Q(v) in Q
True
sage: Q(v) in Z
False
sage: Z(Q(v)) in Z
True
We support coercions and conversions, even though they are not
implemented here::
sage: Z(v)
2-adic valuation
"""
if isinstance(x.parent(), DiscretePseudoValuationSpace):
if x.domain() is not self.domain():
try:
return self(x.change_domain(self.domain()))
except NotImplementedError:
pass
else:
return x
raise ValueError("element can not be converted into the space of %r"%(self,))
class ElementMethods:
r"""
Provides methods for discrete pseudo-valuations that are added
automatically to valuations in this space.
EXAMPLES:
Here is an example of a method that is automagically added to a
discrete valuation::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: H = DiscretePseudoValuationSpace(QQ)
sage: pAdicValuation(QQ, 2).is_discrete_pseudo_valuation() # indirect doctest
True
The methods will be provided even if the concrete types is not created
with :meth:`__make_element_class__`::
sage: from mac_lane.valuation import DiscretePseudoValuation
sage: m = DiscretePseudoValuation(H)
sage: m.parent() is H
True
sage: m.is_discrete_pseudo_valuation()
True
However, the category framework advises you to use inheritance::
sage: m._test_category()
Traceback (most recent call last):
...
AssertionError: False is not true
Using :meth:`__make_element_class__`, makes your concrete valuation
inherit from this class::
sage: m = H.__make_element_class__(DiscretePseudoValuation)(H)
sage: m._test_category()
"""
def is_discrete_pseudo_valuation(self):
r"""
Return whether this valuation is a discrete pseudo-valuation.
EXAMPLES::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: pAdicValuation(QQ, 2).is_discrete_pseudo_valuation()
True
"""
return True
@abstract_method
def is_discrete_valuation(self):
r"""
Return whether this valuation is a discrete valuation, i.e.,
whether it is a :meth:`is_discrete_pseudo_valuation` that only
sends zero to `\infty`.
EXAMPLES::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: pAdicValuation(QQ, 2).is_discrete_valuation()
True
"""
def is_negative_pseudo_valuation(self):
r"""
Return whether this valuation is a discrete pseudo-valuation that
does attain `-\infty`, i.e., it is non-trivial and its domain
contains an element with valuation `\infty` that has an inverse.
EXAMPLES::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: pAdicValuation(QQ, 2).is_negative_pseudo_valuation()
False
"""
from sage.categories.all import Fields
if self.is_discrete_valuation():
return False
elif self.domain() in Fields():
return True
raise NotImplementedError
@cached_method
def is_trivial(self):
r"""
Return whether this valuation is trivial, i.e., whether it is
constant `\infty` or constant zero for everything but the zero
element.
Subclasses need to override this method if they do not implement
:meth:`uniformizer`.
EXAMPLES::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: pAdicValuation(QQ, 7).is_trivial()
False
"""
from sage.rings.all import infinity
if self(self.domain().one()) is infinity:
# the constant infinity
return True
if self(self.uniformizer()) != 0:
# not constant on the non-zero elements
return False
return True
@abstract_method
def uniformizer(self):
r"""
Return an element in :meth:`domain` which has positive valuation
and generates the value group of this valuation.
EXAMPLES::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: pAdicValuation(QQ, 11).uniformizer()
11
Trivial valuations have no uniformizer::
sage: v = DiscretePseudoValuationSpace(QQ).an_element()
sage: v.is_trivial()
True
sage: v.uniformizer()
Traceback (most recent call last):
...
ValueError: Trivial valuations do not define a uniformizing element
"""
@cached_method
def value_group(self):
r"""
Return the value group of this discrete pseudo-valuation, the
discrete additive subgroup of the rational numbers which is
generated by the valuation of the :meth:`uniformizer`.
EXAMPLES::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: pAdicValuation(QQ, 2).value_group()
Additive Abelian Group generated by 1
A pseudo-valuation that is `\infty` everywhere, does not have a
value group::
sage: v = DiscretePseudoValuationSpace(QQ).an_element()
sage: v.value_group()
Traceback (most recent call last):
...
ValueError: The trivial pseudo-valuation that is infinity everywhere does not have a value group.
"""
from value_group import DiscreteValueGroup
return DiscreteValueGroup(self(self.uniformizer()))
def value_semigroup(self):
r"""
Return the value semigroup of this discrete pseudo-valuation, the
additive subsemigroup of the rational numbers which is generated by
the valuations of the elements in :meth:`domain`.
EXAMPLES:
Most commonly, in particular over fields, the semigroup is the
group generated by the valuation of the uniformizer::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: G = pAdicValuation(QQ, 2).value_semigroup(); G
Additive Abelian Semigroup generated by -1, 1
sage: G in AdditiveMagmas().AdditiveAssociative().AdditiveUnital().AdditiveInverse()
True
If the domain is a discrete valuation ring, then the semigroup
consists of the positive elements of the :meth:`value_group`::
sage: pAdicValuation(Zp(2), 2).value_semigroup()
Additive Abelian Semigroup generated by 1
The semigroup can have a more complicated structure when the
uniformizer is not in the domain::
sage: v = pAdicValuation(ZZ, 2)
sage: R.<x> = ZZ[]
sage: w = GaussValuation(R, v)
sage: u = w.augmentation(x, 5/3)
sage: u.value_semigroup()
Additive Abelian Semigroup generated by 1, 5/3
"""
from sage.categories.fields import Fields
if self.domain() in Fields():
from value_group import DiscreteValueSemigroup
return DiscreteValueSemigroup([]) + self.value_group()
raise NotImplementedError("can not determine value semigroup of %r"%(self,))
def element_with_valuation(self, s):
r"""
Return an element in the :meth:`domain` of this valuation with
valuation ``s``.
EXAMPLES::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: v = pAdicValuation(ZZ, 2)
sage: v.element_with_valuation(10)
1024
"""
from sage.rings.all import QQ, ZZ, infinity
s = QQ.coerce(s)
if s not in self.value_semigroup():
raise ValueError("s must be in the value semigroup of this valuation but %r is not in %r"%(s, self.value_semigroup()))
if s == 0:
return self.domain().one()
exp = s/self.value_group().gen()
if exp not in ZZ:
raise NotImplementedError("s must be a multiple of %r but %r is not"%(self.value_group().gen(), s))
ret = self.domain()(self.uniformizer() ** ZZ(exp))
return self.simplify(ret, error=s)
@abstract_method
def residue_ring(self):
r"""
Return the residue ring of this valuation, i.e., the elements of
non-negative valuation modulo the elements of positive valuation.
This is identical to :meth:`residue_field` when a residue field
exists.
EXAMPLES::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: pAdicValuation(QQ, 2).residue_ring()
Finite Field of size 2
sage: TrivialValuation(QQ).residue_ring()
Rational Field
Note that a residue ring always exists, even when a residue field
may not::
sage: TrivialPseudoValuation(QQ).residue_ring()
Quotient of Rational Field by the ideal (1)
sage: TrivialValuation(ZZ).residue_ring()
Integer Ring
sage: GaussValuation(ZZ['x'], pAdicValuation(ZZ, 2)).residue_ring()
Univariate Polynomial Ring in x over Finite Field of size 2 (using NTL)
"""
def residue_field(self):
r"""
Return the residue field of this valuation, i.e., the field of
fractions of the :meth:`residue_ring`, the elements of non-negative
valuation modulo the elements of positive valuation.
EXAMPLES::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: pAdicValuation(QQ, 2).residue_field()
Finite Field of size 2
sage: TrivialValuation(QQ).residue_field()
Rational Field
sage: TrivialValuation(ZZ).residue_field()
Rational Field
sage: GaussValuation(ZZ['x'], pAdicValuation(ZZ, 2)).residue_field()
Rational function field in x over Finite Field of size 2
"""
ret = self.residue_ring()
from sage.categories.fields import Fields
if ret in Fields():
return ret
from sage.rings.polynomial.polynomial_ring import is_PolynomialRing
if is_PolynomialRing(ret):
from sage.rings.function_field.all import FunctionField
return FunctionField(ret.base_ring().fraction_field(), names=(ret.variable_name(),))
return ret.fraction_field()
@abstract_method
def reduce(self, x):
r"""
Return the image of ``x`` in the :meth:`residue_ring` of this
valuation.
EXAMPLES::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: v = pAdicValuation(QQ, 2)
sage: v.reduce(2)
0
sage: v.reduce(1)
1
sage: v.reduce(1/3)
1
sage: v.reduce(1/2)
Traceback (most recent call last):
...
ValueError: reduction is only defined for elements of non-negative valuation
"""
@abstract_method
def lift(self, X):
r"""
Return a lift of ``X`` in :meth:`domain` which reduces down to
``X`` again via :meth:`reduce`.
EXAMPLES::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: v = pAdicValuation(QQ, 2)
sage: v.lift(v.residue_ring().one())
1
"""
def extension(self, ring):
r"""
Return the unique extension of this valuation to ``ring``.
EXAMPLES::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: v = pAdicValuation(ZZ, 2)
sage: w = v.extension(QQ)
sage: w.domain()
Rational Field
"""
extensions = self.extensions(ring)
assert(len(extensions))
if len(extensions) > 1:
raise ValueError("there is no unique extension of %r from %r to %r"%(self, self.domain(), ring))
return extensions[0]
def extensions(self, ring):
r"""
Return the extensions of this valuation to ``ring``.
EXAMPLES::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: v = pAdicValuation(ZZ, 2)
sage: v.extensions(QQ)
[2-adic valuation]
"""
if ring is self.domain():
return [self]
raise NotImplementedError("extending %r from %r to %r not implemented"%(self, self.domain(), ring))
def restriction(self, ring):
r"""
Return the restriction of this valuation to ``ring``.
EXAMPLES::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: v = pAdicValuation(QQ, 2)
sage: w = v.restriction(ZZ)
sage: w.domain()
Integer Ring
"""
if ring is self.domain():
return self
raise NotImplementedError("restricting %r from %r to %r not implemented"%(self, self.domain(), ring))
def change_domain(self, ring):
r"""
Return this valuation over ``ring``.
Unlike :meth:`extension` or meth:`reduction`, this might not be
completely sane mathematically. It is essentially a conversion of
this valuation into another space of valuations.
EXAMPLES::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: v = pAdicValuation(QQ, 3)
sage: v.change_domain(ZZ)
3-adic valuation
"""
if ring is self.domain():
return self
if self.domain().is_subring(ring):
return self.extension(ring)
if ring.is_subring(self.domain()):
return self.restriction(ring)
raise NotImplementedError("changing %r from %r to %r not implemented"%(self, self.domain(), ring))
def scale(self, scalar):
r"""
Return this valuation scaled by ``scalar``.
INPUT:
- ``scalar`` -- a non-negative rational number or infinity
EXAMPLES::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: v = pAdicValuation(ZZ, 3)
sage: w = v.scale(3)
sage: w(3)
3
Scaling can also be done through multiplication with a scalar::
sage: w/3 == v
True
Multiplication by zero produces the trivial discrete valuation::
sage: w = 0*v
sage: w(3)
0
sage: w(0)
+Infinity
Multiplication by infinity produces the trivial discrete
pseudo-valuation::
sage: w = infinity*v
sage: w(3)
+Infinity
sage: w(0)
+Infinity
"""
from sage.rings.all import infinity
if scalar is infinity:
from trivial_valuation import TrivialPseudoValuation
return TrivialPseudoValuation(self.domain())
if scalar == 0:
from trivial_valuation import TrivialValuation
return TrivialValuation(self.domain())
if scalar == 1:
return self
if scalar < 0:
raise ValueError("scalar must be non-negative")
if self.is_trivial():
return self
from scaled_valuation import ScaledValuation_generic
if isinstance(self, ScaledValuation_generic):
return self._base_valuation.scale(scalar * self._scale)
from scaled_valuation import ScaledValuation
return ScaledValuation(self, scalar)
def separating_element(self, others):
r"""
Return an element in the domain of this valuation which has
positive valuation with respect to this valuation but negative
valuation with respect to the valuations in ``others``.
EXAMPLES::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: v2 = pAdicValuation(QQ, 2)
sage: v3 = pAdicValuation(QQ, 3)
sage: v5 = pAdicValuation(QQ, 5)
sage: v2.separating_element([v3,v5])
4/15
"""
try:
iter(others)
except TypeError:
raise ValueError("others must be a list of valuations")
for other in others + [self]:
if other.parent() is not self.parent():
raise ValueError("all valuations must be valuations on %r but %r is a valuation on %r"%(self.domain(), other, other.domain()))
if not other.is_discrete_valuation():
raise ValueError("all valuationss must be discrete valuations but %r is not"%(other,))
if other.is_trivial():
raise ValueError("all valuations must be non-trivial but %r is not"%(other,))
if len(others)==0:
return self.uniformizer()
# see the proof of Lemma 6.9 in http://www1.spms.ntu.edu.sg/~frederique/antchap6.pdf
ret = self._strictly_separating_element(others[0])
for i in range(1, len(others)):
# ret is an element which separates self and others[:i]
if others[i](ret) < 0:
# it also separates self and others[i]
continue
delta = self._strictly_separating_element(others[i])
if others[i](ret) == 0:
# combining powers of ret and delta, we produce a
# separating element for self and others[:i+1]
factor = ret
ret = delta
while any(other(ret) >= 0 for other in others[:i]):
assert(others[i](ret) < 0)
ret *= factor
else: # others[i](ret) > 0
# construct an element which approximates a unit with respect to others[i]
# and has negative valuation with respect to others[:i]
from sage.rings.all import NN
for r in iter(NN):
# When we enter this loop we are essentially out of
# luck. The size of the coefficients is likely going
# through the roof here and this is not going to
# terminate in reasonable time.
factor = (ret**r)/(1+ret**r)
ret = factor * delta
if all([other(ret) < 0 for other in others[:i+1]]):
break
return ret
def _strictly_separating_element(self, other):
r"""
Return an element in the domain of this valuation which has
positive valuation with respect to this valuation but negative
valuation with respect to ``other``.
.. NOTE::
Overriding this method tends to be a nuissance as you need to
handle all possible types (as in Python type) of valuations.
This is essentially the same problem that you have when
implementing operators such as ``+`` or ``>=``. A sufficiently
fancy multimethod implementation could solve that here but
there is currently nothing like that in Sage/Python.
EXAMPLES::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: v2 = pAdicValuation(QQ, 2)
sage: v3 = pAdicValuation(QQ, 3)
sage: v2._strictly_separating_element(v3)
2/3
"""
from sage.rings.all import ZZ, NN, infinity
numerator = self._weakly_separating_element(other)
n = self(numerator)
nn = other(numerator)
assert(n > 0)
assert(nn is not infinity)
if (nn < 0):
return numerator
denominator = other._weakly_separating_element(self)
d = self(denominator)
dd = other(denominator)
assert(dd > 0)
assert(d is not infinity)
if d < 0:
return self.domain()(1/denominator)
# We need non-negative integers a and b such that
# a*n - b*d > 0 and a*nn - b*dd < 0
if nn == 0:
# the above becomes b != 0 and a/b > d/n
b = 1
if d/n in ZZ:
a = d/n + 1
else:
a = (d/n).ceil()
else:
# Since n,nn,d,dd are all non-negative this is essentially equivalent to
# a/b > d/n and b/a > nn/dd
# which is
# dd/nn > a/b > d/n
assert(dd/nn > d/n)
for b in iter(NN):
# we naĩvely find the smallest b which can satisfy such an equation
# there are faster algorithms for this
# https://dl.acm.org/citation.cfm?id=1823943&CFID=864015776&CFTOKEN=26270402
if b == 0:
continue
assert(b <= n + nn) # (a+b)/(n+nn) is a solution
if nn/dd/b in ZZ:
a = nn/dd/b + 1
else:
a = (nn/dd/b).ceil()
assert(a/b > d/n)
if dd/nn > a/b:
break
ret = self.domain()(numerator**a / denominator**b)
assert(self(ret) > 0)
assert(other(ret) < 0)
return ret
def _weakly_separating_element(self, other):
r"""
Return an element in the domain of this valuation which has
positive valuation with respect to this valuation and higher
valuation with respect to this valuation than with respect to
``other``.
.. NOTE::
Overriding this method tends to be a nuissance as you need to
handle all possible types (as in Python type) of valuations.
This is essentially the same problem that you have when
implementing operators such as ``+`` or ``>=``. A sufficiently
fancy multimethod implementation could solve that here but
there is currently nothing like that in Sage/Python.
EXAMPLES::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: v2 = pAdicValuation(QQ, 2)
sage: v3 = pAdicValuation(QQ, 3)
sage: v2._weakly_separating_element(v3)
2
"""
ret = self.uniformizer()
if self(ret) > other(ret):
return ret
raise NotImplementedError("weakly separating element for %r and %r"%(self, other))
def shift(self, x, s):
r"""
Shift ``x`` in its expansion with respect to :meth:`uniformizer` by
``s`` "digits".
For non-negative ``s``, this just returns ``x`` multiplied by a
power of the uniformizer `\pi`.
For negative ``s``, it does the same but when not over a field, it
drops coefficients in the `\pi`-adic expension which have negative
valuation.
EXAMPLES::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: v = pAdicValuation(ZZ, 2)
sage: v.shift(1, 10)
1024
sage: v.shift(11, -1)
5
For some rings, there is no clear `\pi`-adic expansion. In this
case, this method performs negative shifts by iterated division by
the uniformizer and substraction of a lift of the reduction::
sage: R.<x> = ZZ[]
sage: v = pAdicValuation(ZZ, 2)
sage: w = GaussValuation(R, v)
sage: w.shift(x, 1)
2*x
sage: w.shift(2*x, -1)
x
sage: w.shift(x + 2*x^2, -1)
x^2
"""
from sage.rings.all import ZZ
x = self.domain().coerce(x)
s = self.value_group()(s)
if s == 0:
return x
s = ZZ(s / self.value_group().gen())
if s > 0:
return x * self.uniformizer()**s
else: # s < 0
if ~self.uniformizer() in self.domain():
return x / self.uniformizer()**(-s)
else:
for i in range(-s):
if self(x) < 0:
raise NotImplementedError("can not compute general shifts over non-fields which do contain elements of negative valuation")
x -= self.lift(self.reduce(x))
x //= self.uniformizer()
return x
def simplify(self, x, error=None, force=False):
r"""
Return a simplified version of ``x``.
Produce an element which differs from ``x`` by an element of
valuation strictly greater than the valuation of ``x`` (or strictly
greater than ``error`` if set.)
EXAMPLES::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: v = pAdicValuation(ZZ, 2)
sage: v.simplify(6, force=True)
2
sage: v.simplify(6, error=0, force=True)
0
"""
x = self.domain().coerce(x)
if error is not None and self(x) > error:
return self.domain().zero()
return x
def lower_bound(self, x):
r"""
Return a lower bound of this valuation at ``x``.
Use this method to get an approximation of the valuation of ``x``
when speed is more important than accuracy.
EXAMPLES::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: v = pAdicValuation(ZZ, 2)
sage: v.lower_bound(2^10)
10
"""
return self(x)
def upper_bound(self, x):
r"""
Return an upper bound of this valuation at ``x``.
Use this method to get an approximation of the valuation of ``x``
when speed is more important than accuracy.
EXAMPLES::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: v = pAdicValuation(ZZ, 2)
sage: v.upper_bound(2^10)
10
"""
return self(x)
def _relative_size(self, x):
r"""
Return an estimate on the coefficient size of ``x``.
The number returned is an estimate on the factor between the number of