This repository has been archived by the owner on Jan 30, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
pushout.py
4394 lines (3667 loc) · 161 KB
/
pushout.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
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
"""
Coercion via Construction Functors
"""
from __future__ import print_function, absolute_import
from six.moves import range
import six
from sage.misc.lazy_import import lazy_import
from .functor import Functor, IdentityFunctor_generic
lazy_import('sage.categories.commutative_additive_groups', 'CommutativeAdditiveGroups')
lazy_import('sage.categories.commutative_rings', 'CommutativeRings')
lazy_import('sage.categories.groups', 'Groups')
lazy_import('sage.categories.objects', 'Objects')
lazy_import('sage.categories.rings', 'Rings', at_startup=True)
lazy_import('sage.structure.parent', 'CoercionException')
# TODO, think through the rankings, and override pushout where necessary.
class ConstructionFunctor(Functor):
"""
Base class for construction functors.
A construction functor is a functorial algebraic construction,
such as the construction of a matrix ring over a given ring
or the fraction field of a given ring.
In addition to the class :class:`~sage.categories.functor.Functor`,
construction functors provide rules for combining and merging
constructions. This is an important part of Sage's coercion model,
namely the pushout of two constructions: When a polynomial ``p`` in
a variable ``x`` with integer coefficients is added to a rational
number ``q``, then Sage finds that the parents ``ZZ['x']`` and
``QQ`` are obtained from ``ZZ`` by applying a polynomial ring
construction respectively the fraction field construction. Each
construction functor has an attribute ``rank``, and the rank of
the polynomial ring construction is higher than the rank of the
fraction field construction. This means that the pushout of ``QQ``
and ``ZZ['x']``, and thus a common parent in which ``p`` and ``q``
can be added, is ``QQ['x']``, since the construction functor with
a lower rank is applied first.
::
sage: F1, R = QQ.construction()
sage: F1
FractionField
sage: R
Integer Ring
sage: F2, R = (ZZ['x']).construction()
sage: F2
Poly[x]
sage: R
Integer Ring
sage: F3 = F2.pushout(F1)
sage: F3
Poly[x](FractionField(...))
sage: F3(R)
Univariate Polynomial Ring in x over Rational Field
sage: from sage.categories.pushout import pushout
sage: P.<x> = ZZ[]
sage: pushout(QQ,P)
Univariate Polynomial Ring in x over Rational Field
sage: ((x+1) + 1/2).parent()
Univariate Polynomial Ring in x over Rational Field
When composing two construction functors, they are sometimes
merged into one, as is the case in the Quotient construction::
sage: Q15, R = (ZZ.quo(15*ZZ)).construction()
sage: Q15
QuotientFunctor
sage: Q35, R = (ZZ.quo(35*ZZ)).construction()
sage: Q35
QuotientFunctor
sage: Q15.merge(Q35)
QuotientFunctor
sage: Q15.merge(Q35)(ZZ)
Ring of integers modulo 5
Functors can not only be applied to objects, but also to morphisms in the
respective categories. For example::
sage: P.<x,y> = ZZ[]
sage: F = P.construction()[0]; F
MPoly[x,y]
sage: A.<a,b> = GF(5)[]
sage: f = A.hom([a+b,a-b],A)
sage: F(A)
Multivariate Polynomial Ring in x, y over Multivariate Polynomial Ring in a, b over Finite Field of size 5
sage: F(f)
Ring endomorphism of Multivariate Polynomial Ring in x, y over Multivariate Polynomial Ring in a, b over Finite Field of size 5
Defn: Induced from base ring by
Ring endomorphism of Multivariate Polynomial Ring in a, b over Finite Field of size 5
Defn: a |--> a + b
b |--> a - b
sage: F(f)(F(A)(x)*a)
(a + b)*x
"""
def __mul__(self, other):
"""
Compose ``self`` and ``other`` to a composite construction
functor, unless one of them is the identity.
NOTE:
The product is in functorial notation, i.e., when applying the
product to an object, the second factor is applied first.
TESTS::
sage: from sage.categories.pushout import IdentityConstructionFunctor
sage: I = IdentityConstructionFunctor()
sage: F = QQ.construction()[0]
sage: P = ZZ['t'].construction()[0]
sage: F*P
FractionField(Poly[t](...))
sage: P*F
Poly[t](FractionField(...))
sage: (F*P)(ZZ)
Fraction Field of Univariate Polynomial Ring in t over Integer Ring
sage: I*P is P
True
sage: F*I is F
True
"""
if not isinstance(self, ConstructionFunctor) and not isinstance(other, ConstructionFunctor):
raise CoercionException("Non-constructive product")
if isinstance(other,IdentityConstructionFunctor):
return self
if isinstance(self,IdentityConstructionFunctor):
return other
return CompositeConstructionFunctor(other, self)
def pushout(self, other):
"""
Composition of two construction functors, ordered by their ranks.
NOTE:
- This method seems not to be used in the coercion model.
- By default, the functor with smaller rank is applied first.
TESTS::
sage: F = QQ.construction()[0]
sage: P = ZZ['t'].construction()[0]
sage: F.pushout(P)
Poly[t](FractionField(...))
sage: P.pushout(F)
Poly[t](FractionField(...))
"""
if self.rank > other.rank:
return self * other
else:
return other * self
def __eq__(self, other):
"""
Equality here means that they are mathematically equivalent, though they may have
specific implementation data. This method will usually be overloaded in subclasses.
by default, only the types of the functors are compared. Also see the \code{merge}
function.
TESTS::
sage: from sage.categories.pushout import IdentityConstructionFunctor
sage: I = IdentityConstructionFunctor()
sage: F = QQ.construction()[0]
sage: P = ZZ['t'].construction()[0]
sage: I == F # indirect doctest
False
sage: I == I # indirect doctest
True
"""
return type(self) == type(other)
def __ne__(self, other):
"""
Check whether ``self`` is not equal to ``other``.
EXAMPLES::
sage: from sage.categories.pushout import IdentityConstructionFunctor
sage: I = IdentityConstructionFunctor()
sage: F = QQ.construction()[0]
sage: P = ZZ['t'].construction()[0]
sage: I != F # indirect doctest
True
sage: I != I # indirect doctest
False
"""
return not (self == other)
def _repr_(self):
"""
NOTE:
By default, it returns the name of the construction functor's class.
Usually, this method will be overloaded.
TESTS::
sage: F = QQ.construction()[0]
sage: F # indirect doctest
FractionField
sage: Q = ZZ.quo(2).construction()[0]
sage: Q # indirect doctest
QuotientFunctor
"""
s = str(type(self))
import re
return re.sub("<.*'.*\.([^.]*)'>", "\\1", s)
def merge(self, other):
"""
Merge ``self`` with another construction functor, or return None.
NOTE:
The default is to merge only if the two functors coincide. But this
may be overloaded for subclasses, such as the quotient functor.
EXAMPLES::
sage: F = QQ.construction()[0]
sage: P = ZZ['t'].construction()[0]
sage: F.merge(F)
FractionField
sage: F.merge(P)
sage: P.merge(F)
sage: P.merge(P)
Poly[t]
"""
if self == other:
return self
else:
return None
def commutes(self, other):
"""
Determine whether ``self`` commutes with another construction functor.
NOTE:
By default, ``False`` is returned in all cases (even if the two
functors are the same, since in this case :meth:`merge` will apply
anyway). So far there is no construction functor that overloads
this method. Anyway, this method only becomes relevant if two
construction functors have the same rank.
EXAMPLES::
sage: F = QQ.construction()[0]
sage: P = ZZ['t'].construction()[0]
sage: F.commutes(P)
False
sage: P.commutes(F)
False
sage: F.commutes(F)
False
"""
return False
def expand(self):
"""
Decompose ``self`` into a list of construction functors.
NOTE:
The default is to return the list only containing ``self``.
EXAMPLES::
sage: F = QQ.construction()[0]
sage: F.expand()
[FractionField]
sage: Q = ZZ.quo(2).construction()[0]
sage: Q.expand()
[QuotientFunctor]
sage: P = ZZ['t'].construction()[0]
sage: FP = F*P
sage: FP.expand()
[FractionField, Poly[t]]
"""
return [self]
# See the pushout() function below for explanation.
coercion_reversed = False
def common_base(self, other_functor, self_bases, other_bases):
r"""
This function is called by :func:`pushout` when no common parent
is found in the construction tower.
.. NOTE::
The main use is for multivariate construction functors,
which use this function to implement recursion for
:func:`pushout`.
INPUT:
- ``other_functor`` -- a construction functor.
- ``self_bases`` -- the arguments passed to this functor.
- ``other_bases`` -- the arguments passed to the functor
``other_functor``.
OUTPUT:
Nothing, since a
:class:`~sage.structure.coerce_exceptions.CoercionException`
is raised.
.. NOTE::
Overload this function in derived class, see
e.e. :class:`MultivariateConstructionFunctor`.
TESTS::
sage: from sage.categories.pushout import pushout
sage: pushout(QQ, cartesian_product([ZZ])) # indirect doctest
Traceback (most recent call last):
...
CoercionException: No common base ("join") found for
FractionField(Integer Ring) and The cartesian_product functorial construction(Integer Ring).
"""
self._raise_common_base_exception_(
other_functor, self_bases, other_bases)
def _raise_common_base_exception_(self, other_functor,
self_bases, other_bases,
reason=None):
r"""
Raise a coercion exception.
INPUT:
- ``other_functor`` -- a functor.
- ``self_bases`` -- the arguments passed to this functor.
- ``other_bases`` -- the arguments passed to the functor
``other_functor``.
- ``reason`` -- a string or ``None`` (default).
TESTS::
sage: from sage.categories.pushout import pushout
sage: pushout(QQ, cartesian_product([QQ])) # indirect doctest
Traceback (most recent call last):
...
CoercionException: No common base ("join") found for
FractionField(Integer Ring) and The cartesian_product functorial construction(Rational Field).
"""
if not isinstance(self_bases, (tuple, list)):
self_bases = (self_bases,)
if not isinstance(other_bases, (tuple, list)):
other_bases = (other_bases,)
if reason is None:
reason = '.'
else:
reason = ': ' + reason + '.'
raise CoercionException(
'No common base ("join") found for %s(%s) and %s(%s)%s' %
(self, ', '.join(str(b) for b in self_bases),
other_functor, ', '.join(str(b) for b in other_bases),
reason))
class CompositeConstructionFunctor(ConstructionFunctor):
"""
A Construction Functor composed by other Construction Functors.
INPUT:
``F1, F2,...``: A list of Construction Functors. The result is the
composition ``F1`` followed by ``F2`` followed by ...
EXAMPLES::
sage: from sage.categories.pushout import CompositeConstructionFunctor
sage: F = CompositeConstructionFunctor(QQ.construction()[0],ZZ['x'].construction()[0],QQ.construction()[0],ZZ['y'].construction()[0])
sage: F
Poly[y](FractionField(Poly[x](FractionField(...))))
sage: F == loads(dumps(F))
True
sage: F == CompositeConstructionFunctor(*F.all)
True
sage: F(GF(2)['t'])
Univariate Polynomial Ring in y over Fraction Field of Univariate Polynomial Ring in x over Fraction Field of Univariate Polynomial Ring in t over Finite Field of size 2 (using NTL)
"""
def __init__(self, *args):
"""
TESTS::
sage: from sage.categories.pushout import CompositeConstructionFunctor
sage: F = CompositeConstructionFunctor(QQ.construction()[0],ZZ['x'].construction()[0],QQ.construction()[0],ZZ['y'].construction()[0])
sage: F
Poly[y](FractionField(Poly[x](FractionField(...))))
sage: F == CompositeConstructionFunctor(*F.all)
True
"""
self.all = []
for c in args:
if isinstance(c, list):
self.all += c
elif isinstance(c, CompositeConstructionFunctor):
self.all += c.all
else:
self.all.append(c)
Functor.__init__(self, self.all[0].domain(), self.all[-1].codomain())
def _apply_functor_to_morphism(self, f):
"""
Apply the functor to an object of ``self``'s domain.
TESTS::
sage: from sage.categories.pushout import CompositeConstructionFunctor
sage: F = CompositeConstructionFunctor(QQ.construction()[0],ZZ['x'].construction()[0],QQ.construction()[0],ZZ['y'].construction()[0])
sage: R.<a,b> = QQ[]
sage: f = R.hom([a+b, a-b])
sage: F(f) # indirect doctest
Ring endomorphism of Univariate Polynomial Ring in y over Fraction Field of Univariate Polynomial Ring in x over Fraction Field of Multivariate Polynomial Ring in a, b over Rational Field
Defn: Induced from base ring by
Ring endomorphism of Fraction Field of Univariate Polynomial Ring in x over Fraction Field of Multivariate Polynomial Ring in a, b over Rational Field
Defn: Induced from base ring by
Ring endomorphism of Univariate Polynomial Ring in x over Fraction Field of Multivariate Polynomial Ring in a, b over Rational Field
Defn: Induced from base ring by
Ring endomorphism of Fraction Field of Multivariate Polynomial Ring in a, b over Rational Field
Defn: a |--> a + b
b |--> a - b
"""
for c in self.all:
f = c(f)
return f
def _apply_functor(self, R):
"""
Apply the functor to an object of ``self``'s domain.
TESTS::
sage: from sage.categories.pushout import CompositeConstructionFunctor
sage: F = CompositeConstructionFunctor(QQ.construction()[0],ZZ['x'].construction()[0],QQ.construction()[0],ZZ['y'].construction()[0])
sage: R.<a,b> = QQ[]
sage: F(R) # indirect doctest
Univariate Polynomial Ring in y over Fraction Field of Univariate Polynomial Ring in x over Fraction Field of Multivariate Polynomial Ring in a, b over Rational Field
"""
for c in self.all:
R = c(R)
return R
def __eq__(self, other):
"""
TESTS::
sage: from sage.categories.pushout import CompositeConstructionFunctor
sage: F = CompositeConstructionFunctor(QQ.construction()[0],ZZ['x'].construction()[0],QQ.construction()[0],ZZ['y'].construction()[0])
sage: F == loads(dumps(F)) # indirect doctest
True
"""
if isinstance(other, CompositeConstructionFunctor):
return self.all == other.all
else:
return type(self) == type(other)
def __ne__(self, other):
"""
Check whether ``self`` is not equal to ``other``.
EXAMPLES::
sage: from sage.categories.pushout import CompositeConstructionFunctor
sage: F = CompositeConstructionFunctor(QQ.construction()[0],ZZ['x'].construction()[0],QQ.construction()[0],ZZ['y'].construction()[0])
sage: F != loads(dumps(F)) # indirect doctest
False
"""
return not (self == other)
def __mul__(self, other):
"""
Compose construction functors to a composit construction functor, unless one of them is the identity.
NOTE:
The product is in functorial notation, i.e., when applying the product to an object
then the second factor is applied first.
EXAMPLES::
sage: from sage.categories.pushout import CompositeConstructionFunctor
sage: F1 = CompositeConstructionFunctor(QQ.construction()[0],ZZ['x'].construction()[0])
sage: F2 = CompositeConstructionFunctor(QQ.construction()[0],ZZ['y'].construction()[0])
sage: F1*F2
Poly[x](FractionField(Poly[y](FractionField(...))))
"""
if isinstance(self, CompositeConstructionFunctor):
all = [other] + self.all
elif isinstance(other,IdentityConstructionFunctor):
return self
else:
all = other.all + [self]
return CompositeConstructionFunctor(*all)
def _repr_(self):
"""
TESTS::
sage: from sage.categories.pushout import CompositeConstructionFunctor
sage: F = CompositeConstructionFunctor(QQ.construction()[0],ZZ['x'].construction()[0],QQ.construction()[0],ZZ['y'].construction()[0])
sage: F # indirect doctest
Poly[y](FractionField(Poly[x](FractionField(...))))
"""
s = "..."
for c in self.all:
s = "%s(%s)" % (c,s)
return s
def expand(self):
"""
Return expansion of a CompositeConstructionFunctor.
NOTE:
The product over the list of components, as returned by
the ``expand()`` method, is equal to ``self``.
EXAMPLES::
sage: from sage.categories.pushout import CompositeConstructionFunctor
sage: F = CompositeConstructionFunctor(QQ.construction()[0],ZZ['x'].construction()[0],QQ.construction()[0],ZZ['y'].construction()[0])
sage: F
Poly[y](FractionField(Poly[x](FractionField(...))))
sage: prod(F.expand()) == F
True
"""
return list(reversed(self.all))
class IdentityConstructionFunctor(ConstructionFunctor):
"""
A construction functor that is the identity functor.
TESTS::
sage: from sage.categories.pushout import IdentityConstructionFunctor
sage: I = IdentityConstructionFunctor()
sage: I(RR) is RR
True
sage: I == loads(dumps(I))
True
"""
rank = -100
def __init__(self):
"""
TESTS::
sage: from sage.categories.pushout import IdentityConstructionFunctor
sage: I = IdentityConstructionFunctor()
sage: IdentityFunctor(Sets()) == I
True
sage: I(RR) is RR
True
"""
from sage.categories.sets_cat import Sets
ConstructionFunctor.__init__(self, Sets(), Sets())
def _apply_functor(self, x):
"""
Return the argument unaltered.
TESTS::
sage: from sage.categories.pushout import IdentityConstructionFunctor
sage: I = IdentityConstructionFunctor()
sage: I(RR) is RR # indirect doctest
True
"""
return x
def _apply_functor_to_morphism(self, f):
"""
Return the argument unaltered.
TESTS::
sage: from sage.categories.pushout import IdentityConstructionFunctor
sage: I = IdentityConstructionFunctor()
sage: f = ZZ['t'].hom(['x'],QQ['x'])
sage: I(f) is f # indirect doctest
True
"""
return f
def __eq__(self, other):
"""
TESTS::
sage: from sage.categories.pushout import IdentityConstructionFunctor
sage: I = IdentityConstructionFunctor()
sage: I == IdentityFunctor(Sets()) # indirect doctest
True
sage: I == QQ.construction()[0]
False
"""
c = (type(self) == type(other))
if not c:
from sage.categories.functor import IdentityFunctor_generic
if isinstance(other, IdentityFunctor_generic):
return True
return c
def __ne__(self, other):
"""
Check whether ``self`` is not equal to ``other``.
EXAMPLES::
sage: from sage.categories.pushout import IdentityConstructionFunctor
sage: I = IdentityConstructionFunctor()
sage: I != IdentityFunctor(Sets()) # indirect doctest
False
sage: I != QQ.construction()[0]
True
"""
return not (self == other)
def __mul__(self, other):
"""
Compose construction functors to a composit construction functor, unless one of them is the identity.
NOTE:
The product is in functorial notation, i.e., when applying the product to an object
then the second factor is applied first.
TESTS::
sage: from sage.categories.pushout import IdentityConstructionFunctor
sage: I = IdentityConstructionFunctor()
sage: F = QQ.construction()[0]
sage: P = ZZ['t'].construction()[0]
sage: I*F is F # indirect doctest
True
sage: F*I is F
True
sage: I*P is P
True
sage: P*I is P
True
"""
if isinstance(self, IdentityConstructionFunctor):
return other
else:
return self
class MultivariateConstructionFunctor(ConstructionFunctor):
"""
An abstract base class for functors that take
multiple inputs (e.g. Cartesian products).
TESTS::
sage: from sage.categories.pushout import pushout
sage: A = cartesian_product((QQ['z'], QQ))
sage: B = cartesian_product((ZZ['t']['z'], QQ))
sage: pushout(A, B)
The Cartesian product of (Univariate Polynomial Ring in z over
Univariate Polynomial Ring in t over Rational Field,
Rational Field)
sage: A.construction()
(The cartesian_product functorial construction,
(Univariate Polynomial Ring in z over Rational Field, Rational Field))
sage: pushout(A, B)
The Cartesian product of (Univariate Polynomial Ring in z over Univariate Polynomial Ring in t over Rational Field, Rational Field)
"""
def common_base(self, other_functor, self_bases, other_bases):
r"""
This function is called by :func:`pushout` when no common parent
is found in the construction tower.
INPUT:
- ``other_functor`` -- a construction functor.
- ``self_bases`` -- the arguments passed to this functor.
- ``other_bases`` -- the arguments passed to the functor
``other_functor``.
OUTPUT:
A parent.
If no common base is found a :class:`sage.structure.coerce_exceptions.CoercionException`
is raised.
.. NOTE::
Overload this function in derived class, see
e.g. :class:`MultivariateConstructionFunctor`.
TESTS::
sage: from sage.categories.pushout import pushout
sage: pushout(cartesian_product([ZZ]), QQ) # indirect doctest
Traceback (most recent call last):
...
CoercionException: No common base ("join") found for
The cartesian_product functorial construction(Integer Ring) and FractionField(Integer Ring):
(Multivariate) functors are incompatible.
sage: pushout(cartesian_product([ZZ]), cartesian_product([ZZ, QQ])) # indirect doctest
Traceback (most recent call last):
...
CoercionException: No common base ("join") found for
The cartesian_product functorial construction(Integer Ring) and
The cartesian_product functorial construction(Integer Ring, Rational Field):
Functors need the same number of arguments.
"""
if self != other_functor:
self._raise_common_base_exception_(
other_functor, self_bases, other_bases,
'(Multivariate) functors are incompatible')
if len(self_bases) != len(other_bases):
self._raise_common_base_exception_(
other_functor, self_bases, other_bases,
'Functors need the same number of arguments')
from sage.structure.element import coercion_model
Z_bases = tuple(coercion_model.common_parent(S, O)
for S, O in zip(self_bases, other_bases))
return self(Z_bases)
class PolynomialFunctor(ConstructionFunctor):
"""
Construction functor for univariate polynomial rings.
EXAMPLES::
sage: P = ZZ['t'].construction()[0]
sage: P(GF(3))
Univariate Polynomial Ring in t over Finite Field of size 3
sage: P == loads(dumps(P))
True
sage: R.<x,y> = GF(5)[]
sage: f = R.hom([x+2*y,3*x-y],R)
sage: P(f)((x+y)*P(R).0)
(-x + y)*t
By :trac:`9944`, the construction functor distinguishes sparse and
dense polynomial rings. Before, the following example failed::
sage: R.<x> = PolynomialRing(GF(5), sparse=True)
sage: F,B = R.construction()
sage: F(B) is R
True
sage: S.<x> = PolynomialRing(ZZ)
sage: R.has_coerce_map_from(S)
False
sage: S.has_coerce_map_from(R)
False
sage: S.0 + R.0
2*x
sage: (S.0 + R.0).parent()
Univariate Polynomial Ring in x over Finite Field of size 5
sage: (S.0 + R.0).parent().is_sparse()
False
"""
rank = 9
def __init__(self, var, multi_variate=False, sparse=False):
"""
TESTS::
sage: from sage.categories.pushout import PolynomialFunctor
sage: P = PolynomialFunctor('x')
sage: P(GF(3))
Univariate Polynomial Ring in x over Finite Field of size 3
There is an optional parameter ``multi_variate``, but
apparently it is not used::
sage: Q = PolynomialFunctor('x',multi_variate=True)
sage: Q(ZZ)
Univariate Polynomial Ring in x over Integer Ring
sage: Q == P
True
"""
from .rings import Rings
Functor.__init__(self, Rings(), Rings())
self.var = var
self.multi_variate = multi_variate
self.sparse = sparse
def _apply_functor(self, R):
"""
Apply the functor to an object of ``self``'s domain.
TESTS::
sage: P = ZZ['x'].construction()[0]
sage: P(GF(3)) # indirect doctest
Univariate Polynomial Ring in x over Finite Field of size 3
"""
from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
return PolynomialRing(R, self.var, sparse=self.sparse)
def _apply_functor_to_morphism(self, f):
"""
Apply the functor ``self`` to the morphism `f`.
TESTS::
sage: P = ZZ['x'].construction()[0]
sage: P(ZZ.hom(GF(3))) # indirect doctest
Ring morphism:
From: Univariate Polynomial Ring in x over Integer Ring
To: Univariate Polynomial Ring in x over Finite Field of size 3
Defn: Induced from base ring by
Ring Coercion morphism:
From: Integer Ring
To: Finite Field of size 3
"""
from sage.rings.polynomial.polynomial_ring_homomorphism import PolynomialRingHomomorphism_from_base
R = self._apply_functor(f.domain())
S = self._apply_functor(f.codomain())
return PolynomialRingHomomorphism_from_base(R.Hom(S), f)
def __eq__(self, other):
"""
TESTS::
sage: from sage.categories.pushout import MultiPolynomialFunctor
sage: Q = MultiPolynomialFunctor(('x',),'lex')
sage: P = ZZ['x'].construction()[0]
sage: P
Poly[x]
sage: Q
MPoly[x]
sage: P == Q
True
sage: P == loads(dumps(P))
True
sage: P == QQ.construction()[0]
False
"""
if isinstance(other, PolynomialFunctor):
return self.var == other.var
elif isinstance(other, MultiPolynomialFunctor):
return (other == self)
else:
return False
def __ne__(self, other):
"""
Check whether ``self`` is not equal to ``other``.
EXAMPLES::
sage: from sage.categories.pushout import MultiPolynomialFunctor
sage: Q = MultiPolynomialFunctor(('x',),'lex')
sage: P = ZZ['x'].construction()[0]
sage: P != Q
False
sage: P != loads(dumps(P))
False
sage: P != QQ.construction()[0]
True
"""
return not (self == other)
def merge(self, other):
"""
Merge ``self`` with another construction functor, or return None.
NOTE:
Internally, the merging is delegated to the merging of
multipolynomial construction functors. But in effect,
this does the same as the default implementation, that
returns ``None`` unless the to-be-merged functors coincide.
EXAMPLES::
sage: P = ZZ['x'].construction()[0]
sage: Q = ZZ['y','x'].construction()[0]
sage: P.merge(Q)
sage: P.merge(P) is P
True
"""
if isinstance(other, MultiPolynomialFunctor):
return other.merge(self)
elif self == other:
# i.e., they only differ in sparsity
if not self.sparse:
return self
return other
else:
return None
def _repr_(self):
"""
TESTS::
sage: P = ZZ['x'].construction()[0]
sage: P # indirect doctest
Poly[x]
"""
return "Poly[%s]" % self.var
class MultiPolynomialFunctor(ConstructionFunctor):
"""
A constructor for multivariate polynomial rings.
EXAMPLES::
sage: P.<x,y> = ZZ[]
sage: F = P.construction()[0]; F
MPoly[x,y]
sage: A.<a,b> = GF(5)[]
sage: F(A)
Multivariate Polynomial Ring in x, y over Multivariate Polynomial Ring in a, b over Finite Field of size 5
sage: f = A.hom([a+b,a-b],A)
sage: F(f)
Ring endomorphism of Multivariate Polynomial Ring in x, y over Multivariate Polynomial Ring in a, b over Finite Field of size 5
Defn: Induced from base ring by
Ring endomorphism of Multivariate Polynomial Ring in a, b over Finite Field of size 5
Defn: a |--> a + b
b |--> a - b
sage: F(f)(F(A)(x)*a)
(a + b)*x
"""
rank = 9
def __init__(self, vars, term_order):
"""
EXAMPLES::
sage: F = sage.categories.pushout.MultiPolynomialFunctor(['x','y'], None)
sage: F
MPoly[x,y]
sage: F(ZZ)
Multivariate Polynomial Ring in x, y over Integer Ring
sage: F(CC)
Multivariate Polynomial Ring in x, y over Complex Field with 53 bits of precision
"""
Functor.__init__(self, Rings(), Rings())
self.vars = vars
self.term_order = term_order
def _apply_functor(self, R):
"""
Apply the functor to an object of ``self``'s domain.
EXAMPLES::
sage: R.<x,y,z> = QQ[]
sage: F = R.construction()[0]; F
MPoly[x,y,z]
sage: type(F)
<class 'sage.categories.pushout.MultiPolynomialFunctor'>
sage: F(ZZ) # indirect doctest
Multivariate Polynomial Ring in x, y, z over Integer Ring
sage: F(RR) # indirect doctest
Multivariate Polynomial Ring in x, y, z over Real Field with 53 bits of precision