before you do all the expensive simplification, find out if r[d] != first

```
if r[d] != 0:
    numerator = ...
    if numerator == 0:
        etc...
```

Do you really need full simplification or will a simple `expand_mul` work? Should simplify (or something) be applied to the difference or will it be sufficient to apply it to the two expressions, i.e. should you have `numerator = simplify(dif1 - dif2)`?