def f5_reduce(f, B):
 order = Polyn(f).ring.order
 domain = Polyn(f).ring.domain
 if (not Polyn(f)):
  return f
 while True:
  g = f
  for h in B:
   if Polyn(h):
    if monomial_divides(Polyn(h).LM, Polyn(f).LM):
     t = term_div(Polyn(f).LT, Polyn(h).LT, domain)
     if (sig_cmp(sig_mult(Sign(h), t[0]), Sign(f), order) < 0):
      hp = lbp_mul_term(h, t)
      f = lbp_sub(f, hp)
      break
  if ((g == f) or (not Polyn(f))):
   return f

def f5_reduce ( f , B ) : order = Polyn ( f ) . ring . order domain = Polyn ( f ) . ring . domain if ( not Polyn ( f ) ) : return f while True : g = f for h in B : if Polyn ( h ) : if monomial_divides ( Polyn ( h ) . LM , Polyn ( f ) . LM ) : t = term_div ( Polyn ( f ) . LT , Polyn ( h ) . LT , domain ) if ( sig_cmp ( sig_mult ( Sign ( h ) , t [ 0 ] ) , Sign ( f ) , order ) < 0 ) : hp = lbp_mul_term ( h , t ) f = lbp_sub ( f , hp ) break if ( ( g == f ) or ( not Polyn ( f ) ) ) : return f
f5-reduce a labeled polynomial f by b .