[10/24] rework assumptions.py to use FactRules (assume typeinfo)
commit61a52fb02495bec47fdbefdd8a155c243844b8f1
authorKirill Smelkov <kirr@landau.phys.spbu.ru>
Fri, 25 Jul 2008 18:03:06 +0000 (25 22:03 +0400)
committerKirill Smelkov <kirr@landau.phys.spbu.ru>
Fri, 25 Jul 2008 18:03:06 +0000 (25 22:03 +0400)
tree598eb1c834908f4e7997c74ac10f396b5c533b0d
parent8dd5371cf0d3c90f50dd6e4de0a65eccba3e9117
[10/24] rework assumptions.py to use FactRules    (assume typeinfo)

The problem
-----------

if we have an expression, say

    s = 1 + pi

its ._assumptions may evolve in time, e.g. if we'll ask

    s.is_positive   -> True     (it asks evalf)

it will add 'positive' to ._assumptions.

Now let's look at Basic._eval_expand_trig, I quote:

    def _eval_expand_trig(self, *args):
        ...
        terms = [ term._eval_expand_trig(*args) for term in sargs ]
        return self.__class__(*terms, **self._assumptions)

    b = a.expand(trig=True)

It seems it wants to create an object of the same class  (self.__class__) and
with the same assumptions (self._assumptions)

Now the problem is that if even original s assumptions are empty, at expanded
instance creation time there sit 'positive' as well.

So expand will return *different* object with *different* starting assumptions
compared to what s had on its creation time, and btw do you remember
assumptions affect hash?

So hash(a) will be != hash(b) and this is bad!

To overcome this I introduce .assumptions0 property and .new() method.

- assumptions0 is used to obtain typinfo assumptions, and
- new -- to spawn new instance of self + assumptions0 typinfo applied.

Alos, failing test that demonstrates the problem is attached. We'll switch the
whole SymPy to .new and .assumptions0, and this will cure the problem and make
the test pass.

Signed-off-by: Kirill Smelkov <kirr@landau.phys.spbu.ru>
Signed-off-by: Ondrej Certik <ondrej@certik.cz>
Signed-off-by: Mateusz Paprocki <mattpap@gmail.com>
sympy/core/basic.py
sympy/core/tests/test_assumptions.py