@@ -515,6 +515,7 @@ def get_sole_polynomial(xs: MultiSet[Value]) -> MultiSet[MultiSet[Value]]:
515515 get_sole_polynomial(MultiSet(polynomial(xss))) => xss
516516 """
517517
518+
518519@ruleset
519520def to_polynomial_ruleset (
520521 n1 : Value,
@@ -560,29 +561,29 @@ nested. For example, we might end up with a term like `polynomial(MultiSet(Multi
560561with just ` polynomial(xs) ` . We define two additional rules to cover cases like this:
561562
562563``` python
563- yield rule(
564- eq(n1).to(polynomial(mss)),
565- # For each monomial, if any of its terms is a polynomial with a single monomial, flatten
566- # that into the monomial, otherwise keep it as is
567- mss1 == mss.map(partial(multiset_flat_map, get_monomial)),
568- mss != mss1, # skip if this is a no-op
569- name = " unwrap monomial" ,
570- ).then(
571- union(n1).with_(polynomial(mss1)),
572- delete(polynomial(mss)),
573- set_(get_sole_polynomial(MultiSet(polynomial(mss1)))).to(mss1),
574- )
575- yield rule(
576- eq(n1).to(polynomial(mss)),
577- # If any of the monomials just has a single item which is a polynomial, then flatten that into the outer polynomial
578- mss1 == multiset_flat_map(UnstableFn(get_sole_polynomial), mss),
579- mss != mss1,
580- name = " unwrap polynomial" ,
581- ).then(
582- union(n1).with_(polynomial(mss1)),
583- delete(polynomial(mss)),
584- set_(get_sole_polynomial(MultiSet(polynomial(mss1)))).to(mss1),
585- )
564+ yield rule(
565+ eq(n1).to(polynomial(mss)),
566+ # For each monomial, if any of its terms is a polynomial with a single monomial, flatten
567+ # that into the monomial, otherwise keep it as is
568+ mss1 == mss.map(partial(multiset_flat_map, get_monomial)),
569+ mss != mss1, # skip if this is a no-op
570+ name = " unwrap monomial" ,
571+ ).then(
572+ union(n1).with_(polynomial(mss1)),
573+ delete(polynomial(mss)),
574+ set_(get_sole_polynomial(MultiSet(polynomial(mss1)))).to(mss1),
575+ )
576+ yield rule(
577+ eq(n1).to(polynomial(mss)),
578+ # If any of the monomials just has a single item which is a polynomial, then flatten that into the outer polynomial
579+ mss1 == multiset_flat_map(UnstableFn(get_sole_polynomial), mss),
580+ mss != mss1,
581+ name = " unwrap polynomial" ,
582+ ).then(
583+ union(n1).with_(polynomial(mss1)),
584+ delete(polynomial(mss)),
585+ set_(get_sole_polynomial(MultiSet(polynomial(mss1)))).to(mss1),
586+ )
586587```
587588
588589We have avoided the need to match inside of containers by instead using higher order functions to apply blockwise
@@ -643,12 +644,12 @@ def factor_ruleset(
643644 eq(n).to(polynomial(mss)),
644645 # Find factor that shows up in most monomials, at least two of them
645646 counts == MultiSet.sum_multisets(mss.map(MultiSet.reset_counts)),
646- eq(picked_term).to(counts.pick_max()), # on ties pick an arbitrary one
647+ eq(picked_term).to(counts.pick_max()), # on ties pick an arbitrary one
647648 # Only factor out if it appears in more than one monomial
648649 counts.count(picked_term) > 1 ,
649650 # The factor we choose is the largest intersection between all the monomials that have that that factored term
650651 picked == mss.filter(partial(multiset_contains_swapped, picked_term)),
651- factor == multiset_fold(MultiSet.__and__ , picked.pick(), picked), # intersection
652+ factor == multiset_fold(MultiSet.__and__ , picked.pick(), picked), # intersection
652653 divided == picked.map(partial(multiset_subtract_swapped, factor)),
653654 # remainder is those monomials that do not contain the factor
654655 remainder == mss.filter(partial(multiset_not_contains_swapped, picked_term)),
0 commit comments