def sdm_spoly(f, g, O, K, phantom=None):
 if ((not f) or (not g)):
  return sdm_zero()
 LM1 = sdm_LM(f)
 LM2 = sdm_LM(g)
 if (LM1[0] != LM2[0]):
  return sdm_zero()
 LM1 = LM1[1:]
 LM2 = LM2[1:]
 lcm = monomial_lcm(LM1, LM2)
 m1 = monomial_div(lcm, LM1)
 m2 = monomial_div(lcm, LM2)
 c = K.quo((- sdm_LC(f, K)), sdm_LC(g, K))
 r1 = sdm_add(sdm_mul_term(f, (m1, K.one), O, K), sdm_mul_term(g, (m2, c), O, K), O, K)
 if (phantom is None):
  return r1
 r2 = sdm_add(sdm_mul_term(phantom[0], (m1, K.one), O, K), sdm_mul_term(phantom[1], (m2, c), O, K), O, K)
 return (r1, r2)

def sdm_spoly ( f , g , O , K , phantom = None ) : if ( ( not f ) or ( not g ) ) : return sdm_zero ( ) LM1 = sdm_LM ( f ) LM2 = sdm_LM ( g ) if ( LM1 [ 0 ] != LM2 [ 0 ] ) : return sdm_zero ( ) LM1 = LM1 [ 1 : ] LM2 = LM2 [ 1 : ] lcm = monomial_lcm ( LM1 , LM2 ) m1 = monomial_div ( lcm , LM1 ) m2 = monomial_div ( lcm , LM2 ) c = K . quo ( ( - sdm_LC ( f , K ) ) , sdm_LC ( g , K ) ) r1 = sdm_add ( sdm_mul_term ( f , ( m1 , K . one ) , O , K ) , sdm_mul_term ( g , ( m2 , c ) , O , K ) , O , K ) if ( phantom is None ) : return r1 r2 = sdm_add ( sdm_mul_term ( phantom [ 0 ] , ( m1 , K . one ) , O , K ) , sdm_mul_term ( phantom [ 1 ] , ( m2 , c ) , O , K ) , O , K ) return ( r1 , r2 )
compute the generalized s-polynomial of f and g .