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

Commit

Permalink
src/sage/sets/cartesian_product.py (CartesianProduct): Dispatch to im…
Browse files Browse the repository at this point in the history
…plementation classes CartesianProduct_eq_by_factors, CartesianProduct_unique
  • Loading branch information
Matthias Koeppe committed Aug 17, 2022
1 parent be359bf commit 7c303cb
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/sage/combinat/posets/cartesian_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
# https://www.gnu.org/licenses/
# ****************************************************************************

from sage.sets.cartesian_product import CartesianProduct
from sage.sets.cartesian_product import CartesianProduct_unique


class CartesianProductPoset(CartesianProduct):
class CartesianProductPoset(CartesianProduct_unique):
r"""
A class implementing Cartesian products of posets (and elements
thereof). Compared to :class:`CartesianProduct` you are able to
Expand Down
8 changes: 4 additions & 4 deletions src/sage/groups/group_semidirect_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
# ****************************************************************************
from sage.categories.commutative_additive_groups import CommutativeAdditiveGroups
from sage.categories.groups import Groups
from sage.sets.cartesian_product import CartesianProduct
from sage.sets.cartesian_product import CartesianProduct_unique
from sage.misc.cachefunc import cached_method


class GroupSemidirectProductElement(CartesianProduct.Element):
class GroupSemidirectProductElement(CartesianProduct_unique.Element):
r"""
Element class for :class:`GroupSemidirectProduct`.
"""
Expand Down Expand Up @@ -130,7 +130,7 @@ def to_opposite(self):
return Gop((h, par._twist(~h, g)))


class GroupSemidirectProduct(CartesianProduct):
class GroupSemidirectProduct(CartesianProduct_unique):
r"""
Return the semidirect product of the groups ``G`` and ``H`` using the homomorphism ``twist``.
Expand Down Expand Up @@ -280,7 +280,7 @@ def check_implemented_group(x):
self._prefix1 = prefix1
self._print_tuple = print_tuple
self._category = category
CartesianProduct.__init__(self, (G, H), category=category)
super().__init__((G, H), category=category)

def act_to_right(self):
r"""
Expand Down
39 changes: 20 additions & 19 deletions src/sage/sets/cartesian_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,23 @@ def cartesian_factors(self):
return self.value


class CartesianProduct(CartesianProduct_with_element_wrapper, UniqueRepresentation):
class CartesianProduct_eq_by_factors(CartesianProduct_with_element_wrapper):
def __eq__(self, other):
if isinstance(other, CartesianProduct_base):
# No flattening, hence we are equal if and only if our factors are equal
return self.cartesian_factors() == other.cartesian_factors()
return super().__eq__(other)

def __hash__(self):
# No flattening, hence we are equal if and only if our factors are equal
return hash(self.cartesian_factors())


class CartesianProduct_unique(CartesianProduct_with_element_wrapper, UniqueRepresentation):
pass


class CartesianProduct(CartesianProduct_base):

@staticmethod
def __classcall_private__(cls, sets, category, flatten=False):
Expand Down Expand Up @@ -413,28 +429,13 @@ def __classcall_private__(cls, sets, category, flatten=False):
sage: cartesian_product([G, G]) is cartesian_product([G, G])
True
"""
assert cls == CartesianProduct
# Trac #19195: EnumeratedSetFromIterator instances are not safe to be passed
# to UniqueRepresentation because EnumeratedSetFromIterator.__eq__ resorts
# to a semi-decision procedure for equality.
if not any(isinstance(set, EnumeratedSetFromIterator) and set not in FiniteEnumeratedSets()
for set in sets):
# UniqueRepresentation is safe to use.
return UniqueRepresentation.__classcall__(cls, sets, category, flatten)
return CartesianProduct_unique(sets, category)
else:
return WithPicklingByInitArgs.__classcall__(cls, sets, category, flatten)

def __eq__(self, other):
if not any(isinstance(set, EnumeratedSetFromIterator) and set not in FiniteEnumeratedSets()
for set in self._sets):
# Use WithEqualityById
return super().__eq__(other)
# No flattening, hence we are equal if and only if our factors are equal
return self.cartesian_factors() == other.cartesian_factors()

def __hash__(self):
if not any(isinstance(set, EnumeratedSetFromIterator) and set not in FiniteEnumeratedSets()
for set in self._sets):
# Use WithEqualityById
return super().__hash__()
# No flattening, hence we are equal if and only if our factors are equal
return hash(self.cartesian_factors())
return CartesianProduct_eq_by_factors(sets, category)

0 comments on commit 7c303cb

Please sign in to comment.