def test_from_nulity_matroid(nulity_matroid, expected): E, _ = nulity_matroid cl1 = from_nulity_matroid(nulity_matroid) cl2 = expected for X in powset(E): print(cl1(X), cl2(X)) assert all(cl1(X) == cl2(X) for X in powset(E))
def from_hyperplanes_matroid( matroid: tuple[set[T], list[set[T]]]) -> list[set[T]]: """COnstruct flats from a matroid defined by hyperplanes. Args: matroid (tuple[set[T], list[set[T]]]): A matroid defined by hyperplanes. Returns: list[set[T]]: The flats of a given matroid. """ E, Hs = matroid # Fs is a group generated by Hs under the intersection. Hs.append(E) all_intersection = lambda Xs: [ X for X in powset(E) if X in [A & B for A, B in combinations_with_replacement(Xs, 2)] ] tmp = all_intersection(Hs) # Repeat unless the set is closed under the intersection. # This loop must be stopped because E is finite, and E includes any intersection. while True: next_tmp = all_intersection(tmp) if len(tmp) == len(next_tmp): return tmp tmp = next_tmp
def cocircuits(self) -> list[set[int]]: # Cs*(U_{0,n}) = ∅ if self.k == 0: return [] # Cs*(U_{k,n}) = { C* ⊆ E : |C*| = n - k + 1 } (k ≠ 0) return [ C_ast for C_ast in powset(self.E) if len(C_ast) == self.n - self.k + 1 ]
def from_dependent_matroid(matroid: tuple[set[T], list[set]]) -> list[set[T]]: """Construct independent sets from a matroid defined by dependent sets Args: matroid (tuple[set[T], list[set]]): A matroid defined by dependent sets Returns: list[set[T]]: The independent sets of a given matroid. """ E, Ds = matroid return [I for I in powset(E) if I not in Ds]
def from_bases_matroid(matroid: tuple[set[T], list[set[T]]]) -> list[set[T]]: """Construct independent sets from a matroid defined by bases. Args: matroid (tuple[set[T], list[set[T]]]): Matroid defined by bases. Returns: list[set[T]]: The bases of a given matroid. """ E, Bs = matroid return [I for I in powset(E) if any(map(lambda B: I <= B, Bs))]
def from_circuits_matroid(matroid: tuple[set[T], list[set[T]]]) -> list[set[T]]: """Construct dependent sets from a matroid defined by circuits. Args: matroid (tuple[set[T], list[set[T]]]): A matroid defined by circuits. Returns: list[set[T]]: The dependent sets of a matroid. """ E, Cs = matroid return [D for D in powset(E) if any(map(lambda C: C <= D, Cs))]
def from_nulity_matroid(matroid: tuple[set[T], Callable[[set[T]], int]]) -> list[set[T]]: """Construnct dependent sets from a matroid defined by a nulity function. Args: matroid (tuple[set[T], Callable[[set[T]], int]]): A matroid defined by a rank function. Returns: list[set[T]]: The dependent sets of a matroid. """ E, n = matroid # Cs = { C ⊆ E : n(C) ≠ 0 } return [C for C in powset(E) if n(C) != 0]
def from_rank_matroid(matroid: tuple[set[T], Callable[[set[T]], int]]) -> list[set[T]]: """Construnct dependent sets from a matroid defined by a rank function. Args: matroid (tuple[set[T], Callable[[set[T]], int]]): A matroid defined by a rank function. Returns: list[set[T]]: The dependent sets of a matroid. """ E, r = matroid # Cs = {C ⊆ E : r(C) ≠ |C|} return [C for C in powset(E) if r(C) != len(C)]
def from_independent_matroid(matroid: tuple[set[T], list[set[T]]]) -> list[set[T]]: """Construct dependent sets from a matroid defined by dependent sets. Args: matroid (tuple[set[T], list[set[T]]]): A matroid defined by independent sets. Returns: list[set[T]]: The dependent sets of a given matroid. """ E, Is = matroid # Ds = {D ⊆ E : D ∉ Is} return [D for D in powset(E) if D not in Is]
def from_bases_matroid(matroid: tuple[set[T], list[set[T]]]) -> list[set[T]]: """Construct dependent sets from a matroid defined by bases. Args: matroid (tuple[set[T], list[set[T]]]): A matroid defined by bases. Returns: list[set[T]]: The dependent sets of a given matroid. """ E, Bs = matroid # Ds = { D ⊆ E : D - B ≠ ∅,∀B ∈ Bs} return [D for D in powset(E) if all(map(lambda B: D - B, Bs)) ]
def from_closure_matroid(matroid: tuple[set[T], Callable[[set[T]], set[T]]]) -> Callable[[set[T]], int]: """Construct a nulity function from a matroid defined by a closure function. Args: matroid (tuple[set[T], Callable[[set[T]], set[T]]]): A matroid defined by a closure function Returns: Callable[[set[T]], int]: The nulity function of a given matroid. """ E, cl = matroid # n(X) = |X| - min{ |I| : X ⊆ cl(I) }. return lambda X: len(X) - min(len(I) for I in powset(E) if X <= cl(I))
def from_nulity_matroid( matroid: tuple[set[T], Callable[[set[T]], int]]) -> list[set[T]]: """Construct bases from a matroid defined by a nulity function. Args: matroid (tuple[set[T], Callable[[set[T]]], int]): A matroid defined by a nulity function. Returns: list[set[T]]: The bases of a given matroid. """ E, n = matroid # Bs = { B ⊆ E : n(B) = 0 and n(E) = |E| - |B| } return [B for B in powset(E) if (n(B) == 0) and (n(E) == len(E) - len(B))]
def from_nulity_matroid( matroid: tuple[set[T], Callable[[set[T]], int]]) -> list[set[T]]: """Construct spanning sets from a matroid defined by a nulity function. Args: matroid (tuple[set[T], Callable[[set[T]], int]]): A matroid defined by a nulity function Returns: list[set[T]]: The spanning set of a given matroid. """ E, n = matroid # Ss = { S ⊆ E : n(E) - n(S) = |E| - |S| } return [S for S in powset(E) if n(E) - n(S) == len(E) - len(S)]
def from_rank_matroid( matroid: tuple[set[T], Callable[[set[T]], int]]) -> list[set[T]]: """Construct bases from a matroid defined by a rank function. Args: matroid (tuple[set[T], Callable[[set[T]]], int]): A matroid defined by a rank function. Returns: list[set[T]]: The bases of a given matroid. """ E, r = matroid # Bs = { B ⊆ E : |B| = r(B) = r(E) } return [B for B in powset(E) if (len(B) == r(B)) and (len(B) == r(E))]
def from_rank_matroid( matroid: tuple[set[T], Callable[[set[T]], int]]) -> list[set[T]]: """Construct spanning sets from a matroid defined by a rank function. Args: matroid (tuple[set[T], Callable[[set[T]], int]]): A matroid defined by a rank function Returns: list[set[T]]: The spanning set of a given matroid. """ E, r = matroid # Ss = { S ⊆ E : r(S) = r(E) } return [S for S in powset(E) if r(S) == r(E)]
def from_rank_matroid( matroid: tuple[set[T], Callable[[set[T]], int]]) -> list[set[T]]: """Construct independent sets from a matroid defined by a rank function. Args: matroid (tuple[set[T], Callable[[set[T]], int]]): A matroid defined by a rank function. Returns: list[set[T]]: The independent sets of a given matroid. """ E, r = matroid # Is = { I ⊆ E : r(I) = |I| } return [I for I in powset(E) if r(I) == len(I)]
def from_nulity_matroid( matroid: tuple[set[T], Callable[[set[T]], int]]) -> list[set[T]]: """Construct independent sets from a matroid defined by a nulity function. Args: matroid (tuple[set[T], Callable[[set[T]], int]]): A matroid defined by a nulity function. Returns: list[set[T]]: The independent sets of a given matroid. """ E, n = matroid # Is = { I ⊆ E : n(I) = 0 } return [I for I in powset(E) if n(I) == 0]
def from_circuits_matroid( matroid: tuple[set[T], list[set[T]]]) -> list[set[T]]: """Construct independent sets from a matroid defined by circuits. Args: matroid (tuple[set[T], list[set[T]]]): A matroid defined by circuits. Returns: tuple[set[T], list[set[T]]]: The independent sets of a given matroid. """ E, Cs = matroid # Is = {I ⊆ E : C ⊈ I, ∀C ∈ Cs} return [I for I in powset(E) if all(map(lambda C: not (C <= I), Cs))]
def from_closure_matroid( matroid: tuple[set[T], Callable[[set[T]], set[T]]]) -> list[set[T]]: """Construct flats from a matroid defined by a closure function. Args: matroid (tuple[set[T], Callable[[set[T]], set[T]]]): A matroid defined by a closure function. Returns: list[set[T]]: The flats of a given matroid. """ E, cl = matroid # Fs = { F ⊆ E | cl(F) = F } return [F for F in powset(E) if cl(F) == F]
def from_rank_matroid( matroid: tuple[set[T], Callable[[set[T]], int]]) -> list[set[T]]: """Construct hyperplanes from a matroid defined by a rank function. Args: matroid (tuple[set[T], Callable[[set[T]], int]]): A matroid defined by a rank function. Returns: list[set[T]]: The hyperplanes of a given matroid. """ E, r = matroid # Hs: maximal set of { H ⊆ E : r(H) = r(E) - 1 } Hs_ = [H for H in powset(E) if r(H) == r(E) - 1] return [H for H in Hs_ if all(map(lambda H_: not H < H_, Hs_))]
def from_nulity_matroid( matroid: tuple[set[T], Callable[[set[T]], int]]) -> list[set[T]]: """Construct hyperplanes from a matroid defined by a nulity function. Args: matroid (tuple[set[T], Callable[[set[T]], int]]): A matroid defined by a nulity function. Returns: list[set[T]]: The hyperplanes of a given matroid. """ E, n = matroid # Hs: maximal set of { H ⊆ E : n(E) - n(H) = |E| - |H| - 1 } Hs_ = [H for H in powset(E) if n(E) - n(H) == len(E) - len(H) - 1] return [H for H in Hs_ if all(map(lambda H_: not H < H_, Hs_))]
def union(self, matroid: Matroid) -> Matroid: """Calculate the union of this and another matroids. Args: matroid (Matroid): A matroid. Returns: Matroid: The union of this and the other matroids. """ r1 = self.rank_function r2 = matroid.rank_function # r(X) = min{ r1(Y) + r2(Y) + |X - Y| : Y ⊆ X } r = lambda X: min(r1(Y) + r2(Y) + len(X - Y) for Y in powset(X)) return Matroid((self.ground_set | matroid.ground_set, r), axiom=MatroidAxiom.RANK_FUNCTION, axiom_check=False)
def from_closure_matroid( matroid: tuple[set[T], Callable[[set[T]], set[T]]]) -> Callable[[set[T]], int]: """Construct a rank function from a matroid defined by a closure function. Args: matroid (tuple[set[T], Callable[[set[T]], set[T]]]): A matroid defined by a closure function. Returns: Callable[[set[T]], int]: The rank function of a given matroid. """ E, cl = matroid # r(X) = min{ |I| : X ⊆ cl(I) }, ∀X ⊆ E. return lambda X: min(len(I) for I in powset(E) if X <= cl(I))
def from_closure_matroid( matroid: tuple[set[T], Callable[[set[T]], set[T]]]) -> list[set[T]]: """Construct independent sets from a matroid defined by a closure function. Args: matroid (tuple[set[T], Callable[[set[T]], set[T]]): A matroid defined by a closure function. Returns: list[set[T]]: The independent sets of a given matroid. """ E, cl = matroid # Is = { I ⊆ E : i ∉ cl(I\{i}), ∀i ∈ I } return [ I for I in powset(E) if all(map(lambda i: i not in cl(I - {i}), I)) ]
def from_rank_matroid( matroid: tuple[set[T], Callable[[set[T]], int]]) -> list[set[T]]: """Construct flats from a matroid defined by a rank function. Args: matroid (tuple[set[T], Callable[[set[T]], int]]): A matroid defined by a rank function. Returns: list[set[T]]: The flats of a given matroid. """ E, r = matroid # Fs = { F ⊆ E : r(F ∪ {e}) = r(F) + 1, ∀e ∈ E\F } return [ F for F in powset(E) if all(map(lambda e: r(F | {e}) == r(F) + 1, E - F)) ]
def from_nulity_matroid( matroid: tuple[set[T], Callable[[set[T]], int]]) -> list[set[T]]: """Construct flats from a matroid defined by a nulity function. Args: matroid (tuple[set[T], Callable[[set[T]], int]]): A matroid defined by a nulity function. Returns: list[set[T]]: The flats of a given matroid. """ E, n = matroid # Fs = { F ⊆ E : n(F ∪ {e}) - n(F) = |F ∪ {e}| - |F| + 1, ∀e ∈ E\F } return [ F for F in powset(E) if all( map(lambda e: n(F | {e}) - n(F) == len(F | {e}) - len(F) - 1, E - F)) ]
def from_spanning_matroid( matroid: tuple[set[T], list[set[T]]]) -> list[set[T]]: """Construct hyperplanes from a matroid defined by spanning sets. Args: matroid (tuple[set[T], list[set[T]]]): A matroid defined by spanning sets. Returns: list[set[T]]: The hyperplanes of a given matroid. """ E, Ss = matroid # Hs is the maximal set of the non-spanning sets { N ⊆ E : N ∉ Ss } non_spannings = [N for N in powset(E) if N not in Ss] # Maximalization return [ H for H in non_spannings if all(map(lambda X: not (H < X), non_spannings)) ]
def from_bases_matroid(matroid: tuple[set[T], list[set[T]]]) -> list[set[T]]: """Construct hyperplanes from a matroid defined by bases. Args: matroid (tuple[set[T], list[set[T]]]): A matroid defined by bases. Returns: list[set[T]]: The hyperplanes of a given matroid. """ E, Bs = matroid # Hs is the maximal set in { H ⊆ E : B ⊈ H, ∀B ∈ Bs } set_containing_no_bases = [ X for X in powset(E) if all(map(lambda B: not (B <= X), Bs)) ] # Maximalization return [ H for H in set_containing_no_bases if all(map(lambda X: not (H < X), set_containing_no_bases)) ]
def cobases(self) -> list[set[int]]: # Bs*(U_{k,n}) = { B* ⊆ E : |B*| = n - k } return [ B_ast for B_ast in powset(self.E) if len(B_ast) == self.n - self.k ]
def codependent_sets(self) -> list[set[int]]: # Ds*(U_{k,n}) = { D* ⊆ E : |D*| > n - k } return [ D_ast for D_ast in powset(self.E) if len(D_ast) > self.n - self.k ]