示例#1
0
 def compositions_dict(value: int, parameters: Tuple[str, ...]):
     for comp in compositions(
             value,
             len(parameters),
         (0, ) * len(parameters),
         (None, ) * len(parameters),
     ):
         yield dict(zip(parameters, comp))
 def get_terms(self, parent_terms: Callable[[int], Terms],
               subterms: SubTerms, n: int) -> Terms:
     new_terms: Terms = Counter()
     size_compositions = utils.compositions(n, len(subterms),
                                            self.min_sizes, self.max_sizes)
     for sizes in size_compositions:
         for param_value_pairs in self.params_value_pairs_combinations(
                 sizes, subterms):
             new_param = self._new_param(*(p for p, _ in param_value_pairs))
             new_terms[new_param] += utils.prod(
                 (v for _, v in param_value_pairs))
     return new_terms
 def get_sub_objects(
     self, subobjs: SubObjects, n: int
 ) -> Iterator[Tuple[Parameters, Tuple[
         List[Optional[CombinatorialObjectType]], ...]]]:
     size_compositions = utils.compositions(n, len(subobjs), self.min_sizes,
                                            self.max_sizes)
     for sizes in size_compositions:
         for param_objs_pairs in self.params_value_pairs_combinations(
                 sizes, subobjs):
             new_param = self._new_param(*(p for p, _ in param_objs_pairs))
             children_objs = cast(
                 Iterator[List[Optional[CombinatorialObjectType]]],
                 (objs for _, objs in param_objs_pairs),
             )
             yield (new_param, tuple(children_objs))
 def _c(
     self,
     children_subterms: SubTerms,
 ) -> Terms:
     """
     Dict representing the polynomial that multiply the polynomial of the
     flipped child for length n.
     """
     min_sizes = self._min_sizes[:self.idx] + self._min_sizes[self.idx + 1:]
     max_sizes = self._max_sizes[:self.idx] + self._max_sizes[self.idx + 1:]
     possible_sizes = utils.compositions(self._parent_shift,
                                         self.number_of_children - 1,
                                         min_sizes, max_sizes)
     other_children_subterms = (children_subterms[:self.idx] +
                                children_subterms[self.idx + 1:])
     res: Terms = Counter()
     for sizes in possible_sizes:
         for param_value_pairs in CartesianProduct.params_value_pairs_combinations(
                 sizes, other_children_subterms):
             new_param = self._other_new_param(
                 *(p for p, _ in param_value_pairs))
             res[new_param] += utils.prod((v for _, v in param_value_pairs))
             assert res[new_param] >= 0
     return res
 def _a(
     self,
     n: int,
     parent_subterm: Callable[[int], Terms],
     children_subterms: SubTerms,
 ) -> Terms:
     """
     A terms dict representing the polynomial for the rule parent terms minus the
     contribution of the childs when the size of the flipped child in the composition
     is smaller than n.
     """
     max_sizes = (self._max_sizes[:self.idx] + (n - 1, ) +
                  self._max_sizes[self.idx + 1:])
     possible_sizes = utils.compositions(n + self._parent_shift,
                                         self.number_of_children,
                                         self._min_sizes, max_sizes)
     res: Terms = Counter(parent_subterm(n + self._parent_shift))
     for sizes in possible_sizes:
         for param_value_pairs in CartesianProduct.params_value_pairs_combinations(
                 sizes, children_subterms):
             new_param = self._new_param(*(p for p, _ in param_value_pairs))
             res[new_param] -= utils.prod((v for _, v in param_value_pairs))
             assert res[new_param] >= 0
     return res