예제 #1
0
 def match(self, subjects, substitution):
     subject_ids = Multiset()
     pattern_ids = Multiset()
     for subject in subjects:
         subject_id, subject_pattern_ids = self.subjects[subject]
         subject_ids.add(subject_id)
         pattern_ids.update(subject_pattern_ids)
     for pattern_index, pattern_set, pattern_vars in self.patterns.values():
         if pattern_set:
             if not pattern_set <= pattern_ids:
                 continue
             bipartite_match_iter = self._match_with_bipartite(subject_ids, pattern_set, substitution)
             for bipartite_substitution, matched_subjects in bipartite_match_iter:
                 if pattern_vars:
                     ids = subject_ids - matched_subjects
                     remaining = Multiset(self.subjects[id] for id in ids)  # pylint: disable=not-an-iterable
                     sequence_var_iter = self._match_sequence_variables(
                         remaining, pattern_vars, bipartite_substitution
                     )
                     for result_substitution in sequence_var_iter:
                         yield pattern_index, result_substitution
                 elif len(subjects) == len(pattern_set):
                     yield pattern_index, bipartite_substitution
         elif pattern_vars:
             sequence_var_iter = self._match_sequence_variables(Multiset(subjects), pattern_vars, substitution)
             for variable_substitution in sequence_var_iter:
                 yield pattern_index, variable_substitution
         elif len(subjects) == 0:
             yield pattern_index, substitution
예제 #2
0
 def match(self, subjects: Sequence[Expression], substitution: Substitution) -> Iterator[Tuple[int, Substitution]]:
     subject_ids = Multiset()
     pattern_ids = Multiset()
     if self.max_optional_count > 0:
         subject_id, subject_pattern_ids = self.subjects[None]
         subject_ids.add(subject_id)
         for _ in range(self.max_optional_count):
             pattern_ids.update(subject_pattern_ids)
     for subject in op_iter(subjects):
         subject_id, subject_pattern_ids = self.subjects[subject]
         subject_ids.add(subject_id)
         pattern_ids.update(subject_pattern_ids)
     for pattern_index, pattern_set, pattern_vars in self.patterns.values():
         if pattern_set:
             if not pattern_set <= pattern_ids:
                 continue
             bipartite_match_iter = self._match_with_bipartite(subject_ids, pattern_set, substitution)
             for bipartite_substitution, matched_subjects in bipartite_match_iter:
                 ids = subject_ids - matched_subjects
                 remaining = Multiset(self.subjects_by_id[id] for id in ids if self.subjects_by_id[id] is not None)
                 if pattern_vars:
                     sequence_var_iter = self._match_sequence_variables(
                         remaining, pattern_vars, bipartite_substitution
                     )
                     for result_substitution in sequence_var_iter:
                         yield pattern_index, result_substitution
                 elif len(remaining) == 0:
                     yield pattern_index, bipartite_substitution
         elif pattern_vars:
             sequence_var_iter = self._match_sequence_variables(Multiset(op_iter(subjects)), pattern_vars, substitution)
             for variable_substitution in sequence_var_iter:
                 yield pattern_index, variable_substitution
         elif op_len(subjects) == 0:
             yield pattern_index, substitution
 def del_2(self, faces: List[Tuple[int]]):
     """
     boundary of a list of faces, i.e. an arbitrary 2-chain over Z/2Z
     """
     boundary_list = Multiset()
     for face in faces:
         boundary_list.update(Multiset(self.boundary_2(face)))
     return boundary_list
 def delta_1(self, stars: List[Tuple[int]]):
     """
     coboundary of a list of stars, i.e. an arbitrary 0-cochain over Z/2Z
     """
     coboundary_list = Multiset()
     for star in stars:
         coboundary_list.update(Multiset(self.boundary_1(star)))
     return coboundary_list
 def del_1(self, edges: List[Tuple[int]]):
     """
     boundary of a list of edges, i.e. an arbitrary 1-chain over Z/2Z
     """
     boundary_list = Multiset()
     for edge in edges:
         boundary_list.update(Multiset(self.boundary_1(edge)))
     return boundary_list
 def delta_2(self, edges: List[Tuple[int]]):
     """
     coboundary of a list of edges, i.e. an arbitrary 1-cochain over Z/2Z
     given by a list of cycles in alpha
     """
     coboundary_list = Multiset()
     for edge in edges:
         coboundary_list.update(Multiset(self.boundary_1(edge)))
     return coboundary_list
예제 #7
0
 def test_correctness_randomized(self, variables, values):
     values = Multiset(values)
     for subst in commutative_sequence_variable_partition_iter(values, variables):
         assert len(variables) == len(subst)
         result_union = Multiset()
         for var in variables:
             assert len(subst[var.name]) >= var.minimum
             result_union.update(subst[var.name] * var.count)
         assert result_union == values
예제 #8
0
def pull(path):
	if not path:
		raise ValueError

	m = Multiset()
	with open(path) as f:
		for line in f:
			m.update(set(line.split(' ')))
	
	return m
예제 #9
0
def __eval_add(element: Element, *args, **kwargs) -> Multiset:
    if get_tag(element) != 'add':
        raise Exception('Element is not an add operator!')
    ms = Multiset()
    for subterm in element:
        constituent = eval_term(subterm, *args, **kwargs)
        if not isinstance(constituent, Multiset):
            raise Exception('Constituent of add must be a multiset!')
        ms.update(constituent)

    return ms
def multiply_units(*args):
    this_units = Multiset()
    for each_unit in args:
        if get_unit_type(each_unit) == 'multiply':
            this_units.update(get_complexunit_set(each_unit))
        else:
            this_units.add(each_unit)
    if len(this_units) == 1:
        return list(this_units)[0]
    else:
        return ('multiply', FrozenMultiset(this_units))
예제 #11
0
 def test_correctness(self, variables, values, expected_iter_count):
     values = Multiset(values)
     variables = [VariableWithCount('var{:d}'.format(i), c, m, None) for i, (c, m) in enumerate(variables)]
     count = 0
     for subst in commutative_sequence_variable_partition_iter(values, variables):
         assert len(variables) == len(subst), "Wrong number of variables in the substitution"
         result_union = Multiset()
         for var in variables:
             assert len(subst[var.name]) >= var.minimum, "Variable did not get its minimum number of expressions"
             result_union.update(subst[var.name] * var.count)
         assert result_union == values, "Substitution is not a partition of the values"
         count += 1
     assert count == expected_iter_count, "Invalid number of substitution in the iterable"
예제 #12
0
def test_update(initial, add, result):
    ms = Multiset(initial)
    ms.update(*add)
    assert sorted(ms) == result
    assert len(ms) == len(result)
예제 #13
0
from multiset import Multiset

x = Multiset()

for i in range(10**10 + 1):
    x.update(str(i))

    if i % 10000000 == 0:
        print(i)

    for digit, count in x.items():

        if count == i:
            print(digit, i)