def test_132_genf(): searcher = TileScope([Perm((0, 2, 1))], point_placements) spec = searcher.auto_search(smallest=True) spec = spec.expand_verified() av = Av([Perm((0, 2, 1))]) for i in range(10): assert set(av.of_length(i)) == set( gp.patt for gp in spec.generate_objects_of_size(i)) assert spec.random_sample_object_of_size(i).patt in av gf = spec.get_genf() gf = sympy.series(spec.get_genf(), n=15) x = sympy.Symbol("x") assert [gf.coeff(x, n) for n in range(13)] == [ 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, ]
def test_1234_fusion(): __location__ = os.path.realpath( os.path.join(os.getcwd(), os.path.dirname(__file__))) with open(os.path.join(__location__, "spec-1234.json")) as f: d = json.loads(f.read()) spec = CombinatorialSpecification.from_dict(d) assert isinstance(spec, CombinatorialSpecification) assert not any("NOTIMPLEMENTED" in str(eq.rhs) for eq in spec.get_equations()) assert [spec.count_objects_of_size(i) for i in range(15)] == [ 1, 1, 2, 6, 23, 103, 513, 2761, 15767, 94359, 586590, 3763290, 24792705, 167078577, 1148208090, ] av = Av([Perm((0, 1, 2, 3))]) for i in range(10): assert set(av.of_length(i)) == set( gp.patt for gp in spec.generate_objects_of_size(i)) assert spec.random_sample_object_of_size(i).patt in av
def test_123_positive_fusions(): pack = TileScopePack.insertion_row_and_col_placements( row_only=True).make_fusion(tracked=True, apply_first=True) css = TileScope("123", pack) spec = css.auto_search(status_update=30) spec = spec.expand_verified() print(spec) assert isinstance(spec, CombinatorialSpecification) assert [spec.count_objects_of_size(i) for i in range(20)] == [ 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 2674440, 9694845, 35357670, 129644790, 477638700, 1767263190, ] av = Av([Perm((0, 1, 2))]) for i in range(10): assert set(av.of_length(i)) == set( gp.patt for gp in spec.generate_objects_of_size(i)) assert spec.random_sample_object_of_size(i).patt in av
def test_reverse_equiv(): """A specification that should use reverse equivalence.""" pack = TileScopePack( initial_strats=[ strat.FactorFactory(), strat.RequirementCorroborationFactory(), strat.RequirementPlacementFactory(partial=False), ], inferral_strats=[strat.RowColumnSeparationStrategy()], expansion_strats=[[strat.CellInsertionFactory()]], ver_strats=[strat.BasicVerificationStrategy()], iterative=False, name="test pack", ) basis = (Perm((0, 1, 3, 2)), Perm((0, 2, 3, 1)), Perm((1, 0, 3, 2))) # From https://oeis.org/A033321 expected_enum = [ 1, 1, 2, 6, 21, 79, 311, 1265, 5275, 22431, 96900, 424068, 1876143 ] x, f = sympy.symbols("x f") expected_min_poly = sympy.sympify("-4*f^2*x^2 + 8*f^2*x - 4*f*x - 4*f + 4") searcher = TileScope(basis, pack) spec = searcher.auto_search(smallest=True) assert [spec.count_objects_of_size(i) for i in range(13)] == expected_enum genf = spec.get_genf() assert sympy.simplify(expected_min_poly.subs(f, genf)) == 0 assert taylor_expand(genf, 12) == expected_enum # In order to avoid ReccursionError we go incrementally for i in range(0, 100): spec.count_objects_of_size(i) assert spec.count_objects_of_size(50) == 86055297645519796258217673160170 assert ( spec.count_objects_of_size(100) == 2733073112795720153237297124938915907723365837935699807314396095313) len4_perms = tuple(spec.generate_objects_of_size(4)) assert len(len4_perms) == 21 assert all(p not in len4_perms for p in basis) len8_perms = tuple(spec.generate_objects_of_size(8)) assert len(len8_perms) == 5275 assert len(set(len8_perms)) == 5275 for _ in range(10): gp = spec.random_sample_object_of_size(10) print(gp) assert gp.patt.avoids(*basis) av = Av(basis) for i in range(10): assert set(av.of_length(i)) == set( gp.patt for gp in spec.generate_objects_of_size(i)) assert spec.random_sample_object_of_size(i).patt in av
class MockAvCoPatts: def __init__(self, av_patts, co_patts): self.base_perm_set = Av(av_patts) if isinstance(co_patts, (Perm, MeshPatt)): self.filter = lambda perm: perm.contains(co_patts) elif all(isinstance(patt, (Perm, MeshPatt)) for patt in co_patts): self.filter = lambda perm: any( perm.contains(patt) for patt in co_patts) else: raise ValueError("Variable 'co_patts' not as expected: " "'{}'".format(co_patts)) def of_length(self, size): return filter(self.filter, self.base_perm_set.of_length(size))
def simples_avoiding(basis): """ Yield all simple permutations that avoid basis. You may wish to use the 'Av' class from permuta for iterating over permutations that avoid a given basis. A naive approach to know when to terminate is to use the fact that every length n simple contains a simple of length n - 1 or length n - 2. """ av = Av(basis) longest = 4 length = 4 while True: if length - 2 > longest: break for perm in av.of_length(length): if perm.is_simple(): yield perm longest = length length += 1
def test_321_1324(): searcher = TileScope("321_1324", reginsenc) spec = searcher.auto_search() assert isinstance(spec, CombinatorialSpecification) for i in range(20): gp = spec.random_sample_object_of_size(i) assert all(cell == (0, 0) for cell in gp.pos) assert gp.patt.avoids(Perm((2, 1, 0)), Perm((0, 2, 1, 3))) av = Av([Perm((2, 1, 0)), Perm((0, 2, 1, 3))]) for i in range(10): assert set(av.of_length(i)) == set( gp.patt for gp in spec.generate_objects_of_size(i)) assert [spec.count_objects_of_size(i) for i in range(50)] == [ 1, 1, 2, 5, 13, 32, 72, 148, 281, 499, 838, 1343, 2069, 3082, 4460, 6294, 8689, 11765, 15658, 20521, 26525, 33860, 42736, 53384, 66057, 81031, 98606, 119107, 142885, 170318, 201812, 237802, 278753, 325161, 377554, 436493, 502573, 576424, 658712, 750140, 851449, 963419, 1086870, 1222663, 1371701, 1534930, 1713340, 1907966, 2119889, 2350237, ]