diff options
Diffstat (limited to 'src/act4e_solutions')
-rw-r--r-- | src/act4e_solutions/maps_representation.py | 39 | ||||
-rw-r--r-- | src/act4e_solutions/sets_representation.py | 11 |
2 files changed, 40 insertions, 10 deletions
diff --git a/src/act4e_solutions/maps_representation.py b/src/act4e_solutions/maps_representation.py index 92e7924..39c885c 100644 --- a/src/act4e_solutions/maps_representation.py +++ b/src/act4e_solutions/maps_representation.py @@ -1,5 +1,5 @@ from pprint import pprint -from typing import Any, TypeVar, Dict, List +from typing import Any, TypeVar, Dict, List, Tuple import act4e_interfaces as I from .sets_representation import SolFiniteSetRepresentation @@ -11,9 +11,9 @@ B = TypeVar("B") class MyFiniteMap(I.FiniteMap[A, B]): _source: I.FiniteSet[A] _target: I.FiniteSet[B] - _values: List[List] + _values: List[List] # of pairs (A, B) - def __init__(self, source: I.FiniteSet[A], target: I.FiniteSet[B], values: Dict[A, B]): + def __init__(self, source: I.FiniteSet[A], target: I.FiniteSet[B], values: List[Tuple[A, B]]): self._source = source self._target = target self._values = values @@ -33,12 +33,17 @@ class MyFiniteMap(I.FiniteMap[A, B]): assert a in self._source.elements() rv = None for v in self._values: - if v[0] == a: - print(f"{v[0]} equals {a}") + if self._source.equal(v[0], a): + # print(f"{v[0]} equals {a}") rv = v[1] + assert self._target.contains(rv) + print("ret: ", rv) + print("the type of rv is", type(rv)) print() + + # assert type(rv) != bool return rv @@ -46,17 +51,22 @@ class SolFiniteMapRepresentation(I.FiniteMapRepresentation): def load(self, h: I.IOHelper, s: I.FiniteMap_desc) -> I.FiniteMap[A, B]: fsr = SolFiniteSetRepresentation() + if not all([k in s.keys() for k in ["source", "target", "values"]]): + raise I.InvalidFormat() + A = fsr.load(h, s["source"]) B = fsr.load(h, s["target"]) values = s["values"] # Check that the values are are in the domain and codomain for v in values: - if not v[0] in A.elements(): + a, b = A.load(h, v[0]), B.load(h, v[1]) + + if not A.contains(a): print(f"{v[0]} is not in A: {A.elements()}") raise I.InvalidFormat() - if not v[1] in B.elements(): + if not B.contains(b): print(f"{v[1]} is not in B: {B.elements()}") raise I.InvalidFormat() @@ -71,4 +81,17 @@ class SolFiniteMapRepresentation(I.FiniteMapRepresentation): def save(self, h: I.IOHelper, m: I.FiniteMap[Any, Any]) -> I.FiniteMap_desc: - raise NotImplementedError() + assert False + fsr = SolFiniteSetRepresentation() + d = { + "source": fsr.save(m.source()), + "target": fsr.save(m.target()), + "values": [] + } + + for a in m.source().elements(): + b = m(a) + d["values"].append([m.source().save(h, a), m.target().save(h, b)]) + + + return d diff --git a/src/act4e_solutions/sets_representation.py b/src/act4e_solutions/sets_representation.py index 9e231c1..627b408 100644 --- a/src/act4e_solutions/sets_representation.py +++ b/src/act4e_solutions/sets_representation.py @@ -61,5 +61,12 @@ class SolFiniteSetRepresentation(I.FiniteSetRepresentation): raise I.InvalidFormat() def save(self, h: I.IOHelper, f: I.FiniteSet[Any]) -> I.FiniteSet_desc: - all_elements = [f.save(h, e) for e in f.elements()] - return {"elements": all_elements} + if isinstance(f, MyFiniteSetProduct): + return {"product": [{"elements": c.elements()} for c in f.components()]} + + elif isinstance(f, MyFiniteSet): + all_elements = [f.save(h, e) for e in f.elements()] + return {"elements": all_elements} + + else: + raise I.InvalidFormat() |