def s_polynomial(f, g, order):
    """
    Return S-polynomial of f and g with respect to the order.

    S(f, g) = (lc(g)*T/lb(f))*f - (lc(f)*T/lb(g))*g,
    where T = lcm(lb(f), lb(g)).
    """
    f_lb, f_lc = order.leading_term(f) # term = (base, coeff)
    g_lb, g_lc = order.leading_term(g)
    t = f_lb.lcm(g_lb)
    return f.term_mul((t - f_lb, g_lc)) - g.term_mul((t - g_lb, f_lc))