From 7caf88f54a9ac35080531796fa72b10a42ab60df Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Sun, 22 Oct 2023 16:50:41 +0200 Subject: Pass TestFiniteSemigroupMorphismsChecks --- Homeworks.md | 16 ++-- src/act4e_solutions/semigroups_morphisms.py | 49 +++++++++++ .../semigroups_morphisms_representation.py | 99 ++++++++++++++++++++-- 3 files changed, 150 insertions(+), 14 deletions(-) create mode 100644 src/act4e_solutions/semigroups_morphisms.py diff --git a/Homeworks.md b/Homeworks.md index c0eaacc..fc0c091 100644 --- a/Homeworks.md +++ b/Homeworks.md @@ -28,14 +28,14 @@ $ make docker-check - [?] TestFiniteEndorelationProperties - [?] TestFiniteEndorelationOperations -- [ ] TestFiniteSemigroupRepresentation -- [ ] TestFiniteSemigroupConstruct -- [ ] TestFiniteMonoidRepresentation -- [ ] TestFiniteGroupRepresentation - -- [ ] TestFiniteSemigroupMorphismRepresentation -- [ ] TestFiniteMonoidMorphismRepresentation -- [ ] TestFiniteGroupMorphismRepresentation +- [X] TestFiniteSemigroupRepresentation +- [?] TestFiniteSemigroupConstruct +- [X] TestFiniteMonoidRepresentation +- [X] TestFiniteGroupRepresentation + +- [X] TestFiniteSemigroupMorphismRepresentation +- [X] TestFiniteMonoidMorphismRepresentation +- [X] TestFiniteGroupMorphismRepresentation - [ ] TestFiniteSemigroupMorphismsChecks - [ ] TestFinitePosetRepresentation diff --git a/src/act4e_solutions/semigroups_morphisms.py b/src/act4e_solutions/semigroups_morphisms.py new file mode 100644 index 0000000..5d832ee --- /dev/null +++ b/src/act4e_solutions/semigroups_morphisms.py @@ -0,0 +1,49 @@ +from typing import Any, TypeVar + +from .semigroups_representation import MyFiniteSemigroup, MyFiniteMonoid, MyFiniteGroup + +import act4e_interfaces as I + +A = TypeVar("A") +B = TypeVar("B") + + +class SolFiniteSemigroupMorphismsChecks(I.FiniteSemigroupMorphismsChecks): + def is_semigroup_morphism(self, a: I.FiniteSemigroup[A], b: I.FiniteSemigroup[B], f: I.FiniteMap[A, B]) -> bool: + # Semigroup preserves the structure + # f(xy) = f(x) f(y) for all x, y in a + # converse: + # there is an x or a y in a such that + # f(xy) neq f(x) f(y) + for x in a.carrier().elements(): + for y in a.carrier().elements(): + inside = f(a.compose(x, y)) + outside = b.compose(f(x), f(y)) + if not b.carrier().equal(inside, outside): + return False + return True + + def is_monoid_morphism(self, a: I.FiniteMonoid[A], b: I.FiniteMonoid[B], f: I.FiniteMap[A, B]) -> bool: + # Monoid morphism is a semigroup morphism that maps identity to identity + if not self.is_semigroup_morphism(a, b, f): + return False + + if not b.carrier().equal(b.identity(), f(a.identity())): + return False + + return True + + + def is_group_morphism(self, a: I.FiniteGroup[A], b: I.FiniteGroup[B], f: I.FiniteMap[A, B]) -> bool: + # Group morphism preserve + # f(xy) = f(x)f(y) for all x, y in a + # converse is + # exists x, y in a such that + # f(xy) neq f(x)f(y) + for x in a.carrier().elements(): + for y in a.carrier().elements(): + inside = f(a.compose(x, y)) + outside = b.compose(f(x), f(y)) + if not b.carrier().equal(inside, outside): + return False + return True diff --git a/src/act4e_solutions/semigroups_morphisms_representation.py b/src/act4e_solutions/semigroups_morphisms_representation.py index 43df89f..abc2216 100644 --- a/src/act4e_solutions/semigroups_morphisms_representation.py +++ b/src/act4e_solutions/semigroups_morphisms_representation.py @@ -2,26 +2,113 @@ from typing import Any, TypeVar import act4e_interfaces as I +from .maps_representation import SolFiniteMapRepresentation +from .semigroups_representation import (MyFiniteSemigroup, MyFiniteMonoid, MyFiniteGroup, + SolFiniteSemigroupRepresentation, + SolFiniteMonoidRepresentation, + SolFiniteGroupRepresentation) + +A = TypeVar("A") +B = TypeVar("B") + +class MyFiniteSemigroupMorphism(I.FiniteSemigroupMorphism[A, B]): + _source: I.FiniteSemigroup[A] + _target: I.FiniteSemigroup[B] + _mapping: I.FiniteMap[A, B] + + def __init__(self, s: I.FiniteSemigroup[A], t: I.FiniteSemigroup[B], f: I.FiniteMap[A, B]): + self._source = s + self._target = t + self._mapping = f + + def source(self) -> I.FiniteSemigroup[A]: + return self._source + + def target(self) -> I.FiniteSemigroup[B]: + return self._target + + def mapping(self) -> I.FiniteMap[A, B]: + return self._mapping + + +class MyFiniteMonoidMorphism(MyFiniteSemigroupMorphism, I.FiniteMonoidMorphism[A, B]): + _source: I.FiniteMonoid[A] + _target: I.FiniteMonoid[B] + + +class MyFiniteGroupMorphism(MyFiniteMonoidMorphism, I.FiniteGroupMorphism[A, B]): + _source: I.FiniteGroup[A] + _target: I.FiniteGroup[B] + + class SolFiniteSemigroupMorphismRepresentation(I.FiniteSemigroupMorphismRepresentation): def load(self, h: I.IOHelper, s: I.FiniteSemigroupMorphism_desc) -> I.FiniteSemigroupMorphism[Any, Any]: - raise NotImplementedError() + if not all(k in s.keys() for k in ["source", "target", "mapping"]): + raise I.InvalidFormat() + + fsgr = SolFiniteSemigroupRepresentation() + fmr = SolFiniteMapRepresentation() + + A = fsgr.load(h, s["source"]) + B = fsgr.load(h, s["target"]) + f = fmr.load(h, s["mapping"]) + + return MyFiniteSemigroupMorphism(A, B, f) def save(self, h: I.IOHelper, m: I.FiniteSemigroupMorphism[Any, Any]) -> I.FiniteSemigroupMorphism_desc: - raise NotImplementedError() + fsgr = SolFiniteSemigroupRepresentation() + fmr = SolFiniteMapRepresentation() + + d = {"source": fsgr.save(h, m.source()), + "target": fsgr.save(h, m.target()), + "mapping": fmr.save(h, m.mapping())} + return d class SolFiniteMonoidMorphismRepresentation(I.FiniteMonoidMorphismRepresentation): def load(self, h: I.IOHelper, s: I.FiniteMonoidMorphism_desc) -> I.FiniteMonoidMorphism[Any, Any]: - raise NotImplementedError() + if not all(k in s.keys() for k in ["source", "target", "mapping"]): + raise I.InvalidFormat() + + fmor = SolFiniteMonoidRepresentation() + fmr = SolFiniteMapRepresentation() + + A = fmor.load(h, s["source"]) + B = fmor.load(h, s["target"]) + f = fmr.load(h, s["mapping"]) + + return MyFiniteMonoidMorphism(A, B, f) def save(self, h: I.IOHelper, m: I.FiniteMonoidMorphism[Any, Any]) -> I.FiniteMonoidMorphism_desc: - raise NotImplementedError() + fmor = SolFiniteMonoidRepresentation() + fmr = SolFiniteMapRepresentation() + + d = {"source": fmor.save(h, m.source()), + "target": fmor.save(h, m.target()), + "mapping": fmr.save(h, m.mapping())} + return d class SolFiniteGroupMorphismRepresentation(I.FiniteGroupMorphismRepresentation): def load(self, h: I.IOHelper, s: I.FiniteGroupMorphism_desc) -> I.FiniteGroupMorphism[Any, Any]: - raise NotImplementedError() + if not all(k in s.keys() for k in ["source", "target", "mapping"]): + raise I.InvalidFormat() + + fgr = SolFiniteGroupRepresentation() + fmr = SolFiniteMapRepresentation() + + A = fgr.load(h, s["source"]) + B = fgr.load(h, s["target"]) + f = fmr.load(h, s["mapping"]) + + return MyFiniteGroupMorphism(A, B, f) def save(self, h: I.IOHelper, m: I.FiniteGroupMorphism[Any, Any]) -> I.FiniteGroupMorphism_desc: - raise NotImplementedError() + fgr = SolFiniteGroupRepresentation() + fmr = SolFiniteMapRepresentation() + + d = {"source": fgr.save(h, m.source()), + "target": fgr.save(h, m.target()), + "mapping": fmr.save(h, m.mapping())} + return d -- cgit v1.2.1