Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/categories/fields.py
4045 views
1
r"""
2
Fields
3
"""
4
#*****************************************************************************
5
# Copyright (C) 2005 David Kohel <[email protected]>
6
# William Stein <[email protected]>
7
# 2008 Teresa Gomez-Diaz (CNRS) <[email protected]>
8
# 2008-2009 Nicolas M. Thiery <nthiery at users.sf.net>
9
#
10
# Distributed under the terms of the GNU General Public License (GPL)
11
# http://www.gnu.org/licenses/
12
#******************************************************************************
13
14
from sage.categories.category import Category
15
from sage.categories.category_singleton import Category_singleton, Category_contains_method_by_parent_class
16
from sage.categories.euclidean_domains import EuclideanDomains
17
from sage.categories.unique_factorization_domains import UniqueFactorizationDomains
18
from sage.categories.division_rings import DivisionRings
19
20
from sage.misc.cachefunc import cached_method
21
from sage.misc.lazy_attribute import lazy_class_attribute
22
from sage.rings.field import is_Field
23
24
class Fields(Category_singleton):
25
"""
26
The category of (commutative) fields, i.e. commutative rings where
27
all non-zero elements have multiplicative inverses
28
29
EXAMPLES::
30
31
sage: K = Fields()
32
sage: K
33
Category of fields
34
sage: Fields().super_categories()
35
[Category of euclidean domains, Category of unique factorization domains, Category of division rings]
36
37
sage: K(IntegerRing())
38
Rational Field
39
sage: K(PolynomialRing(GF(3), 'x'))
40
Fraction Field of Univariate Polynomial Ring in x over
41
Finite Field of size 3
42
sage: K(RealField())
43
Real Field with 53 bits of precision
44
45
TESTS::
46
47
sage: TestSuite(Fields()).run()
48
"""
49
50
def super_categories(self):
51
"""
52
EXAMPLES::
53
54
sage: Fields().super_categories()
55
[Category of euclidean domains, Category of unique factorization domains, Category of division rings]
56
57
"""
58
return [EuclideanDomains(), UniqueFactorizationDomains(), DivisionRings()]
59
60
def __contains__(self, x):
61
"""
62
EXAMPLES::
63
64
sage: GF(4, "a") in Fields()
65
True
66
sage: QQ in Fields()
67
True
68
sage: ZZ in Fields()
69
False
70
sage: IntegerModRing(4) in Fields()
71
False
72
sage: InfinityRing in Fields()
73
False
74
75
This implementation will not be needed anymore once every
76
field in Sage will be properly declared in the category
77
:class:`Fields`().
78
79
Caveat: this should eventually be fixed::
80
81
sage: gap.Rationals in Fields()
82
False
83
84
typically by implementing the method :meth:`category`
85
appropriately for Gap objects::
86
87
sage: GR = gap.Rationals
88
sage: GR.category = lambda : Fields()
89
sage: GR in Fields()
90
True
91
"""
92
try:
93
return self._contains_helper(x) or is_Field(x)
94
except:
95
return False
96
97
@lazy_class_attribute
98
def _contains_helper(cls):
99
"""
100
Helper for containment tests in the category of fields.
101
102
This helper just tests whether the given object's category
103
is already known to be a sub-category of the category of
104
fields. There are, however, rings that are initialised
105
as plain commutative rings and found out to be fields
106
only afterwards. Hence, this helper alone is not enough
107
for a proper containment test.
108
109
TESTS::
110
111
sage: P.<x> = QQ[]
112
sage: Q = P.quotient(x^2+2)
113
sage: Q.category()
114
Join of Category of commutative algebras over Rational Field and Category of subquotients of monoids and Category of quotients of semigroups
115
sage: F = Fields()
116
sage: F._contains_helper(Q)
117
False
118
sage: Q in F # This changes the category!
119
True
120
sage: F._contains_helper(Q)
121
True
122
123
"""
124
return Category_contains_method_by_parent_class(cls())
125
126
def _call_(self, x):
127
"""
128
Construct a field from the data in ``x``
129
130
EXAMPLES::
131
132
sage: K = Fields()
133
sage: K
134
Category of fields
135
sage: Fields().super_categories()
136
[Category of euclidean domains, Category of unique factorization domains, Category of division rings]
137
138
sage: K(IntegerRing()) # indirect doctest
139
Rational Field
140
sage: K(PolynomialRing(GF(3), 'x')) # indirect doctest
141
Fraction Field of Univariate Polynomial Ring in x over
142
Finite Field of size 3
143
sage: K(RealField())
144
Real Field with 53 bits of precision
145
"""
146
try:
147
return x.fraction_field()
148
except AttributeError:
149
raise TypeError, "unable to associate a field to %s"%x
150
151
class ParentMethods:
152
def is_field(self):
153
"""
154
Return True, since this in an object of the category of fields.
155
156
EXAMPLES::
157
158
sage: Parent(QQ,category=Fields()).is_field()
159
True
160
161
"""
162
return True
163
164
def is_integrally_closed(self):
165
r"""
166
167
Return ``True``, as per :meth:`IntegralDomain.is_integraly_closed`:
168
for every field `F`, `F` is its own field of fractions,
169
hence every element of `F` is integral over `F`.
170
171
EXAMPLES::
172
173
sage: QQ.is_integrally_closed()
174
True
175
sage: QQbar.is_integrally_closed()
176
True
177
sage: Z5 = GF(5); Z5
178
Finite Field of size 5
179
sage: Z5.is_integrally_closed()
180
True
181
"""
182
return True
183
184
def _test_characteristic_fields(self, **options):
185
"""
186
Run generic tests on the method :meth:`.characteristic`.
187
188
EXAMPLES::
189
190
sage: QQ._test_characteristic_fields()
191
192
.. NOTE::
193
194
We cannot call this method ``_test_characteristic`` since that
195
would overwrite the method in the super category, and for
196
cython classes just calling
197
``super(sage.categories.fields.Fields().parent_class,
198
self)._test_characteristic`` doesn't have the desired effect.
199
200
.. SEEALSO::
201
202
:meth:`sage.categories.rings.Rings.ParentMethods._test_characteristic`
203
"""
204
tester = self._tester(**options)
205
try:
206
char = self.characteristic()
207
tester.assertTrue(char.is_zero() or char.is_prime())
208
except AttributeError:
209
return
210
# raised when self.one() does not have a additive_order() [or when char is an int and not an Integer which is already checked by _test_characteristic for rings]
211
except NotImplementedError:
212
return
213
214
class ElementMethods:
215
# Fields are unique factorization domains, so, there is gcd and lcm
216
# Of course, in general gcd and lcm in a field are not very interesting.
217
# However, they should be implemented!
218
def gcd(self,other):
219
"""
220
Greatest common divisor.
221
222
NOTE:
223
224
Since we are in a field and the greatest common divisor is
225
only determined up to a unit, it is correct to either return
226
zero or one. Note that fraction fields of unique factorization
227
domains provide a more sophisticated gcd.
228
229
EXAMPLES::
230
231
sage: GF(5)(1).gcd(GF(5)(1))
232
1
233
sage: GF(5)(1).gcd(GF(5)(0))
234
1
235
sage: GF(5)(0).gcd(GF(5)(0))
236
0
237
238
For fields of characteristic zero (i.e., containing the
239
integers as a sub-ring), evaluation in the integer ring is
240
attempted. This is for backwards compatibility::
241
242
sage: gcd(6.0,8); gcd(6.0,8).parent()
243
2
244
Integer Ring
245
246
If this fails, we resort to the default we see above::
247
248
sage: gcd(6.0*CC.0,8*CC.0); gcd(6.0*CC.0,8*CC.0).parent()
249
1.00000000000000
250
Complex Field with 53 bits of precision
251
252
AUTHOR:
253
254
- Simon King (2011-02): Trac ticket #10771
255
256
"""
257
P = self.parent()
258
try:
259
other = P(other)
260
except (TypeError, ValueError):
261
raise ArithmeticError, "The second argument can not be interpreted in the parent of the first argument. Can't compute the gcd"
262
from sage.rings.integer_ring import ZZ
263
if ZZ.is_subring(P):
264
try:
265
return ZZ(self).gcd(ZZ(other))
266
except TypeError:
267
pass
268
# there is no custom gcd, so, we resort to something that always exists
269
# (that's new behaviour)
270
if self==0 and other==0:
271
return P.zero()
272
return P.one()
273
274
def lcm(self,other):
275
"""
276
Least common multiple.
277
278
NOTE:
279
280
Since we are in a field and the least common multiple is
281
only determined up to a unit, it is correct to either return
282
zero or one. Note that fraction fields of unique factorization
283
domains provide a more sophisticated lcm.
284
285
EXAMPLES::
286
287
sage: GF(2)(1).lcm(GF(2)(0))
288
0
289
sage: GF(2)(1).lcm(GF(2)(1))
290
1
291
292
If the field contains the integer ring, it is first
293
attempted to compute the gcd there::
294
295
sage: lcm(15.0,12.0); lcm(15.0,12.0).parent()
296
60
297
Integer Ring
298
299
If this fails, we resort to the default we see above::
300
301
sage: lcm(6.0*CC.0,8*CC.0); lcm(6.0*CC.0,8*CC.0).parent()
302
1.00000000000000
303
Complex Field with 53 bits of precision
304
sage: lcm(15.2,12.0)
305
1.00000000000000
306
307
AUTHOR:
308
309
- Simon King (2011-02): Trac ticket #10771
310
311
"""
312
P = self.parent()
313
try:
314
other = P(other)
315
except (TypeError, ValueError):
316
raise ArithmeticError, "The second argument can not be interpreted in the parent of the first argument. Can't compute the lcm"
317
from sage.rings.integer_ring import ZZ
318
if ZZ.is_subring(P):
319
try:
320
return ZZ(self).lcm(ZZ(other))
321
except TypeError:
322
pass
323
# there is no custom lcm, so, we resort to something that always exists
324
if self==0 or other==0:
325
return P.zero()
326
return P.one()
327
328