This repository has been archived by the owner on Mar 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
value_group.py
695 lines (535 loc) · 22.5 KB
/
value_group.py
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
# -*- coding: utf-8 -*-
r"""
Value groups of discrete valuations
This file defines additive sub(semi-)groups of \QQ and related structures.
EXAMPLES::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: v = pAdicValuation(ZZ, 2)
sage: v.value_group()
Additive Abelian Group generated by 1
sage: v.value_semigroup()
Additive Abelian Semigroup generated by 1
AUTHORS:
- Julian Rüth (2013-09-06): initial version
"""
#*****************************************************************************
# Copyright (C) 2013-2016 Julian Rüth <[email protected]>
#
# 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.structure.parent import Parent
from sage.structure.unique_representation import UniqueRepresentation
from sage.rings.all import QQ, ZZ, infinity
from sage.misc.cachefunc import cached_method
class DiscreteValuationCodomain(UniqueRepresentation, Parent):
r"""
The codomain of discrete valuations, the rational numbers extended by
`\pm\infty`.
EXAMPLES::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: C = DiscreteValuationCodomain(); C
Codomain of Discrete Valuations
TESTS::
sage: TestSuite(C).run() # long time
"""
def __init__(self):
r"""
TESTS::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: isinstance(pAdicValuation(QQ, 2).codomain(), DiscreteValuationCodomain)
True
"""
from sage.sets.finite_enumerated_set import FiniteEnumeratedSet
from sage.categories.additive_monoids import AdditiveMonoids
UniqueRepresentation.__init__(self)
Parent.__init__(self, facade=(QQ, FiniteEnumeratedSet([infinity, -infinity])), category=AdditiveMonoids())
def _element_constructor_(self, x):
r"""
Create an element from ``x``.
INPUT:
- ``x`` -- a rational number or `\infty`
TESTS::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: DiscreteValuationCodomain()(0)
0
sage: DiscreteValuationCodomain()(infinity)
+Infinity
sage: DiscreteValuationCodomain()(-infinity)
-Infinity
"""
if x is infinity:
return x
if x is -infinity:
return x
if x not in QQ:
raise ValueError("must be a rational number or infinity")
return QQ.coerce(x)
def _repr_(self):
r"""
Return a printable representation.
EXAMPLES::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: DiscreteValuationCodomain() # indirect doctest
Codomain of Discrete Valuations
"""
return "Codomain of Discrete Valuations"
class DiscreteValueGroup(UniqueRepresentation, Parent):
r"""
The value group of a discrete valuation, an additive subgroup of \QQ
generated by ``generator``.
INPUT:
- ``generator`` -- a rational number
.. NOTE::
We do not rely on the functionality provided by additive abelian groups
in Sage since these require the underlying set to be the integers.
Therefore, we roll our own \Z-module here.
We could have used :class:`AdditiveAbelianGroupWrapper` here, but it
seems to be somewhat outdated. In particular, generic group
functionality should now come from the category and not from the
super-class. A facade of \Q appeared to be the better approach.
EXAMPLES::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: D1 = DiscreteValueGroup(0); D1
Trivial Additive Abelian Group
sage: D2 = DiscreteValueGroup(4/3); D2
Additive Abelian Group generated by 4/3
sage: D3 = DiscreteValueGroup(-1/3); D3
Additive Abelian Group generated by 1/3
TESTS::
sage: TestSuite(D1).run() # long time
sage: TestSuite(D2).run() # long time
sage: TestSuite(D3).run() # long time
"""
@staticmethod
def __classcall__(cls, generator):
r"""
Normalizes ``generator`` to a positive rational so that this is a
unique parent.
TESTS::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: DiscreteValueGroup(1) is DiscreteValueGroup(-1)
True
"""
generator = QQ.coerce(generator)
generator = generator.abs()
return super(DiscreteValueGroup, cls).__classcall__(cls, generator)
def __init__(self, generator):
r"""
TESTS::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: isinstance(DiscreteValueGroup(0), DiscreteValueGroup)
True
"""
from sage.categories.modules import Modules
self._generator = generator
# We can not set the facade to DiscreteValuationCodomain since there
# are some issues with iterated facades currently
UniqueRepresentation.__init__(self)
Parent.__init__(self, facade=QQ, category=Modules(ZZ))
def _element_constructor_(self, x):
r"""
Create an element in this group from ``x``.
INPUT:
- ``x`` -- a rational number
TESTS::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: DiscreteValueGroup(0)(0)
0
sage: DiscreteValueGroup(0)(1)
Traceback (most recent call last):
...
ValueError: `1` is not in Trivial Additive Abelian Group.
sage: DiscreteValueGroup(1)(1)
1
sage: DiscreteValueGroup(1)(1/2)
Traceback (most recent call last):
...
ValueError: `1/2` is not in Additive Abelian Group generated by 1.
"""
x = QQ.coerce(x)
if x == 0 or (self._generator != 0 and x/self._generator in ZZ):
return x
raise ValueError("`{0}` is not in {1}.".format(x,self))
def _repr_(self):
r"""
Return a printable representation for this group.
EXAMPLES::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: DiscreteValueGroup(0) # indirect doctest
Trivial Additive Abelian Group
"""
if self.is_trivial():
return "Trivial Additive Abelian Group"
return "Additive Abelian Group generated by %r"%(self._generator,)
def __add__(self, other):
r"""
Return the subgroup of \QQ generated by this group and ``other``.
INPUT:
- ``other`` -- a discrete value group or a rational number
EXAMPLES::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: D = DiscreteValueGroup(1/2)
sage: D + 1/3
Additive Abelian Group generated by 1/6
sage: D + D
Additive Abelian Group generated by 1/2
sage: D + 1
Additive Abelian Group generated by 1/2
sage: DiscreteValueGroup(2/7) + DiscreteValueGroup(4/9)
Additive Abelian Group generated by 2/63
"""
if isinstance(other, DiscreteValueGroup):
return DiscreteValueGroup(self._generator.gcd(other._generator))
if isinstance(other, DiscreteValueSemigroup):
return other + self
from sage.structure.element import is_Element
if is_Element(other) and QQ.has_coerce_map_from(other.parent()):
return self + DiscreteValueGroup(other)
raise ValueError("`other` must be a DiscreteValueGroup or a rational number")
def _mul_(self, other, switch_sides=False):
r"""
Return the group generated by ``other`` times the generator of this
group.
INPUT:
- ``other`` -- a rational number
EXAMPLES::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: D = DiscreteValueGroup(1/2)
sage: 1/2 * D
Additive Abelian Group generated by 1/4
sage: D * (1/2)
Additive Abelian Group generated by 1/4
sage: D * 0
Trivial Additive Abelian Group
"""
other = QQ.coerce(other)
return DiscreteValueGroup(self._generator*other)
def index(self, other):
r"""
Return the index of ``other`` in this group.
INPUT:
- ``other`` -- a subgroup of this group
EXAMPLES::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: DiscreteValueGroup(3/8).index(DiscreteValueGroup(3))
8
sage: DiscreteValueGroup(3).index(DiscreteValueGroup(3/8))
Traceback (most recent call last):
...
ValueError: other must be a subgroup of this group
sage: DiscreteValueGroup(3).index(DiscreteValueGroup(0))
Traceback (most recent call last):
...
ValueError: other must have finite index in this group
sage: DiscreteValueGroup(0).index(DiscreteValueGroup(0))
1
sage: DiscreteValueGroup(0).index(DiscreteValueGroup(3))
Traceback (most recent call last):
...
ValueError: other must be a subgroup of this group
"""
if not isinstance(other, DiscreteValueGroup):
raise ValueError("other must be a DiscreteValueGroup")
if other._generator not in self:
raise ValueError("other must be a subgroup of this group")
if other._generator == 0:
if self._generator == 0:
return ZZ(1)
else:
raise ValueError("other must have finite index in this group")
return ZZ(other._generator / self._generator)
def numerator(self):
r"""
Return the numerator of a generator of this group.
EXAMPLES::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: DiscreteValueGroup(3/8).numerator()
3
"""
return self._generator.numerator()
def denominator(self):
r"""
Return the denominator of a generator of this group.
EXAMPLES::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: DiscreteValueGroup(3/8).denominator()
8
"""
return self._generator.denominator()
def gen(self):
r"""
Return a generator of this group.
EXAMPLES::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: DiscreteValueGroup(-3/8).gen()
3/8
"""
return self._generator
def some_elements(self):
r"""
Return some typical elements in this group.
EXAMPLES::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: DiscreteValueGroup(-3/8).some_elements()
[3/8, -3/8, 0, 42, 3/2, -3/2, 9/8, -9/8]
"""
return [self._generator, -self._generator] + [x for x in QQ.some_elements() if x in self]
def is_trivial(self):
r"""
Return whether this is the trivial additive abelian group.
EXAMPLES::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: DiscreteValueGroup(-3/8).is_trivial()
False
sage: DiscreteValueGroup(0).is_trivial()
True
"""
return self._generator.is_zero()
class DiscreteValueSemigroup(UniqueRepresentation, Parent):
r"""
The value semigroup of a discrete valuation, an additive subsemigroup of
\QQ generated by ``generators``.
INPUT:
- ``generators`` -- rational numbers
EXAMPLES::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: D1 = DiscreteValueSemigroup(0); D1
Trivial Additive Abelian Semigroup
sage: D2 = DiscreteValueSemigroup(4/3); D2
Additive Abelian Semigroup generated by 4/3
sage: D3 = DiscreteValueSemigroup([-1/3, 1/2]); D3
Additive Abelian Semigroup generated by -1/3, 1/2
TESTS::
sage: TestSuite(D1).run() # long time
sage: TestSuite(D2).run() # long time
sage: TestSuite(D3).run() # long time
"""
@staticmethod
def __classcall__(cls, generators):
r"""
Normalize ``generators``.
TESTS:
We do not find minimal generators or something like that but just sort the
generators and drop generators that are trivially contained in the
semigroup generated by the remaining generators::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: DiscreteValueSemigroup([1,2]) is DiscreteValueSemigroup([1])
True
In this case, the normalization is not sufficient to determine that
these are the same semigroup::
sage: DiscreteValueSemigroup([1,-1,1/3]) is DiscreteValueSemigroup([1/3,-1/3])
False
"""
if generators in QQ:
generators = [generators]
generators = list(set([QQ.coerce(g) for g in generators if g != 0]))
generators.sort()
simplified_generators = generators
# this is not very efficient but there should never be more than a
# couple of generators
for g in generators:
for h in generators:
if g == h: continue
from sage.rings.all import NN
if h/g in NN:
simplified_generators.remove(h)
break
return super(DiscreteValueSemigroup, cls).__classcall__(cls, tuple(simplified_generators))
def __init__(self, generators):
r"""
TESTS::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: isinstance(DiscreteValueSemigroup([0]), DiscreteValueSemigroup)
True
"""
from sage.categories.all import AdditiveMagmas
self._generators = generators
category = AdditiveMagmas().AdditiveAssociative().AdditiveUnital()
if all([-g in generators for g in generators]):
# check whether this is trivially a group
# is_group() performs a complete check that is very costly and
# refines the category
category = category.AdditiveInverse()
# We can not set the facade to DiscreteValuationCodomain since there
# are some issues with iterated facades currently
Parent.__init__(self, facade=QQ, category=category)
def _solve_linear_program(self, target):
r"""
Return the coefficients of a linear combination to write ``target`` in
terms of the generators of this semigroup.
Return ``None`` if no such combination exists.
EXAMPLES::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: D = DiscreteValueSemigroup([2,3,5])
sage: D._solve_linear_program(12)
{0: 1, 1: 0, 2: 2}
sage: 1*2 + 0*3 + 2*5
12
"""
if len(self._generators) == 0:
if target == 0:
return {}
else:
return None
if len(self._generators) == 1:
from sage.rings.all import NN
exp = target / self._generators[0]
if exp not in NN:
return None
return {0 : exp}
if len(self._generators) == 2 and self._generators[0] == - self._generators[1]:
from sage.rings.all import ZZ
exp = target / self._generators[0]
if exp not in ZZ:
return None
return {0: exp, 1: 0}
from sage.numerical.mip import MixedIntegerLinearProgram, MIPSolverException
P = MixedIntegerLinearProgram(maximization=False, solver="ppl")
x = P.new_variable(integer=True, nonnegative=True)
constraint = sum([g*x[i] for i,g in enumerate(self._generators)]) == target
P.add_constraint(constraint)
P.set_objective(None)
try:
P.solve()
except MIPSolverException:
return None
return P.get_values(x)
def _element_constructor_(self, x):
r"""
Create an element in this group from ``x``.
INPUT:
- ``x`` -- a rational number
TESTS::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: DiscreteValueSemigroup([])(0)
0
sage: DiscreteValueSemigroup([])(1)
Traceback (most recent call last):
...
ValueError: `1` is not in Trivial Additive Abelian Semigroup.
sage: DiscreteValueSemigroup([1])(1)
1
sage: DiscreteValueSemigroup([1])(-1)
Traceback (most recent call last):
...
ValueError: `-1` is not in Additive Abelian Semigroup generated by 1.
"""
x = QQ.coerce(x)
if x in self._generators or self._solve_linear_program(x) is not None:
return x
raise ValueError("`{0}` is not in {1}.".format(x,self))
def _repr_(self):
r"""
Return a printable representation for this semigroup.
EXAMPLES::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: DiscreteValueSemigroup(0) # indirect doctest
Trivial Additive Abelian Semigroup
"""
if self.is_trivial():
return "Trivial Additive Abelian Semigroup"
return "Additive Abelian Semigroup generated by %s"%(', '.join([repr(g) for g in self._generators]),)
def __add__(self, other):
r"""
Return the subsemigroup of \QQ generated by this semigroup and ``other``.
INPUT:
- ``other`` -- a discrete value (semi-)group or a rational number
EXAMPLES::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: D = DiscreteValueSemigroup(1/2)
sage: D + 1/3
Additive Abelian Semigroup generated by 1/3, 1/2
sage: D + D
Additive Abelian Semigroup generated by 1/2
sage: D + 1
Additive Abelian Semigroup generated by 1/2
sage: DiscreteValueGroup(2/7) + DiscreteValueSemigroup(4/9)
Additive Abelian Semigroup generated by -2/7, 2/7, 4/9
"""
if isinstance(other, DiscreteValueSemigroup):
return DiscreteValueSemigroup(self._generators + other._generators)
if isinstance(other, DiscreteValueGroup):
return DiscreteValueSemigroup(self._generators + (other._generator, -other._generator))
from sage.structure.element import is_Element
if is_Element(other) and QQ.has_coerce_map_from(other.parent()):
return self + DiscreteValueSemigroup(other)
raise ValueError("`other` must be a DiscreteValueGroup, a DiscreteValueSemigroup or a rational number")
def _mul_(self, other, switch_sides=False):
r"""
Return the semigroup generated by ``other`` times the generators of this
semigroup.
INPUT:
- ``other`` -- a rational number
EXAMPLES::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: D = DiscreteValueSemigroup(1/2)
sage: 1/2 * D
Additive Abelian Semigroup generated by 1/4
sage: D * (1/2)
Additive Abelian Semigroup generated by 1/4
sage: D * 0
Trivial Additive Abelian Semigroup
"""
other = QQ.coerce(other)
return DiscreteValueSemigroup([g*other for g in self._generators])
def gens(self):
r"""
Return the generators of this semigroup.
EXAMPLES::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: DiscreteValueSemigroup(-3/8).gens()
(-3/8,)
"""
return tuple(self._generators)
def some_elements(self):
r"""
Return some typical elements in this semigroup.
EXAMPLES::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: list(DiscreteValueSemigroup([-3/8,1/2]).some_elements())
[0, -3/8, 1/2, ...]
"""
yield self(0)
if self.is_trivial():
return
for g in self._generators:
yield g
from sage.rings.all import ZZ
for x in (ZZ**len(self._generators)).some_elements():
yield QQ.coerce(sum([abs(c)*g for c,g in zip(x,self._generators)]))
def is_trivial(self):
r"""
Return whether this is the trivial additive abelian semigroup.
EXAMPLES::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: DiscreteValueSemigroup(-3/8).is_trivial()
False
sage: DiscreteValueSemigroup([]).is_trivial()
True
"""
return len(self._generators) == 0
@cached_method
def is_group(self):
r"""
Return whether this semigroup is a group.
EXAMPLES::
sage: sys.path.append(os.getcwd()); from mac_lane import * # optional: standalone
sage: DiscreteValueSemigroup(1).is_group()
False
sage: D = DiscreteValueSemigroup([-1, 1])
sage: D.is_group()
True
Invoking this method also changes the category of this semigroup if it
is a group::
sage: D in AdditiveMagmas().AdditiveAssociative().AdditiveUnital().AdditiveInverse()
True
"""
for x in self._generators:
if -x not in self:
return False
else:
self._refine_category_(self.category().AdditiveInverse())
return True