Skip to content
This repository was archived by the owner on Jan 30, 2023. It is now read-only.

Commit 2282a39

Browse files
committed
Added refinement of categories for IntegralDomains.
Similar to the check x in Fields(), x in IntegralDomains() now changes the category of x to contain the category of integral domains if x turns out to be an integral domain.
1 parent efa66ca commit 2282a39

File tree

2 files changed

+102
-3
lines changed

2 files changed

+102
-3
lines changed

src/sage/categories/integral_domains.py

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,33 @@
11
r"""
22
Integral domains
3+
4+
AUTHORS:
5+
6+
- Teresa Gomez-Diaz (2008): initial version
7+
8+
- Julian Rueth (2013-09-10): added category refinement similar to the one done by Fields
9+
310
"""
411
#*****************************************************************************
512
# Copyright (C) 2008 Teresa Gomez-Diaz (CNRS) <[email protected]>
13+
# 2013 Julian Rueth <[email protected]>
614
#
715
# Distributed under the terms of the GNU General Public License (GPL)
816
# http://www.gnu.org/licenses/
917
#******************************************************************************
1018

1119
from sage.categories.category import Category
12-
from sage.categories.category_singleton import Category_singleton
20+
from sage.categories.category_singleton import Category_singleton, Category_contains_method_by_parent_class
1321
from sage.misc.cachefunc import cached_method
1422
from sage.categories.commutative_rings import CommutativeRings
23+
from sage.misc.lazy_attribute import lazy_class_attribute
1524
from sage.categories.domains import Domains
25+
import sage.rings.ring
1626

1727
class IntegralDomains(Category_singleton):
1828
"""
19-
The category of integral domains
20-
commutative rings with no zero divisors
29+
The category of integral domains, i.e., non-trivial commutative rings with
30+
no zero divisors
2131
2232
EXAMPLES::
2333
@@ -40,6 +50,60 @@ def super_categories(self):
4050
"""
4151
return [CommutativeRings(), Domains()]
4252

53+
def __contains__(self, x):
54+
"""
55+
Return whether ``x`` is in the category of integral domains.
56+
57+
EXAMPLES::
58+
59+
sage: GF(4, "a") in IntegralDomains()
60+
True
61+
sage: QQ in IntegralDomains()
62+
True
63+
sage: ZZ in IntegralDomains()
64+
True
65+
sage: IntegerModRing(4) in IntegralDomains()
66+
False
67+
68+
Test that :trac:`15183` has been fixed::
69+
70+
sage: IntegerModRing(2) in IntegralDomains()
71+
True
72+
73+
"""
74+
try:
75+
return self._contains_helper(x) or sage.rings.ring._is_IntegralDomain(x)
76+
except StandardError:
77+
return False
78+
79+
@lazy_class_attribute
80+
def _contains_helper(cls):
81+
"""
82+
Helper for containment tests in the category of integral domains.
83+
84+
This helper just tests whether the given object's category is already
85+
known to be a sub-category of the category of integral domains. There
86+
are, however, rings that are initialised as plain commutative rings and
87+
found out to be integral domains only afterwards. Hence, this helper
88+
alone is not enough for a proper containment test.
89+
90+
TESTS::
91+
92+
sage: P.<x> = QQ[]
93+
sage: Q = P.quotient(x^2+2)
94+
sage: Q.category()
95+
Join of Category of commutative algebras over Rational Field and Category of subquotients of monoids and Category of quotients of semigroups
96+
sage: ID = IntegralDomains()
97+
sage: ID._contains_helper(Q)
98+
False
99+
sage: Q in ID # This changes the category!
100+
True
101+
sage: ID._contains_helper(Q)
102+
True
103+
104+
"""
105+
return Category_contains_method_by_parent_class(cls())
106+
43107
class ParentMethods:
44108
def is_integral_domain(self):
45109
"""

src/sage/rings/ring.pyx

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2096,11 +2096,46 @@ def is_Field(x):
20962096
deprecation(13370, "use 'R in Fields()', not 'is_Field(R)'")
20972097
return _is_Field(x)
20982098

2099+
cpdef bint _is_IntegralDomain(x) except -2:
2100+
"""
2101+
Return whether ``x`` is an integral domain.
2102+
2103+
EXAMPLES::
2104+
2105+
sage: from sage.rings.ring import _is_IntegralDomain
2106+
sage: _is_IntegralDomain(QQ)
2107+
True
2108+
sage: _is_IntegralDomain(IntegerModRing(2))
2109+
True
2110+
sage: _is_IntegralDomain(IntegerModRing(4))
2111+
False
2112+
sage: _is_IntegralDomain(5)
2113+
False
2114+
2115+
.. NOTE::
2116+
2117+
``_is_IntegralDomain(R)`` is for internal use. It is better (and
2118+
faster) to use ``R in IntegralDomains()`` instead.
2119+
2120+
"""
2121+
# The result is not immediately returned, since we want to refine x's
2122+
# category, so that calling x in IntegralDomains() will be faster next
2123+
# time.
2124+
try:
2125+
result = isinstance(x, IntegralDomain) or x.is_integral_domain()
2126+
except AttributeError:
2127+
result = False
2128+
if result:
2129+
x._refine_category_(_IntegralDomains)
2130+
return result
2131+
20992132
# This imports is_Field, so must be executed after is_Field is defined.
21002133
from sage.categories.algebras import Algebras
21012134
from sage.categories.commutative_algebras import CommutativeAlgebras
21022135
from sage.categories.fields import Fields
2136+
from sage.categories.integral_domains import IntegralDomains
21032137
_Fields = Fields()
2138+
_IntegralDomains = IntegralDomains()
21042139

21052140
cdef class Field(PrincipalIdealDomain):
21062141
"""

0 commit comments

Comments
 (0)