Skip to content

Commit

Permalink
Detect whether the base ring is GF(2) in a consistent and robust way.
Browse files Browse the repository at this point in the history
  • Loading branch information
jhpalmieri committed Sep 25, 2023
1 parent 3386b97 commit e0aaa04
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
27 changes: 22 additions & 5 deletions src/sage/homology/homology_vector_space_with_basis.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ def dual(self):
sage: coh.dual() is hom
True
"""
if self.base_ring() == GF(2):
if is_GF2(self.base_ring()):
if self._cohomology:
return HomologyVectorSpaceWithBasis_mod2(self.base_ring(),
self.complex())
Expand Down Expand Up @@ -590,7 +590,7 @@ def __init__(self, base_ring, cell_complex, category=None):
sage: H = simplicial_complexes.Sphere(3).homology_with_basis(GF(2))
sage: TestSuite(H).run()
"""
if base_ring != GF(2):
if not is_GF2(base_ring):
raise ValueError
category = Modules(base_ring).WithBasis().Graded().FiniteDimensional().or_subcategory(category)
category = Category.join((category,
Expand Down Expand Up @@ -641,7 +641,7 @@ def _acted_upon_(self, a, self_on_left):
.. MATH::
(f \cdot a) (x) = f (a \cdot x)
(f \cdot a) (x) = f (a \cdot x).
So given `f` (a.k.a. ``self``) and `a`, we compute `f (a
\cdot x)` for all basis elements `x` in `H^{m-n}`,
Expand Down Expand Up @@ -1015,7 +1015,7 @@ def __init__(self, base_ring, cell_complex):
sage: H = RP2.cohomology_ring(GF(2))
sage: TestSuite(H).run()
"""
if base_ring != GF(2):
if not is_GF2(base_ring):
raise ValueError("the base ring must be GF(2)")
category = Algebras(base_ring).WithBasis().Graded().FiniteDimensional()
category = Category.join((category,
Expand Down Expand Up @@ -1105,11 +1105,12 @@ def Sq(self, i):
P = scomplex.cohomology_ring(self.base_ring())
self = P.sum_of_terms(self.monomial_coefficients().items())
if not isinstance(scomplex, (SimplicialComplex, SimplicialSet_arbitrary)):
print(scomplex, isinstance(scomplex, SimplicialComplex))
raise NotImplementedError('Steenrod squares are not implemented for '
'this type of cell complex')
scomplex = P.complex()
base_ring = P.base_ring()
if base_ring.characteristic() != 2:
if not is_GF2(base_ring):
# This should never happen: the class should only be
# instantiated in characteristic 2.
raise ValueError('Steenrod squares are only defined in characteristic 2')
Expand Down Expand Up @@ -1428,3 +1429,19 @@ def sum_indices(k, i_k_plus_one, S_k_plus_one):
return [[S_k]]
return [[i_k] + l for i_k in range(S_k, i_k_plus_one)
for l in sum_indices(k-1, i_k, S_k)]

def is_GF2(R):
"""
Return ``True`` iff ``R`` is isomorphic to the field GF(2).
EXAMPLES::
sage: from sage.homology.homology_vector_space_with_basis import is_GF2
sage: is_GF2(GF(2))
True
sage: is_GF2(GF(2, impl='ntl'))
True
sage: is_GF2(GF(3))
False
"""
return 2 == R.cardinality() == R.characteristic()
13 changes: 8 additions & 5 deletions src/sage/topology/cell_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
from sage.structure.sage_object import SageObject
from sage.rings.integer_ring import ZZ
from sage.rings.rational_field import QQ
from sage.rings.finite_rings.finite_field_constructor import GF
from sage.misc.abstract_method import abstract_method


Expand Down Expand Up @@ -866,10 +865,12 @@ def homology_with_basis(self, base_ring=QQ, cohomology=False):
[h^{3,0}]
"""
from sage.homology.homology_vector_space_with_basis import \
HomologyVectorSpaceWithBasis, HomologyVectorSpaceWithBasis_mod2
HomologyVectorSpaceWithBasis, HomologyVectorSpaceWithBasis_mod2, \
is_GF2

if cohomology:
return self.cohomology_ring(base_ring)
if base_ring == GF(2):
if is_GF2(base_ring):
return HomologyVectorSpaceWithBasis_mod2(base_ring, self)
return HomologyVectorSpaceWithBasis(base_ring, self, cohomology)

Expand Down Expand Up @@ -977,8 +978,10 @@ def cohomology_ring(self, base_ring=QQ):
Cohomology ring of Simplicial complex with 9 vertices and
18 facets over Rational Field
"""
from sage.homology.homology_vector_space_with_basis import CohomologyRing, CohomologyRing_mod2
if base_ring == GF(2):
from sage.homology.homology_vector_space_with_basis import CohomologyRing, \
CohomologyRing_mod2, is_GF2

if is_GF2(base_ring):
return CohomologyRing_mod2(base_ring, self)
return CohomologyRing(base_ring, self)

Expand Down

0 comments on commit e0aaa04

Please sign in to comment.