Michael Yuxi Dong - 11 months ago 87

Python Question

If I use

`simplify()`

`log((exp(x)+1)/exp(x))`

`log(1+exp(-x))`

`ln((exp(x)+1)/exp(x))`

`log(1+exp(-x))`

Answer

You can more directly just use ** sympy.polys.polytools.cancel()**, which is available as a method on your expression with

`.cancel()`

. ```
>>> from sympy.abc import x
>>> from sympy import *
>>> my_expr = log((exp(x)+1)/exp(x))
>>> my_expr.cancel()
log(1 + exp(-x))
```

This is what is doing the work of simplifying your expression inside `simplify()`

.

A very naive benchmark:

```
>>> import timeit
>>> %timeit my_expr.simplify()
100 loops, best of 3: 7.78 ms per loop
>>> %timeit my_expr.cancel()
1000 loops, best of 3: 972 µs per loop
```

Source (Stackoverflow)