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
/
morphism.pyx
2120 lines (1744 loc) · 65.5 KB
/
morphism.pyx
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
r"""
Homomorphisms of rings
We give a large number of examples of ring homomorphisms.
EXAMPLES:
Natural inclusion `\ZZ \hookrightarrow \QQ`::
sage: H = Hom(ZZ, QQ)
sage: phi = H([1])
sage: phi(10)
10
sage: phi(3/1)
3
sage: phi(2/3)
Traceback (most recent call last):
...
TypeError: 2/3 fails to convert into the map's domain Integer Ring, but a `pushforward` method is not properly implemented
There is no homomorphism in the other direction::
sage: H = Hom(QQ, ZZ)
sage: H([1])
Traceback (most recent call last):
...
TypeError: images do not define a valid homomorphism
EXAMPLES:
Reduction to finite field::
sage: H = Hom(ZZ, GF(9, 'a'))
sage: phi = H([1])
sage: phi(5)
2
sage: psi = H([4])
sage: psi(5)
2
Map from single variable polynomial ring::
sage: R.<x> = ZZ[]
sage: phi = R.hom([2], GF(5))
sage: phi
Ring morphism:
From: Univariate Polynomial Ring in x over Integer Ring
To: Finite Field of size 5
Defn: x |--> 2
sage: phi(x + 12)
4
Identity map on the real numbers::
sage: f = RR.hom([RR(1)]); f
Ring endomorphism of Real Field with 53 bits of precision
Defn: 1.00000000000000 |--> 1.00000000000000
sage: f(2.5)
2.50000000000000
sage: f = RR.hom( [2.0] )
Traceback (most recent call last):
...
TypeError: images do not define a valid homomorphism
Homomorphism from one precision of field to another.
From smaller to bigger doesn't make sense::
sage: R200 = RealField(200)
sage: f = RR.hom( R200 )
Traceback (most recent call last):
...
TypeError: Natural coercion morphism from Real Field with 53 bits of precision to Real Field with 200 bits of precision not defined.
From bigger to small does::
sage: f = RR.hom( RealField(15) )
sage: f(2.5)
2.500
sage: f(RR.pi())
3.142
Inclusion map from the reals to the complexes::
sage: i = RR.hom([CC(1)]); i
Ring morphism:
From: Real Field with 53 bits of precision
To: Complex Field with 53 bits of precision
Defn: 1.00000000000000 |--> 1.00000000000000
sage: i(RR('3.1'))
3.10000000000000
A map from a multivariate polynomial ring to itself::
sage: R.<x,y,z> = PolynomialRing(QQ,3)
sage: phi = R.hom([y,z,x^2]); phi
Ring endomorphism of Multivariate Polynomial Ring in x, y, z over Rational Field
Defn: x |--> y
y |--> z
z |--> x^2
sage: phi(x+y+z)
x^2 + y + z
An endomorphism of a quotient of a multi-variate polynomial ring::
sage: R.<x,y> = PolynomialRing(QQ)
sage: S.<a,b> = quo(R, ideal(1 + y^2))
sage: phi = S.hom([a^2, -b])
sage: phi
Ring endomorphism of Quotient of Multivariate Polynomial Ring in x, y over Rational Field by the ideal (y^2 + 1)
Defn: a |--> a^2
b |--> -b
sage: phi(b)
-b
sage: phi(a^2 + b^2)
a^4 - 1
The reduction map from the integers to the integers modulo 8, viewed as
a quotient ring::
sage: R = ZZ.quo(8*ZZ)
sage: pi = R.cover()
sage: pi
Ring morphism:
From: Integer Ring
To: Ring of integers modulo 8
Defn: Natural quotient map
sage: pi.domain()
Integer Ring
sage: pi.codomain()
Ring of integers modulo 8
sage: pi(10)
2
sage: pi.lift()
Set-theoretic ring morphism:
From: Ring of integers modulo 8
To: Integer Ring
Defn: Choice of lifting map
sage: pi.lift(13)
5
Inclusion of ``GF(2)`` into ``GF(4,'a')``::
sage: k = GF(2)
sage: i = k.hom(GF(4, 'a'))
sage: i
Ring Coercion morphism:
From: Finite Field of size 2
To: Finite Field in a of size 2^2
sage: i(0)
0
sage: a = i(1); a.parent()
Finite Field in a of size 2^2
We next compose the inclusion with reduction from the integers to
``GF(2)``::
sage: pi = ZZ.hom(k)
sage: pi
Ring Coercion morphism:
From: Integer Ring
To: Finite Field of size 2
sage: f = i * pi
sage: f
Composite map:
From: Integer Ring
To: Finite Field in a of size 2^2
Defn: Ring Coercion morphism:
From: Integer Ring
To: Finite Field of size 2
then
Ring Coercion morphism:
From: Finite Field of size 2
To: Finite Field in a of size 2^2
sage: a = f(5); a
1
sage: a.parent()
Finite Field in a of size 2^2
Inclusion from `\QQ` to the 3-adic field::
sage: phi = QQ.hom(Qp(3, print_mode = 'series'))
sage: phi
Ring Coercion morphism:
From: Rational Field
To: 3-adic Field with capped relative precision 20
sage: phi.codomain()
3-adic Field with capped relative precision 20
sage: phi(394)
1 + 2*3 + 3^2 + 2*3^3 + 3^4 + 3^5 + O(3^20)
An automorphism of a quotient of a univariate polynomial ring::
sage: R.<x> = PolynomialRing(QQ)
sage: S.<sqrt2> = R.quo(x^2-2)
sage: sqrt2^2
2
sage: (3+sqrt2)^10
993054*sqrt2 + 1404491
sage: c = S.hom([-sqrt2])
sage: c(1+sqrt2)
-sqrt2 + 1
Note that Sage verifies that the morphism is valid::
sage: (1 - sqrt2)^2
-2*sqrt2 + 3
sage: c = S.hom([1-sqrt2]) # this is not valid
Traceback (most recent call last):
...
TypeError: images do not define a valid homomorphism
Endomorphism of power series ring::
sage: R.<t> = PowerSeriesRing(QQ); R
Power Series Ring in t over Rational Field
sage: f = R.hom([t^2]); f
Ring endomorphism of Power Series Ring in t over Rational Field
Defn: t |--> t^2
sage: R.set_default_prec(10)
sage: s = 1/(1 + t); s
1 - t + t^2 - t^3 + t^4 - t^5 + t^6 - t^7 + t^8 - t^9 + O(t^10)
sage: f(s)
1 - t^2 + t^4 - t^6 + t^8 - t^10 + t^12 - t^14 + t^16 - t^18 + O(t^20)
Frobenius on a power series ring over a finite field::
sage: R.<t> = PowerSeriesRing(GF(5))
sage: f = R.hom([t^5]); f
Ring endomorphism of Power Series Ring in t over Finite Field of size 5
Defn: t |--> t^5
sage: a = 2 + t + 3*t^2 + 4*t^3 + O(t^4)
sage: b = 1 + t + 2*t^2 + t^3 + O(t^5)
sage: f(a)
2 + t^5 + 3*t^10 + 4*t^15 + O(t^20)
sage: f(b)
1 + t^5 + 2*t^10 + t^15 + O(t^25)
sage: f(a*b)
2 + 3*t^5 + 3*t^10 + t^15 + O(t^20)
sage: f(a)*f(b)
2 + 3*t^5 + 3*t^10 + t^15 + O(t^20)
Homomorphism of Laurent series ring::
sage: R.<t> = LaurentSeriesRing(QQ, 10)
sage: f = R.hom([t^3 + t]); f
Ring endomorphism of Laurent Series Ring in t over Rational Field
Defn: t |--> t + t^3
sage: s = 2/t^2 + 1/(1 + t); s
2*t^-2 + 1 - t + t^2 - t^3 + t^4 - t^5 + t^6 - t^7 + t^8 - t^9 + O(t^10)
sage: f(s)
2*t^-2 - 3 - t + 7*t^2 - 2*t^3 - 5*t^4 - 4*t^5 + 16*t^6 - 9*t^7 + O(t^8)
sage: f = R.hom([t^3]); f
Ring endomorphism of Laurent Series Ring in t over Rational Field
Defn: t |--> t^3
sage: f(s)
2*t^-6 + 1 - t^3 + t^6 - t^9 + t^12 - t^15 + t^18 - t^21 + t^24 - t^27 + O(t^30)
Note that the homomorphism must result in a converging Laurent
series, so the valuation of the image of the generator must be
positive::
sage: R.hom([1/t])
Traceback (most recent call last):
...
TypeError: images do not define a valid homomorphism
sage: R.hom([1])
Traceback (most recent call last):
...
TypeError: images do not define a valid homomorphism
Complex conjugation on cyclotomic fields::
sage: K.<zeta7> = CyclotomicField(7)
sage: c = K.hom([1/zeta7]); c
Ring endomorphism of Cyclotomic Field of order 7 and degree 6
Defn: zeta7 |--> -zeta7^5 - zeta7^4 - zeta7^3 - zeta7^2 - zeta7 - 1
sage: a = (1+zeta7)^5; a
zeta7^5 + 5*zeta7^4 + 10*zeta7^3 + 10*zeta7^2 + 5*zeta7 + 1
sage: c(a)
5*zeta7^5 + 5*zeta7^4 - 4*zeta7^2 - 5*zeta7 - 4
sage: c(zeta7 + 1/zeta7) # this element is obviously fixed by inversion
-zeta7^5 - zeta7^4 - zeta7^3 - zeta7^2 - 1
sage: zeta7 + 1/zeta7
-zeta7^5 - zeta7^4 - zeta7^3 - zeta7^2 - 1
Embedding a number field into the reals::
sage: R.<x> = PolynomialRing(QQ)
sage: K.<beta> = NumberField(x^3 - 2)
sage: alpha = RR(2)^(1/3); alpha
1.25992104989487
sage: i = K.hom([alpha],check=False); i
Ring morphism:
From: Number Field in beta with defining polynomial x^3 - 2
To: Real Field with 53 bits of precision
Defn: beta |--> 1.25992104989487
sage: i(beta)
1.25992104989487
sage: i(beta^3)
2.00000000000000
sage: i(beta^2 + 1)
2.58740105196820
An example from Jim Carlson::
sage: K = QQ # by the way :-)
sage: R.<a,b,c,d> = K[]; R
Multivariate Polynomial Ring in a, b, c, d over Rational Field
sage: S.<u> = K[]; S
Univariate Polynomial Ring in u over Rational Field
sage: f = R.hom([0,0,0,u], S); f
Ring morphism:
From: Multivariate Polynomial Ring in a, b, c, d over Rational Field
To: Univariate Polynomial Ring in u over Rational Field
Defn: a |--> 0
b |--> 0
c |--> 0
d |--> u
sage: f(a+b+c+d)
u
sage: f( (a+b+c+d)^2 )
u^2
TESTS::
sage: H = Hom(ZZ, QQ)
sage: H == loads(dumps(H))
True
::
sage: K.<zeta7> = CyclotomicField(7)
sage: c = K.hom([1/zeta7])
sage: c == loads(dumps(c))
True
::
sage: R.<t> = PowerSeriesRing(GF(5))
sage: f = R.hom([t^5])
sage: f == loads(dumps(f))
True
"""
#*****************************************************************************
# Copyright (C) 2006 William Stein <wstein@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
# http://www.gnu.org/licenses/
#*****************************************************************************
from __future__ import print_function
import ideal
import homset
from cpython.object cimport Py_EQ, Py_NE
from sage.structure.richcmp cimport (richcmp, rich_to_bool,
richcmp_not_equal)
def is_RingHomomorphism(phi):
"""
Return ``True`` if ``phi`` is of type :class:`RingHomomorphism`.
EXAMPLES::
sage: f = Zmod(8).cover()
sage: sage.rings.morphism.is_RingHomomorphism(f)
True
sage: sage.rings.morphism.is_RingHomomorphism(2/3)
False
"""
return isinstance(phi, RingHomomorphism)
cdef class RingMap(Morphism):
"""
Set-theoretic map between rings.
"""
def __init__(self, parent):
"""
This is an abstract base class that isn't directly
instantiated, but we will do so anyways as a test.
TESTS::
sage: f = sage.rings.morphism.RingMap(ZZ.Hom(ZZ))
sage: type(f)
<type 'sage.rings.morphism.RingMap'>
"""
Morphism.__init__(self, parent)
def _repr_type(self):
"""
TESTS::
sage: f = sage.rings.morphism.RingMap(ZZ.Hom(ZZ))
sage: type(f)
<type 'sage.rings.morphism.RingMap'>
sage: f._repr_type()
'Set-theoretic ring'
sage: f
Set-theoretic ring endomorphism of Integer Ring
"""
return "Set-theoretic ring"
def __hash__(self):
return Morphism.__hash__(self)
cdef class RingMap_lift(RingMap):
r"""
Given rings `R` and `S` such that for any
`x \in R` the function ``x.lift()`` is an
element that naturally coerces to `S`, this returns the
set-theoretic ring map `R \to S` sending `x` to
``x.lift()``.
EXAMPLES::
sage: R.<x,y> = QQ[]
sage: S.<xbar,ybar> = R.quo( (x^2 + y^2, y) )
sage: S.lift()
Set-theoretic ring morphism:
From: Quotient of Multivariate Polynomial Ring in x, y over Rational Field by the ideal (x^2 + y^2, y)
To: Multivariate Polynomial Ring in x, y over Rational Field
Defn: Choice of lifting map
sage: S.lift() == 0
False
Since :trac:`11068`, it is possible to create
quotient rings of non-commutative rings by two-sided
ideals. It was needed to modify :class:`RingMap_lift`
so that rings can be accepted that are no instances
of :class:`sage.rings.ring.Ring`, as in the following
example::
sage: MS = MatrixSpace(GF(5),2,2)
sage: I = MS*[MS.0*MS.1,MS.2+MS.3]*MS
sage: Q = MS.quo(I)
sage: Q.0*Q.1 # indirect doctest
[0 1]
[0 0]
"""
def __init__(self, R, S):
"""
Create a lifting ring map.
EXAMPLES::
sage: f = Zmod(8).lift() # indirect doctest
sage: f(3)
3
sage: type(f(3))
<type 'sage.rings.integer.Integer'>
sage: type(f)
<type 'sage.rings.morphism.RingMap_lift'>
"""
from sage.categories.sets_cat import Sets
H = R.Hom(S, Sets())
RingMap.__init__(self, H)
self.S = S # for efficiency
try:
S._coerce_(R(0).lift())
except TypeError:
raise TypeError("No natural lift map")
cdef _update_slots(self, dict _slots):
"""
Helper for copying and pickling.
EXAMPLES::
sage: f = Zmod(8).lift()
sage: g = copy(f) # indirect doctest
sage: g(3) == f(3)
True
sage: f == g
True
sage: f is g
False
"""
self.S = _slots['S']
Morphism._update_slots(self, _slots)
cdef dict _extra_slots(self, dict _slots):
"""
Helper for copying and pickling.
EXAMPLES::
sage: f = Zmod(8).lift()
sage: g = copy(f) # indirect doctest
sage: g(3) == f(3)
True
"""
_slots['S'] = self.S
return Morphism._extra_slots(self, _slots)
def _richcmp_(self, other, int op):
"""
Compare a ring lifting maps ``self`` to ``other``.
Ring lifting maps never compare equal to any other data type.
If ``other`` is a ring lifting maps, the parents of ``self`` and
``other`` are compared.
EXAMPLES::
sage: f = Zmod(8).lift()
sage: g = Zmod(10).lift()
sage: f == f
True
sage: f == g
False
Verify that :trac:`5758` has been fixed::
sage: Zmod(8).lift() == 1
False
"""
if op not in [Py_EQ, Py_NE]:
return NotImplemented
if not isinstance(other, RingMap_lift):
return (op == Py_NE)
# Since they are lifting maps they are determined by their
# parents, i.e., by the domain and codomain, since we just
# compare those.
return richcmp(self.parent(), other.parent(), op)
def __hash__(self):
"""
Return the hash of this morphism.
TESTS::
sage: f = Zmod(8).lift()
sage: type(f)
<type 'sage.rings.morphism.RingMap_lift'>
sage: hash(f) == hash(f)
True
sage: {f: 1}[f]
1
sage: g = Zmod(10).lift()
sage: hash(f) == hash(g)
False
"""
return hash((self.domain(), self.codomain()))
def _repr_defn(self):
"""
Used in printing out lifting maps.
EXAMPLES::
sage: f = Zmod(8).lift()
sage: f._repr_defn()
'Choice of lifting map'
sage: f
Set-theoretic ring morphism:
From: Ring of integers modulo 8
To: Integer Ring
Defn: Choice of lifting map
"""
return "Choice of lifting map"
cpdef Element _call_(self, x):
"""
Evaluate this function at ``x``.
EXAMPLES::
sage: f = Zmod(8).lift()
sage: type(f)
<type 'sage.rings.morphism.RingMap_lift'>
sage: f(-1) # indirect doctest
7
sage: type(f(-1))
<type 'sage.rings.integer.Integer'>
"""
return self.S._coerce_c(x.lift())
cdef class RingHomomorphism(RingMap):
"""
Homomorphism of rings.
"""
def __init__(self, parent):
"""
Initialize ``self``.
EXAMPLES::
sage: f = ZZ.hom(Zmod(6)); f
Ring Coercion morphism:
From: Integer Ring
To: Ring of integers modulo 6
sage: isinstance(f, sage.rings.morphism.RingHomomorphism)
True
"""
if not homset.is_RingHomset(parent):
raise TypeError("parent must be a ring homset")
RingMap.__init__(self, parent)
def __nonzero__(self):
"""
Every ring map is nonzero unless the domain or codomain is the
0 ring, since there is no zero map between rings, since 1 goes
to 1.
EXAMPLES:
Usually ring morphisms are nonzero::
sage: bool(ZZ.hom(QQ,[1]))
True
However, they aren't if ``1 == 0`` in the codomain::
sage: R1 = Zmod(1)
sage: phi = R1.hom(R1, [1])
sage: bool(phi)
False
sage: bool(ZZ.hom(R1, [1]))
False
"""
return bool(self.codomain().one())
def _repr_type(self):
"""
Used internally in printing this morphism.
TESTS:
This never actually gets called, since derived classes
override it. Nevertheless, we call it directly to illustrate
that it works as a default.::
sage: phi = ZZ.hom(QQ,[1])
sage: phi._repr_type()
'Ring Coercion'
sage: sage.rings.morphism.RingHomomorphism._repr_type(phi)
'Ring'
"""
return "Ring"
def _set_lift(self, lift):
r"""
Used internally to define a lifting homomorphism associated to
this homomorphism, which goes in the other direction. I.e.,
if ``self`` is from `R` to `S`, then the lift must be a set-theoretic
map from `S` to `R` such that ``self(lift(x)) == x``.
INPUT:
- ``lift`` -- a ring map
OUTPUT:
Changes the state of ``self``.
EXAMPLES::
sage: f = ZZ.hom(Zmod(7))
sage: f._set_lift(Zmod(7).lift())
sage: f.lift()
Set-theoretic ring morphism:
From: Ring of integers modulo 7
To: Integer Ring
Defn: Choice of lifting map
"""
if not isinstance(lift, RingMap):
raise TypeError("lift must be a RingMap")
if lift.domain() != self.codomain():
raise TypeError("lift must have correct domain")
if lift.codomain() != self.domain():
raise TypeError("lift must have correct codomain")
self._lift = lift
cdef _update_slots(self, dict _slots):
"""
Helper for copying and pickling.
EXAMPLES::
sage: f = ZZ.hom(Zmod(6))
sage: g = copy(f) # indirect doctest
sage: g == f
True
sage: g is f
False
sage: g(7)
1
"""
if '_lift' in _slots:
self._lift = _slots['_lift']
Morphism._update_slots(self, _slots)
cdef dict _extra_slots(self, dict _slots):
"""
Helper for copying and pickling.
EXAMPLES::
sage: f = ZZ.hom(Zmod(6))
sage: g = copy(f) # indirect doctest
sage: g == f
True
sage: g is f
False
sage: g(7)
1
"""
try:
_slots['_lift'] = self._lift
except AttributeError:
pass
return Morphism._extra_slots(self, _slots)
def _composition_(self, right, homset):
"""
If ``homset`` is a homset of rings and ``right`` is a
ring homomorphism given by the images of generators,
(indirectly in the case of homomorphisms from relative
number fields), the composition with ``self`` will be
of the appropriate type.
Otherwise, a formal composite map is returned.
EXAMPLES::
sage: R.<x,y> = QQ[]
sage: S.<a,b> = QQ[]
sage: f = R.hom([a+b,a-b])
sage: g = S.hom(Frac(S))
sage: g*f # indirect doctest
Ring morphism:
From: Multivariate Polynomial Ring in x, y over Rational Field
To: Fraction Field of Multivariate Polynomial Ring in a, b over Rational Field
Defn: x |--> a + b
y |--> a - b
When ``right`` is defined by the images of generators, the
result has the type of a homomorphism between its domain and
codomain::
sage: C = CyclotomicField(24)
sage: f = End(C)[1]
sage: type(f*f) == type(f)
True
An example where the domain of ``right`` is a relative number field::
sage: PQ.<X> = QQ[]
sage: K.<a, b> = NumberField([X^2 - 2, X^2 - 3])
sage: e, u, v, w = End(K)
sage: u*v
Relative number field endomorphism of Number Field in a with defining polynomial X^2 - 2 over its base field
Defn: a |--> -a
b |--> b
An example where ``right`` is not a ring homomorphism::
sage: from sage.categories.morphism import SetMorphism
sage: h = SetMorphism(Hom(R,S,Rings()), lambda p: p[0])
sage: g*h
Composite map:
From: Multivariate Polynomial Ring in x, y over Rational Field
To: Fraction Field of Multivariate Polynomial Ring in a, b over Rational Field
Defn: Generic morphism:
From: Multivariate Polynomial Ring in x, y over Rational Field
To: Multivariate Polynomial Ring in a, b over Rational Field
then
Ring Coercion morphism:
From: Multivariate Polynomial Ring in a, b over Rational Field
To: Fraction Field of Multivariate Polynomial Ring in a, b over Rational Field
AUTHORS:
- Simon King (2010-05)
- Francis Clarke (2011-02)
"""
from sage.all import Rings
if homset.homset_category().is_subcategory(Rings()):
if isinstance(right, RingHomomorphism_im_gens):
try:
return homset([self(g) for g in right.im_gens()], False)
except ValueError:
pass
from sage.rings.number_field.morphism import RelativeNumberFieldHomomorphism_from_abs
if isinstance(right, RelativeNumberFieldHomomorphism_from_abs):
try:
return homset(self*right.abs_hom())
except ValueError:
pass
return sage.categories.map.Map._composition_(self, right, homset)
def is_zero(self):
r"""
Return ``True`` if this is the zero map and ``False`` otherwise.
A *ring* homomorphism is considered to be 0 if and only if it
sends the 1 element of the domain to the 0 element of the codomain.
Since rings in Sage all have a 1 element, the zero homomorphism is
only to a ring of order 1, where ``1 == 0``, e.g., the ring
``Integers(1)``.
EXAMPLES:
First an example of a map that is obviously nonzero::
sage: h = Hom(ZZ, QQ)
sage: f = h.natural_map()
sage: f.is_zero()
False
Next we make the zero ring as `\ZZ/1\ZZ`::
sage: R = Integers(1)
sage: R
Ring of integers modulo 1
sage: h = Hom(ZZ, R)
sage: f = h.natural_map()
sage: f.is_zero()
True
Finally we check an example in characteristic 2::
sage: h = Hom(ZZ, GF(2))
sage: f = h.natural_map()
sage: f.is_zero()
False
"""
return self(self.domain()(1)) == self.codomain()(0)
def pushforward(self, I):
"""
Returns the pushforward of the ideal `I` under this ring
homomorphism.
EXAMPLES::
sage: R.<x,y> = QQ[]; S.<xx,yy> = R.quo([x^2,y^2]); f = S.cover()
sage: f.pushforward(R.ideal([x,3*x+x*y+y^2]))
Ideal (xx, xx*yy + 3*xx) of Quotient of Multivariate Polynomial Ring in x, y over Rational Field by the ideal (x^2, y^2)
"""
if not ideal.is_Ideal(I):
raise TypeError("I must be an ideal")
R = self.codomain()
return R.ideal([self(y) for y in I.gens()])
def inverse_image(self, I):
"""
Return the inverse image of the ideal `I` under this ring
homomorphism.
EXAMPLES:
This is not implemented in any generality yet::
sage: f = ZZ.hom(ZZ)
sage: f.inverse_image(ZZ.ideal(2))
Traceback (most recent call last):
...
NotImplementedError
"""
raise NotImplementedError
def lift(self, x=None):
"""
Return a lifting homomorphism associated to this homomorphism, if
it has been defined.
If ``x`` is not ``None``, return the value of the lift morphism on
``x``.
EXAMPLES::
sage: R.<x,y> = QQ[]
sage: f = R.hom([x,x])
sage: f(x+y)
2*x
sage: f.lift()
Traceback (most recent call last):
...
ValueError: no lift map defined
sage: g = R.hom(R)
sage: f._set_lift(g)
sage: f.lift() == g
True
sage: f.lift(x)
x
"""
if self._lift is None:
raise ValueError("no lift map defined")
if x is None:
return self._lift
return self._lift(x)
cdef class RingHomomorphism_coercion(RingHomomorphism):
def __init__(self, parent, check = True):
"""
Initialize ``self``.
INPUT:
- ``parent`` -- ring homset
- ``check`` -- bool (default: ``True``)
EXAMPLES::
sage: f = ZZ.hom(QQ); f # indirect doctest
Ring Coercion morphism:
From: Integer Ring
To: Rational Field
sage: f == loads(dumps(f))
True
"""
RingHomomorphism.__init__(self, parent)
# putting in check allows us to define subclasses of RingHomomorphism_coercion that implement _coerce_map_from
if check and not self.codomain().has_coerce_map_from(self.domain()):
raise TypeError("Natural coercion morphism from %s to %s not defined."%(self.domain(), self.codomain()))
def _repr_type(self):
"""
Used internally when printing this.
EXAMPLES::
sage: f = ZZ.hom(QQ)
sage: type(f)
<type 'sage.rings.morphism.RingHomomorphism_coercion'>
sage: f._repr_type()
'Ring Coercion'
"""
return "Ring Coercion"
def _richcmp_(self, other, int op):
"""
Compare a ring coercion morphism ``self`` to ``other``.
Ring coercion morphisms never compare equal to any other data type. If
other is a ring coercion morphism, the parents of ``self`` and
``other`` are compared.
EXAMPLES::
sage: f = ZZ.hom(QQ)
sage: g = ZZ.hom(ZZ)
sage: f == g
False
sage: h = Zmod(6).lift()
sage: f == h
False
"""
if op not in [Py_EQ, Py_NE]:
return NotImplemented
if not isinstance(other, RingHomomorphism_coercion):
return (op == Py_NE)
# Since they are coercion morphisms they are determined by
# their parents, i.e., by the domain and codomain, so we just
# compare those.
return richcmp(self.parent(), other.parent(), op)
def __hash__(self):
"""
Return the hash of this morphism.
TESTS::
sage: f = ZZ.hom(QQ)
sage: type(f)
<type 'sage.rings.morphism.RingHomomorphism_coercion'>
sage: hash(f) == hash(f)
True
sage: {f: 1}[f]
1
"""
return hash((self.domain(), self.codomain()))
cpdef Element _call_(self, x):
"""
Evaluate this coercion morphism at ``x``.
EXAMPLES::
sage: f = ZZ.hom(QQ); type(f)
<type 'sage.rings.morphism.RingHomomorphism_coercion'>
sage: f(2) == 2
True
sage: type(f(2)) # indirect doctest
<type 'sage.rings.rational.Rational'>