def from_dict(dict1: dict) -> 'P(P(M x M) x N)': r"""Return a :term:`multiclan` with a single :term:`relation` where the :term:`couplet`\s are the elements of ``dict1``.""" rel = _relations.from_dict(dict1) mclan = _mo.Multiset(rel, direct_load=True) mclan.cache_multiclan(_mo.CacheStatus.IS) mclan.cache_functional(_mo.CacheStatus.IS) mclan.cache_regular(_mo.CacheStatus.IS) return mclan
def test_json(self): """Test loading clan from json.""" data = [{"id": 1, "name": "foo"}, {"id": 2, "name": "bar"}, {"id": 3, "name": "baz"}] import json json_string = json.dumps(data) expected = Set(relations.from_dict(d) for d in data) actual = import_json(io.StringIO(json_string)) self.assertEqual(expected, actual)
def make_board(_puzzle): assert len(_puzzle) == GRID_SIZE*GRID_SIZE board = set() for i, v in enumerate(_puzzle): value = 0 if v == '.' else int(v) col = (i % GRID_SIZE) + 1 row = int(i / GRID_SIZE) + 1 band = int((row-1) / BLOCK_SIZE)+1 stack = int((col-1) / BLOCK_SIZE)+1 # cell = {'row': row, 'col': col} cell = {'row': row, 'col': col, 'band': band, 'stack': stack} if value != 0: cell['value'] = value board.add(relations.from_dict(cell)) return Set(board, direct_load=True).cache_is_clan(True).cache_is_left_functional(True)
def make_board(_puzzle): assert len(_puzzle) == GRID_SIZE * GRID_SIZE board = set() for i, value in enumerate(_puzzle): value = 0 if value == '.' else int(value) col = (i % GRID_SIZE) + 1 row = int(i / GRID_SIZE) + 1 band = int((row - 1) / BLOCK_SIZE) + 1 stack = int((col - 1) / BLOCK_SIZE) + 1 # cell = {'row': row, 'col': col} cell = {'row': row, 'col': col, 'band': band, 'stack': stack} if value != 0: cell['value'] = value board.add(relations.from_dict(cell)) return Set(board, direct_load=True).cache_clan(CacheStatus.IS).cache_functional(CacheStatus.IS)
def test_json(self): """Test loading clan from json.""" data = [{ "id": 1, "name": "foo" }, { "id": 2, "name": "bar" }, { "id": 3, "name": "baz" }] import json json_string = json.dumps(data) expected = Set(relations.from_dict(d) for d in data) actual = import_json(io.StringIO(json_string)) self.assertEqual(expected, actual)
import algebraixlib.extension as extension import algebraixlib.partition as partition from algebraixlib.undef import Undef verbose = False # verbose = True BLOCK_SIZE = 3 GRID_SIZE = BLOCK_SIZE*BLOCK_SIZE BLOCK_VALUES_CLAN = Set(Set(Couplet('value', i) ).cache_is_relation(True).cache_is_left_functional(True) for i in range(1, GRID_SIZE+1) ).cache_is_clan(True).cache_is_left_functional(True) BANDS_STACKS = Set((relations.from_dict({'row': r, 'col': c, 'band': int((r-1) / BLOCK_SIZE)+1, 'stack': int((c-1) / BLOCK_SIZE)+1}) for r, c in itertools.product(list(range(1, GRID_SIZE+1)), list(range(1, GRID_SIZE+1)))) ).cache_is_clan(True).cache_is_left_functional(True) def _sorted(iterable, key=None): return sorted(iterable, key=key) def _unsorted(iterable, key=None): _ = key return iterable # sorting is for debugging...for performance don't sort # _sort = _sorted # To get consistent timing/debugging
import algebraixlib.extension as extension import algebraixlib.partition as partition from algebraixlib.undef import Undef VERBOSE = False # VERBOSE = True BLOCK_SIZE = 3 GRID_SIZE = BLOCK_SIZE * BLOCK_SIZE BLOCK_VALUES_CLAN = Set( Set(Couplet('value', i)).cache_relation(CacheStatus.IS).cache_functional(CacheStatus.IS) for i in range(1, GRID_SIZE + 1)).cache_clan(CacheStatus.IS).cache_functional( CacheStatus.IS).cache_regular(CacheStatus.IS) BANDS_STACKS = Set( (relations.from_dict({'row': r, 'col': c, 'band': int((r - 1) / BLOCK_SIZE) + 1, 'stack': int((c - 1) / BLOCK_SIZE) + 1}) for r, c in itertools.product(list(range(1, GRID_SIZE + 1)), list(range(1, GRID_SIZE + 1)))) ).cache_clan(CacheStatus.IS).cache_functional(CacheStatus.IS).cache_regular(CacheStatus.IS) def _sorted(iterable, key=None): return sorted(iterable, key=key) def _unsorted(iterable, key=None): _ = key return iterable # sorting is for debugging...for performance don't sort # _SORT = _sorted # To get consistent timing/debugging _SORT = _unsorted