Add: current code expects coeff to always live in .args[0]
commit418baf6f410bd1e897996d8e4c9960b0df0a7ef1
authorKirill Smelkov <kirr@landau.phys.spbu.ru>
Sat, 9 Aug 2008 14:08:51 +0000 (9 18:08 +0400)
committerKirill Smelkov <kirr@landau.phys.spbu.ru>
Sat, 9 Aug 2008 14:08:51 +0000 (9 18:08 +0400)
treef60daa18e628e71822f00c24361eccfcb70eecba
parentfc28d53d0cd3230e354fb32b0f4227d8171fef2b
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>
sympy/core/add.py
sympy/core/tests/test_arit.py