def sdm_nf_buchberger(f, G, O, K, phantom=None):
 from itertools import repeat
 h = f
 T = list(G)
 if (phantom is not None):
  hp = phantom[0]
  Tp = list(phantom[1])
  phantom = True
 else:
  Tp = repeat([])
  phantom = False
 while h:
  try:
   (g, gp) = next(((g, gp) for (g, gp) in zip(T, Tp) if sdm_monomial_divides(sdm_LM(g), sdm_LM(h))))
  except StopIteration:
   break
  if phantom:
   (h, hp) = sdm_spoly(h, g, O, K, phantom=(hp, gp))
  else:
   h = sdm_spoly(h, g, O, K)
 if phantom:
  return (h, hp)
 return h

def sdm_nf_buchberger ( f , G , O , K , phantom = None ) : from itertools import repeat h = f T = list ( G ) if ( phantom is not None ) : hp = phantom [ 0 ] Tp = list ( phantom [ 1 ] ) phantom = True else : Tp = repeat ( [ ] ) phantom = False while h : try : ( g , gp ) = next ( ( ( g , gp ) for ( g , gp ) in zip ( T , Tp ) if sdm_monomial_divides ( sdm_LM ( g ) , sdm_LM ( h ) ) ) ) except StopIteration : break if phantom : ( h , hp ) = sdm_spoly ( h , g , O , K , phantom = ( hp , gp ) ) else : h = sdm_spoly ( h , g , O , K ) if phantom : return ( h , hp ) return h
compute a weak normal form of f with respect to g and order o .