Add: current code expects coeff to always live in .args[0]
For example Add.as_coeff_terms do this
# -2 + 2 * a -> -1, 2-2*a
if self.args[0].is_Number and self.args[0].is_negative:
return -S.One,(-self,)
return S.One,(self,)
(note how it checks arg[0] for .is_Number)
But when we introduced sort-args-by-hash in
2e496abeb32b we broke this
invariant.
Let's restore it -- it is really needed by current SymPy code.
For example #974 was caused by this problem (test included), and on i386 once
observed the following:
In [1]: n = Symbol('n', integer=True)
In [2]: e = n-2
In [3]: e.args
Out[3]: (n, -2)
Which was the cause of this:
In [4]: (n-2).as_coeff_terms()
Out[4]: (1, (-2 + n,)) <-- should be (-1, (2 - n,))
As .as_coeff_terms() catches this on i386 -- test for it was also written.
All tests pass -- tested on i386 and amd64.
Signed-off-by: Kirill Smelkov <kirr@landau.phys.spbu.ru>
Signed-off-by: Ondrej Certik <ondrej@certik.cz>