def __divmod__(self, other): if not isinstance(other, Polynomial): dm_list = [(exp, divmod(coeff, other)) for exp, coeff in self.Data] return ( Polynomial(self.Base, [(exp, quot) for (exp, (quot, rem)) in dm_list]), Polynomial(self.Base, [(exp, rem) for (exp, (quot, rem)) in dm_list]), ) if other.Base != self.Base: assert self.VarLess == other.VarLess if self.VarLess(self.Base, other.Base): dm_list = [(exp, divmod(coeff, other)) for exp, coeff in self.Data] return ( Polynomial(self.Base, [(exp, quot) for (exp, (quot, rem)) in dm_list]), Polynomial(self.Base, [(exp, rem) for (exp, (quot, rem)) in dm_list]), ) else: other_unit = Polynomial(other.Base, ((0, other.unit),), self.VarLess) quot, rem = divmod(other_unit, other) return quot * self, rem * self if other.degree == -1: raise DivisionByZeroError quot = Polynomial(self.Base, ()) rem = self other_lead_coeff = other.Data[-1][1] other_lead_exp = other.Data[-1][0] coeffs_are_field = isinstance(traits(self.Unit), FieldTraits) from pymbolic.primitives import quotient while rem.degree >= other.degree: if coeffs_are_field: coeff_factor = quotient(rem.Data[-1][1], other_lead_coeff) else: coeff_factor, lead_rem = divmod(rem.Data[-1][1], other_lead_coeff) if lead_rem: return quot, rem deg_diff = rem.Data[-1][0] - other_lead_exp this_fac = Polynomial(self.Base, ((deg_diff, coeff_factor),)) quot += this_fac rem -= this_fac * other return quot, rem
def __divmod__(self, other): if not isinstance(other, Polynomial): dm_list = [(exp, divmod(coeff, other)) for exp, coeff in self.Data] return Polynomial(self.Base, [(exp, quot) for (exp, (quot, rem)) in dm_list]),\ Polynomial(self.Base, [(exp, rem) for (exp, (quot, rem)) in dm_list]) if other.Base != self.Base: assert self.VarLess == other.VarLess if self.VarLess(self.Base, other.Base): dm_list = [(exp, divmod(coeff, other)) for exp, coeff in self.Data] return Polynomial(self.Base, [(exp, quot) for (exp, (quot, rem)) in dm_list]),\ Polynomial(self.Base, [(exp, rem) for (exp, (quot, rem)) in dm_list]) else: other_unit = Polynomial(other.Base, ((0, other.unit), ), self.VarLess) quot, rem = divmod(other_unit, other) return quot * self, rem * self if other.degree == -1: raise DivisionByZeroError quot = Polynomial(self.Base, ()) rem = self other_lead_coeff = other.Data[-1][1] other_lead_exp = other.Data[-1][0] coeffs_are_field = isinstance(traits(self.Unit), FieldTraits) from pymbolic.primitives import quotient while rem.degree >= other.degree: if coeffs_are_field: coeff_factor = quotient(rem.Data[-1][1], other_lead_coeff) else: coeff_factor, lead_rem = divmod(rem.Data[-1][1], other_lead_coeff) if lead_rem: return quot, rem deg_diff = rem.Data[-1][0] - other_lead_exp this_fac = Polynomial(self.Base, ((deg_diff, coeff_factor), )) quot += this_fac rem -= this_fac * other return quot, rem
def __init__(self, numerator, denominator=1): d_unit = traits.traits(denominator).get_unit(denominator) numerator /= d_unit denominator /= d_unit self.Numerator = numerator self.Denominator = denominator
def get_unit(x): lc = leading_coefficient(x) return traits(lc).get_unit(lc)