I'm sorry Sir, but I still do not understand the logic.

Lets say match[b] = 3*x*f(x) + f(x). So factor_terms(match[b]) becomes f(x)*(3*x + 1) , which is stored in u.

And I want my r2[b] to be (3*x + 1) . In the method that you have described r2[b] becomes 3*x*f(x) + f(x) / f(x)*(3*x + 1) which is equal to 1

If I do u = factor_terms(match[b]).as_independent(f(x))[1] , u becomes f(x) and my r2[b]  becomes 3*x*f(x) + f(x) / f(x) which is equal to 3*x + 1

Do correct me if I'm wrong.