summaryrefslogtreecommitdiffstats
path: root/src/act4e_solutions/maps_representation.py
diff options
context:
space:
mode:
authorNao Pross <np@0hm.ch>2023-10-17 15:03:23 +0200
committerNao Pross <np@0hm.ch>2023-10-17 15:03:23 +0200
commitda2e878bf982ea2a0fe7727b68dfa211409973bb (patch)
tree4031f5794976911c96ba36742c0c32156a73c0b0 /src/act4e_solutions/maps_representation.py
parentSketch TestFiniteMapRepresentation (diff)
downloadact4e-impl_fail.tar.gz
act4e-impl_fail.zip
Something weird is going on hereimpl_fail
Diffstat (limited to 'src/act4e_solutions/maps_representation.py')
-rw-r--r--src/act4e_solutions/maps_representation.py39
1 files changed, 31 insertions, 8 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