def friendly_graded(f, d):
vec=BoolePolynomialVector()
for t in f.terms:
if t.deg()!=d:
continue
else:
vec.append(t)
return add_up_polynomials(vec)
We leave it to the heuristic of the add_up_polynomials function how to add up the monomials. For example a divide and conquer strategy is quite good here.