For example, how can we make this simplify to zero (or show me where I’ve made a dumb mistake and then it just works)
import sympy
sympy.var('s t', real=True)
(s - 2 * sympy.Min(s, t) - sympy.Abs(s - t)).simplify() # should be zero?
I see evidence that sympy knows about these kind of relations but I’m not clear what triggers it to go from $|t – s|$ to a representation involving min or max.
>Solution :
You can use rewrite(Piecewise):
In [239]: e
Out[239]: s - │s - t│ - 2⋅Min(s, t)
In [240]: e.rewrite(sympy.Piecewise)
Out[240]:
⎛⎧s for s ≤ t⎞ ⎛⎧s - t for s - t ≥ 0⎞
s - 2⋅⎜⎨ ⎟ - ⎜⎨ ⎟
⎝⎩t otherwise⎠ ⎝⎩-s + t otherwise ⎠
In [241]: e.rewrite(Piecewise).simplify()
Out[241]: -t