In cases like this when match[b] = x*f(x) + f(x)*2 , gcd(g.args) gives f(x) while match[b].primitive()[1] gives x*f(x) + f(x)*2 . Wouldn't primitive fail in those cases?