def test_priority_union_right_inverse_implementations_are_eq(): for i, (a, b, c) in enumerate(all3VecIO): oldAnswer = fv.stack_to_set(fv.right_inv_priority_union(c=c, b=b)) newAnswer = fv.stack_to_set(fv.right_inv_priority_union_old(c=c, b=b)) missingFromNew = oldAnswer - newAnswer extraInNew = newAnswer - oldAnswer assert oldAnswer == newAnswer, f"{i}:{a},{b},{c}\n{missingFromNew}\n{extraInNew}"
def test_intersection_of_every_pair_of_LCs_is_the_LC_of_the_meet(): for i, (a, b) in enumerate([(a, b) for a in all3Vecs for b in all3Vecs]): lcA, lcB = fv.lower_closure(a), fv.lower_closure(b) lcAs, lcBs = fv.stack_to_set(lcA), fv.stack_to_set(lcB) cap = lcAs.intersection(lcBs) meet = fv.meet_specification(a, b) lcMeet = fv.lower_closure(meet) lcMeets = fv.stack_to_set(lcMeet) assert cap == lcMeets, f"{i}:{a},{b}\n{cap}\n{lcMeets}"
def test_right_inverse_is_associative(): counterexamples = set() for xWrapped, yWrapped, zWrapped in all3VecTriplesSet: x, y, z = xWrapped.unwrap(), yWrapped.unwrap(), zWrapped.unwrap() xy = fv.right_inv_priority_union(x, y) xyWrapped = None if xy is None else fv.stack_to_set(xy) if xy is None: xy_z = None else: xy_zWrapped = set() for xyPrimeWrapped in xyWrapped: xyPrime = xyPrimeWrapped.unwrap() if fv.right_inv_priority_union(xyPrime, z) is not None: xy_zWrapped.union( fv.stack_to_set(fv.right_inv_priority_union( xyPrime, z))) xy_z = fv.hashableArrays_to_stack( xy_zWrapped) if len(xy_zWrapped) > 0 else None # xy_z = np.unique(fv.hashableArrays_to_stack( # grand_union([ # fv.stack_to_set(fv.right_inv_priority_union(xyPrime, z)) # for xyPrime in xyWrapped if fv.right_inv_priority_union(xyPrime, z) is not None]) # ), axis=0) yz = fv.right_inv_priority_union(y, z) yzWrapped = None if yz is None else fv.stack_to_set(yz) if yz is None: x_yz = None else: x_yzWrapped = set() for yzPrimeWrapped in yzWrapped: yzPrime = yzPrimeWrapped.unwrap() if fv.right_inv_priority_union(x, yzPrime) is not None: x_yzWrapped.union( fv.stack_to_set(fv.right_inv_priority_union( x, yzPrime))) x_yz = fv.hashableArrays_to_stack( x_yzWrapped) if len(x_yzWrapped) > 0 else None # x_yz = np.unique(fv.hashableArrays_to_stack( # grand_union([ # fv.stack_to_set(fv.right_inv_priority_union(x, yzPrime)) # for yzPrime in yzWrapped if fv.right_inv_priority_union(x, yzPrime) is not None]) # ), axis=0) # x_yz = None if yz is None else fv.right_inv_priority_union(x, yz) if xy_z is None and not x_yz is None: counterexamples.add((xWrapped, yWrapped, zWrapped)) elif xy_z is not None and x_yz is None: counterexamples.add((xWrapped, yWrapped, zWrapped)) elif xy_z is not None and x_yz is not None: if not np.array_equal(xy_z, x_yz): counterexamples.add((xWrapped, yWrapped, zWrapped)) else: pass assert len(counterexamples) == 0, f"{counterexamples}"
def test_intersection_of_every_pair_of_UCs_is_the_UC_of_the_join(): for i, (a, b) in enumerate([(a, b) for a in all3Vecs for b in all3Vecs]): ucA, ucB = fv.upper_closure(a), fv.upper_closure(b) ucAs, ucBs = fv.stack_to_set(ucA), fv.stack_to_set(ucB) cap = ucAs.intersection(ucBs) join = fv.join_specification(a, b) if join is not None: ucJoin = fv.upper_closure(join) ucJoins = fv.stack_to_set(ucJoin) assert cap == ucJoins, f"{i}:{a},{b}\n{cap}\n{ucJoins}" else: assert len(cap) == 0, f"{i}:{a},{b}\n{cap}"
def test_priority_union_stack_right_as_expected(): counterexamples = set() for xWrapped in all3VecsSet: x = xWrapped.unwrap() stackRightResult = fv.priority_union(x, all3Vecs) nonVectorResult = np.array([fv.priority_union(x, v) for v in all3Vecs], dtype=INT8) stackRightResultSet = fv.stack_to_set(stackRightResult) nonVectorResultSet = fv.stack_to_set(nonVectorResult) if stackRightResultSet != nonVectorResultSet: counterexamples.add((xWrapped)) assert len(counterexamples) == 0, f"Counterexamples:\n{counterexamples}"
def test_cap_of_every_interval_pair_is_empty_or_an_interval_with_natural_bounds( ): counterexamples = set() for i, intervalLeftWrapped in enumerate(all3VecIntervals): intervalLeft = fv.hashableArrays_to_stack(intervalLeftWrapped) maxLeft, minLeft = fv.max_of(intervalLeft), fv.min_of(intervalLeft) for j, intervalRightWrapped in enumerate(all3VecIntervals): intervalRight = fv.hashableArrays_to_stack(intervalRightWrapped) maxRight, minRight = fv.max_of(intervalRight), fv.min_of( intervalRight) capSet = intervalLeftWrapped.intersection(intervalRightWrapped) capStack = fv.hashableArrays_to_stack(capSet) est_cap_max = fv.meet_specification(maxLeft, maxRight) est_cap_min = fv.join_specification(minLeft, minRight) if len(capSet) > 0: est_cap = fv.interval(est_cap_min, est_cap_max) est_capSet = fv.stack_to_set(est_cap) if not est_capSet == capSet: counterexamples.add( ((i, intervalLeftWrapped), (j, intervalRightWrapped), (capSet, est_capSet))) else: assert est_cap_max is None or est_cap_min is None or not fv.lte_specification( est_cap_min, est_cap_max), f"{est_cap_max}, {est_cap_min}" assert len(counterexamples) == 0, f"Counterexamples:\n{counterexamples}"
def test_every_defined_left_inverse_yields_a_bounded_lattice_that_is_an_interval( ): counterexamples = set() for i, (a, b, c) in enumerate(all3VecIO): possibleBs = fv.left_inv_priority_union(a, c) possibleBSet = fv.stack_to_set(possibleBs) if not possibleBSet in all3VecIntervals: counterexamples.add((i, (fv.HashableArray(a), fv.HashableArray(b), fv.HashableArray(c)))) assert len(counterexamples) == 0, f"{counterexamples}"
def test_priority_union_right_inverse_all_expected_to_be_possible_are(): Ms = fv.stack_to_set(all3Vecs) allPairs = {(c, b) for c in Ms for b in Ms} for (cWrapped, bWrapped) in allPairs: c, b = cWrapped.unwrap(), bWrapped.unwrap() ri = fv.right_inv_priority_union(c, b) if ri is not None: for a in ri: assert np.array_equal(fv.priority_union(a, b), c), f"{a}+{b}≠{c}"
def test_priority_union_left_inverse_all_expected_to_be_possible_are(): Ms = fv.stack_to_set(all3Vecs) allPairs = {(a, c) for a in Ms for c in Ms} for (aWrapped, cWrapped) in allPairs: a, c = aWrapped.unwrap(), cWrapped.unwrap() li = fv.left_inv_priority_union(a, c) if li is not None: for b in li: assert np.array_equal(fv.priority_union(a, b), c), f"{a}+{b}≠{c}"
def test_join_specification_possible_stack_implementations_eq(): Ms = fv.stack_to_set(all3Vecs) allPairs = {(a, b) for a in Ms for b in Ms} allJoinablePairsA = { (a, b) for (a, b) in allPairs if fv.join_specification_possible(a.unwrap(), b.unwrap()) } allJoinablePairsB = {(a, b) for (a, b) in allPairs if fv.join_specification_possible_stack( np.array([a.unwrap(), b.unwrap()], dtype=INT8))} missingFromB = allJoinablePairsA - allJoinablePairsB missingFromA = allJoinablePairsB - allJoinablePairsA assert allJoinablePairsA == allJoinablePairsB, f"Missing from stack-based result:\n{missingFromB}\nMissing from pairwise result:\n{missingFromA}"
def left_inverse_is_a_lower_closure(i, a, b, c): possibleBs = fv.left_inv_priority_union(a, c) possibleBsSet = fv.stack_to_set(possibleBs) return possibleBs is None or possibleBsSet in allLC3sCompressed
np.array([0, 1, 2, 1], dtype=INT8)) assert np.array_equal(int_to_trits(19, 4), np.array([0, 2, 0, 1], dtype=INT8)) assert np.array_equal(int_to_trits(np.array([16, 19]), 4), np.vstack([[0, 1, 2, 1], [0, 2, 0, 1]])) # def test_trits_ints_inverse(): # # trits_to_ints # # ints_to_trits # pass all3Vecs = np.array([[x, y, z] for x in {+1, 0, -1} for y in {+1, 0, -1} for z in {+1, 0, -1}], dtype=INT8) all3VecsSet = fv.stack_to_set(all3Vecs) all3VecPairsSet = {(a, b) for a in all3VecsSet for b in all3VecsSet} all3VecTriplesSet = {(a, b, c) for a in all3VecsSet for b in all3VecsSet for c in all3VecsSet} ####################################### # stack-oriented join implementations # ####################################### def test_join_specification_possible_stack_implementations_eq(): Ms = fv.stack_to_set(all3Vecs) allPairs = {(a, b) for a in Ms for b in Ms} allJoinablePairsA = { (a, b)